Skip to content

Commit

Permalink
Merge 45cf47f into f35b173
Browse files Browse the repository at this point in the history
  • Loading branch information
nkalyesubula committed Aug 28, 2019
2 parents f35b173 + 45cf47f commit c467e97
Show file tree
Hide file tree
Showing 8 changed files with 154 additions and 2 deletions.
13 changes: 13 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
"method-override": "^2.3.10",
"multer": "^1.4.2",
"nyc": "^14.1.1",
"open": "^6.4.0",
"passport": "^0.4.0",
"passport-facebook": "^3.0.0",
"passport-google-oauth": "^2.0.0",
Expand Down
48 changes: 48 additions & 0 deletions src/controllers/articles.controller.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import open from 'open';
import 'dotenv/config';
import slug from 'slug';
import _ from 'lodash';
Expand All @@ -8,6 +9,7 @@ import articleService from '../services/article.service';
import Helper from '../helpers/helper';
import NotificationServices from '../services/notification.service';
import cloudinaryHelper from '../helpers/cloudinaryHelper';
import OpenUrlHelper from '../helpers/share.article.helper';

const { notifyViaEmailAndPush } = NotificationServices;

Expand Down Expand Up @@ -195,6 +197,52 @@ class Articles {
updatedArticle
});
}

/**
*
*
* @static
* @param {*} req
* @param {*} res
* @returns {Object} share article over email and social media channelds
* @memberof Articles
*/
static async shareArticle(req, res) {
const article = await db.findOne({
where: { slug: req.params.slug }
});

if (!article) {
return res.status(404).json({
status: 404,
message: 'Article is not found.'
});
}
const location = `${process.env.BACKEND_URL}/api/${process.env.API_VERSION}`;
const url = `${location}/articles/${req.params.slug}`;
switch (req.params.channel) {
case 'facebook':
await OpenUrlHelper.openUrl(`https:www.facebook.com/sharer/sharer.php?u=${url}`);
return res.status(200).json({
status: 200,
message: `Article shared to ${req.params.channel}`,
});
case 'twitter':
await OpenUrlHelper.openUrl(`https://twitter.com/intent/tweet?url=${url}`);
return res.status(200).json({
status: 200,
message: `Article shared to ${req.params.channel}`,
});
case 'mail':
await OpenUrlHelper.openUrl(`mailto:?subject=${article.title}&body=${url}`);
return res.status(200).json({
status: 200,
message: `Article shared to ${req.params.channel}`,
});
default:
break;
}
}
}

export default Articles;
7 changes: 7 additions & 0 deletions src/helpers/share.article.helper.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import open from 'open';

const openUrl = url => open(url);

const OpenUrlHelper = { openUrl };

export default OpenUrlHelper;
1 change: 1 addition & 0 deletions src/routes/api/article/article.routes.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ router.get('/', checkQuery, articleController.getAllArticles);
router.get('/:slug', articleController.getOneArticle);
router.delete('/:slug', [auth, confirmEmailAuth], articleController.deleteArticle);
router.patch('/:slug', [auth, confirmEmailAuth], imageUpload.array('images', 10), articleController.UpdateArticle);
router.post('/:slug/share/:channel', [auth, confirmEmailAuth], articleController.shareArticle);

router.post('/:articleId/tags', [auth, confirmEmailAuth], checkArticle, tagLimit, tagLength, createArticleTag);
router.get('/:articleId/tags', checkArticle, getArticleTags);
Expand Down
26 changes: 26 additions & 0 deletions src/routes/api/article/doc.yml
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,30 @@
$ref: '#/responses/BadRequest'
404:
$ref: '#/responses/Notfound'
/articles/{slug}/share/{channel}:
post:
description: >
Share an article on mail and social media channels
parameters:
- name: slug
in: path
description: Slug for article
type: string
required: true
- name: channel
in: path
description: Channel to share to
type: string
required: true
- name: x-access-token
in: header
schema:
type: string
required:
- authorization
responses:
200:
description: Article successfully shared
/articles:
post:
summary: Create an article
Expand Down Expand Up @@ -438,3 +462,5 @@ definitions:
$ref: '#/responses/BadRequest'
404:
$ref: '#/responses/Notfound'


4 changes: 2 additions & 2 deletions src/services/notification.service.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ class NotificationServices {
.filter(eachUser => eachUser.inAppNotification === true);
const message = `${followedUser.username} just published an article`;
const location = `${process.env.BACKEND_URL}/api/${process.env.API_VERSION}`;
const url = `${location}/api/v1/articles/${slug}`;
const url = `${location}/articles/${slug}`;


if (myFollowersWithEmailSub.length !== 0) {
Expand Down Expand Up @@ -101,7 +101,7 @@ class NotificationServices {
if (usersWhoFavoritedWithEmailSub.length !== 0) {
const emailAddresses = usersWhoFavoritedWithEmailSub.map(each => each.email);
const location = `${process.env.BACKEND_URL}/api/${process.env.API_VERSION}`;
const url = `${location}/api/v1/articles/${slug}`;
const url = `${location}/articles/${slug}`;
const emailTemplate = newCommentOnFavoritedArticlesTemplate(req.auth.username, url);
await sendEmail(emailAddresses, 'Hi there', emailTemplate);
}
Expand Down
56 changes: 56 additions & 0 deletions test/share.article.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
import sinon from 'sinon';
import { expect } from './test-setup';
import Article from '../src/controllers/articles.controller';
import OpenUrlHelper from '../src/helpers/share.article.helper';

describe('test for sharing an article', () => {
it('test for sharing an article via facebook', async () => {
const req = {
params: { slug: 'fakeslug', channel: 'facebook' },
auth: {
email: 'user@gmail.com'
}
};
const res = {
status() { },
send() { },
json() { }
};
sinon.stub(res, 'status').returnsThis();
sinon.stub(OpenUrlHelper, 'openUrl').returns(true);
await Article.shareArticle(req, res);
expect(res.status).to.have.been.calledWith(200);
});
it('test for sharing an article via twitter', async () => {
const req = {
params: { slug: 'fakeslug', channel: 'twitter' },
auth: {
email: 'user@gmail.com'
}
};
const res = {
status() { },
send() { },
json() { }
};
sinon.stub(res, 'status').returnsThis();
await Article.shareArticle(req, res);
expect(res.status).to.have.been.calledWith(200);
});
it('test for sharing an article via mail', async () => {
const req = {
params: { slug: 'fakeslug', channel: 'twitter' },
auth: {
email: 'user@gmail.com'
}
};
const res = {
status() { },
send() { },
json() { }
};
sinon.stub(res, 'status').returnsThis();
await Article.shareArticle(req, res);
expect(res.status).to.have.been.calledWith(200);
});
});

0 comments on commit c467e97

Please sign in to comment.