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