From 643d175ef623dcf512d609cfd401e937aad54369 Mon Sep 17 00:00:00 2001 From: roschaefer Date: Thu, 29 Aug 2019 15:30:19 +0200 Subject: [PATCH] Implement notifications resolver --- .../notifications/notificationsMiddleware.js | 9 ++--- .../src/middleware/permissionsMiddleware.js | 1 + backend/src/models/Notification.js | 36 ------------------- backend/src/models/index.js | 1 - backend/src/schema/resolvers/notifications.js | 30 ++++++++++++++++ backend/src/schema/types/type/NOTIFIED.gql | 12 +++++++ 6 files changed, 46 insertions(+), 43 deletions(-) delete mode 100644 backend/src/models/Notification.js create mode 100644 backend/src/schema/types/type/NOTIFIED.gql diff --git a/backend/src/middleware/notifications/notificationsMiddleware.js b/backend/src/middleware/notifications/notificationsMiddleware.js index c9dfe406c..56c7366d8 100644 --- a/backend/src/middleware/notifications/notificationsMiddleware.js +++ b/backend/src/middleware/notifications/notificationsMiddleware.js @@ -25,8 +25,7 @@ const notifyUsers = async (label, id, idsOfUsers, reason, context) => { MATCH (user: User) WHERE user.id in $idsOfUsers AND NOT (user)<-[:BLOCKED]-(author) - CREATE (notification: Notification {id: apoc.create.uuid(), read: false, reason: $reason, createdAt: $createdAt }) - MERGE (post)-[:NOTIFIED]->(notification)-[:NOTIFIED]->(user) + MERGE (post)-[:NOTIFIED {id: apoc.create.uuid(), read: false, reason: $reason, createdAt: $createdAt }]->(user) ` break } @@ -37,8 +36,7 @@ const notifyUsers = async (label, id, idsOfUsers, reason, context) => { WHERE user.id in $idsOfUsers AND NOT (user)<-[:BLOCKED]-(author) AND NOT (user)<-[:BLOCKED]-(postAuthor) - CREATE (notification: Notification {id: apoc.create.uuid(), read: false, reason: $reason, createdAt: $createdAt }) - MERGE (comment)-[:NOTIFIED]->(notification)-[:NOTIFIED]->(user) + MERGE (comment)-[:NOTIFIED {id: apoc.create.uuid(), read: false, reason: $reason, createdAt: $createdAt }]->(user) ` break } @@ -49,8 +47,7 @@ const notifyUsers = async (label, id, idsOfUsers, reason, context) => { WHERE user.id in $idsOfUsers AND NOT (user)<-[:BLOCKED]-(author) AND NOT (author)<-[:BLOCKED]-(user) - CREATE (notification: Notification {id: apoc.create.uuid(), read: false, reason: $reason, createdAt: $createdAt }) - MERGE (comment)-[:NOTIFIED]->(notification)-[:NOTIFIED]->(user) + MERGE (comment)-[:NOTIFIED {id: apoc.create.uuid(), read: false, reason: $reason, createdAt: $createdAt }]->(user) ` break } diff --git a/backend/src/middleware/permissionsMiddleware.js b/backend/src/middleware/permissionsMiddleware.js index 83c29d19d..2b29cd152 100644 --- a/backend/src/middleware/permissionsMiddleware.js +++ b/backend/src/middleware/permissionsMiddleware.js @@ -160,6 +160,7 @@ const permissions = shield( PostsEmotionsCountByEmotion: allow, PostsEmotionsByCurrentUser: allow, blockedUsers: isAuthenticated, + notifications: isAuthenticated, }, Mutation: { '*': deny, diff --git a/backend/src/models/Notification.js b/backend/src/models/Notification.js deleted file mode 100644 index b54a99574..000000000 --- a/backend/src/models/Notification.js +++ /dev/null @@ -1,36 +0,0 @@ -import uuid from 'uuid/v4' - -module.exports = { - id: { - type: 'uuid', - primary: true, - default: uuid, - }, - read: { - type: 'boolean', - default: false, - }, - reason: { - type: 'string', - valid: ['mentioned_in_post', 'mentioned_in_comment', 'comment_on_post'], - invalid: [null], - default: 'mentioned_in_post', - }, - createdAt: { - type: 'string', - isoDate: true, - default: () => new Date().toISOString(), - }, - user: { - type: 'relationship', - relationship: 'NOTIFIED', - target: 'User', - direction: 'out', - }, - post: { - type: 'relationship', - relationship: 'NOTIFIED', - target: 'Post', - direction: 'in', - }, -} diff --git a/backend/src/models/index.js b/backend/src/models/index.js index 295082de4..5a4510ac6 100644 --- a/backend/src/models/index.js +++ b/backend/src/models/index.js @@ -7,6 +7,5 @@ export default { EmailAddress: require('./EmailAddress.js'), SocialMedia: require('./SocialMedia.js'), Post: require('./Post.js'), - Notification: require('./Notification.js'), Category: require('./Category.js'), } diff --git a/backend/src/schema/resolvers/notifications.js b/backend/src/schema/resolvers/notifications.js index ddc1985cf..fcb8db1a4 100644 --- a/backend/src/schema/resolvers/notifications.js +++ b/backend/src/schema/resolvers/notifications.js @@ -2,6 +2,36 @@ import { neo4jgraphql } from 'neo4j-graphql-js' export default { Query: { + notifications: async (parent, params, context, resolveInfo) => { + const { user, driver } = context + let session + let notifications + try { + session = context.driver.session() + const cypher = ` + MATCH (resource)-[notification:NOTIFIED]->(user:User {id:$id}) + RETURN resource, notification, user + ` + const result = await session.run(cypher, { id: user.id }) + const resourceTypes = ['Post', 'Comment'] + notifications = await result.records.map(record => { + return { + ...record.get('notification').properties, + from: { + __typename: record.get('resource').labels.find(l => resourceTypes.includes(l)), + ...record.get('resource').properties + }, + to: { + __typename: 'User', + ...record.get('user').properties, + } + } + }) + } finally { + session.close() + } + return notifications + }, Notification: (object, params, context, resolveInfo) => { return neo4jgraphql(object, params, context, resolveInfo, false) }, diff --git a/backend/src/schema/types/type/NOTIFIED.gql b/backend/src/schema/types/type/NOTIFIED.gql new file mode 100644 index 000000000..40a6f1b22 --- /dev/null +++ b/backend/src/schema/types/type/NOTIFIED.gql @@ -0,0 +1,12 @@ +type NOTIFIED { + from: NotificationSource + to: User + createdAt: String + read: Boolean +} + +union NotificationSource = Post | Comment + +type Query { + notifications: [NOTIFIED] +}