mirror of
https://github.com/IT4Change/Ocelot-Social.git
synced 2025-12-13 07:45:56 +00:00
Implement parsing of mention links
This commit is contained in:
parent
cd2bbbef30
commit
4a6ef3f9f6
17
backend/src/middleware/notifications/extractMentions.js
Normal file
17
backend/src/middleware/notifications/extractMentions.js
Normal file
@ -0,0 +1,17 @@
|
|||||||
|
import cheerio from 'cheerio'
|
||||||
|
const ID_REGEX = /\/profile\/([\w\-.!~*'"(),]+)/g
|
||||||
|
|
||||||
|
export default function (content) {
|
||||||
|
const $ = cheerio.load(content)
|
||||||
|
const urls = $('.mention').map((_, el) => {
|
||||||
|
return $(el).attr('href')
|
||||||
|
}).get()
|
||||||
|
const ids = []
|
||||||
|
urls.forEach((url) => {
|
||||||
|
let match
|
||||||
|
while ((match = ID_REGEX.exec(url)) != null) {
|
||||||
|
ids.push(match[1])
|
||||||
|
}
|
||||||
|
})
|
||||||
|
return ids
|
||||||
|
}
|
||||||
46
backend/src/middleware/notifications/extractMentions.spec.js
Normal file
46
backend/src/middleware/notifications/extractMentions.spec.js
Normal file
@ -0,0 +1,46 @@
|
|||||||
|
import extractIds from './extractMentions'
|
||||||
|
|
||||||
|
describe('extract', () => {
|
||||||
|
describe('searches through links', () => {
|
||||||
|
it('ignores links without .mention class', () => {
|
||||||
|
const content = '<p>Something inspirational about <a href="/profile/u2" target="_blank">@bob-der-baumeister</a> and <a href="/profile/u3" target="_blank">@jenny-rostock</a>.</p>'
|
||||||
|
expect(extractIds(content)).toEqual([])
|
||||||
|
})
|
||||||
|
|
||||||
|
describe('given a link with .mention class', () => {
|
||||||
|
it('extracts ids', () => {
|
||||||
|
const content = '<p>Something inspirational about <a href="/profile/u2" class="mention" target="_blank">@bob-der-baumeister</a> and <a href="/profile/u3/jenny-rostock" class="mention" target="_blank">@jenny-rostock</a>.</p>'
|
||||||
|
expect(extractIds(content)).toEqual(['u2', 'u3'])
|
||||||
|
})
|
||||||
|
|
||||||
|
describe('handles links', () => {
|
||||||
|
it('with slug and id', () => {
|
||||||
|
const content = '<p>Something inspirational about <a href="/profile/u2/bob-der-baumeister" class="mention" target="_blank">@bob-der-baumeister</a> and <a href="/profile/u3/jenny-rostock/" class="mention" target="_blank">@jenny-rostock</a>.</p>'
|
||||||
|
expect(extractIds(content)).toEqual(['u2', 'u3'])
|
||||||
|
})
|
||||||
|
|
||||||
|
it('with domains', () => {
|
||||||
|
const content = '<p>Something inspirational about <a href="http://localhost:3000/profile/u2/bob-der-baumeister" class="mention" target="_blank">@bob-der-baumeister</a> and <a href="http://localhost:3000//profile/u3/jenny-rostock/" class="mention" target="_blank">@jenny-rostock</a>.</p>'
|
||||||
|
expect(extractIds(content)).toEqual(['u2', 'u3'])
|
||||||
|
})
|
||||||
|
|
||||||
|
it('special characters', () => {
|
||||||
|
const content = '<p>Something inspirational about <a href="http://localhost:3000/profile/u!*(),2/bob-der-baumeister" class="mention" target="_blank">@bob-der-baumeister</a> and <a href="http://localhost:3000//profile/u.~-3/jenny-rostock/" class="mention" target="_blank">@jenny-rostock</a>.</p>'
|
||||||
|
expect(extractIds(content)).toEqual(['u!*(),2', 'u.~-3'])
|
||||||
|
})
|
||||||
|
})
|
||||||
|
|
||||||
|
describe('does not crash if', () => {
|
||||||
|
it('`href` contains no user id', () => {
|
||||||
|
const content = '<p>Something inspirational about <a href="/profile" class="mention" target="_blank">@bob-der-baumeister</a> and <a href="/profile/" class="mention" target="_blank">@jenny-rostock</a>.</p>'
|
||||||
|
expect(extractIds(content)).toEqual([])
|
||||||
|
})
|
||||||
|
|
||||||
|
it('`href` is empty or invalid', () => {
|
||||||
|
const content = '<p>Something inspirational about <a href="" class="mention" target="_blank">@bob-der-baumeister</a> and <a href="not-a-url" class="mention" target="_blank">@jenny-rostock</a>.</p>'
|
||||||
|
expect(extractIds(content)).toEqual([])
|
||||||
|
})
|
||||||
|
})
|
||||||
|
})
|
||||||
|
})
|
||||||
|
})
|
||||||
@ -1,11 +1,11 @@
|
|||||||
import { extractSlugs } from './mentions'
|
import extractIds from './extractMentions'
|
||||||
|
|
||||||
const notify = async (resolve, root, args, context, resolveInfo) => {
|
const notify = async (resolve, root, args, context, resolveInfo) => {
|
||||||
const post = await resolve(root, args, context, resolveInfo)
|
const post = await resolve(root, args, context, resolveInfo)
|
||||||
|
|
||||||
const session = context.driver.session()
|
const session = context.driver.session()
|
||||||
const { content, id: postId } = post
|
const { content, id: postId } = post
|
||||||
const slugs = extractSlugs(content)
|
const slugs = extractIds(content)
|
||||||
const createdAt = (new Date()).toISOString()
|
const createdAt = (new Date()).toISOString()
|
||||||
const cypher = `
|
const cypher = `
|
||||||
match(u:User) where u.slug in $slugs
|
match(u:User) where u.slug in $slugs
|
||||||
|
|||||||
@ -1,10 +0,0 @@
|
|||||||
const MENTION_REGEX = /\s@([\w_-]+)/g
|
|
||||||
|
|
||||||
export function extractSlugs (content) {
|
|
||||||
let slugs = []
|
|
||||||
let match
|
|
||||||
while ((match = MENTION_REGEX.exec(content)) != null) {
|
|
||||||
slugs.push(match[1])
|
|
||||||
}
|
|
||||||
return slugs
|
|
||||||
}
|
|
||||||
@ -1,18 +0,0 @@
|
|||||||
import { extractSlugs } from './mentions'
|
|
||||||
|
|
||||||
describe('extract', () => {
|
|
||||||
describe('searches through links', () => {
|
|
||||||
it('ignores links without .mention class', () => {
|
|
||||||
const content = '<p>Something inspirational about <a href="/profile/u2" target="_blank">@bob-der-baumeister</a> and <a href="/profile/u3" target="_blank">@jenny-rostock</a>.</p>'
|
|
||||||
expect(extractSlugs(content)).toEqual([])
|
|
||||||
})
|
|
||||||
|
|
||||||
describe('given a link with .mention class', () => {
|
|
||||||
const content = '<p>Something inspirational about <a href="/profile/u2" class="mention" target="_blank">@bob-der-baumeister</a> and <a href="/profile/u3/jenny-rostock" class="mention" target="_blank">@jenny-rostock</a>.</p>'
|
|
||||||
|
|
||||||
it('extracts ID', () => {
|
|
||||||
expect(extractSlugs(content)).toEqual(['u2', 'u3'])
|
|
||||||
})
|
|
||||||
})
|
|
||||||
})
|
|
||||||
})
|
|
||||||
Loading…
x
Reference in New Issue
Block a user