mirror of
https://github.com/Ocelot-Social-Community/Ocelot-Social.git
synced 2025-12-13 07:46:06 +00:00
unread room count query as function
This commit is contained in:
parent
45db186dfd
commit
884ee1713c
@ -1,6 +1,6 @@
|
|||||||
import { neo4jgraphql } from 'neo4j-graphql-js'
|
import { neo4jgraphql } from 'neo4j-graphql-js'
|
||||||
import Resolver from './helpers/Resolver'
|
import Resolver from './helpers/Resolver'
|
||||||
import RoomResolver from './rooms'
|
import { getUnreadRoomsCount } from './rooms'
|
||||||
import { pubsub, ROOM_COUNT_UPDATED } from '../../server'
|
import { pubsub, ROOM_COUNT_UPDATED } from '../../server'
|
||||||
|
|
||||||
export default {
|
export default {
|
||||||
@ -90,16 +90,16 @@ export default {
|
|||||||
record.get('message'),
|
record.get('message'),
|
||||||
)
|
)
|
||||||
|
|
||||||
// TODO change user in context - mark message as seen - chattingUser is the correct user.
|
|
||||||
const roomCountUpdated = await RoomResolver.Query.UnreadRooms(null, null, context, null)
|
|
||||||
|
|
||||||
// send subscriptions
|
|
||||||
await pubsub.publish(ROOM_COUNT_UPDATED, { roomCountUpdated, user: message.otherUser })
|
|
||||||
|
|
||||||
return message
|
return message
|
||||||
})
|
})
|
||||||
try {
|
try {
|
||||||
const message = await writeTxResultPromise
|
const message = await writeTxResultPromise
|
||||||
|
|
||||||
|
const roomCountUpdated = await getUnreadRoomsCount(currentUserId, session)
|
||||||
|
|
||||||
|
// send subscriptions
|
||||||
|
await pubsub.publish(ROOM_COUNT_UPDATED, { roomCountUpdated, user: message.otherUser })
|
||||||
|
|
||||||
return message
|
return message
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
throw new Error(error)
|
throw new Error(error)
|
||||||
|
|||||||
@ -3,6 +3,18 @@ import Resolver from './helpers/Resolver'
|
|||||||
import { pubsub, ROOM_COUNT_UPDATED } from '../../server'
|
import { pubsub, ROOM_COUNT_UPDATED } from '../../server'
|
||||||
import { withFilter } from 'graphql-subscriptions'
|
import { withFilter } from 'graphql-subscriptions'
|
||||||
|
|
||||||
|
export const getUnreadRoomsCount = async (userId, session) => {
|
||||||
|
return session.readTransaction(async (transaction) => {
|
||||||
|
const unreadRoomsCypher = `
|
||||||
|
MATCH (:User { id: $userId })-[:CHATS_IN]->(room:Room)<-[:INSIDE]-(message:Message)<-[:CREATED]-(sender:User)
|
||||||
|
WHERE NOT sender.id = $userId AND NOT message.seen
|
||||||
|
RETURN toString(COUNT(DISTINCT room)) AS count
|
||||||
|
`
|
||||||
|
const unreadRoomsTxResponse = await transaction.run(unreadRoomsCypher, { userId })
|
||||||
|
return unreadRoomsTxResponse.records.map((record) => record.get('count'))[0]
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
export default {
|
export default {
|
||||||
Subscription: {
|
Subscription: {
|
||||||
roomCountUpdated: {
|
roomCountUpdated: {
|
||||||
@ -27,17 +39,8 @@ export default {
|
|||||||
user: { id: currentUserId },
|
user: { id: currentUserId },
|
||||||
} = context
|
} = context
|
||||||
const session = context.driver.session()
|
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 {
|
try {
|
||||||
const count = await readTxResultPromise
|
const count = await getUnreadRoomsCount(currentUserId, session)
|
||||||
return count
|
return count
|
||||||
} finally {
|
} finally {
|
||||||
session.close()
|
session.close()
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user