Create notifications on CreatePost

This commit is contained in:
Robert Schäfer 2019-04-06 00:33:10 +02:00
parent c1785af8e1
commit cf1f655451
4 changed files with 36 additions and 1 deletions

View File

@ -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,

View File

@ -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
}
}

View File

@ -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 })
})