add related user to notified model

This commit is contained in:
Moriz Wahl 2023-03-15 17:34:16 +01:00
parent 60b9262d7a
commit 997dff4dec
4 changed files with 44 additions and 24 deletions

View File

@ -51,15 +51,13 @@ const publishNotifications = async (context, promises) => {
}) })
} }
const handleJoinGroup = async (resolve, root, args, context, resolveInfo) => { const handleJoinGroup = async (resolve, root, args, context, resolveInfo) => {
const { groupId } = args const { groupId, userId } = args
const user = await resolve(root, args, context, resolveInfo) const user = await resolve(root, args, context, resolveInfo)
if (user) { if (user) {
await publishNotifications( await publishNotifications(context, [
context, notifyOwnersOfGroup(groupId, userId, 'user_joined_group', context),
[notifyOwnersOfGroup(groupId, 'user_joined_group', context)] ])
)
} }
return user return user
} }
@ -107,7 +105,7 @@ const postAuthorOfComment = async (commentId, { context }) => {
} }
} }
const notifyOwnersOfGroup = async (groupId, reason, context) => { const notifyOwnersOfGroup = async (groupId, userId, reason, context) => {
const cypher = ` const cypher = `
MATCH (group:Group { id: $groupId })<-[membership:MEMBER_OF]-(owner:User) MATCH (group:Group { id: $groupId })<-[membership:MEMBER_OF]-(owner:User)
WHERE membership.role = 'owner' WHERE membership.role = 'owner'
@ -117,6 +115,7 @@ const notifyOwnersOfGroup = async (groupId, reason, context) => {
SET notification.read = FALSE SET notification.read = FALSE
SET notification.createdAt = COALESCE(notification.createdAt, toString(datetime())) SET notification.createdAt = COALESCE(notification.createdAt, toString(datetime()))
SET notification.updatedAt = toString(datetime()) SET notification.updatedAt = toString(datetime())
SET notification.relatedUserId = $userId
RETURN notification {.*, from: group, to: properties(owner)} RETURN notification {.*, from: group, to: properties(owner)}
` `
const session = context.driver.session() const session = context.driver.session()
@ -124,6 +123,7 @@ const notifyOwnersOfGroup = async (groupId, reason, context) => {
const notificationTransactionResponse = await transaction.run(cypher, { const notificationTransactionResponse = await transaction.run(cypher, {
groupId, groupId,
reason, reason,
userId,
}) })
return notificationTransactionResponse.records.map((record) => record.get('notification')) return notificationTransactionResponse.records.map((record) => record.get('notification'))
}) })
@ -131,7 +131,7 @@ const notifyOwnersOfGroup = async (groupId, reason, context) => {
const notifications = await writeTxResultPromise const notifications = await writeTxResultPromise
return notifications return notifications
} catch (error) { } catch (error) {
throw new Error(error) throw new Error(error)
} finally { } finally {
session.close() session.close()
} }

View File

@ -6,12 +6,11 @@ import createServer, { pubsub } from '../../server'
import { import {
createGroupMutation, createGroupMutation,
joinGroupMutation, joinGroupMutation,
leaveGroupMutation, // leaveGroupMutation,
changeGroupMemberRoleMutation, // changeGroupMemberRoleMutation,
removeUserFromGroupMutation, // removeUserFromGroupMutation,
} from '../../graphql/groups' } from '../../graphql/groups'
let server, query, mutate, notifiedUser, authenticatedUser let server, query, mutate, notifiedUser, authenticatedUser
let publishSpy let publishSpy
const driver = getDriver() const driver = getDriver()
@ -100,6 +99,9 @@ describe('notifications', () => {
read read
reason reason
createdAt createdAt
relatedUser {
id
}
from { from {
__typename __typename
... on Post { ... on Post {
@ -196,6 +198,7 @@ describe('notifications', () => {
id: 'c47', id: 'c47',
content: commentContent, content: commentContent,
}, },
relatedUser: null,
}, },
], ],
}, },
@ -368,6 +371,7 @@ describe('notifications', () => {
id: 'p47', id: 'p47',
content: expectedUpdatedContent, content: expectedUpdatedContent,
}, },
relatedUser: null,
}, },
], ],
}, },
@ -524,6 +528,7 @@ describe('notifications', () => {
id: 'c47', id: 'c47',
content: commentContent, content: commentContent,
}, },
relatedUser: null,
}, },
], ],
}, },
@ -558,6 +563,7 @@ describe('notifications', () => {
id: 'c47', id: 'c47',
content: commentContent, content: commentContent,
}, },
relatedUser: null,
}, },
], ],
}, },
@ -630,8 +636,7 @@ describe('notifications', () => {
describe('group notifications', () => { describe('group notifications', () => {
let groupOwner let groupOwner
let group
beforeEach(async () => { beforeEach(async () => {
groupOwner = await neode.create( groupOwner = await neode.create(
'User', 'User',
@ -673,10 +678,12 @@ describe('notifications', () => {
authenticatedUser = await groupOwner.toJson() authenticatedUser = await groupOwner.toJson()
}) })
it('works', async () => { it('has the notification in database', async () => {
await expect(query({ await expect(
query: notificationQuery, query({
})).resolves.toMatchObject({ query: notificationQuery,
}),
).resolves.toMatchObject({
data: { data: {
notifications: [ notifications: [
{ {
@ -686,7 +693,10 @@ describe('notifications', () => {
from: { from: {
__typename: 'Group', __typename: 'Group',
id: 'closed-group', id: 'closed-group',
} },
relatedUser: {
id: 'you',
},
}, },
], ],
}, },

View File

@ -47,12 +47,21 @@ export default {
` `
MATCH (resource {deleted: false, disabled: false})-[notification:NOTIFIED]->(user:User {id:$id}) MATCH (resource {deleted: false, disabled: false})-[notification:NOTIFIED]->(user:User {id:$id})
${whereClause} ${whereClause}
WITH user, notification, resource, OPTIONAL MATCH (resource)<-[membership:MEMBER_OF]-(relatedUser:User { id: notification.relatedUserId })
WITH user, notification, resource, membership, relatedUser,
[(resource)<-[:WROTE]-(author:User) | author {.*}] AS authors, [(resource)<-[:WROTE]-(author:User) | author {.*}] AS authors,
[(resource)-[:COMMENTS]->(post:Post)<-[:WROTE]-(author:User) | post{.*, author: properties(author)} ] AS posts [(resource)-[:COMMENTS]->(post:Post)<-[:WROTE]-(author:User) | post {.*, author: properties(author)} ] AS posts
WITH resource, user, notification, authors, posts, WITH resource, user, notification, authors, posts, relatedUser, membership,
resource {.*, __typename: labels(resource)[0], author: authors[0], post: posts[0]} AS finalResource resource {.*,
RETURN notification {.*, from: finalResource, to: properties(user)} __typename: labels(resource)[0],
author: authors[0],
post: posts[0],
myRole: membership.role } AS finalResource
RETURN notification {.*,
from: finalResource,
to: properties(user),
relatedUser: properties(relatedUser)
}
${orderByClause} ${orderByClause}
${offset} ${limit} ${offset} ${limit}
`, `,

View File

@ -6,6 +6,7 @@ type NOTIFIED {
updatedAt: String! updatedAt: String!
read: Boolean read: Boolean
reason: NotificationReason reason: NotificationReason
relatedUser: User
} }
union NotificationSource = Post | Comment | Group union NotificationSource = Post | Comment | Group