Prechádzať zdrojové kódy

Use single post notification model for both mentions and reply notifications

sbkwgh 8 rokov pred
rodič
commit
baf07a754a

+ 0 - 13
models/MentionNotification.js

@@ -1,13 +0,0 @@
-module.exports = (sequelize, DataTypes) => {
-	let MentionNotification = sequelize.define('MentionNotification', {}, {
-		classMethods: {
-			associate (models) {
-				MentionNotification.belongsTo(models.User)
-				MentionNotification.belongsTo(models.Post)
-				MentionNotification.belongsTo(models.Notification)
-			}
-		}
-	})
-
-	return MentionNotification
-}

+ 14 - 14
models/Notification.js

@@ -8,32 +8,32 @@ module.exports = (sequelize, DataTypes) => {
 			type: DataTypes.BOOLEAN,
 			defaultValue: false
 		},
-		type: DataTypes.ENUM('mention', 'thread update') 
+		type: DataTypes.ENUM('mention', 'thread update', 'reply') 
 	}, {
 		classMethods: {
 			associate (models) {
-				Notification.hasOne(models.MentionNotification)
+				Notification.hasOne(models.PostNotification)
 				Notification.belongsTo(models.User)
 			},
-			//Props fields: user, post, mention
-			async createMention (props) {
-				let { MentionNotification, User, Post } = sequelize.models
+			//Props fields: userFrom, usernameTo, post, type
+			async createPostNotification (props) {
+				let { PostNotification, User, Post } = sequelize.models
 
-				let user = await User.findOne({ where: { username: props.mention } })
-				if(!user) return null
+				let userTo = await User.findOne({ where: { username: props.usernameTo } })
+				if(!userTo) return null
 				
-				let notification = await Notification.create({ type: 'mention' })
-				let mentionNotification = await MentionNotification.create()
+				let notification = await Notification.create({ type: props.type })
+				let postNotification = await PostNotification.create()
 
-				await mentionNotification.setUser(props.user)
-				await mentionNotification.setPost(props.post)
+				await postNotification.setUser(props.userFrom)
+				await postNotification.setPost(props.post)
 
-				await notification.setMentionNotification(mentionNotification)
-				await notification.setUser(user)
+				await notification.setPostNotification(postNotification)
+				await notification.setUser(userTo)
 
 				let reloadedNotification = await notification.reload({
 					include: [{
-						model: MentionNotification,
+						model: PostNotification,
 						include: [Post, { model: User, attributes: ['createdAt', 'username', 'color'] }]
 					}]
 				})

+ 13 - 0
models/post_notification.js

@@ -0,0 +1,13 @@
+module.exports = (sequelize, DataTypes) => {
+	let PostNotification = sequelize.define('PostNotification', {}, {
+		classMethods: {
+			associate (models) {
+				PostNotification.belongsTo(models.User)
+				PostNotification.belongsTo(models.Post)
+				PostNotification.belongsTo(models.Notification)
+			}
+		}
+	})
+
+	return PostNotification
+}

+ 2 - 2
routes/notification.js

@@ -2,7 +2,7 @@ let express = require('express')
 let router = express.Router()
 
 const Errors = require('../lib/errors')
-let { Notification, User, Post, MentionNotification } = require('../models')
+let { Notification, User, Post, PostNotification } = require('../models')
 
 router.all('*', (req, res, next) => {
 	if(req.session.loggedIn) {
@@ -23,7 +23,7 @@ router.get('/', async (req, res) => {
 			},
 			order: [['id', 'DESC']],
 			include: [{
-				model: MentionNotification,
+				model: PostNotification,
 				include: [Post, { model: User, attributes: ['createdAt', 'username', 'color'] }]
 			}]
 		})

+ 23 - 3
routes/post.js

@@ -132,7 +132,7 @@ router.post('/', async (req, res) => {
 		if(req.body.replyingToId) {
 			replyingToPost = await Post.findById(
 				req.body.replyingToId,
-				{ include: [Thread] }
+				{ include: [Thread, { model: User, attributes: ['username'] }] }
 			)
 
 			if(!replyingToPost) {
@@ -144,6 +144,21 @@ router.post('/', async (req, res) => {
 
 				await post.setReplyingTo(replyingToPost)
 				await replyingToPost.addReplies(post)
+
+				let replyNotification = await Notification.createPostNotification({
+					usernameTo: replyingToPost.User.username,
+					userFrom: user,
+					type: 'reply',
+					post: replyingToPost
+				})
+				
+				let ioUsers = req.app.get('io-users')
+				if(ioUsers[replyingToPost.User.username]) {
+					req.app
+						.get('io')
+						.to(ioUsers[replyingToPost.User.username])
+						.emit('notification', replyNotification.toJSON())
+				}
 			}
 		} else {
 			post = await Post.create({ content: req.body.content, postNumber: thread.postsCount })
@@ -159,10 +174,15 @@ router.post('/', async (req, res) => {
 				let mention = req.body.mentions[i]
 				let ioUsers = req.app.get('io-users')
 
-				let notification = await Notification.createMention({ mention, user, post })
+				let mentionNotification = await Notification.createPostNotification({
+					usernameTo: mention,
+					userFrom: user,
+					type: 'mention',
+					post
+				})
 				
 				if(ioUsers[mention]) {
-					req.app.get('io').to(ioUsers[mention]).emit('notification', notification.toJSON())
+					req.app.get('io').to(ioUsers[mention]).emit('notification', mentionNotification.toJSON())
 				}
 			}
 		}