diff --git a/backend/src/middleware/dateTimeMiddleware.js b/backend/src/middleware/dateTimeMiddleware.js index c8af53a7a..ff1fcc996 100644 --- a/backend/src/middleware/dateTimeMiddleware.js +++ b/backend/src/middleware/dateTimeMiddleware.js @@ -12,11 +12,9 @@ export default { CreatePost: setCreatedAt, CreateComment: setCreatedAt, CreateOrganization: setCreatedAt, - CreateNotification: setCreatedAt, UpdateUser: setUpdatedAt, UpdatePost: setUpdatedAt, UpdateComment: setUpdatedAt, UpdateOrganization: setUpdatedAt, - UpdateNotification: setUpdatedAt, }, } diff --git a/backend/src/middleware/permissionsMiddleware.js b/backend/src/middleware/permissionsMiddleware.js index 2b29cd152..6f8ff8c2d 100644 --- a/backend/src/middleware/permissionsMiddleware.js +++ b/backend/src/middleware/permissionsMiddleware.js @@ -149,7 +149,6 @@ const permissions = shield( Category: allow, Tag: allow, Report: isModerator, - Notification: isAdmin, statistics: allow, currentUser: allow, Post: or(onlyEnabledContent, isModerator), @@ -169,7 +168,6 @@ const permissions = shield( Signup: isAdmin, SignupVerification: allow, CreateInvitationCode: and(isAuthenticated, or(not(invitationLimitReached), isAdmin)), - UpdateNotification: belongsToMe, UpdateUser: onlyYourself, CreatePost: isAuthenticated, UpdatePost: isAuthor, @@ -199,6 +197,7 @@ const permissions = shield( RemovePostEmotions: isAuthenticated, block: isAuthenticated, unblock: isAuthenticated, + markAsRead: belongsToMe }, User: { email: isMyOwn, diff --git a/backend/src/schema/index.js b/backend/src/schema/index.js index b8f120057..e8fa63d97 100644 --- a/backend/src/schema/index.js +++ b/backend/src/schema/index.js @@ -20,6 +20,7 @@ export default applyScalars( 'Statistics', 'LoggedInUser', 'SocialMedia', + 'NOTIFIED', ], // add 'User' here as soon as possible }, @@ -32,6 +33,7 @@ export default applyScalars( 'Statistics', 'LoggedInUser', 'SocialMedia', + 'NOTIFIED', ], // add 'User' here as soon as possible }, diff --git a/backend/src/schema/resolvers/notifications.js b/backend/src/schema/resolvers/notifications.js index fcb8db1a4..17b8e7532 100644 --- a/backend/src/schema/resolvers/notifications.js +++ b/backend/src/schema/resolvers/notifications.js @@ -2,15 +2,40 @@ import { neo4jgraphql } from 'neo4j-graphql-js' export default { Query: { - notifications: async (parent, params, context, resolveInfo) => { + notifications: async (parent, args, context, resolveInfo) => { const { user, driver } = context let session let notifications + let whereClause + let orderByClause + switch(args.read) { + case true: + whereClause = 'WHERE notification.read = TRUE' + break; + case false: + whereClause = 'WHERE notification.read = FALSE' + break; + default: + whereClause = '' + } + switch(args.orderBy) { + case 'createdAt_asc': + orderByClause = 'ORDER BY notification.createdAt ASC' + break; + case 'createdAt_desc': + orderByClause = 'ORDER BY notification.createdAt DESC' + break; + default: + orderByClause = '' + } + try { session = context.driver.session() const cypher = ` MATCH (resource)-[notification:NOTIFIED]->(user:User {id:$id}) + ${whereClause} RETURN resource, notification, user + ${orderByClause} ` const result = await session.run(cypher, { id: user.id }) const resourceTypes = ['Post', 'Comment'] @@ -32,13 +57,10 @@ export default { } return notifications }, - Notification: (object, params, context, resolveInfo) => { - return neo4jgraphql(object, params, context, resolveInfo, false) - }, }, Mutation: { - UpdateNotification: (object, params, context, resolveInfo) => { - return neo4jgraphql(object, params, context, resolveInfo, false) - }, + markAsRead: async (parent, params, context, resolveInfo) => { + return null + } }, } diff --git a/backend/src/schema/resolvers/notifications.spec.js b/backend/src/schema/resolvers/notifications.spec.js index 3ca7727e4..c173eeef1 100644 --- a/backend/src/schema/resolvers/notifications.spec.js +++ b/backend/src/schema/resolvers/notifications.spec.js @@ -26,10 +26,10 @@ afterEach(async () => { await factory.cleanDatabase() }) -describe('Notification', () => { +describe('notifications', () => { const notificationQuery = gql` query { - Notification { + notifications { id } } @@ -41,10 +41,6 @@ describe('Notification', () => { await expect(client.request(notificationQuery)).rejects.toThrow('Not Authorised') }) }) -}) - -describe('currentUser notifications', () => { - const variables = {} describe('authenticated', () => { let headers @@ -160,15 +156,13 @@ describe('currentUser notifications', () => { describe('filter for read: false', () => { const queryCurrentUserNotificationsFilterRead = gql` query($read: Boolean) { - currentUser { - notifications(read: $read, orderBy: createdAt_desc) { + notifications(read: $read, orderBy: createdAt_desc) { + id + post { + id + } + comment { id - post { - id - } - comment { - id - } } } } @@ -204,15 +198,13 @@ describe('currentUser notifications', () => { describe('no filters', () => { const queryCurrentUserNotifications = gql` query { - currentUser { - notifications(orderBy: createdAt_desc) { + notifications(orderBy: createdAt_desc) { + id + post { + id + } + comment { id - post { - id - } - comment { - id - } } } } diff --git a/backend/src/schema/types/type/NOTIFIED.gql b/backend/src/schema/types/type/NOTIFIED.gql index 40a6f1b22..0be774f30 100644 --- a/backend/src/schema/types/type/NOTIFIED.gql +++ b/backend/src/schema/types/type/NOTIFIED.gql @@ -7,6 +7,15 @@ type NOTIFIED { union NotificationSource = Post | Comment -type Query { - notifications: [NOTIFIED] +enum NOTIFIEDOrdering { + createdAt_asc + createdAt_desc +} + +type Query { + notifications(read: Boolean, orderBy: NOTIFIEDOrdering): [NOTIFIED] +} + +type Mutation { + markAsRead(id: ID!): NOTIFIED } diff --git a/backend/src/schema/types/type/Notification.gql b/backend/src/schema/types/type/Notification.gql deleted file mode 100644 index a3543445f..000000000 --- a/backend/src/schema/types/type/Notification.gql +++ /dev/null @@ -1,9 +0,0 @@ -type Notification { - id: ID! - read: Boolean - reason: ReasonNotification - createdAt: String - user: User @relation(name: "NOTIFIED", direction: "OUT") - post: Post @relation(name: "NOTIFIED", direction: "IN") - comment: Comment @relation(name: "NOTIFIED", direction: "IN") -} diff --git a/backend/src/schema/types/type/User.gql b/backend/src/schema/types/type/User.gql index 49592e8ba..9bbef50e4 100644 --- a/backend/src/schema/types/type/User.gql +++ b/backend/src/schema/types/type/User.gql @@ -24,8 +24,6 @@ type User { createdAt: String updatedAt: String - notifications(read: Boolean): [Notification]! @relation(name: "NOTIFIED", direction: "IN") - friends: [User]! @relation(name: "FRIENDS", direction: "BOTH") friendsCount: Int! @cypher(statement: "MATCH (this)<-[:FRIENDS]->(r:User) RETURN COUNT(DISTINCT r)") diff --git a/backend/src/seed/factories/index.js b/backend/src/seed/factories/index.js index 56518bd06..4cc143e68 100644 --- a/backend/src/seed/factories/index.js +++ b/backend/src/seed/factories/index.js @@ -8,7 +8,6 @@ import createComment from './comments.js' import createCategory from './categories.js' import createTag from './tags.js' import createReport from './reports.js' -import createNotification from './notifications.js' export const seedServerHost = 'http://127.0.0.1:4001' @@ -31,7 +30,6 @@ const factories = { Category: createCategory, Tag: createTag, Report: createReport, - Notification: createNotification, } export const cleanDatabase = async (options = {}) => { diff --git a/backend/src/seed/factories/notifications.js b/backend/src/seed/factories/notifications.js deleted file mode 100644 index d14d4294a..000000000 --- a/backend/src/seed/factories/notifications.js +++ /dev/null @@ -1,17 +0,0 @@ -import uuid from 'uuid/v4' - -export default function(params) { - const { id = uuid(), read = false } = params - - return { - mutation: ` - mutation($id: ID, $read: Boolean) { - CreateNotification(id: $id, read: $read) { - id - read - } - } - `, - variables: { id, read }, - } -}