From 1c6a5503dbfad133f80e379d53b09d5435a56486 Mon Sep 17 00:00:00 2001 From: mattwr18 Date: Wed, 5 Feb 2020 19:06:55 +0100 Subject: [PATCH] Get subscriptions working with notifyUserOfComment - Co-Authored-By: Tirokk --- .../notifications/notificationsMiddleware.js | 40 ++++++++++--------- backend/src/schema/resolvers/notifications.js | 12 +++--- 2 files changed, 27 insertions(+), 25 deletions(-) diff --git a/backend/src/middleware/notifications/notificationsMiddleware.js b/backend/src/middleware/notifications/notificationsMiddleware.js index a91ed2c06..9890979e9 100644 --- a/backend/src/middleware/notifications/notificationsMiddleware.js +++ b/backend/src/middleware/notifications/notificationsMiddleware.js @@ -54,7 +54,7 @@ const notifyUsersOfMention = async (label, id, idsOfUsers, reason, context) => { AND NOT (user)-[:BLOCKED]-(author) MERGE (post)-[notification:NOTIFIED {reason: $reason}]->(user) WITH notification, user, - post {.*, __typename: labels(post)[0], author: properties(author) } as finalResource + post {.*, __typename: labels(post)[0], author: properties(author) } AS finalResource ` break } @@ -67,7 +67,7 @@ const notifyUsersOfMention = async (label, id, idsOfUsers, reason, context) => { AND NOT (user)-[:BLOCKED]-(postAuthor) MERGE (comment)-[notification:NOTIFIED {reason: $reason}]->(user) WITH notification, user, - comment {.*, __typename: labels(comment)[0], author: properties(commenter), post: post {.*, author: properties(postAuthor)} } as finalResource + comment {.*, __typename: labels(comment)[0], author: properties(commenter), post: post {.*, author: properties(postAuthor)} } AS finalResource ` break } @@ -101,24 +101,26 @@ const notifyUsersOfMention = async (label, id, idsOfUsers, reason, context) => { const notifyUsersOfComment = async (label, commentId, postAuthorId, reason, context) => { await validateNotifyUsers(label, reason) const session = context.driver.session() - + const writeTxResultPromise = await session.writeTransaction(async transaction => { + const notificationTransactionResponse = await transaction.run( + ` + MATCH (postAuthor:User {id: $postAuthorId})-[:WROTE]->(post:Post)<-[:COMMENTS]-(comment:Comment { id: $commentId })<-[:WROTE]-(commenter:User) + WHERE NOT (postAuthor)-[:BLOCKED]-(commenter) + MERGE (comment)-[notification:NOTIFIED {reason: $reason}]->(postAuthor) + SET notification.read = FALSE + SET notification.createdAt = COALESCE(notification.createdAt, toString(datetime())) + SET notification.updatedAt = toString(datetime()) + WITH notification, postAuthor, post, + comment {.*, __typename: labels(comment)[0], author: properties(commenter), post: post {.*, author: properties(postAuthor) } } AS finalResource + RETURN notification {.*, from: finalResource, to: properties(postAuthor)} + `, + { commentId, postAuthorId, reason }, + ) + return notificationTransactionResponse.records.map(record => record.get('notification')) + }) try { - await session.writeTransaction(async transaction => { - await transaction.run( - ` - MATCH (postAuthor:User {id: $postAuthorId})-[:WROTE]->(post:Post)<-[:COMMENTS]-(comment:Comment { id: $commentId })<-[:WROTE]-(commenter:User) - WHERE NOT (postAuthor)-[:BLOCKED]-(commenter) - MERGE (comment)-[notification:NOTIFIED {reason: $reason}]->(postAuthor) - SET notification.read = FALSE - SET ( - CASE - WHEN notification.createdAt IS NULL - THEN notification END ).createdAt = toString(datetime()) - SET notification.updatedAt = toString(datetime()) - `, - { commentId, postAuthorId, reason }, - ) - }) + const [notification] = await writeTxResultPromise + return pubsub.publish(NOTIFICATION_ADDED, { notificationAdded: notification }) } finally { session.close() } diff --git a/backend/src/schema/resolvers/notifications.js b/backend/src/schema/resolvers/notifications.js index 50c6798aa..116ac3e0e 100644 --- a/backend/src/schema/resolvers/notifications.js +++ b/backend/src/schema/resolvers/notifications.js @@ -51,10 +51,10 @@ export default { MATCH (resource {deleted: false, disabled: false})-[notification:NOTIFIED]->(user:User {id:$id}) ${whereClause} WITH user, notification, resource, - [(resource)<-[:WROTE]-(author:User) | author {.*}] as authors, - [(resource)-[:COMMENTS]->(post:Post)<-[:WROTE]-(author:User) | post{.*, author: properties(author)} ] as posts + [(resource)<-[:WROTE]-(author:User) | author {.*}] AS authors, + [(resource)-[:COMMENTS]->(post:Post)<-[:WROTE]-(author:User) | post{.*, author: properties(author)} ] AS posts WITH resource, user, notification, authors, posts, - resource {.*, __typename: labels(resource)[0], author: authors[0], post: posts[0]} as finalResource + resource {.*, __typename: labels(resource)[0], author: authors[0], post: posts[0]} AS finalResource RETURN notification {.*, from: finalResource, to: properties(user)} ${orderByClause} ${offset} ${limit} @@ -82,10 +82,10 @@ export default { MATCH (resource {id: $resourceId})-[notification:NOTIFIED {read: FALSE}]->(user:User {id:$id}) SET notification.read = TRUE WITH user, notification, resource, - [(resource)<-[:WROTE]-(author:User) | author {.*}] as authors, - [(resource)-[:COMMENTS]->(post:Post)<-[:WROTE]-(author:User) | post{.*, author: properties(author)} ] as posts + [(resource)<-[:WROTE]-(author:User) | author {.*}] AS authors, + [(resource)-[:COMMENTS]->(post:Post)<-[:WROTE]-(author:User) | post{.*, author: properties(author)} ] AS posts WITH resource, user, notification, authors, posts, - resource {.*, __typename: labels(resource)[0], author: authors[0], post: posts[0]} as finalResource + resource {.*, __typename: labels(resource)[0], author: authors[0], post: posts[0]} AS finalResource RETURN notification {.*, from: finalResource, to: properties(user)} `, { resourceId: args.id, id: currentUser.id },