Implement right order of notifications middleware

This commit is contained in:
Robert Schäfer 2019-04-16 17:48:42 +02:00
parent 4a6ef3f9f6
commit f86e98b32e
3 changed files with 13 additions and 8 deletions

View File

@ -20,9 +20,9 @@ export default schema => {
validUrlMiddleware,
sluggifyMiddleware,
excerptMiddleware,
notificationsMiddleware,
xssMiddleware,
fixImageUrlsMiddleware,
notificationsMiddleware,
softDeleteMiddleware,
userMiddleware,
includedFieldsMiddleware,

View File

@ -1,20 +1,20 @@
import extractIds from './extractMentions'
const notify = async (resolve, root, args, context, resolveInfo) => {
const ids = extractIds(args.content) // before mention class gets removed
const post = await resolve(root, args, context, resolveInfo)
const session = context.driver.session()
const { content, id: postId } = post
const slugs = extractIds(content)
const createdAt = (new Date()).toISOString()
const cypher = `
match(u:User) where u.slug in $slugs
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, { slugs, createdAt, postId })
await session.run(cypher, { ids, createdAt, postId })
session.close()
return post

View File

@ -54,26 +54,31 @@ describe('currentUser { notifications }', () => {
})
describe('who mentions me in a post', () => {
let post
const title = 'Mentioning Al Capone'
const content = 'Hey <a class="mention" href="/profile/you/al-capone">@al-capone</a> how do you do?'
beforeEach(async () => {
const content = 'Hey @al-capone how do you do?'
const title = 'Mentioning Al Capone'
const createPostMutation = `
mutation($title: String!, $content: String!) {
CreatePost(title: $title, content: $content) {
id
title
content
}
}
`
authorClient = new GraphQLClient(host, { headers: authorHeaders })
await authorClient.request(createPostMutation, { title, content })
const { CreatePost } = await authorClient.request(createPostMutation, { title, content })
post = CreatePost
})
it('sends you a notification', async () => {
const newContent = 'Hey <a href="/profile/you/al-capone" target=\"_blank\">@al-capone</a> how do you do?'
const expected = {
currentUser: {
notifications: [
{ read: false, post: { content: 'Hey @al-capone how do you do?' } }
{ read: false, post: { content: newContent } }
]
}
}