From f372cdcbdba94820407f69d1cfa727d2093e5b47 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Robert=20Sch=C3=A4fer?= Date: Mon, 8 Apr 2019 12:01:09 +0200 Subject: [PATCH] Avoid to send out notifications for email adresses --- backend/src/middleware/notifications/mentions.js | 10 ++++++++++ .../src/middleware/notifications/mentions.spec.js | 15 +++++++++++++++ backend/src/middleware/notificationsMiddleware.js | 8 ++------ 3 files changed, 27 insertions(+), 6 deletions(-) create mode 100644 backend/src/middleware/notifications/mentions.js create mode 100644 backend/src/middleware/notifications/mentions.spec.js diff --git a/backend/src/middleware/notifications/mentions.js b/backend/src/middleware/notifications/mentions.js new file mode 100644 index 000000000..fb4a049f2 --- /dev/null +++ b/backend/src/middleware/notifications/mentions.js @@ -0,0 +1,10 @@ +const MENTION_REGEX = /\s@(\S+)/g + +export function extractSlugs(content) { + let slugs = [] + let match + while ((match = MENTION_REGEX.exec(content)) != null) { + slugs.push(match[1]) + } + return slugs +} diff --git a/backend/src/middleware/notifications/mentions.spec.js b/backend/src/middleware/notifications/mentions.spec.js new file mode 100644 index 000000000..8fe9221b3 --- /dev/null +++ b/backend/src/middleware/notifications/mentions.spec.js @@ -0,0 +1,15 @@ +import { extractSlugs } from './mentions' + +describe('extract', () => { + describe('finds mentions in the form of', () => { + it('@user', () => { + const content = 'Hello @user' + expect(extractSlugs(content)).toEqual(['user']) + }) + }) + + it('ignores email addresses', () => { + const content = 'Hello somebody@example.org' + expect(extractSlugs(content)).toEqual([]) + }) +}) diff --git a/backend/src/middleware/notificationsMiddleware.js b/backend/src/middleware/notificationsMiddleware.js index 1150ab0d9..30205278b 100644 --- a/backend/src/middleware/notificationsMiddleware.js +++ b/backend/src/middleware/notificationsMiddleware.js @@ -1,16 +1,12 @@ -const MENTION_REGEX = /@(\S+)/g +import { extractSlugs } from './notifications/mentions' 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 slugs = extractSlugs(content) 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