diff --git a/backend/src/middleware/permissionsMiddleware.js b/backend/src/middleware/permissionsMiddleware.js index 2c8d7ff63..d3aa7ce9a 100644 --- a/backend/src/middleware/permissionsMiddleware.js +++ b/backend/src/middleware/permissionsMiddleware.js @@ -143,6 +143,7 @@ export default shield( blockUser: isAuthenticated, unblockUser: isAuthenticated, markAsRead: isAuthenticated, + markAllAsRead: isAuthenticated, AddEmailAddress: isAuthenticated, VerifyEmailAddress: isAuthenticated, pinPost: isAdmin, diff --git a/backend/src/schema/resolvers/notifications.js b/backend/src/schema/resolvers/notifications.js index 3c01ddb97..a96181a50 100644 --- a/backend/src/schema/resolvers/notifications.js +++ b/backend/src/schema/resolvers/notifications.js @@ -99,6 +99,35 @@ export default { session.close() } }, + markAllAsRead: async (parent, args, context, resolveInfo) => { + const { user: currentUser } = context + const session = context.driver.session() + const writeTxResultPromise = session.writeTransaction(async (transaction) => { + const markAllNotificationAsReadTransactionResponse = await transaction.run( + ` + MATCH (resource {deleted: false, disabled: false})-[notification:NOTIFIED {read: FALSE}]->(user:User {id:$id}) + SET notification.read = TRUE + 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)} + `, + { id: currentUser.id }, + ) + log(markAllNotificationAsReadTransactionResponse) + return markAllNotificationAsReadTransactionResponse.records.map((record) => + record.get('notification'), + ) + }) + try { + const notifications = await writeTxResultPromise + return notifications + } finally { + session.close() + } + }, }, NOTIFIED: { id: async (parent) => { diff --git a/backend/src/schema/types/type/NOTIFIED.gql b/backend/src/schema/types/type/NOTIFIED.gql index 88ecd3882..864cdea4d 100644 --- a/backend/src/schema/types/type/NOTIFIED.gql +++ b/backend/src/schema/types/type/NOTIFIED.gql @@ -29,6 +29,7 @@ type Query { type Mutation { markAsRead(id: ID!): NOTIFIED + markAllAsRead: [NOTIFIED] } type Subscription { diff --git a/webapp/graphql/User.js b/webapp/graphql/User.js index 3b015dacc..8a0494402 100644 --- a/webapp/graphql/User.js +++ b/webapp/graphql/User.js @@ -136,6 +136,42 @@ export const markAsReadMutation = (i18n) => { ` } +export const markAllAsReadMutation = (i18n) => { + return gql` + ${userFragment} + ${commentFragment} + ${postFragment} + + mutation { + markAllAsRead { + id + read + reason + createdAt + updatedAt + from { + __typename + ... on Post { + ...post + author { + ...user + } + } + ... on Comment { + ...comment + post { + ...post + author { + ...user + } + } + } + } + } + } + ` +} + export const notificationAdded = () => { return gql` ${userFragment}