-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
31 changed files
with
1,244 additions
and
548 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -45,4 +45,5 @@ node_modules | |
# Build | ||
dist/ | ||
.vscode/settings.json | ||
. editorconfig | ||
.editorconfig | ||
client/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
import Model from '../models'; | ||
import StatusResponse from '../helpers/StatusResponse'; | ||
import { findNotificationById } from '../lib/notifications'; | ||
|
||
const { notifications } = Model; | ||
/** @description NotificationsController class | ||
* @return {object} the response object | ||
* @public | ||
*/ | ||
class NotificationsController { | ||
/** @description function to like an article | ||
* @param {string} req is the request parameter | ||
* @param {string} res is the response parameter | ||
* @return {object} the response object | ||
* @public | ||
*/ | ||
static async getUsersNotification(req, res) { | ||
const { userId } = req.params; | ||
const user = req.app.locals.user.userId; | ||
if (user !== parseInt(userId, 10)) { | ||
return StatusResponse.unauthorized(res, { message: 'access cannot be granted' }); | ||
} | ||
const findNotifications = await notifications.findAndCountAll({ | ||
where: { | ||
userId | ||
} | ||
}); | ||
if (findNotifications) { | ||
return StatusResponse.success(res, findNotifications); | ||
} | ||
return StatusResponse.notfound(res, { message: 'no notificationa found' }); | ||
} | ||
|
||
/** @description function get one notification | ||
* @param {string} req is the request parameter | ||
* @param {string} res is the response parameter | ||
* @return {object} the response object | ||
* @public | ||
*/ | ||
static async getOneNotification(req, res) { | ||
const { notificationId, userId } = req.params; | ||
const user = req.app.locals.user.userId; | ||
|
||
if (user !== parseInt(userId, 10)) { | ||
return StatusResponse.unauthorized(res, { message: 'access cannot be granted' }); | ||
} | ||
const findNotifications = await notifications.findOne({ | ||
where: { | ||
id: notificationId | ||
} | ||
}); | ||
if (findNotifications) { | ||
return StatusResponse.success(res, findNotifications); | ||
} | ||
return StatusResponse.notfound(res, { message: 'notification was found' }); | ||
} | ||
|
||
/** @description function get one notification | ||
* @param {string} req is the request parameter | ||
* @param {string} res is the response parameter | ||
* @return {object} the response object | ||
* @public | ||
*/ | ||
static async updateNotification(req, res) { | ||
const { notificationId, userId } = req.params; | ||
const user = req.app.locals.user.userId; | ||
try { | ||
if (user !== parseInt(userId, 10)) { | ||
return StatusResponse.unauthorized(res, { message: 'access cannot be granted' }); | ||
} | ||
const notification = await findNotificationById(notificationId); | ||
if (notification) { | ||
await notification.update({ | ||
isRead: true | ||
}); | ||
return StatusResponse.success(res, { message: 'notification was updated successfully' }); | ||
} | ||
return StatusResponse.notfound(res, { message: 'no notification found' }); | ||
} catch (error) { | ||
return StatusResponse.internalServerError(res, { message: 'server error' }); | ||
} | ||
} | ||
} | ||
|
||
export default NotificationsController; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
import UsermodelQuery from '../lib/UserModelQuery'; | ||
import mailer from '../helpers/mailer'; | ||
import { createNotification } from '../lib/notifications'; | ||
|
||
/** @description Notifications class | ||
* @public | ||
*/ | ||
class Notifications { | ||
/** @description function to add a notification | ||
* @param {object} payload is the response parameter | ||
* @param {object} io is the response parameter | ||
* @return {object} the response object | ||
* @public | ||
*/ | ||
static async addNotification(payload, io) { | ||
try { | ||
const { | ||
to: { userId, title, slug }, type, from: user, event: { createdAt, id } | ||
} = payload; | ||
const articleOwner = await UsermodelQuery.getUserById(userId); | ||
const sender = await UsermodelQuery.getUserById(user); | ||
const { dataValues: { email } } = articleOwner; | ||
const { profile: { dataValues: { username } } } = sender; | ||
const info = { | ||
title, | ||
type, | ||
link: slug, | ||
userId, | ||
senderUsername: username, | ||
senderId: user, | ||
event: { createdAt, id } | ||
}; | ||
const created = await createNotification(info); | ||
if (created) { | ||
await mailer.sendNotificationMail(email, username, info); | ||
return io.emit('Notification Created', info); | ||
} | ||
return false; | ||
} catch (error) { | ||
return error; | ||
} | ||
} | ||
|
||
/** @description function to add a follow notification | ||
* @param {object} payload is the response parameter | ||
* @param {object} io is the response parameter | ||
* @return {object} the response object | ||
* @public | ||
*/ | ||
static async followNotification(payload, io) { | ||
try { | ||
const { to: intFollowId, type, from: userId } = payload; | ||
const followed = await UsermodelQuery.getUserById(intFollowId); | ||
const follower = await UsermodelQuery.getUserById(userId); | ||
const { dataValues: { email } } = followed; | ||
const { profile: { dataValues: { username } } } = follower; | ||
const info = { | ||
title: 'followed', | ||
type, | ||
userId, | ||
senderId: intFollowId, | ||
link: '' | ||
}; | ||
const created = await createNotification(info); | ||
if (created) { | ||
await mailer.sendNotificationMail(email, username, info); | ||
return io.emit('Notification Created', info); | ||
} | ||
return false; | ||
} catch (error) { | ||
return error; | ||
} | ||
} | ||
} | ||
export default Notifications; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
|
||
|
||
export default { | ||
ARTICLE_NOTIFICATION_EVENT: 'ARTICLE_NOTIFICATION_EVENT', | ||
COMMENT_NOTIFICATION_EVENT: 'COMMENT_NOTIFICATION_EVENT', | ||
RATING_INTERACTION_EVENT: 'RATING_INTERACTION_EVENT,', | ||
FOLLOW_INTERACTION_EVENT: 'FOLLOW_INTERACTION_EVENT', | ||
UPDATEHIGHLIGHT_EVENT: 'UPDATEHIGHLIGHT_EVENT' | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
import eventEmitter from '../helpers/eventEmitter'; | ||
import Notifications from './Notifications'; | ||
import eventTypes from './eventTypes'; | ||
import highlightsLogic from '../lib/highlightsLogic'; | ||
|
||
const { updateHighlights } = highlightsLogic; | ||
|
||
|
||
const handlers = { | ||
register(io) { | ||
eventEmitter.on(eventTypes.ARTICLE_NOTIFICATION_EVENT, | ||
payload => Notifications.addNotification(payload, io)); | ||
|
||
eventEmitter.on(eventTypes.COMMENT_NOTIFICATION_EVENT, | ||
payload => Notifications.addNotification(payload, io)); | ||
|
||
eventEmitter.on(eventTypes.RATING_INTERACTION_EVENT, | ||
payload => Notifications.addNotification(payload, io)); | ||
|
||
eventEmitter.on(eventTypes.FOLLOW_INTERACTION_EVENT, | ||
payload => Notifications.followNotification(payload, io)); | ||
|
||
eventEmitter.on(eventTypes.UPDATEHIGHLIGHT_EVENT, (highlightedPortions, body, userId) => { | ||
updateHighlights(highlightedPortions, body, userId); | ||
}); | ||
} | ||
}; | ||
|
||
export default handlers; |
Oops, something went wrong.