From 1827889582e1ddb8749dbcd228f4ab975682e151 Mon Sep 17 00:00:00 2001 From: mattwr18 Date: Thu, 5 Dec 2019 20:42:45 +0100 Subject: [PATCH] Refactor notificationsMiddleware/locations - start refactoring - locations does not have any automated tests, which makes it more difficult to refactor and have confidence that functionality will not be broken - notificationsMiddleware in progress --- backend/src/jwt/decode.js | 8 +- .../middleware/hashtags/hashtagsMiddleware.js | 2 +- backend/src/middleware/nodes/locations.js | 15 ++-- .../notifications/notificationsMiddleware.js | 83 ++++++------------- .../notificationsMiddleware.spec.js | 6 +- 5 files changed, 39 insertions(+), 75 deletions(-) diff --git a/backend/src/jwt/decode.js b/backend/src/jwt/decode.js index 3b0efea57..41e9d005e 100644 --- a/backend/src/jwt/decode.js +++ b/backend/src/jwt/decode.js @@ -23,14 +23,12 @@ export default async (driver, authorizationHeader) => { `, { id }, ) - return updateUserLastActiveTransactionResponse.records.map( - record => record.get('user'), - ) + return updateUserLastActiveTransactionResponse.records.map(record => record.get('user')) }) try { const [currentUser] = await writeTxResultPromise - if (!currentUser) return null - return { + if (!currentUser) return null + return { token, ...currentUser, } diff --git a/backend/src/middleware/hashtags/hashtagsMiddleware.js b/backend/src/middleware/hashtags/hashtagsMiddleware.js index cbb5e5320..7d8593fd5 100644 --- a/backend/src/middleware/hashtags/hashtagsMiddleware.js +++ b/backend/src/middleware/hashtags/hashtagsMiddleware.js @@ -3,7 +3,7 @@ import extractHashtags from '../hashtags/extractHashtags' const updateHashtagsOfPost = async (postId, hashtags, context) => { if (!hashtags.length) return const session = context.driver.session() - + try { await session.writeTransaction(txc => { return txc.run( diff --git a/backend/src/middleware/nodes/locations.js b/backend/src/middleware/nodes/locations.js index 3e0ca6855..d80d08a9a 100644 --- a/backend/src/middleware/nodes/locations.js +++ b/backend/src/middleware/nodes/locations.js @@ -38,7 +38,7 @@ const createLocation = async (session, mapboxData) => { lng: mapboxData.center && mapboxData.center.length ? mapboxData.center[1] : null, } - let query = + let mutation = 'MERGE (l:Location {id: $id}) ' + 'SET l.name = $nameEN, ' + 'l.nameEN = $nameEN, ' + @@ -53,12 +53,17 @@ const createLocation = async (session, mapboxData) => { 'l.type = $type' if (data.lat && data.lng) { - query += ', l.lat = $lat, l.lng = $lng' + mutation += ', l.lat = $lat, l.lng = $lng' } - query += ' RETURN l.id' + mutation += ' RETURN l.id' - await session.run(query, data) - session.close() + try { + await session.writeTransaction(transaction => { + return transaction.run(mutation, data) + }) + } finally { + session.close() + } } const createOrUpdateLocations = async (userId, locationName, driver) => { diff --git a/backend/src/middleware/notifications/notificationsMiddleware.js b/backend/src/middleware/notifications/notificationsMiddleware.js index ac199a67d..3d6d13790 100644 --- a/backend/src/middleware/notifications/notificationsMiddleware.js +++ b/backend/src/middleware/notifications/notificationsMiddleware.js @@ -1,23 +1,22 @@ import extractMentionedUsers from './mentions/extractMentionedUsers' -const postAuthorOfComment = async (comment, { context }) => { - const cypherFindUser = ` - MATCH (user: User)-[:WROTE]->(:Post)<-[:COMMENTS]-(:Comment { id: $commentId }) - RETURN user { .id } - ` +const postAuthorOfComment = async (commentId, { context }) => { const session = context.driver.session() - let result + let postAuthorId try { - result = await session.run(cypherFindUser, { - commentId: comment.id, + postAuthorId = await session.readTransaction(transaction => { + return transaction.run( + ` + MATCH (author:User)-[:WROTE]->(:Post)<-[:COMMENTS]-(:Comment { id: $commentId }) + RETURN author { .id } as authorId + `, + { commentId }, + ) }) + return postAuthorId.records.map(record => record.get('authorId')) } finally { session.close() } - const [postAuthor] = await result.records.map(record => { - return record.get('user') - }) - return postAuthor } const notifyUsers = async (label, id, idsOfUsers, reason, context) => { @@ -90,10 +89,8 @@ const notifyUsers = async (label, id, idsOfUsers, reason, context) => { } const session = context.driver.session() try { - await session.run(cypher, { - id, - idsOfUsers, - reason, + await session.writeTransaction(transaction => { + return transaction.run(cypher, { id, idsOfUsers, reason }) }) } finally { session.close() @@ -102,56 +99,24 @@ const notifyUsers = async (label, id, idsOfUsers, reason, context) => { const handleContentDataOfPost = async (resolve, root, args, context, resolveInfo) => { const idsOfUsers = extractMentionedUsers(args.content) - const post = await resolve(root, args, context, resolveInfo) - - if (post) { - await notifyUsers('Post', post.id, idsOfUsers, 'mentioned_in_post', context) - } - - return post + if (post) return notifyUsers('Post', post.id, idsOfUsers, 'mentioned_in_post', context) } const handleContentDataOfComment = async (resolve, root, args, context, resolveInfo) => { - let idsOfUsers = extractMentionedUsers(args.content) - const comment = await resolve(root, args, context, resolveInfo) - - if (comment) { - const postAuthor = await postAuthorOfComment(comment, { context }) - idsOfUsers = idsOfUsers.filter(id => id !== postAuthor.id) - - await notifyUsers('Comment', comment.id, idsOfUsers, 'mentioned_in_comment', context) - } - - return comment + const { content, id: commentId } = args + let idsOfUsers = extractMentionedUsers(content) + const [postAuthor] = await postAuthorOfComment(commentId, { context }) + idsOfUsers = idsOfUsers.filter(id => id !== postAuthor.id) + if (idsOfUsers && idsOfUsers.length) + await notifyUsers('Comment', commentId, idsOfUsers, 'mentioned_in_comment', context) + if (context.user.id !== postAuthor.id) + await notifyUsers('Comment', commentId, [postAuthor.id], 'commented_on_post', context) } const handleCreateComment = async (resolve, root, args, context, resolveInfo) => { - const comment = await handleContentDataOfComment(resolve, root, args, context, resolveInfo) - - if (comment) { - const cypherFindUser = ` - MATCH (user: User)-[:WROTE]->(:Post)<-[:COMMENTS]-(:Comment { id: $commentId }) - RETURN user { .id } - ` - const session = context.driver.session() - let result - try { - result = await session.run(cypherFindUser, { - commentId: comment.id, - }) - } finally { - session.close() - } - const [postAuthor] = await result.records.map(record => { - return record.get('user') - }) - if (context.user.id !== postAuthor.id) { - await notifyUsers('Comment', comment.id, [postAuthor.id], 'commented_on_post', context) - } - } - - return comment + const comment = await resolve(root, args, context, resolveInfo) + if (comment) return handleContentDataOfComment(resolve, root, args, context, resolveInfo) } export default { diff --git a/backend/src/middleware/notifications/notificationsMiddleware.spec.js b/backend/src/middleware/notifications/notificationsMiddleware.spec.js index 502ddaa8e..6fd94ab69 100644 --- a/backend/src/middleware/notifications/notificationsMiddleware.spec.js +++ b/backend/src/middleware/notifications/notificationsMiddleware.spec.js @@ -4,11 +4,7 @@ import { createTestClient } from 'apollo-server-testing' import { neode, getDriver } from '../../bootstrap/neo4j' import createServer from '../../server' -let server -let query -let mutate -let notifiedUser -let authenticatedUser +let server, query, mutate, notifiedUser, authenticatedUser const factory = Factory() const driver = getDriver() const instance = neode()