diff --git a/backend/src/schema/resolvers/comments.ts b/backend/src/schema/resolvers/comments.ts index 394695f3a..b9c0271c1 100644 --- a/backend/src/schema/resolvers/comments.ts +++ b/backend/src/schema/resolvers/comments.ts @@ -100,6 +100,14 @@ export default { author: '<-[:WROTE]-(related:User)', post: '-[:COMMENTS]->(related:Post)', }, + count: { + postObservingUsersCount: + '-[:COMMENTS]->(:Post)<-[obs:OBSERVES]-(related:User) WHERE obs.active = true AND NOT related.deleted AND NOT related.disabled', + }, + boolean: { + isPostObservedByMe: + 'MATCH (this)-[:COMMENTS]->(:Post)<-[obs:OBSERVES]-(related:User {id: $cypherParams.currentUserId}) WHERE obs.active = true RETURN COUNT(related) >= 1', + }, }), }, } diff --git a/backend/src/schema/resolvers/observePosts.spec.ts b/backend/src/schema/resolvers/observePosts.spec.ts index e88bae08a..2d98c33a7 100644 --- a/backend/src/schema/resolvers/observePosts.spec.ts +++ b/backend/src/schema/resolvers/observePosts.spec.ts @@ -22,6 +22,8 @@ const createCommentMutation = gql` mutation ($id: ID, $postId: ID!, $content: String!) { CreateComment(id: $id, postId: $postId, content: $content) { id + isPostObservedByMe + postObservingUsersCount } } ` @@ -122,11 +124,20 @@ describe('observing posts', () => { }) it('has another user observing the post AFTER commenting it', async () => { - await mutate({ - mutation: createCommentMutation, - variables: { - postId: 'p2', - content: 'After commenting the post, I should observe the post automatically', + await expect( + mutate({ + mutation: createCommentMutation, + variables: { + postId: 'p2', + content: 'After commenting the post, I should observe the post automatically', + }, + }), + ).resolves.toMatchObject({ + data: { + CreateComment: { + isPostObservedByMe: true, + postObservingUsersCount: 2, + }, }, }) @@ -187,12 +198,21 @@ describe('observing posts', () => { describe('comment the post again', () => { it('does NOT alter the observation state', async () => { - await mutate({ - mutation: createCommentMutation, - variables: { - postId: 'p2', - content: - 'After commenting the post I do not observe again, I should NOT observe the post', + await expect( + mutate({ + mutation: createCommentMutation, + variables: { + postId: 'p2', + content: + 'After commenting the post I do not observe again, I should NOT observe the post', + }, + }), + ).resolves.toMatchObject({ + data: { + CreateComment: { + isPostObservedByMe: false, + postObservingUsersCount: 1, + }, }, }) diff --git a/backend/src/schema/types/type/Comment.gql b/backend/src/schema/types/type/Comment.gql index cf53df41d..b1fd7a838 100644 --- a/backend/src/schema/types/type/Comment.gql +++ b/backend/src/schema/types/type/Comment.gql @@ -47,6 +47,12 @@ type Comment { updatedAt: String deleted: Boolean disabled: Boolean + isPostObservedByMe: Boolean! + @cypher( + statement: "MATCH (this)-[:COMMENTS]->(:Post)<-[obs:OBSERVES]-(u:User {id: $cypherParams.currentUserId}) WHERE obs.active = true RETURN COUNT(u) >= 1" + ) + postObservingUsersCount: Int! + @cypher(statement: "MATCH (this)-[:COMMENTS]->(:Post)<-[obs:OBSERVES]-(u:User) WHERE obs.active = true AND NOT u.disabled = true AND NOT u.deleted = true RETURN COUNT(DISTINCT u)") } type Query { diff --git a/webapp/graphql/Fragments.js b/webapp/graphql/Fragments.js index da1ed0de3..32337230b 100644 --- a/webapp/graphql/Fragments.js +++ b/webapp/graphql/Fragments.js @@ -135,5 +135,7 @@ export const commentFragment = gql` deleted content contentExcerpt + isPostObservedByMe + postObservingUsersCount } `