add saved, distributed and seen props. Handle distributed

This commit is contained in:
Moriz Wahl 2023-07-12 11:01:17 +02:00
parent eeaa166f73
commit 2ea77b784d
4 changed files with 57 additions and 1 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,6 +25,9 @@ export const messageQuery = () => {
username username
avatar avatar
date date
saved
distributed
seen
} }
} }
` `

View File

@ -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,
}), }),
]), ]),
}, },

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 { .* }
` `

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 {