Refactor out transformReturnType function

- we have introduced a better way to return all the info we want without
  needing to make multiple database queries. It was introduced by
@roschaefer in the PR for the notifications query, but we hadn't
refactored markAsRead yet.
- Now that we are subscribing to notifications, we need to have the same
  return info as the notification query.

- Co-authored-by: Tirokk <wolle.huss@pjannto.com>
This commit is contained in:
mattwr18 2020-02-05 18:42:31 +01:00
parent 2f43069ea0
commit 110165691a
2 changed files with 20 additions and 29 deletions

View File

@ -1,10 +1,6 @@
import extractMentionedUsers from './mentions/extractMentionedUsers'
import { validateNotifyUsers } from '../validation/validationMiddleware'
import {
pubsub,
NOTIFICATION_ADDED,
transformReturnType,
} from '../../schema/resolvers/notifications'
import { pubsub, NOTIFICATION_ADDED } from '../../schema/resolvers/notifications'
const handleContentDataOfPost = async (resolve, root, args, context, resolveInfo) => {
const idsOfUsers = extractMentionedUsers(args.content)
@ -57,31 +53,31 @@ const notifyUsersOfMention = async (label, id, idsOfUsers, reason, context) => {
WHERE user.id in $idsOfUsers
AND NOT (user)-[:BLOCKED]-(author)
MERGE (post)-[notification:NOTIFIED {reason: $reason}]->(user)
WITH notification, post AS resource, user
WITH notification, user,
post {.*, __typename: labels(post)[0], author: properties(author) } as finalResource
`
break
}
case 'mentioned_in_comment': {
mentionedCypher = `
MATCH (postAuthor: User)-[:WROTE]->(post: Post)<-[:COMMENTS]-(comment: Comment { id: $id })<-[:WROTE]-(author: User)
MATCH (postAuthor: User)-[:WROTE]->(post: Post)<-[:COMMENTS]-(comment: Comment { id: $id })<-[:WROTE]-(commenter: User)
MATCH (user: User)
WHERE user.id in $idsOfUsers
AND NOT (user)-[:BLOCKED]-(author)
AND NOT (user)-[:BLOCKED]-(commenter)
AND NOT (user)-[:BLOCKED]-(postAuthor)
MERGE (comment)-[notification:NOTIFIED {reason: $reason}]->(user)
WITH notification, comment AS resource, user
WITH notification, user,
comment {.*, __typename: labels(comment)[0], author: properties(commenter), post: post {.*, author: properties(postAuthor)} } as finalResource
`
break
}
}
mentionedCypher += `
WITH notification, finalResource, user
SET notification.read = FALSE
SET (
CASE
WHEN notification.createdAt IS NULL
THEN notification END ).createdAt = toString(datetime())
SET notification.createdAt = COALESCE(notification.createdAt, toString(datetime()))
SET notification.updatedAt = toString(datetime())
RETURN notification, resource, user, labels(resource)[0] AS type
RETURN notification {.*, from: finalResource, to: properties(user)}
`
const session = context.driver.session()
const writeTxResultPromise = session.writeTransaction(async transaction => {
@ -90,7 +86,7 @@ const notifyUsersOfMention = async (label, id, idsOfUsers, reason, context) => {
idsOfUsers,
reason,
})
return notificationTransactionResponse.records.map(transformReturnType)
return notificationTransactionResponse.records.map(record => record.get('notification'))
})
try {
const [notification] = await writeTxResultPromise

View File

@ -4,18 +4,6 @@ import { withFilter } from 'graphql-subscriptions'
export const pubsub = new PubSub()
export const NOTIFICATION_ADDED = 'NOTIFICATION_ADDED'
export const transformReturnType = record => {
return {
...record.get('notification').properties,
from: {
__typename: record.get('type'),
...record.get('resource').properties,
},
to: {
...record.get('user').properties,
},
}
}
export default {
Subscription: {
@ -93,12 +81,19 @@ export default {
`
MATCH (resource {id: $resourceId})-[notification:NOTIFIED {read: FALSE}]->(user:User {id:$id})
SET notification.read = TRUE
RETURN resource, notification, user, labels(resource)[0] AS type
WITH user, notification, resource,
[(resource)<-[:WROTE]-(author:User) | author {.*}] as authors,
[(resource)-[:COMMENTS]->(post:Post)<-[:WROTE]-(author:User) | post{.*, author: properties(author)} ] as posts
WITH resource, user, notification, authors, posts,
resource {.*, __typename: labels(resource)[0], author: authors[0], post: posts[0]} as finalResource
RETURN notification {.*, from: finalResource, to: properties(user)}
`,
{ resourceId: args.id, id: currentUser.id },
)
log(markNotificationAsReadTransactionResponse)
return markNotificationAsReadTransactionResponse.records.map(transformReturnType)
return markNotificationAsReadTransactionResponse.records.map(record =>
record.get('notification'),
)
})
try {
const [notifications] = await writeTxResultPromise