mirror of
https://github.com/IT4Change/Ocelot-Social.git
synced 2026-04-06 01:25:31 +00:00
subscription send on message query
This commit is contained in:
parent
846fe26091
commit
fb1d540eb2
@ -3,6 +3,21 @@ import Resolver from './helpers/Resolver'
|
|||||||
import { getUnreadRoomsCount } from './rooms'
|
import { getUnreadRoomsCount } from './rooms'
|
||||||
import { pubsub, ROOM_COUNT_UPDATED } from '../../server'
|
import { pubsub, ROOM_COUNT_UPDATED } from '../../server'
|
||||||
|
|
||||||
|
const setMessagesAsDistributed = async (undistributedMessagesIds, session) => {
|
||||||
|
return 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
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
export default {
|
export default {
|
||||||
Query: {
|
Query: {
|
||||||
Message: async (object, params, context, resolveInfo) => {
|
Message: async (object, params, context, resolveInfo) => {
|
||||||
@ -18,31 +33,27 @@ export default {
|
|||||||
|
|
||||||
const resolved = await neo4jgraphql(object, params, context, resolveInfo)
|
const resolved = await neo4jgraphql(object, params, context, resolveInfo)
|
||||||
|
|
||||||
|
console.log(resolved)
|
||||||
|
|
||||||
if (resolved) {
|
if (resolved) {
|
||||||
const undistributedMessagesIds = resolved
|
const undistributedMessagesIds = resolved
|
||||||
.filter((msg) => !msg.distributed && msg.senderId !== context.user.id)
|
.filter((msg) => !msg.distributed && msg.senderId !== context.user.id)
|
||||||
.map((msg) => msg.id)
|
.map((msg) => msg.id)
|
||||||
if (undistributedMessagesIds.length > 0) {
|
const session = context.driver.session()
|
||||||
const session = context.driver.session()
|
try {
|
||||||
const writeTxResultPromise = session.writeTransaction(async (transaction) => {
|
if (undistributedMessagesIds.length > 0) {
|
||||||
const setDistributedCypher = `
|
await setMessagesAsDistributed(undistributedMessagesIds, session)
|
||||||
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
|
const roomCountUpdated = await getUnreadRoomsCount(context.user.id, session)
|
||||||
|
console.log(roomCountUpdated)
|
||||||
|
|
||||||
|
// send subscriptions
|
||||||
|
console.log({ roomCountUpdated, user: context.user.id })
|
||||||
|
await pubsub.publish(ROOM_COUNT_UPDATED, { roomCountUpdated, user: context.user.id })
|
||||||
|
} finally {
|
||||||
|
session.close()
|
||||||
}
|
}
|
||||||
|
// send subscription to author to updated the messages
|
||||||
}
|
}
|
||||||
return resolved.reverse()
|
return resolved.reverse()
|
||||||
},
|
},
|
||||||
@ -99,6 +110,7 @@ export default {
|
|||||||
const roomCountUpdated = await getUnreadRoomsCount(message.recipientId, session)
|
const roomCountUpdated = await getUnreadRoomsCount(message.recipientId, session)
|
||||||
|
|
||||||
// send subscriptions
|
// send subscriptions
|
||||||
|
console.log({ roomCountUpdated, user: message.recipientId })
|
||||||
await pubsub.publish(ROOM_COUNT_UPDATED, { roomCountUpdated, user: message.recipientId })
|
await pubsub.publish(ROOM_COUNT_UPDATED, { roomCountUpdated, user: message.recipientId })
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -21,7 +21,7 @@ export default {
|
|||||||
subscribe: withFilter(
|
subscribe: withFilter(
|
||||||
() => pubsub.asyncIterator(ROOM_COUNT_UPDATED),
|
() => pubsub.asyncIterator(ROOM_COUNT_UPDATED),
|
||||||
(payload, variables) => {
|
(payload, variables) => {
|
||||||
return payload.user.id === variables.userId
|
return true // payload.user.id === variables.userId
|
||||||
},
|
},
|
||||||
),
|
),
|
||||||
},
|
},
|
||||||
|
|||||||
@ -19,8 +19,13 @@ type Room {
|
|||||||
|
|
||||||
roomId: String! @cypher(statement: "RETURN this.id")
|
roomId: String! @cypher(statement: "RETURN this.id")
|
||||||
roomName: String! @cypher(statement: "MATCH (this)<-[:CHATS_IN]-(user:User) WHERE NOT user.id = $cypherParams.currentUserId RETURN user.name")
|
roomName: String! @cypher(statement: "MATCH (this)<-[:CHATS_IN]-(user:User) WHERE NOT user.id = $cypherParams.currentUserId RETURN user.name")
|
||||||
avatar: String! @cypher(statement: "MATCH (this)<-[:CHATS_IN]-(user:User) WHERE NOT user.id = $cypherParams.currentUserId RETURN user.avatar.url")
|
avatar: String @cypher(statement: """
|
||||||
|
MATCH (this)<-[:CHATS_IN]-(user:User)
|
||||||
|
WHERE NOT user.id = $cypherParams.currentUserId
|
||||||
|
OPTIONAL MATCH (user)-[:AVATAR_IMAGE]->(image:Image)
|
||||||
|
RETURN image.url
|
||||||
|
""")
|
||||||
|
|
||||||
lastMessageAt: String
|
lastMessageAt: String
|
||||||
|
|
||||||
lastMessage: Message @cypher(statement: """
|
lastMessage: Message @cypher(statement: """
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user