fix(backend): notifications

This commit is contained in:
Moriz Wahl 2023-06-21 14:29:56 +02:00
parent 3451a16ab4
commit 07d10c8e4c
3 changed files with 30 additions and 19 deletions

View File

@ -62,7 +62,7 @@ beforeAll(async () => {
}) })
afterAll(async () => { afterAll(async () => {
await cleanDatabase() // await cleanDatabase()
driver.close() driver.close()
}) })
@ -89,7 +89,7 @@ beforeEach(async () => {
// TODO: avoid database clean after each test in the future if possible for performance and flakyness reasons by filling the database step by step, see issue https://github.com/Ocelot-Social-Community/Ocelot-Social/issues/4543 // TODO: avoid database clean after each test in the future if possible for performance and flakyness reasons by filling the database step by step, see issue https://github.com/Ocelot-Social-Community/Ocelot-Social/issues/4543
afterEach(async () => { afterEach(async () => {
await cleanDatabase() // await cleanDatabase()
}) })
describe('notifications', () => { describe('notifications', () => {

View File

@ -140,16 +140,18 @@ const postAuthorOfComment = async (commentId, { context }) => {
const notifyOwnersOfGroup = async (groupId, userId, reason, context) => { const notifyOwnersOfGroup = async (groupId, userId, reason, context) => {
const cypher = ` const cypher = `
MATCH (user:User { id: $userId })
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'
WITH owner, group WITH owner, group, user, membership
MERGE (group)-[notification:NOTIFIED {reason: $reason}]->(owner) MERGE (group)-[notification:NOTIFIED {reason: $reason}]->(owner)
WITH group, owner, notification WITH group, owner, notification, user, membership
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 SET notification.relatedUserId = $userId
RETURN notification {.*, from: group, to: properties(owner)} WITH owner, group { __typename: 'Group', .*, myRole: membership.roleInGroup } AS finalGroup, user, notification
RETURN notification {.*, from: finalGroup, to: properties(owner), relatedUser: properties(user) }
` `
const session = context.driver.session() const session = context.driver.session()
const writeTxResultPromise = session.writeTransaction(async (transaction) => { const writeTxResultPromise = session.writeTransaction(async (transaction) => {
@ -173,16 +175,20 @@ const notifyOwnersOfGroup = async (groupId, userId, reason, context) => {
const notifyMemberOfGroup = async (groupId, userId, reason, context) => { const notifyMemberOfGroup = async (groupId, userId, reason, context) => {
const { user: owner } = context const { user: owner } = context
const cypher = ` const cypher = `
MATCH (owner:User { id: $ownerId })
MATCH (user:User { id: $userId }) MATCH (user:User { id: $userId })
MATCH (group:Group { id: $groupId }) MATCH (group:Group { id: $groupId })
WITH user, group OPTIONAL MATCH (user)-[membership:MEMBER_OF]->(group)
WITH user, group, owner, membership
MERGE (group)-[notification:NOTIFIED {reason: $reason}]->(user) MERGE (group)-[notification:NOTIFIED {reason: $reason}]->(user)
WITH group, user, notification WITH group, user, notification, owner, membership
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 = $ownerId SET notification.relatedUserId = $ownerId
RETURN notification {.*, from: group, to: properties(user)} WITH group { __typename: 'Group', .*, myRole: membership.roleInGroup } AS finalGroup,
notification, user, owner
RETURN notification {.*, from: finalGroup, to: properties(user), relatedUser: properties(owner) }
` `
const session = context.driver.session() const session = context.driver.session()
const writeTxResultPromise = session.writeTransaction(async (transaction) => { const writeTxResultPromise = session.writeTransaction(async (transaction) => {
@ -242,7 +248,7 @@ const notifyUsersOfMention = async (label, id, idsOfUsers, 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())
RETURN notification {.*, from: finalResource, to: properties(user)} RETURN notification {.*, from: finalResource, to: properties(user), relatedUser: properties(user) }
` `
const session = context.driver.session() const session = context.driver.session()
const writeTxResultPromise = session.writeTransaction(async (transaction) => { const writeTxResultPromise = session.writeTransaction(async (transaction) => {
@ -276,9 +282,14 @@ const notifyUsersOfComment = async (label, commentId, postAuthorId, reason, cont
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())
WITH notification, postAuthor, post, WITH notification, postAuthor, post, commenter,
comment {.*, __typename: labels(comment)[0], author: properties(commenter), post: post {.*, author: properties(postAuthor) } } AS finalResource comment {.*, __typename: labels(comment)[0], author: properties(commenter), post: post {.*, author: properties(postAuthor) } } AS finalResource
RETURN notification {.*, from: finalResource, to: properties(postAuthor)} RETURN notification {
.*,
from: finalResource,
to: properties(postAuthor),
relatedUser: properties(commenter)
}
`, `,
{ commentId, postAuthorId, reason }, { commentId, postAuthorId, reason },
) )

View File

@ -234,32 +234,32 @@ describe('Message', () => {
})).resolves.toMatchObject({ })).resolves.toMatchObject({
errors: undefined, errors: undefined,
data: { data: {
Message: [ Message: expect.arrayContaining([
{ expect.objectContaining({
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', senderId: 'chatting-user',
username: 'Chatting User', username: 'Chatting User',
avatar: expect.any(String), avatar: expect.any(String),
date: expect.any(String), date: expect.any(String),
}, }),
{ expect.objectContaining({
id: expect.any(String), id: expect.any(String),
content: 'A nice response message to chatting user', content: 'A nice response message to chatting user',
senderId: 'other-chatting-user', senderId: 'other-chatting-user',
username: 'Other Chatting User', username: 'Other Chatting User',
avatar: expect.any(String), avatar: expect.any(String),
date: expect.any(String), date: expect.any(String),
}, }),
{ expect.objectContaining({
id: expect.any(String), id: expect.any(String),
content: 'And another nice message to other chatting user', content: 'And another nice message to other chatting user',
senderId: 'chatting-user', senderId: 'chatting-user',
username: 'Chatting User', username: 'Chatting User',
avatar: expect.any(String), avatar: expect.any(String),
date: expect.any(String), date: expect.any(String),
}, }),
], ]),
}, },
}) })
}) })