From 58bc9aa414d7befd568c5cdd5d0a63956e772f4c Mon Sep 17 00:00:00 2001 From: mattwr18 Date: Sat, 4 Apr 2020 19:47:36 +0200 Subject: [PATCH] feat: limit notifications to 25 for menu --- backend/src/db/factories.js | 2 +- backend/src/db/seed.js | 18 +++++++++-- .../src/middleware/permissionsMiddleware.js | 1 + backend/src/models/User.js | 6 ---- backend/src/schema/resolvers/notifications.js | 30 ++++++++++++++++--- backend/src/schema/types/type/NOTIFIED.gql | 1 + webapp/components/CommentList/CommentList.vue | 2 +- .../NotificationMenu/NotificationMenu.vue | 26 +++++++++++----- webapp/graphql/User.js | 7 +++++ webapp/locales/en.json | 1 + 10 files changed, 71 insertions(+), 23 deletions(-) diff --git a/backend/src/db/factories.js b/backend/src/db/factories.js index 1ebf063ff..df96e4359 100644 --- a/backend/src/db/factories.js +++ b/backend/src/db/factories.js @@ -64,7 +64,7 @@ Factory.define('basicUser') password: '1234', role: 'user', about: faker.lorem.paragraph, - termsAndConditionsAgreedVersion: '0.0.1', + termsAndConditionsAgreedVersion: '0.0.4', termsAndConditionsAgreedAt: '2019-08-01T10:47:19.212Z', allowEmbedIframes: false, showShoutsPublicly: false, diff --git a/backend/src/db/seed.js b/backend/src/db/seed.js index 47633b507..e0465f47f 100644 --- a/backend/src/db/seed.js +++ b/backend/src/db/seed.js @@ -1151,10 +1151,10 @@ const languages = ['de', 'en', 'es', 'fr', 'it', 'pt', 'pl'] ) await Promise.all( - [...Array(45).keys()].map(() => + [...Array(45).keys()].map((i) => Factory.build( 'post', - {}, + { id: `p${i + 45}` }, { categoryIds: ['cat1'], author: bobDerBaumeister, @@ -1259,7 +1259,19 @@ const languages = ['de', 'en', 'es', 'fr', 'it', 'pt', 'pl'] ), ), ) - + const notificationsCypher = ` + MATCH (post:Post {id: $id}), (user:User {id: 'u3'}) + CREATE (post)-[notification:NOTIFIED {reason: 'mentioned_in_post'}]->(user) + SET notification.read = FALSE + SET notification.createdAt = COALESCE(notification.createdAt, toString(datetime())) + SET notification.updatedAt = toString(datetime()) + RETURN notification; + ` + await Promise.all( + [...Array(45).keys()].map(async (i) => { + await neode.cypher(notificationsCypher, { id: `p${i + 45}` }) + }), + ) await Factory.build('donations') /* eslint-disable-next-line no-console */ console.log('Seeded Data...') diff --git a/backend/src/middleware/permissionsMiddleware.js b/backend/src/middleware/permissionsMiddleware.js index 2c8d7ff63..c34a0af50 100644 --- a/backend/src/middleware/permissionsMiddleware.js +++ b/backend/src/middleware/permissionsMiddleware.js @@ -104,6 +104,7 @@ export default shield( mutedUsers: isAuthenticated, blockedUsers: isAuthenticated, notifications: isAuthenticated, + unreadNotificationsCount: isAuthenticated, Donations: isAuthenticated, }, Mutation: { diff --git a/backend/src/models/User.js b/backend/src/models/User.js index ae7e1ae8c..ca814a828 100644 --- a/backend/src/models/User.js +++ b/backend/src/models/User.js @@ -94,12 +94,6 @@ export default { createdAt: { type: 'string', isoDate: true, default: () => new Date().toISOString() }, }, }, - notifications: { - type: 'relationship', - relationship: 'NOTIFIED', - target: 'User', - direction: 'in', - }, termsAndConditionsAgreedVersion: { type: 'string', allow: [null], diff --git a/backend/src/schema/resolvers/notifications.js b/backend/src/schema/resolvers/notifications.js index 3c01ddb97..768fdc60f 100644 --- a/backend/src/schema/resolvers/notifications.js +++ b/backend/src/schema/resolvers/notifications.js @@ -15,10 +15,9 @@ export default { }, Query: { notifications: async (_parent, args, context, _resolveInfo) => { - const { user: currentUser } = context - const session = context.driver.session() + const { user: currentUser, driver } = context + const session = driver.session() let whereClause, orderByClause - switch (args.read) { case true: whereClause = 'WHERE notification.read = TRUE' @@ -41,7 +40,6 @@ export default { } const offset = args.offset && typeof args.offset === 'number' ? `SKIP ${args.offset}` : '' const limit = args.first && typeof args.first === 'number' ? `LIMIT ${args.first}` : '' - const readTxResultPromise = session.readTransaction(async (transaction) => { const notificationsTransactionResponse = await transaction.run( ` @@ -68,6 +66,30 @@ export default { 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 88ecd3882..d1787ee4c 100644 --- a/backend/src/schema/types/type/NOTIFIED.gql +++ b/backend/src/schema/types/type/NOTIFIED.gql @@ -25,6 +25,7 @@ enum NotificationReason { type Query { notifications(read: Boolean, orderBy: NotificationOrdering, first: Int, offset: Int): [NOTIFIED] + unreadNotificationsCount: Int! } type Mutation { diff --git a/webapp/components/CommentList/CommentList.vue b/webapp/components/CommentList/CommentList.vue index 377380325..608f95fe2 100644 --- a/webapp/components/CommentList/CommentList.vue +++ b/webapp/components/CommentList/CommentList.vue @@ -48,7 +48,7 @@ export default { return anchor === '#comments' }, updateCommentList(updatedComment) { - this.postComments = this.postComments.map((comment) => { + this.post.comments = this.postComments.map((comment) => { return comment.id === updatedComment.id ? updatedComment : comment }) }, diff --git a/webapp/components/NotificationMenu/NotificationMenu.vue b/webapp/components/NotificationMenu/NotificationMenu.vue index ec991f0ef..a4cd3ff34 100644 --- a/webapp/components/NotificationMenu/NotificationMenu.vue +++ b/webapp/components/NotificationMenu/NotificationMenu.vue @@ -18,7 +18,11 @@ @@ -28,7 +32,12 @@ diff --git a/webapp/graphql/User.js b/webapp/graphql/User.js index aeb6ae729..279d072c1 100644 --- a/webapp/graphql/User.js +++ b/webapp/graphql/User.js @@ -174,6 +174,13 @@ export const notificationAdded = () => { } ` } + +export const unreadNotificationsCountQuery = gql` + query { + unreadNotificationsCount + } +` + export const followUserMutation = (i18n) => { return gql` ${userFragment} diff --git a/webapp/locales/en.json b/webapp/locales/en.json index e91f5c37a..cfd3fbe01 100644 --- a/webapp/locales/en.json +++ b/webapp/locales/en.json @@ -450,6 +450,7 @@ "read": "Read", "unread": "Unread" }, + "manyNotifications": "See all {unreadNotificationsCount} notifications", "pageLink": "All notifications", "post": "Post", "reason": {