mirror of
https://github.com/Ocelot-Social-Community/Ocelot-Social.git
synced 2025-12-13 07:46:06 +00:00
Create notifications on CreatePost
This commit is contained in:
parent
c1785af8e1
commit
cf1f655451
@ -10,6 +10,7 @@ import permissionsMiddleware from './permissionsMiddleware'
|
|||||||
import userMiddleware from './userMiddleware'
|
import userMiddleware from './userMiddleware'
|
||||||
import includedFieldsMiddleware from './includedFieldsMiddleware'
|
import includedFieldsMiddleware from './includedFieldsMiddleware'
|
||||||
import orderByMiddleware from './orderByMiddleware'
|
import orderByMiddleware from './orderByMiddleware'
|
||||||
|
import notificationsMiddleware from './notificationsMiddleware'
|
||||||
|
|
||||||
export default schema => {
|
export default schema => {
|
||||||
let middleware = [
|
let middleware = [
|
||||||
@ -19,6 +20,7 @@ export default schema => {
|
|||||||
excerptMiddleware,
|
excerptMiddleware,
|
||||||
xssMiddleware,
|
xssMiddleware,
|
||||||
fixImageUrlsMiddleware,
|
fixImageUrlsMiddleware,
|
||||||
|
notificationsMiddleware,
|
||||||
softDeleteMiddleware,
|
softDeleteMiddleware,
|
||||||
userMiddleware,
|
userMiddleware,
|
||||||
includedFieldsMiddleware,
|
includedFieldsMiddleware,
|
||||||
|
|||||||
31
backend/src/middleware/notificationsMiddleware.js
Normal file
31
backend/src/middleware/notificationsMiddleware.js
Normal 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
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -24,8 +24,10 @@ describe('currentUser { notifications }', () => {
|
|||||||
currentUser {
|
currentUser {
|
||||||
notifications(read: $read, orderBy: createdAt_desc) {
|
notifications(read: $read, orderBy: createdAt_desc) {
|
||||||
id
|
id
|
||||||
|
read
|
||||||
post {
|
post {
|
||||||
id
|
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 })
|
await authorClient.request(createPostMutation, { title, content })
|
||||||
})
|
})
|
||||||
|
|
||||||
Loading…
x
Reference in New Issue
Block a user