diff --git a/backend/src/middleware/notifications/notificationsMiddleware.js b/backend/src/middleware/notifications/notificationsMiddleware.js index 87818c5d7..a91ed2c06 100644 --- a/backend/src/middleware/notifications/notificationsMiddleware.js +++ b/backend/src/middleware/notifications/notificationsMiddleware.js @@ -1,10 +1,6 @@ import extractMentionedUsers from './mentions/extractMentionedUsers' import { validateNotifyUsers } from '../validation/validationMiddleware' -import { - pubsub, - NOTIFICATION_ADDED, - transformReturnType, -} from '../../schema/resolvers/notifications' +import { pubsub, NOTIFICATION_ADDED } from '../../schema/resolvers/notifications' const handleContentDataOfPost = async (resolve, root, args, context, resolveInfo) => { const idsOfUsers = extractMentionedUsers(args.content) @@ -57,31 +53,31 @@ const notifyUsersOfMention = async (label, id, idsOfUsers, reason, context) => { WHERE user.id in $idsOfUsers AND NOT (user)-[:BLOCKED]-(author) MERGE (post)-[notification:NOTIFIED {reason: $reason}]->(user) - WITH notification, post AS resource, user + WITH notification, user, + post {.*, __typename: labels(post)[0], author: properties(author) } as finalResource ` break } case 'mentioned_in_comment': { mentionedCypher = ` - MATCH (postAuthor: User)-[:WROTE]->(post: Post)<-[:COMMENTS]-(comment: Comment { id: $id })<-[:WROTE]-(author: User) + MATCH (postAuthor: User)-[:WROTE]->(post: Post)<-[:COMMENTS]-(comment: Comment { id: $id })<-[:WROTE]-(commenter: User) MATCH (user: User) WHERE user.id in $idsOfUsers - AND NOT (user)-[:BLOCKED]-(author) + AND NOT (user)-[:BLOCKED]-(commenter) AND NOT (user)-[:BLOCKED]-(postAuthor) MERGE (comment)-[notification:NOTIFIED {reason: $reason}]->(user) - WITH notification, comment AS resource, user + WITH notification, user, + comment {.*, __typename: labels(comment)[0], author: properties(commenter), post: post {.*, author: properties(postAuthor)} } as finalResource ` break } } mentionedCypher += ` + WITH notification, finalResource, user SET notification.read = FALSE - SET ( - CASE - WHEN notification.createdAt IS NULL - THEN notification END ).createdAt = toString(datetime()) + SET notification.createdAt = COALESCE(notification.createdAt, toString(datetime())) SET notification.updatedAt = toString(datetime()) - RETURN notification, resource, user, labels(resource)[0] AS type + RETURN notification {.*, from: finalResource, to: properties(user)} ` const session = context.driver.session() const writeTxResultPromise = session.writeTransaction(async transaction => { @@ -90,7 +86,7 @@ const notifyUsersOfMention = async (label, id, idsOfUsers, reason, context) => { idsOfUsers, reason, }) - return notificationTransactionResponse.records.map(transformReturnType) + return notificationTransactionResponse.records.map(record => record.get('notification')) }) try { const [notification] = await writeTxResultPromise diff --git a/backend/src/schema/resolvers/notifications.js b/backend/src/schema/resolvers/notifications.js index c8d36b034..50c6798aa 100644 --- a/backend/src/schema/resolvers/notifications.js +++ b/backend/src/schema/resolvers/notifications.js @@ -4,18 +4,6 @@ import { withFilter } from 'graphql-subscriptions' export const pubsub = new PubSub() export const NOTIFICATION_ADDED = 'NOTIFICATION_ADDED' -export const transformReturnType = record => { - return { - ...record.get('notification').properties, - from: { - __typename: record.get('type'), - ...record.get('resource').properties, - }, - to: { - ...record.get('user').properties, - }, - } -} export default { Subscription: { @@ -93,12 +81,19 @@ export default { ` MATCH (resource {id: $resourceId})-[notification:NOTIFIED {read: FALSE}]->(user:User {id:$id}) SET notification.read = TRUE - RETURN resource, notification, user, labels(resource)[0] AS type + WITH user, notification, resource, + [(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 + RETURN notification {.*, from: finalResource, to: properties(user)} `, { resourceId: args.id, id: currentUser.id }, ) log(markNotificationAsReadTransactionResponse) - return markNotificationAsReadTransactionResponse.records.map(transformReturnType) + return markNotificationAsReadTransactionResponse.records.map(record => + record.get('notification'), + ) }) try { const [notifications] = await writeTxResultPromise