Merge pull request #6566 from Ocelot-Social-Community/unread-rooms-query

feat(backend): unread rooms query
This commit is contained in:
mahula 2023-07-17 12:57:48 +02:00 committed by GitHub
commit 7198122ebf
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 196 additions and 6 deletions

View File

@ -30,3 +30,11 @@ export const roomQuery = () => {
}
`
}
export const unreadRoomsQuery = () => {
return gql`
query {
UnreadRooms
}
`
}

View File

@ -408,6 +408,7 @@ export default shield(
getInviteCode: isAuthenticated, // and inviteRegistration
Room: isAuthenticated,
Message: isAuthenticated,
UnreadRooms: isAuthenticated,
},
Mutation: {
'*': deny,

View File

@ -1,7 +1,8 @@
import { createTestClient } from 'apollo-server-testing'
import Factory, { cleanDatabase } from '../../db/factories'
import { getNeode, getDriver } from '../../db/neo4j'
import { createRoomMutation, roomQuery } from '../../graphql/rooms'
import { createRoomMutation, roomQuery, unreadRoomsQuery } from '../../graphql/rooms'
import { createMessageMutation } from '../../graphql/messages'
import createServer from '../../server'
const driver = getDriver()
@ -34,6 +35,8 @@ afterAll(async () => {
})
describe('Room', () => {
let roomId: string
beforeAll(async () => {
;[chattingUser, otherChattingUser, notChattingUser] = await Promise.all([
Factory.build('user', {
@ -76,8 +79,6 @@ describe('Room', () => {
})
describe('authenticated', () => {
let roomId: string
beforeAll(async () => {
authenticatedUser = await chattingUser.toJson()
})
@ -269,6 +270,117 @@ describe('Room', () => {
})
})
describe('unread rooms query', () => {
describe('unauthenticated', () => {
it('throws authorization error', async () => {
authenticatedUser = null
await expect(
query({
query: unreadRoomsQuery(),
}),
).resolves.toMatchObject({
errors: [{ message: 'Not Authorized!' }],
})
})
})
describe('authenticated', () => {
let otherRoomId: string
beforeAll(async () => {
authenticatedUser = await chattingUser.toJson()
const result = await mutate({
mutation: createRoomMutation(),
variables: {
userId: 'not-chatting-user',
},
})
otherRoomId = result.data.CreateRoom.roomId
await mutate({
mutation: createMessageMutation(),
variables: {
roomId: otherRoomId,
content: 'Message to not chatting user',
},
})
await mutate({
mutation: createMessageMutation(),
variables: {
roomId,
content: '1st message to other chatting user',
},
})
await mutate({
mutation: createMessageMutation(),
variables: {
roomId,
content: '2nd message to other chatting user',
},
})
authenticatedUser = await otherChattingUser.toJson()
const result2 = await mutate({
mutation: createRoomMutation(),
variables: {
userId: 'not-chatting-user',
},
})
otherRoomId = result2.data.CreateRoom.roomId
await mutate({
mutation: createMessageMutation(),
variables: {
roomId: otherRoomId,
content: 'Other message to not chatting user',
},
})
})
describe('as chatting user', () => {
it('has 0 unread rooms', async () => {
authenticatedUser = await chattingUser.toJson()
await expect(
query({
query: unreadRoomsQuery(),
}),
).resolves.toMatchObject({
data: {
UnreadRooms: 0,
},
})
})
})
describe('as other chatting user', () => {
it('has 1 unread rooms', async () => {
authenticatedUser = await otherChattingUser.toJson()
await expect(
query({
query: unreadRoomsQuery(),
}),
).resolves.toMatchObject({
data: {
UnreadRooms: 1,
},
})
})
})
describe('as not chatting user', () => {
it('has 2 unread rooms', async () => {
authenticatedUser = await notChattingUser.toJson()
await expect(
query({
query: unreadRoomsQuery(),
}),
).resolves.toMatchObject({
data: {
UnreadRooms: 2,
},
})
})
})
})
})
describe('query several rooms', () => {
beforeAll(async () => {
authenticatedUser = await chattingUser.toJson()
@ -287,7 +399,7 @@ describe('Room', () => {
})
it('returns the rooms paginated', async () => {
expect(await query({ query: roomQuery(), variables: { first: 2, offset: 0 } })).toMatchObject(
expect(await query({ query: roomQuery(), variables: { first: 3, offset: 0 } })).toMatchObject(
{
errors: undefined,
data: {
@ -338,11 +450,34 @@ describe('Room', () => {
},
]),
},
{
id: expect.any(String),
roomId: expect.any(String),
roomName: 'Not Chatting User',
users: expect.arrayContaining([
{
_id: 'chatting-user',
id: 'chatting-user',
name: 'Chatting User',
avatar: {
url: expect.any(String),
},
},
{
_id: 'not-chatting-user',
id: 'not-chatting-user',
name: 'Not Chatting User',
avatar: {
url: expect.any(String),
},
},
]),
},
],
},
},
)
expect(await query({ query: roomQuery(), variables: { first: 2, offset: 2 } })).toMatchObject(
expect(await query({ query: roomQuery(), variables: { first: 3, offset: 3 } })).toMatchObject(
{
errors: undefined,
data: {

View File

@ -25,6 +25,27 @@ export default {
}
return resolved
},
UnreadRooms: async (object, params, context, resolveInfo) => {
const {
user: { id: currentUserId },
} = context
const session = context.driver.session()
const readTxResultPromise = session.readTransaction(async (transaction) => {
const unreadRoomsCypher = `
MATCH (:User { id: $currentUserId })-[:CHATS_IN]->(room:Room)<-[:INSIDE]-(message:Message)<-[:CREATED]-(sender:User)
WHERE NOT sender.id = $currentUserId AND NOT message.seen
RETURN toString(COUNT(DISTINCT room)) AS count
`
const unreadRoomsTxResponse = await transaction.run(unreadRoomsCypher, { currentUserId })
return unreadRoomsTxResponse.records.map((record) => record.get('count'))[0]
})
try {
const count = await readTxResultPromise
return count
} finally {
session.close()
}
},
},
Mutation: {
CreateRoom: async (_parent, params, context, _resolveInfo) => {

View File

@ -33,4 +33,5 @@ type Query {
id: ID
orderBy: [_RoomOrdering]
): [Room]
UnreadRooms: Int
}

View File

@ -8,18 +8,34 @@
placement: 'bottom-start',
}"
>
<counter-icon icon="chat-bubble" :count="1" danger />
<counter-icon icon="chat-bubble" :count="count" danger />
</base-button>
</nuxt-link>
</template>
<script>
import CounterIcon from '~/components/_new/generic/CounterIcon/CounterIcon'
import { unreadRoomsQuery } from '~/graphql/Rooms'
export default {
name: 'ChatNotificationMenu',
components: {
CounterIcon,
},
data() {
return {
count: 0,
}
},
apollo: {
UnreadRooms: {
query() {
return unreadRoomsQuery()
},
update({ UnreadRooms }) {
this.count = UnreadRooms
},
},
},
}
</script>

View File

@ -27,3 +27,11 @@ export const createRoom = () => gql`
}
}
`
export const unreadRoomsQuery = () => {
return gql`
query {
UnreadRooms
}
`
}