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

View File

@ -6,12 +6,11 @@ import createServer, { pubsub } from '../../server'
import {
createGroupMutation,
joinGroupMutation,
leaveGroupMutation,
changeGroupMemberRoleMutation,
removeUserFromGroupMutation,
// leaveGroupMutation,
// changeGroupMemberRoleMutation,
// removeUserFromGroupMutation,
} from '../../graphql/groups'
let server, query, mutate, notifiedUser, authenticatedUser
let publishSpy
const driver = getDriver()
@ -100,6 +99,9 @@ describe('notifications', () => {
read
reason
createdAt
relatedUser {
id
}
from {
__typename
... on Post {
@ -196,6 +198,7 @@ describe('notifications', () => {
id: 'c47',
content: commentContent,
},
relatedUser: null,
},
],
},
@ -368,6 +371,7 @@ describe('notifications', () => {
id: 'p47',
content: expectedUpdatedContent,
},
relatedUser: null,
},
],
},
@ -524,6 +528,7 @@ describe('notifications', () => {
id: 'c47',
content: commentContent,
},
relatedUser: null,
},
],
},
@ -558,6 +563,7 @@ describe('notifications', () => {
id: 'c47',
content: commentContent,
},
relatedUser: null,
},
],
},
@ -630,8 +636,7 @@ describe('notifications', () => {
describe('group notifications', () => {
let groupOwner
let group
beforeEach(async () => {
groupOwner = await neode.create(
'User',
@ -673,10 +678,12 @@ describe('notifications', () => {
authenticatedUser = await groupOwner.toJson()
})
it('works', async () => {
await expect(query({
query: notificationQuery,
})).resolves.toMatchObject({
it('has the notification in database', async () => {
await expect(
query({
query: notificationQuery,
}),
).resolves.toMatchObject({
data: {
notifications: [
{
@ -686,7 +693,10 @@ describe('notifications', () => {
from: {
__typename: '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})
${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)-[:COMMENTS]->(post:Post)<-[:WROTE]-(author:User) | post{.*, author: properties(author)} ] AS posts
WITH resource, user, notification, authors, posts,
resource {.*, __typename: labels(resource)[0], author: authors[0], post: posts[0]} AS finalResource
RETURN notification {.*, from: finalResource, to: properties(user)}
[(resource)-[:COMMENTS]->(post:Post)<-[:WROTE]-(author:User) | post {.*, author: properties(author)} ] AS posts
WITH resource, user, notification, authors, posts, relatedUser, membership,
resource {.*,
__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}
${offset} ${limit}
`,

View File

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