mirror of
https://github.com/Ocelot-Social-Community/Ocelot-Social.git
synced 2025-12-13 07:46:06 +00:00
create message mutation returns full message object
This commit is contained in:
parent
b9b9fea1dd
commit
4310b3fa48
@ -6,6 +6,10 @@ export const createMessageMutation = () => {
|
|||||||
CreateMessage(roomId: $roomId, content: $content) {
|
CreateMessage(roomId: $roomId, content: $content) {
|
||||||
id
|
id
|
||||||
content
|
content
|
||||||
|
senderId
|
||||||
|
username
|
||||||
|
avatar
|
||||||
|
date
|
||||||
saved
|
saved
|
||||||
distributed
|
distributed
|
||||||
seen
|
seen
|
||||||
|
|||||||
@ -11,6 +11,13 @@ const setRoomProps = (room) => {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const setMessageProps = (message, context) => {
|
||||||
|
message._id = message.id
|
||||||
|
if (message.senderId !== context.user.id) {
|
||||||
|
message.distributed = true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
const roomProperties = async (resolve, root, args, context, info) => {
|
const roomProperties = async (resolve, root, args, context, info) => {
|
||||||
const resolved = await resolve(root, args, context, info)
|
const resolved = await resolve(root, args, context, info)
|
||||||
if (resolved) {
|
if (resolved) {
|
||||||
@ -25,9 +32,24 @@ const roomProperties = async (resolve, root, args, context, info) => {
|
|||||||
return resolved
|
return resolved
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const messageProperties = async (resolve, root, args, context, info) => {
|
||||||
|
const resolved = await resolve(root, args, context, info)
|
||||||
|
if (resolved) {
|
||||||
|
if (isArray(resolved)) {
|
||||||
|
resolved.forEach((message) => {
|
||||||
|
setMessageProps(message, context)
|
||||||
|
})
|
||||||
|
} else {
|
||||||
|
setMessageProps(resolved, context)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return resolved
|
||||||
|
}
|
||||||
|
|
||||||
export default {
|
export default {
|
||||||
Query: {
|
Query: {
|
||||||
Room: roomProperties,
|
Room: roomProperties,
|
||||||
|
Message: messageProperties,
|
||||||
},
|
},
|
||||||
Mutation: {
|
Mutation: {
|
||||||
CreateRoom: roomProperties,
|
CreateRoom: roomProperties,
|
||||||
|
|||||||
@ -125,6 +125,10 @@ 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',
|
||||||
|
senderId: 'chatting-user',
|
||||||
|
username: 'Chatting User',
|
||||||
|
avatar: expect.any(String),
|
||||||
|
date: expect.any(String),
|
||||||
saved: true,
|
saved: true,
|
||||||
distributed: false,
|
distributed: false,
|
||||||
seen: false,
|
seen: false,
|
||||||
|
|||||||
@ -40,12 +40,6 @@ export default {
|
|||||||
}
|
}
|
||||||
// send subscription to author to updated the messages
|
// send subscription to author to updated the messages
|
||||||
}
|
}
|
||||||
resolved.forEach((message) => {
|
|
||||||
message._id = message.id
|
|
||||||
if (message.senderId !== context.user.id) {
|
|
||||||
message.distributed = true
|
|
||||||
}
|
|
||||||
})
|
|
||||||
}
|
}
|
||||||
return resolved
|
return resolved
|
||||||
},
|
},
|
||||||
@ -60,6 +54,7 @@ export default {
|
|||||||
const writeTxResultPromise = session.writeTransaction(async (transaction) => {
|
const writeTxResultPromise = session.writeTransaction(async (transaction) => {
|
||||||
const createMessageCypher = `
|
const createMessageCypher = `
|
||||||
MATCH (currentUser:User { id: $currentUserId })-[:CHATS_IN]->(room:Room { id: $roomId })
|
MATCH (currentUser:User { id: $currentUserId })-[:CHATS_IN]->(room:Room { id: $roomId })
|
||||||
|
OPTIONAL MATCH (currentUser)-[:AVATAR_IMAGE]->(image:Image)
|
||||||
CREATE (currentUser)-[:CREATED]->(message:Message {
|
CREATE (currentUser)-[:CREATED]->(message:Message {
|
||||||
createdAt: toString(datetime()),
|
createdAt: toString(datetime()),
|
||||||
id: apoc.create.uuid(),
|
id: apoc.create.uuid(),
|
||||||
@ -69,7 +64,13 @@ export default {
|
|||||||
seen: false
|
seen: false
|
||||||
})-[:INSIDE]->(room)
|
})-[:INSIDE]->(room)
|
||||||
SET room.lastMessageAt = toString(datetime())
|
SET room.lastMessageAt = toString(datetime())
|
||||||
RETURN message { .* }
|
RETURN message {
|
||||||
|
.*,
|
||||||
|
senderId: currentUser.id,
|
||||||
|
username: currentUser.name,
|
||||||
|
avatar: image.url,
|
||||||
|
date: message.createdAt
|
||||||
|
}
|
||||||
`
|
`
|
||||||
const createMessageTxResponse = await transaction.run(createMessageCypher, {
|
const createMessageTxResponse = await transaction.run(createMessageCypher, {
|
||||||
currentUserId,
|
currentUserId,
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user