Skip to content

Commit

Permalink
feat: implement share highlights across several platforms
Browse files Browse the repository at this point in the history
  • Loading branch information
Mireille Niwemuhuza authored and Mireille Niwemuhuza committed Jul 16, 2019
1 parent ab95184 commit a71ad68
Show file tree
Hide file tree
Showing 8 changed files with 269 additions and 3 deletions.
3 changes: 2 additions & 1 deletion src/api/controllers/highlightController.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@ class Highlight {
occurencyNumber,
});
return res.status(201).json({
Message: `Thank you for highlighting this text ${newHighlight.highlightText}`
Message: `Thank you for highlighting this text ${newHighlight.highlightText}`,
data: newHighlight
});
}
}
Expand Down
20 changes: 20 additions & 0 deletions src/api/controllers/shareHighlights.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
/**
* @Author - Mireille Niwemuhuza
*/
class shareHighlightController {
/**
* @param {object} req
* @param {object} res
* @returns {object} Object representing the response returned
*/

// eslint-disable-next-line require-jsdoc
static async shareHighlights(req, res) {
// const { id } = req.user;
return res.status(200).json({
message: 'Highlight shared!',
});
}
}

export default shareHighlightController;
10 changes: 10 additions & 0 deletions src/api/routes/articlesRouter.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,15 @@ import highlight from '../controllers/highlightController';
import highlightExist from '../../middleware/highlightExist';
import textExist from '../../middleware/textExist';
import upload from '../../handlers/multer';
import shareHighlight from '../../middleware/shareHighlights';
import shareHighlightController from '../controllers/shareHighlights';
import checkHighlight from '../../middleware/checkHighlight';

const articlesRouter = Router();
const {
getViews, commentNumber, facebookShares, twitterShares, emailShares, shares
} = stats;
const { shareHighlights } = shareHighlightController;
const {
createArticle,
getAllArticle,
Expand Down Expand Up @@ -53,6 +57,7 @@ const {
} = commentsController;
const { checkComment, checkParameter, articleExists } = comment;
const { liked, disliked } = checkLikesandDislikes;
const { highlights } = checkHighlight;

articlesRouter
.post('/', verifyToken, upload.fields([{ name: 'gallery', maxCount: 10 }]), validateBody('createArticle'), createArticle)
Expand Down Expand Up @@ -98,6 +103,11 @@ articlesRouter.get('/:slug/share/linkedin', verifyToken, slugExist, shareArticle
articlesRouter.get('/:slug/share/pinterest', verifyToken, slugExist, shareArticle, share);
articlesRouter.get('/:slug/share/email', verifyToken, slugExist, shareArticle, share);

// sharing highlights

articlesRouter.get('/:slug/highlights/:highlightId/share/twitter', verifyToken, slugExist, highlights, shareHighlight, shareHighlights);
articlesRouter.get('/:slug/highlights/:highlightId/share/facebook', verifyToken, slugExist, highlights, shareHighlight, shareHighlights);
articlesRouter.get('/:slug/highlights/:highlightId/share/email', verifyToken, slugExist, highlights, shareHighlight, shareHighlights);
articlesRouter.post('/:slug/bookmark', verifyToken, slugExist, bookmark);

articlesRouter.post('/:slug/report', verifyToken, validateBody('checkComment'), slugExist, reportArticle);
Expand Down
41 changes: 41 additions & 0 deletions src/middleware/checkHighlight.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import models from '../sequelize/models';

const { Article, Highlights } = models;

/**
* @class checkHighlight
* @description a class to check if a highlight belongs to an article
*/
export default class checkHighlight {
/**
* Verify if a highlight belongs to an article
* @param {Object} req - Request
* @param {Object} res - Response
* @param {Function} next -Next
* @returns {Object} The response object
*/
static async highlights(req, res, next) {
const { slug, highlightId } = req.params;
const checkHighlights = await Highlights.findAll({
where: {
id: highlightId
}
});
const getArticleId = await Article.findAll({
where: {
slug
}
});
if (!checkHighlights[0]) {
return res.status(400).json({
message: 'This higlight does not exist!'
});
}
if (checkHighlights[0].dataValues.articleId !== getArticleId[0].dataValues.id) {
return res.status(400).json({
message: 'This higlight does not belong to that article!'
});
}
next();
}
}
40 changes: 40 additions & 0 deletions src/middleware/shareHighlights.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import open from 'open';
import dotenv from 'dotenv';
import share from 'social-share';
import db from '../sequelize/models';

dotenv.config();
const { Article, Highlights, User } = db;
const { APP_URL_FRONTEND } = process.env;

export default async (req, res, next) => {
const { slug, highlightId } = req.params;
const { title, authorId, gallery } = await Article.findOne({ where: { slug } });
const { firstName, lastName } = await User.findOne({ where: { id: authorId } });
const { highlightText } = await Highlights.findOne({ where: { id: highlightId } });
if (gallery) {
const image = gallery[0];
if (req.url.search(/\/twitter/g) > 0) {
const url = share('twitter', {
url: `${APP_URL_FRONTEND}/api/articles/${slug}`,
title: `${highlightText} - written by ${firstName} ${lastName} ${gallery[0]}`
});
await open(`${url}`, `${title}`, { wait: false });
} else if (req.url.search(/\/facebook/g) > 0) {
const url = share('facebook', {
url: `${APP_URL_FRONTEND}/api/articles/${slug}`,
title: `${highlightText} - written by ${firstName} ${lastName} ${image}`
});
await open(`${url}`, `${title}`, { wait: false });
} else if (req.url.search(/\/email/g) > 0) {
const highTitle = `${highlightText} - written by ${firstName} ${lastName} ${image}`;
await open(
`mailto:?subject=${title}&body=${highTitle} ${APP_URL_FRONTEND}/articles/${slug}`,
{
wait: false
}
);
}
}
next();
};
108 changes: 108 additions & 0 deletions swagger.json
Original file line number Diff line number Diff line change
Expand Up @@ -2051,6 +2051,114 @@
"200": {}
}
}
},
"/articles/{articleId}/highlights/{highlightId}/share/twitter": {
"get": {
"tags": [
"Share highlights"
],
"description": "Authors Haven users are able to share highlights across several platforms like twitter, facebook and email",
"parameters": [
{
"name": "token",
"in": "header",
"description": "The access token",
"required": true
},
{
"name": "articleId",
"in": "path",
"description": "article's slug",
"required": true
},
{
"name": "highlightId",
"in": "path",
"description": "Highlight Id",
"required": true
}
],
"produces": [
"application/json"
],
"responses": {
"200": {
"description": "Highlight shared!"
}
}
}
},
"/articles/{articleId}/highlights/{highlightId}/share/facebook": {
"get": {
"tags": [
"Share highlights"
],
"description": "Authors Haven users are able to share highlights across several platforms like twitter, facebook and email",
"parameters": [
{
"name": "token",
"in": "header",
"description": "The access token",
"required": true
},
{
"name": "articleId",
"in": "path",
"description": "article's slug",
"required": true
},
{
"name": "highlightId",
"in": "path",
"description": "Highlight Id",
"required": true
}
],
"produces": [
"application/json"
],
"responses": {
"200": {
"description": "Highlight shared!"
}
}
}
},
"/articles/{articleId}/highlights/{highlightId}/share/email": {
"get": {
"tags": [
"Share highlights"
],
"description": "Authors Haven users are able to share highlights across several platforms like twitter, facebook and email",
"parameters": [
{
"name": "token",
"in": "header",
"description": "The access token",
"required": true
},
{
"name": "articleId",
"in": "path",
"description": "article's slug",
"required": true
},
{
"name": "highlightId",
"in": "path",
"description": "Highlight Id",
"required": true
}
],
"produces": [
"application/json"
],
"responses": {
"200": {
"description": "Highlight shared!"
}
}
}
}
}
}
2 changes: 1 addition & 1 deletion test/auth.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ describe('User Registration', () => {
.send({
firstName: 'Emy',
lastName: 'Rukundo',
username: 'mifeillee',
username: 'mfeillee',
email: 'elie@gmail.com',
password: 'Rukundo1!',
confirmPassword: 'Rukundo1!'
Expand Down
48 changes: 47 additions & 1 deletion test/highlight.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ const invalidHighlight = {
};

describe('Highlight the Article', () => {
let token;
let token, highlightId;
before(async () => {
const user = {
firstName: 'Emy',
Expand Down Expand Up @@ -61,6 +61,8 @@ describe('Highlight the Article', () => {
.end((err, res) => {
res.should.have.status(201);
expect(res.body.Message).to.be.a('string');
highlightId = res.body.data.id;
console.log(highlightId);
done();
});
});
Expand Down Expand Up @@ -95,4 +97,48 @@ describe('Highlight the Article', () => {
done();
});
});
it('should be able to share hightlights', (done) => {
chai
.request(server)
.get(`/api/articles/${newArticle.dataValues.slug}/highlights/${highlightId}/share/twitter`)
.set('token', token)
.end((err, res) => {
expect(res.status).to.equal(200);
expect(res.body).to.be.an('object');
done();
});
});
it('should be able to share hightlights', (done) => {
chai
.request(server)
.get(`/api/articles/${newArticle.slug}/highlights/${highlightId}/share/facebook`)
.set('token', token)
.end((err, res) => {
expect(res.status).to.equal(200);
expect(res.body).to.be.an('object');
done();
});
});
it('should be able to share hightlights', (done) => {
chai
.request(server)
.get(`/api/articles/${newArticle.dataValues.slug}/highlights/${highlightId}/share/email`)
.set('token', token)
.end((err, res) => {
expect(res.status).to.equal(200);
expect(res.body).to.be.an('object');
done();
});
});
it('should check if the highlight belongs to the article', (done) => {
chai
.request(server)
.get('/api/articles/73H7812/highlights/1/share/email')
.set('token', token)
.end((err, res) => {
expect(res.status).to.equal(400);
expect(res.body).to.be.an('object');
done();
});
});
});

0 comments on commit a71ad68

Please sign in to comment.