First approach to extract the Hashtags in the middleware and change the database

This commit is contained in:
Wolfgang Huß 2019-06-28 08:14:21 +02:00
parent 12418eefd9
commit 1d1959561b
8 changed files with 105 additions and 37 deletions

View File

@ -0,0 +1,77 @@
import extractMentionedUsers from './notifications/extractMentionedUsers'
import extractHashtags from './hashtags/extractHashtags'
const notify = async (resolve, root, args, context, resolveInfo) => {
// extract user ids before xss-middleware removes link classes
const ids = extractMentionedUsers(args.content)
console.log('ids: ', ids)
const post = await resolve(root, args, context, resolveInfo)
const session = context.driver.session()
const {
id: postId
} = post
const createdAt = new Date().toISOString()
const cypher = `
match(u:User) where u.id in $ids
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, {
ids,
createdAt,
postId
})
session.close()
return post
}
const updateHashtagsOfPost = async (postId, resolve, root, args, context, resolveInfo) => {
// extract tag (hashtag) ids before xss-middleware removes link classes
const hashtags = extractHashtags(args.content)
console.log('hashtags: ', hashtags)
// const post = await resolve(root, args, context, resolveInfo)
const session = context.driver.session()
// const {
// id: postId
// } = post
// const createdAt = new Date().toISOString()
const cypher = `
MATCH (p:Post { id: $postId })-[oldRelations: TAGGED]->(oldTags: Tag)
DELETE oldRelations
WITH p
UNWIND $hashtags AS tagName
MERGE (t: Tag { id: tagName, name: tagName })
MERGE (p)-[:TAGGED]->(t)
RETURN t
`
await session.run(cypher, {
postId,
hashtags
})
session.close()
}
const handleContentData = async (resolve, root, args, context, resolveInfo) => {
// extract user ids before xss-middleware removes link classes
const post = await notify(resolve, root, args, context, resolveInfo)
await updateHashtagsOfPost(post.id, resolve, root, args, context, resolveInfo)
return post
}
export default {
Mutation: {
CreatePost: handleContentData,
UpdatePost: handleContentData,
},
}

View File

@ -0,0 +1,21 @@
import cheerio from 'cheerio'
const ID_REGEX = /\/search\/hashtag\/([\w\-.!~*'"(),]+)/g
export default function (content) {
if (!content) return []
const $ = cheerio.load(content)
const urls = $('.hashtag')
.map((_, el) => {
return $(el).attr('href')
})
.get()
const hashtags = []
urls.forEach(url => {
console.log('url: ', url)
let match
while ((match = ID_REGEX.exec(url)) != null) {
hashtags.push(match[1])
}
})
return hashtags
}

View File

@ -1,4 +1,4 @@
import extractIds from '.' import extractIds from './extractMentionedUsers'
describe('extractIds', () => { describe('extractIds', () => {
describe('content undefined', () => { describe('content undefined', () => {
@ -56,4 +56,4 @@ describe('extractIds', () => {
}) })
}) })
}) })
}) })

View File

@ -11,7 +11,7 @@ import user from './userMiddleware'
import includedFields from './includedFieldsMiddleware' import includedFields from './includedFieldsMiddleware'
import orderBy from './orderByMiddleware' import orderBy from './orderByMiddleware'
import validation from './validation' import validation from './validation'
import notifications from './notifications' import handleContentData from './handleHtmlContent/handleContentData'
export default schema => { export default schema => {
const middlewares = { const middlewares = {
@ -22,7 +22,7 @@ export default schema => {
validation: validation, validation: validation,
sluggify: sluggify, sluggify: sluggify,
excerpt: excerpt, excerpt: excerpt,
notifications: notifications, handleContentData: handleContentData,
xss: xss, xss: xss,
softDelete: softDelete, softDelete: softDelete,
user: user, user: user,
@ -38,7 +38,7 @@ export default schema => {
'validation', 'validation',
'sluggify', 'sluggify',
'excerpt', 'excerpt',
'notifications', 'handleContentData',
'xss', 'xss',
'softDelete', 'softDelete',
'user', 'user',
@ -57,4 +57,4 @@ export default schema => {
} }
return order.map(key => middlewares[key]) return order.map(key => middlewares[key])
} }

View File

@ -1,30 +0,0 @@
import extractIds from './extractIds'
const notify = async (resolve, root, args, context, resolveInfo) => {
// extract user ids before xss-middleware removes link classes
const ids = extractIds(args.content)
const post = await resolve(root, args, context, resolveInfo)
const session = context.driver.session()
const { id: postId } = post
const createdAt = new Date().toISOString()
const cypher = `
match(u:User) where u.id in $ids
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, { ids, createdAt, postId })
session.close()
return post
}
export default {
Mutation: {
CreatePost: notify,
UpdatePost: notify,
},
}

View File

@ -454,7 +454,7 @@ export default {
}, },
hashtag: { hashtag: {
// TODO: Fill up with input hashtag in search field // TODO: Fill up with input hashtag in search field
url: `/search/hashtag:${item.name}`, url: `/search/hashtag/${item.name}`,
label: item.name, label: item.name,
}, },
} }