Implement notifications resolver

This commit is contained in:
roschaefer 2019-08-29 15:30:19 +02:00
parent f207d66fbe
commit 643d175ef6
6 changed files with 46 additions and 43 deletions

View File

@ -25,8 +25,7 @@ const notifyUsers = async (label, id, idsOfUsers, reason, context) => {
MATCH (user: User)
WHERE user.id in $idsOfUsers
AND NOT (user)<-[:BLOCKED]-(author)
CREATE (notification: Notification {id: apoc.create.uuid(), read: false, reason: $reason, createdAt: $createdAt })
MERGE (post)-[:NOTIFIED]->(notification)-[:NOTIFIED]->(user)
MERGE (post)-[:NOTIFIED {id: apoc.create.uuid(), read: false, reason: $reason, createdAt: $createdAt }]->(user)
`
break
}
@ -37,8 +36,7 @@ const notifyUsers = async (label, id, idsOfUsers, reason, context) => {
WHERE user.id in $idsOfUsers
AND NOT (user)<-[:BLOCKED]-(author)
AND NOT (user)<-[:BLOCKED]-(postAuthor)
CREATE (notification: Notification {id: apoc.create.uuid(), read: false, reason: $reason, createdAt: $createdAt })
MERGE (comment)-[:NOTIFIED]->(notification)-[:NOTIFIED]->(user)
MERGE (comment)-[:NOTIFIED {id: apoc.create.uuid(), read: false, reason: $reason, createdAt: $createdAt }]->(user)
`
break
}
@ -49,8 +47,7 @@ const notifyUsers = async (label, id, idsOfUsers, reason, context) => {
WHERE user.id in $idsOfUsers
AND NOT (user)<-[:BLOCKED]-(author)
AND NOT (author)<-[:BLOCKED]-(user)
CREATE (notification: Notification {id: apoc.create.uuid(), read: false, reason: $reason, createdAt: $createdAt })
MERGE (comment)-[:NOTIFIED]->(notification)-[:NOTIFIED]->(user)
MERGE (comment)-[:NOTIFIED {id: apoc.create.uuid(), read: false, reason: $reason, createdAt: $createdAt }]->(user)
`
break
}

View File

@ -160,6 +160,7 @@ const permissions = shield(
PostsEmotionsCountByEmotion: allow,
PostsEmotionsByCurrentUser: allow,
blockedUsers: isAuthenticated,
notifications: isAuthenticated,
},
Mutation: {
'*': deny,

View File

@ -1,36 +0,0 @@
import uuid from 'uuid/v4'
module.exports = {
id: {
type: 'uuid',
primary: true,
default: uuid,
},
read: {
type: 'boolean',
default: false,
},
reason: {
type: 'string',
valid: ['mentioned_in_post', 'mentioned_in_comment', 'comment_on_post'],
invalid: [null],
default: 'mentioned_in_post',
},
createdAt: {
type: 'string',
isoDate: true,
default: () => new Date().toISOString(),
},
user: {
type: 'relationship',
relationship: 'NOTIFIED',
target: 'User',
direction: 'out',
},
post: {
type: 'relationship',
relationship: 'NOTIFIED',
target: 'Post',
direction: 'in',
},
}

View File

@ -7,6 +7,5 @@ export default {
EmailAddress: require('./EmailAddress.js'),
SocialMedia: require('./SocialMedia.js'),
Post: require('./Post.js'),
Notification: require('./Notification.js'),
Category: require('./Category.js'),
}

View File

@ -2,6 +2,36 @@ import { neo4jgraphql } from 'neo4j-graphql-js'
export default {
Query: {
notifications: async (parent, params, context, resolveInfo) => {
const { user, driver } = context
let session
let notifications
try {
session = context.driver.session()
const cypher = `
MATCH (resource)-[notification:NOTIFIED]->(user:User {id:$id})
RETURN resource, notification, user
`
const result = await session.run(cypher, { id: user.id })
const resourceTypes = ['Post', 'Comment']
notifications = await result.records.map(record => {
return {
...record.get('notification').properties,
from: {
__typename: record.get('resource').labels.find(l => resourceTypes.includes(l)),
...record.get('resource').properties
},
to: {
__typename: 'User',
...record.get('user').properties,
}
}
})
} finally {
session.close()
}
return notifications
},
Notification: (object, params, context, resolveInfo) => {
return neo4jgraphql(object, params, context, resolveInfo, false)
},

View File

@ -0,0 +1,12 @@
type NOTIFIED {
from: NotificationSource
to: User
createdAt: String
read: Boolean
}
union NotificationSource = Post | Comment
type Query {
notifications: [NOTIFIED]
}