diff --git a/backend/src/middleware/notifications/notificationsMiddleware.ts b/backend/src/middleware/notifications/notificationsMiddleware.ts index 7922af90c..706e46c51 100644 --- a/backend/src/middleware/notifications/notificationsMiddleware.ts +++ b/backend/src/middleware/notifications/notificationsMiddleware.ts @@ -140,16 +140,18 @@ const postAuthorOfComment = async (commentId, { context }) => { const notifyOwnersOfGroup = async (groupId, userId, reason, context) => { const cypher = ` + MATCH (user:User { id: $userId }) MATCH (group:Group { id: $groupId })<-[membership:MEMBER_OF]-(owner:User) WHERE membership.role = 'owner' - WITH owner, group + WITH owner, group, user, membership MERGE (group)-[notification:NOTIFIED {reason: $reason}]->(owner) - WITH group, owner, notification + WITH group, owner, notification, user, membership SET notification.read = FALSE SET notification.createdAt = COALESCE(notification.createdAt, toString(datetime())) SET notification.updatedAt = toString(datetime()) SET notification.relatedUserId = $userId - RETURN notification {.*, from: group, to: properties(owner)} + WITH owner, group { __typename: 'Group', .*, myRole: membership.roleInGroup } AS finalGroup, user, notification + RETURN notification {.*, from: finalGroup, to: properties(owner), relatedUser: properties(user) } ` const session = context.driver.session() const writeTxResultPromise = session.writeTransaction(async (transaction) => { @@ -173,16 +175,20 @@ const notifyOwnersOfGroup = async (groupId, userId, reason, context) => { const notifyMemberOfGroup = async (groupId, userId, reason, context) => { const { user: owner } = context const cypher = ` + MATCH (owner:User { id: $ownerId }) MATCH (user:User { id: $userId }) MATCH (group:Group { id: $groupId }) - WITH user, group + OPTIONAL MATCH (user)-[membership:MEMBER_OF]->(group) + WITH user, group, owner, membership MERGE (group)-[notification:NOTIFIED {reason: $reason}]->(user) - WITH group, user, notification + WITH group, user, notification, owner, membership SET notification.read = FALSE SET notification.createdAt = COALESCE(notification.createdAt, toString(datetime())) SET notification.updatedAt = toString(datetime()) SET notification.relatedUserId = $ownerId - RETURN notification {.*, from: group, to: properties(user)} + WITH group { __typename: 'Group', .*, myRole: membership.roleInGroup } AS finalGroup, + notification, user, owner + RETURN notification {.*, from: finalGroup, to: properties(user), relatedUser: properties(owner) } ` const session = context.driver.session() const writeTxResultPromise = session.writeTransaction(async (transaction) => { @@ -242,7 +248,7 @@ const notifyUsersOfMention = async (label, id, idsOfUsers, reason, context) => { SET notification.read = FALSE SET notification.createdAt = COALESCE(notification.createdAt, toString(datetime())) SET notification.updatedAt = toString(datetime()) - RETURN notification {.*, from: finalResource, to: properties(user)} + RETURN notification {.*, from: finalResource, to: properties(user), relatedUser: properties(user) } ` const session = context.driver.session() const writeTxResultPromise = session.writeTransaction(async (transaction) => { @@ -276,9 +282,14 @@ const notifyUsersOfComment = async (label, commentId, postAuthorId, reason, cont SET notification.read = FALSE SET notification.createdAt = COALESCE(notification.createdAt, toString(datetime())) SET notification.updatedAt = toString(datetime()) - WITH notification, postAuthor, post, + WITH notification, postAuthor, post, commenter, comment {.*, __typename: labels(comment)[0], author: properties(commenter), post: post {.*, author: properties(postAuthor) } } AS finalResource - RETURN notification {.*, from: finalResource, to: properties(postAuthor)} + RETURN notification { + .*, + from: finalResource, + to: properties(postAuthor), + relatedUser: properties(commenter) + } `, { commentId, postAuthorId, reason }, ) diff --git a/backend/src/schema/resolvers/messages.spec.ts b/backend/src/schema/resolvers/messages.spec.ts index 45fb10609..a43bd3226 100644 --- a/backend/src/schema/resolvers/messages.spec.ts +++ b/backend/src/schema/resolvers/messages.spec.ts @@ -253,32 +253,32 @@ describe('Message', () => { ).resolves.toMatchObject({ errors: undefined, data: { - Message: [ - { + Message: expect.arrayContaining([ + expect.objectContaining({ id: expect.any(String), content: 'Some nice message to other chatting user', senderId: 'chatting-user', username: 'Chatting User', avatar: expect.any(String), date: expect.any(String), - }, - { + }), + expect.objectContaining({ id: expect.any(String), content: 'A nice response message to chatting user', senderId: 'other-chatting-user', username: 'Other Chatting User', avatar: expect.any(String), date: expect.any(String), - }, - { + }), + expect.objectContaining({ id: expect.any(String), content: 'And another nice message to other chatting user', senderId: 'chatting-user', username: 'Chatting User', avatar: expect.any(String), date: expect.any(String), - }, - ], + }), + ]), }, }) }) diff --git a/webapp/graphql/User.js b/webapp/graphql/User.js index f7fd3c940..fcdac0989 100644 --- a/webapp/graphql/User.js +++ b/webapp/graphql/User.js @@ -243,6 +243,7 @@ export const notificationAdded = () => { ${userFragment} ${commentFragment} ${postFragment} + ${groupFragment} subscription notifications($userId: ID!) { notificationAdded(userId: $userId) { @@ -251,6 +252,9 @@ export const notificationAdded = () => { reason createdAt updatedAt + to { + ...user + } from { __typename ... on Post { @@ -271,6 +275,12 @@ export const notificationAdded = () => { } } } + ... on Group { + ...group + } + } + relatedUser { + ...user } } }