diff --git a/backend/src/middleware/notifications/notificationsMiddleware.js b/backend/src/middleware/notifications/notificationsMiddleware.js index c76b9ca0e..395c3ad5d 100644 --- a/backend/src/middleware/notifications/notificationsMiddleware.js +++ b/backend/src/middleware/notifications/notificationsMiddleware.js @@ -90,7 +90,7 @@ const notifyUsersOfMention = async (label, id, idsOfUsers, reason, context) => { SET notification.read = FALSE SET notification.createdAt = COALESCE(notification.createdAt, toString(datetime())) SET notification.updatedAt = toString(datetime()) - RETURN notification {.*, from: finalResource, to: properties(user)} + RETURN { notificationsCount: toString(size(collect(notification))), notifications: collect(notification {.*, from: finalResource, to: properties(user)}) } as noticationsResult ` const session = context.driver.session() const writeTxResultPromise = session.writeTransaction(async (transaction) => { @@ -99,10 +99,10 @@ const notifyUsersOfMention = async (label, id, idsOfUsers, reason, context) => { idsOfUsers, reason, }) - return notificationTransactionResponse.records.map((record) => record.get('notification')) + return notificationTransactionResponse.records.map((record) => record.get('noticationsResult')) }) try { - const notifications = await writeTxResultPromise + const [notifications] = await writeTxResultPromise return notifications } catch (error) { throw new Error(error) @@ -126,14 +126,14 @@ const notifyUsersOfComment = async (label, commentId, postAuthorId, reason, cont SET notification.updatedAt = toString(datetime()) WITH notification, postAuthor, post, comment {.*, __typename: labels(comment)[0], author: properties(commenter), post: post {.*, author: properties(postAuthor) } } AS finalResource - RETURN notification {.*, from: finalResource, to: properties(postAuthor)} + RETURN { notificationsCount: toString(size(collect(notification))), notifications: collect(notification {.*, from: finalResource, to: properties(postAuthor)}) } as noticationsResult `, { commentId, postAuthorId, reason }, ) - return notificationTransactionResponse.records.map((record) => record.get('notification')) + return notificationTransactionResponse.records.map((record) => record.get('noticationsResult')) }) try { - const notifications = await writeTxResultPromise + const [notifications] = await writeTxResultPromise return notifications } finally { session.close() diff --git a/backend/src/middleware/permissionsMiddleware.js b/backend/src/middleware/permissionsMiddleware.js index c34a0af50..2c8d7ff63 100644 --- a/backend/src/middleware/permissionsMiddleware.js +++ b/backend/src/middleware/permissionsMiddleware.js @@ -104,7 +104,6 @@ export default shield( mutedUsers: isAuthenticated, blockedUsers: isAuthenticated, notifications: isAuthenticated, - unreadNotificationsCount: isAuthenticated, Donations: isAuthenticated, }, Mutation: { diff --git a/backend/src/schema/resolvers/notifications.js b/backend/src/schema/resolvers/notifications.js index 768fdc60f..45f4ec3ad 100644 --- a/backend/src/schema/resolvers/notifications.js +++ b/backend/src/schema/resolvers/notifications.js @@ -8,7 +8,9 @@ export default { subscribe: withFilter( () => pubsub.asyncIterator(NOTIFICATION_ADDED), (payload, variables) => { - return payload.notificationAdded.to.id === variables.userId + const { notifications } = payload.notificationAdded + const [currentUser] = notifications + return currentUser.to.id === variables.userId }, ), }, @@ -50,46 +52,24 @@ export default { [(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)} ${orderByClause} + RETURN { notificationsCount: toString(size(collect(notification))), notifications: collect(notification {.*, from: finalResource, to: properties(user)})} as noticationsResult ${offset} ${limit} `, { id: currentUser.id }, ) log(notificationsTransactionResponse) - return notificationsTransactionResponse.records.map((record) => record.get('notification')) + return notificationsTransactionResponse.records.map((record) => + record.get('noticationsResult'), + ) }) try { - const notifications = await readTxResultPromise + const [notifications] = await readTxResultPromise return notifications } finally { session.close() } }, - unreadNotificationsCount: async (_parent, _args, context, _resolveInfo) => { - const { user: currentUser, driver } = context - const session = driver.session() - - const readTxResultPromise = session.readTransaction(async (transaction) => { - const unreadNotificationsCountTransactionResponse = await transaction.run( - ` - MATCH (resource {deleted: false, disabled: false})-[notification:NOTIFIED {read: false}]->(user:User {id:$id}) - RETURN count(notification) as unreadNotificationsCount - `, - { id: currentUser.id }, - ) - log(unreadNotificationsCountTransactionResponse) - return unreadNotificationsCountTransactionResponse.records.map( - (record) => record.get('unreadNotificationsCount').low, - ) - }) - try { - const [unreadNotificationsCount] = await readTxResultPromise - return unreadNotificationsCount - } finally { - session.close() - } - }, }, Mutation: { markAsRead: async (parent, args, context, resolveInfo) => { diff --git a/backend/src/schema/types/type/NOTIFIED.gql b/backend/src/schema/types/type/NOTIFIED.gql index d1787ee4c..5013194eb 100644 --- a/backend/src/schema/types/type/NOTIFIED.gql +++ b/backend/src/schema/types/type/NOTIFIED.gql @@ -8,6 +8,11 @@ type NOTIFIED { reason: NotificationReason } +type notificationsResult { + notificationsCount: Int! + notifications: [NOTIFIED] +} + union NotificationSource = Post | Comment enum NotificationOrdering { @@ -24,8 +29,7 @@ enum NotificationReason { } type Query { - notifications(read: Boolean, orderBy: NotificationOrdering, first: Int, offset: Int): [NOTIFIED] - unreadNotificationsCount: Int! + notifications(read: Boolean, orderBy: NotificationOrdering, first: Int, offset: Int): notificationsResult! } type Mutation { @@ -33,5 +37,5 @@ type Mutation { } type Subscription { - notificationAdded(userId: ID!): NOTIFIED + notificationAdded(userId: ID!): notificationsResult } diff --git a/webapp/components/NotificationMenu/NotificationMenu.vue b/webapp/components/NotificationMenu/NotificationMenu.vue index a4cd3ff34..f0ca6612d 100644 --- a/webapp/components/NotificationMenu/NotificationMenu.vue +++ b/webapp/components/NotificationMenu/NotificationMenu.vue @@ -1,26 +1,22 @@