From ea28f4afebf9453c7bb6671b3c3ba2cba4c8e627 Mon Sep 17 00:00:00 2001 From: Moriz Wahl Date: Mon, 17 Jul 2023 17:33:51 +0200 Subject: [PATCH] recipient user of message to send subscription, test subscription --- backend/src/schema/resolvers/messages.spec.ts | 17 ++++++++++++++--- backend/src/schema/resolvers/messages.ts | 18 ++++++++++-------- 2 files changed, 24 insertions(+), 11 deletions(-) diff --git a/backend/src/schema/resolvers/messages.spec.ts b/backend/src/schema/resolvers/messages.spec.ts index 628034be5..07ae2dd85 100644 --- a/backend/src/schema/resolvers/messages.spec.ts +++ b/backend/src/schema/resolvers/messages.spec.ts @@ -3,11 +3,13 @@ import Factory, { cleanDatabase } from '../../db/factories' import { getNeode, getDriver } from '../../db/neo4j' import { createRoomMutation, roomQuery } from '../../graphql/rooms' import { createMessageMutation, messageQuery, markMessagesAsSeen } from '../../graphql/messages' -import createServer from '../../server' +import createServer, { pubsub } from '../../server' const driver = getDriver() const neode = getNeode() +const pubsubSpy = jest.spyOn(pubsub, 'publish') + let query let mutate let authenticatedUser @@ -58,6 +60,10 @@ describe('Message', () => { }) describe('create message', () => { + beforeEach(() => { + jest.clearAllMocks() + }) + describe('unauthenticated', () => { it('throws authorization error', async () => { await expect( @@ -80,7 +86,7 @@ describe('Message', () => { }) describe('room does not exist', () => { - it('returns null', async () => { + it('returns null and does not publish subscription', async () => { await expect( mutate({ mutation: createMessageMutation(), @@ -95,6 +101,7 @@ describe('Message', () => { CreateMessage: null, }, }) + expect(pubsubSpy).not.toBeCalled() }) }) @@ -110,7 +117,7 @@ describe('Message', () => { }) describe('user chats in room', () => { - it('returns the message', async () => { + it('returns the message and publishes subscription', async () => { await expect( mutate({ mutation: createMessageMutation(), @@ -135,6 +142,10 @@ describe('Message', () => { }, }, }) + expect(pubsubSpy).toBeCalledWith('ROOM_COUNT_UPDATED', { + roomCountUpdated: '1', + user: 'other-chatting-user', + }) }) describe('room is updated as well', () => { diff --git a/backend/src/schema/resolvers/messages.ts b/backend/src/schema/resolvers/messages.ts index 785b8d99c..ea2ed3614 100644 --- a/backend/src/schema/resolvers/messages.ts +++ b/backend/src/schema/resolvers/messages.ts @@ -58,8 +58,10 @@ export default { const createMessageCypher = ` MATCH (currentUser:User { id: $currentUserId })-[:CHATS_IN]->(room:Room { id: $roomId }) OPTIONAL MATCH (currentUser)-[:AVATAR_IMAGE]->(image:Image) - OPTIONAL MATCH (m:Message)-[:INSIDE]->(room)<-[:CHATS_IN]-(otherUser:User) - WITH MAX(m.indexId) as maxIndex, room, currentUser, image, otherUser + OPTIONAL MATCH (m:Message)-[:INSIDE]->(room) + OPTIONAL MATCH (room)<-[:CHATS_IN]-(recipientUser:User) + WHERE NOT recipientUser.id = $currentUserId + WITH MAX(m.indexId) as maxIndex, room, currentUser, image, recipientUser CREATE (currentUser)-[:CREATED]->(message:Message { createdAt: toString(datetime()), id: apoc.create.uuid(), @@ -72,8 +74,7 @@ export default { SET room.lastMessageAt = toString(datetime()) RETURN message { .*, - room: properties(room), - otherUser: properties(otherUser), + recipientId: recipientUser.id, senderId: currentUser.id, username: currentUser.name, avatar: image.url, @@ -94,11 +95,12 @@ export default { }) try { const message = await writeTxResultPromise + if (message) { + const roomCountUpdated = await getUnreadRoomsCount(message.recipientId, session) - const roomCountUpdated = await getUnreadRoomsCount(currentUserId, session) - - // send subscriptions - await pubsub.publish(ROOM_COUNT_UPDATED, { roomCountUpdated, user: message.otherUser }) + // send subscriptions + await pubsub.publish(ROOM_COUNT_UPDATED, { roomCountUpdated, user: message.recipientId }) + } return message } catch (error) {