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 👍
This commit is contained in:
roschaefer 2019-10-31 16:11:05 +01:00
parent b4a9e3e551
commit cbc547a86b
2 changed files with 20 additions and 19 deletions

View File

@ -1,5 +1,21 @@
import extractMentionedUsers from './mentions/extractMentionedUsers' 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) => { const notifyUsers = async (label, id, idsOfUsers, reason, context) => {
if (!idsOfUsers.length) return 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 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) const comment = await resolve(root, args, context, resolveInfo)
if (comment) { if (comment) {
const session = context.driver.session() const postAuthor = await postAuthorOfComment(comment, { context })
const cypherFindUser = ` idsOfUsers = idsOfUsers.filter(id => id !== postAuthor.id)
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)
}
await notifyUsers('Comment', comment.id, idsOfUsers, 'mentioned_in_comment', context) await notifyUsers('Comment', comment.id, idsOfUsers, 'mentioned_in_comment', context)
} }

View File

@ -479,7 +479,7 @@ describe('notifications', () => {
}) })
beforeEach(async () => { 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.' postContent = 'Content of post where I get mentioned in a comment.'
postAuthor = notifiedUser postAuthor = notifiedUser
}) })