Merge branch 'master' into 6536-chat-remove-message-actions

This commit is contained in:
mahula 2023-07-13 16:03:38 +02:00 committed by GitHub
commit acf45ace4f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 165 additions and 2 deletions

View File

@ -6,6 +6,9 @@ export const createMessageMutation = () => {
CreateMessage(roomId: $roomId, content: $content) { CreateMessage(roomId: $roomId, content: $content) {
id id
content content
saved
distributed
seen
} }
} }
` `
@ -22,7 +25,18 @@ export const messageQuery = () => {
username username
avatar avatar
date date
saved
distributed
seen
} }
} }
` `
} }
export const markMessagesAsSeen = () => {
return gql`
mutation ($messageIds: [String!]) {
MarkMessagesAsSeen(messageIds: $messageIds)
}
`
}

View File

@ -463,6 +463,7 @@ export default shield(
saveCategorySettings: isAuthenticated, saveCategorySettings: isAuthenticated,
CreateRoom: isAuthenticated, CreateRoom: isAuthenticated,
CreateMessage: isAuthenticated, CreateMessage: isAuthenticated,
MarkMessagesAsSeen: isAuthenticated,
}, },
User: { User: {
email: or(isMyOwn, isAdmin), email: or(isMyOwn, isAdmin),

View File

@ -2,7 +2,7 @@ import { createTestClient } from 'apollo-server-testing'
import Factory, { cleanDatabase } from '../../db/factories' import Factory, { cleanDatabase } from '../../db/factories'
import { getNeode, getDriver } from '../../db/neo4j' import { getNeode, getDriver } from '../../db/neo4j'
import { createRoomMutation } from '../../graphql/rooms' import { createRoomMutation } from '../../graphql/rooms'
import { createMessageMutation, messageQuery } from '../../graphql/messages' import { createMessageMutation, messageQuery, markMessagesAsSeen } from '../../graphql/messages'
import createServer from '../../server' import createServer from '../../server'
const driver = getDriver() const driver = getDriver()
@ -122,6 +122,9 @@ describe('Message', () => {
CreateMessage: { CreateMessage: {
id: expect.any(String), id: expect.any(String),
content: 'Some nice message to other chatting user', content: 'Some nice message to other chatting user',
saved: true,
distributed: false,
seen: false,
}, },
}, },
}) })
@ -217,6 +220,9 @@ describe('Message', () => {
username: 'Chatting User', username: 'Chatting User',
avatar: expect.any(String), avatar: expect.any(String),
date: expect.any(String), date: expect.any(String),
saved: true,
distributed: true,
seen: false,
}, },
], ],
}, },
@ -261,6 +267,9 @@ describe('Message', () => {
username: 'Chatting User', username: 'Chatting User',
avatar: expect.any(String), avatar: expect.any(String),
date: expect.any(String), date: expect.any(String),
saved: true,
distributed: true,
seen: false,
}), }),
expect.objectContaining({ expect.objectContaining({
id: expect.any(String), id: expect.any(String),
@ -269,6 +278,9 @@ describe('Message', () => {
username: 'Other Chatting User', username: 'Other Chatting User',
avatar: expect.any(String), avatar: expect.any(String),
date: expect.any(String), date: expect.any(String),
saved: true,
distributed: true,
seen: false,
}), }),
expect.objectContaining({ expect.objectContaining({
id: expect.any(String), id: expect.any(String),
@ -277,6 +289,9 @@ describe('Message', () => {
username: 'Chatting User', username: 'Chatting User',
avatar: expect.any(String), avatar: expect.any(String),
date: expect.any(String), date: expect.any(String),
saved: true,
distributed: false,
seen: false,
}), }),
]), ]),
}, },
@ -308,4 +323,74 @@ describe('Message', () => {
}) })
}) })
}) })
describe('marks massges as seen', () => {
describe('unauthenticated', () => {
beforeAll(() => {
authenticatedUser = null
})
it('throws authorization error', async () => {
await expect(
mutate({
mutation: markMessagesAsSeen(),
variables: {
messageIds: ['some-id'],
},
}),
).resolves.toMatchObject({
errors: [{ message: 'Not Authorized!' }],
})
})
})
describe('authenticated', () => {
const messageIds: string[] = []
beforeAll(async () => {
authenticatedUser = await otherChattingUser.toJson()
const msgs = await query({
query: messageQuery(),
variables: {
roomId,
},
})
msgs.data.Message.forEach((m) => messageIds.push(m.id))
})
it('returns true', async () => {
await expect(
mutate({
mutation: markMessagesAsSeen(),
variables: {
messageIds,
},
}),
).resolves.toMatchObject({
errors: undefined,
data: {
MarkMessagesAsSeen: true,
},
})
})
it('has seen prop set to true', async () => {
await expect(
query({
query: messageQuery(),
variables: {
roomId,
},
}),
).resolves.toMatchObject({
data: {
Message: [
expect.objectContaining({ seen: true }),
expect.objectContaining({ seen: false }),
expect.objectContaining({ seen: true }),
],
},
})
})
})
})
}) })

View File

@ -14,9 +14,37 @@ export default {
}, },
} }
const resolved = await neo4jgraphql(object, params, context, resolveInfo) const resolved = await neo4jgraphql(object, params, context, resolveInfo)
if (resolved) { if (resolved) {
const undistributedMessagesIds = resolved
.filter((msg) => !msg.distributed && msg.senderId !== context.user.id)
.map((msg) => msg.id)
if (undistributedMessagesIds.length > 0) {
const session = context.driver.session()
const writeTxResultPromise = session.writeTransaction(async (transaction) => {
const setDistributedCypher = `
MATCH (m:Message) WHERE m.id IN $undistributedMessagesIds
SET m.distributed = true
RETURN m { .* }
`
const setDistributedTxResponse = await transaction.run(setDistributedCypher, {
undistributedMessagesIds,
})
const messages = await setDistributedTxResponse.records.map((record) => record.get('m'))
return messages
})
try {
await writeTxResultPromise
} finally {
session.close()
}
// send subscription to author to updated the messages
}
resolved.forEach((message) => { resolved.forEach((message) => {
message._id = message.id message._id = message.id
if (message.senderId !== context.user.id) {
message.distributed = true
}
}) })
} }
return resolved return resolved
@ -35,7 +63,10 @@ export default {
CREATE (currentUser)-[:CREATED]->(message:Message { CREATE (currentUser)-[:CREATED]->(message:Message {
createdAt: toString(datetime()), createdAt: toString(datetime()),
id: apoc.create.uuid(), id: apoc.create.uuid(),
content: $content content: $content,
saved: true,
distributed: false,
seen: false
})-[:INSIDE]->(room) })-[:INSIDE]->(room)
RETURN message { .* } RETURN message { .* }
` `
@ -58,6 +89,32 @@ export default {
session.close() session.close()
} }
}, },
MarkMessagesAsSeen: async (_parent, params, context, _resolveInfo) => {
const { messageIds } = params
const currentUserId = context.user.id
const session = context.driver.session()
const writeTxResultPromise = session.writeTransaction(async (transaction) => {
const setSeenCypher = `
MATCH (m:Message)<-[:CREATED]-(user:User)
WHERE m.id IN $messageIds AND NOT user.id = $currentUserId
SET m.seen = true
RETURN m { .* }
`
const setSeenTxResponse = await transaction.run(setSeenCypher, {
messageIds,
currentUserId,
})
const messages = await setSeenTxResponse.records.map((record) => record.get('m'))
return messages
})
try {
await writeTxResultPromise
// send subscription to author to updated the messages
return true
} finally {
session.close()
}
},
}, },
Message: { Message: {
...Resolver('Message', { ...Resolver('Message', {

View File

@ -16,6 +16,10 @@ type Message {
username: String! @cypher(statement: "MATCH (this)<-[:CREATED]-(user:User) RETURN user.name") username: String! @cypher(statement: "MATCH (this)<-[:CREATED]-(user:User) RETURN user.name")
avatar: String @cypher(statement: "MATCH (this)<-[:CREATED]-(:User)-[:AVATAR_IMAGE]->(image:Image) RETURN image.url") avatar: String @cypher(statement: "MATCH (this)<-[:CREATED]-(:User)-[:AVATAR_IMAGE]->(image:Image) RETURN image.url")
date: String! @cypher(statement: "RETURN this.createdAt") date: String! @cypher(statement: "RETURN this.createdAt")
saved: Boolean
distributed: Boolean
seen: Boolean
} }
type Mutation { type Mutation {
@ -23,6 +27,8 @@ type Mutation {
roomId: ID! roomId: ID!
content: String! content: String!
): Message ): Message
MarkMessagesAsSeen(messageIds: [String!]): Boolean
} }
type Query { type Query {