From cf1f655451277d98da70957d829def3b3022c97c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Robert=20Sch=C3=A4fer?= Date: Sat, 6 Apr 2019 00:33:10 +0200 Subject: [PATCH] Create notifications on CreatePost --- backend/src/middleware/index.js | 2 ++ .../src/middleware/notificationMiddleware.js | 0 .../src/middleware/notificationsMiddleware.js | 31 +++++++++++++++++++ ...pec.js => notificationsMiddleware.spec.js} | 4 ++- 4 files changed, 36 insertions(+), 1 deletion(-) delete mode 100644 backend/src/middleware/notificationMiddleware.js create mode 100644 backend/src/middleware/notificationsMiddleware.js rename backend/src/middleware/{notificationMiddleware.spec.js => notificationsMiddleware.spec.js} (94%) diff --git a/backend/src/middleware/index.js b/backend/src/middleware/index.js index 8f86a88e6..8d893a78b 100644 --- a/backend/src/middleware/index.js +++ b/backend/src/middleware/index.js @@ -10,6 +10,7 @@ import permissionsMiddleware from './permissionsMiddleware' import userMiddleware from './userMiddleware' import includedFieldsMiddleware from './includedFieldsMiddleware' import orderByMiddleware from './orderByMiddleware' +import notificationsMiddleware from './notificationsMiddleware' export default schema => { let middleware = [ @@ -19,6 +20,7 @@ export default schema => { excerptMiddleware, xssMiddleware, fixImageUrlsMiddleware, + notificationsMiddleware, softDeleteMiddleware, userMiddleware, includedFieldsMiddleware, diff --git a/backend/src/middleware/notificationMiddleware.js b/backend/src/middleware/notificationMiddleware.js deleted file mode 100644 index e69de29bb..000000000 diff --git a/backend/src/middleware/notificationsMiddleware.js b/backend/src/middleware/notificationsMiddleware.js new file mode 100644 index 000000000..1150ab0d9 --- /dev/null +++ b/backend/src/middleware/notificationsMiddleware.js @@ -0,0 +1,31 @@ +const MENTION_REGEX = /@(\S+)/g + +const notify = async (resolve, root, args, context, resolveInfo) => { + const post = await resolve(root, args, context, resolveInfo) + + const session = context.driver.session() + const { content, id: postId } = post + const slugs = [] + const createdAt = (new Date()).toISOString() + let match + while ((match = MENTION_REGEX.exec(content)) != null) { + slugs.push(match[1]) + } + const cypher = ` + match(u:User) where u.slug in $slugs + match(p:Post) where p.id = $postId + create(n:Notification{id: apoc.create.uuid(), read: false, createdAt: $createdAt}) + merge (n)-[:NOTIFIED]->(u) + merge (p)-[:NOTIFIED]->(n) + ` + await session.run(cypher, { slugs, createdAt, postId }) + session.close() + + return post +} + +export default { + Mutation: { + CreatePost: notify + } +} diff --git a/backend/src/middleware/notificationMiddleware.spec.js b/backend/src/middleware/notificationsMiddleware.spec.js similarity index 94% rename from backend/src/middleware/notificationMiddleware.spec.js rename to backend/src/middleware/notificationsMiddleware.spec.js index ccb38fcbf..9fed4a59a 100644 --- a/backend/src/middleware/notificationMiddleware.spec.js +++ b/backend/src/middleware/notificationsMiddleware.spec.js @@ -24,8 +24,10 @@ describe('currentUser { notifications }', () => { currentUser { notifications(read: $read, orderBy: createdAt_desc) { id + read post { id + content } } } @@ -65,7 +67,7 @@ describe('currentUser { notifications }', () => { } } ` - authorClient = new GraphQLClient(host, authorHeaders) + authorClient = new GraphQLClient(host, { headers: authorHeaders }) await authorClient.request(createPostMutation, { title, content }) })