From 67d68db23109bae5740f600d564fe45f8ba805c1 Mon Sep 17 00:00:00 2001 From: mattwr18 Date: Fri, 13 Sep 2019 18:26:02 +0200 Subject: [PATCH] Update post resolver to fix embarrasing bugs - when a user updates a post, we should not override every property in our database with the new params, since we have read-only properties like createdAt that we don't want to go deleting aimlessly. --- .../mentions/extractMentionedUsers.js | 2 +- .../notifications/notificationsMiddleware.js | 26 +++++-------------- backend/src/schema/resolvers/notifications.js | 2 +- backend/src/schema/resolvers/posts.js | 10 +++---- .../NotificationMenu/NotificationMenu.vue | 13 +++++----- 5 files changed, 21 insertions(+), 32 deletions(-) diff --git a/backend/src/middleware/notifications/mentions/extractMentionedUsers.js b/backend/src/middleware/notifications/mentions/extractMentionedUsers.js index e245e84b5..3ba845043 100644 --- a/backend/src/middleware/notifications/mentions/extractMentionedUsers.js +++ b/backend/src/middleware/notifications/mentions/extractMentionedUsers.js @@ -1,6 +1,6 @@ import cheerio from 'cheerio' -export default function(content) { +export default content => { if (!content) return [] const $ = cheerio.load(content) const userIds = $('a.mention[data-mention-id]') diff --git a/backend/src/middleware/notifications/notificationsMiddleware.js b/backend/src/middleware/notifications/notificationsMiddleware.js index effb7f4d0..ad1012c7a 100644 --- a/backend/src/middleware/notifications/notificationsMiddleware.js +++ b/backend/src/middleware/notifications/notificationsMiddleware.js @@ -24,13 +24,9 @@ const notifyUsers = async (label, id, idsOfUsers, reason, context) => { MATCH (user: User) WHERE user.id in $idsOfUsers AND NOT (user)<-[:BLOCKED]-(author) - MERGE (post)-[notification:NOTIFIED {reason: $reason}]->(user) + CREATE (post)-[notification:NOTIFIED {reason: $reason}]->(user) SET notification.read = FALSE - SET notification.updatedAt = toString(datetime()) - SET ( - CASE - WHEN notification.createdAt IS NULL - THEN notification END ).createdAt = toString(datetime()) + SET notification.createdAt = toString(datetime()) ` break } @@ -41,13 +37,9 @@ const notifyUsers = async (label, id, idsOfUsers, reason, context) => { WHERE user.id in $idsOfUsers AND NOT (user)<-[:BLOCKED]-(author) AND NOT (user)<-[:BLOCKED]-(postAuthor) - MERGE (comment)-[notification:NOTIFIED {reason: $reason}]->(user) + CREATE (comment)-[notification:NOTIFIED {reason: $reason}]->(user) SET notification.read = FALSE - SET notification.updatedAt = toString(datetime()) - SET ( - CASE - WHEN notification.createdAt IS NULL - THEN notification END ).createdAt = toString(datetime()) + SET notification.createdAt = toString(datetime()) ` break } @@ -58,19 +50,14 @@ const notifyUsers = async (label, id, idsOfUsers, reason, context) => { WHERE user.id in $idsOfUsers AND NOT (user)<-[:BLOCKED]-(author) AND NOT (author)<-[:BLOCKED]-(user) - MERGE (comment)-[notification:NOTIFIED {reason: $reason}]->(user) + CREATE (comment)-[notification:NOTIFIED {reason: $reason}]->(user) SET notification.read = FALSE - SET notification.updatedAt = toString(datetime()) - SET ( - CASE - WHEN notification.createdAt IS NULL - THEN notification END ).createdAt = toString(datetime()) + SET notification.createdAt = toString(datetime()) ` break } } await session.run(cypher, { - label, id, idsOfUsers, reason, @@ -92,6 +79,7 @@ const handleContentDataOfPost = async (resolve, root, args, context, resolveInfo const handleContentDataOfComment = async (resolve, root, args, context, resolveInfo) => { const idsOfUsers = extractMentionedUsers(args.content) + const comment = await resolve(root, args, context, resolveInfo) if (comment) { diff --git a/backend/src/schema/resolvers/notifications.js b/backend/src/schema/resolvers/notifications.js index 0219df02c..3c1adbc5c 100644 --- a/backend/src/schema/resolvers/notifications.js +++ b/backend/src/schema/resolvers/notifications.js @@ -15,7 +15,7 @@ const transformReturnType = record => { export default { Query: { - notifications: async (parent, args, context, resolveInfo) => { + notifications: async (_parent, args, context, _resolveInfo) => { const { user: currentUser } = context const session = context.driver.session() let notifications diff --git a/backend/src/schema/resolvers/posts.js b/backend/src/schema/resolvers/posts.js index 2be17d3ec..e65fa9b76 100644 --- a/backend/src/schema/resolvers/posts.js +++ b/backend/src/schema/resolvers/posts.js @@ -74,7 +74,7 @@ export default { }, }, Mutation: { - CreatePost: async (object, params, context, resolveInfo) => { + CreatePost: async (_parent, params, context, _resolveInfo) => { const { categoryIds } = params delete params.categoryIds params = await fileUpload(params, { file: 'imageUpload', url: 'image' }) @@ -110,14 +110,14 @@ export default { return post }, - UpdatePost: async (object, params, context, resolveInfo) => { + UpdatePost: async (_parent, params, context, _resolveInfo) => { const { categoryIds } = params delete params.categoryIds params = await fileUpload(params, { file: 'imageUpload', url: 'image' }) const session = context.driver.session() let updatePostCypher = `MATCH (post:Post {id: $params.id}) - SET post = $params + SET post += $params SET post.updatedAt = toString(datetime()) ` @@ -143,12 +143,12 @@ export default { const transactionRes = await session.run(updatePostCypher, updatePostVariables) const [post] = transactionRes.records.map(record => { - return record.get('post') + return record.get('post').properties }) session.close() - return post.properties + return post }, DeletePost: async (object, args, context, resolveInfo) => { diff --git a/webapp/components/notifications/NotificationMenu/NotificationMenu.vue b/webapp/components/notifications/NotificationMenu/NotificationMenu.vue index 51b90089f..0b71f3911 100644 --- a/webapp/components/notifications/NotificationMenu/NotificationMenu.vue +++ b/webapp/components/notifications/NotificationMenu/NotificationMenu.vue @@ -1,12 +1,13 @@