From cbc547a86b2599f80baba206ca25a39b29e5b5cc Mon Sep 17 00:00:00 2001 From: roschaefer Date: Thu, 31 Oct 2019 16:11:05 +0100 Subject: [PATCH] refactor: small refactoring for readability @KapilJ your solution is right now the best solution, I think. Probably the ideal solution would be if we could implement the `CreateComment` resolver in such a way that it is doing eager loading of the `comment->post->author` relationship and resolving a commment object which has the required objects all set. Then you wouldn't have to refetch all the stuff. But I think this is OK for now :+1: --- .../notifications/notificationsMiddleware.js | 37 ++++++++++--------- .../notificationsMiddleware.spec.js | 2 +- 2 files changed, 20 insertions(+), 19 deletions(-) diff --git a/backend/src/middleware/notifications/notificationsMiddleware.js b/backend/src/middleware/notifications/notificationsMiddleware.js index 2b3f1c2dd..718f0b1e4 100644 --- a/backend/src/middleware/notifications/notificationsMiddleware.js +++ b/backend/src/middleware/notifications/notificationsMiddleware.js @@ -1,5 +1,21 @@ import extractMentionedUsers from './mentions/extractMentionedUsers' +const postAuthorOfComment = async (comment, { context }) => { + const session = context.driver.session() + const cypherFindUser = ` + MATCH (user: User)-[:WROTE]->(:Post)<-[:COMMENTS]-(:Comment { id: $commentId }) + RETURN user { .id } + ` + const result = await session.run(cypherFindUser, { + commentId: comment.id, + }) + session.close() + const [postAuthor] = await result.records.map(record => { + return record.get('user') + }) + return postAuthor +} + const notifyUsers = async (label, id, idsOfUsers, reason, context) => { if (!idsOfUsers.length) return @@ -90,27 +106,12 @@ const handleContentDataOfPost = async (resolve, root, args, context, resolveInfo } const handleContentDataOfComment = async (resolve, root, args, context, resolveInfo) => { - const idsOfUsers = extractMentionedUsers(args.content) + let idsOfUsers = extractMentionedUsers(args.content) const comment = await resolve(root, args, context, resolveInfo) if (comment) { - const session = context.driver.session() - const cypherFindUser = ` - MATCH (user: User)-[:WROTE]->(:Post)<-[:COMMENTS]-(:Comment { id: $commentId }) - RETURN user { .id } - ` - const result = await session.run(cypherFindUser, { - commentId: comment.id, - }) - session.close() - const [postAuthor] = await result.records.map(record => { - return record.get('user') - }) - var index = idsOfUsers.indexOf(postAuthor.id) - - if (index > -1) { - idsOfUsers.splice(index) - } + const postAuthor = await postAuthorOfComment(comment, { context }) + idsOfUsers = idsOfUsers.filter(id => id !== postAuthor.id) await notifyUsers('Comment', comment.id, idsOfUsers, 'mentioned_in_comment', context) } diff --git a/backend/src/middleware/notifications/notificationsMiddleware.spec.js b/backend/src/middleware/notifications/notificationsMiddleware.spec.js index cabcefa19..18ee998db 100644 --- a/backend/src/middleware/notifications/notificationsMiddleware.spec.js +++ b/backend/src/middleware/notifications/notificationsMiddleware.spec.js @@ -479,7 +479,7 @@ describe('notifications', () => { }) beforeEach(async () => { - title = 'Post where Im the author and I get mentioned in a comment' + title = "Post where I'm the author and I get mentioned in a comment" postContent = 'Content of post where I get mentioned in a comment.' postAuthor = notifiedUser })