From 21e82d2ea5068f9f98df8ceb6d68f66b0bee53ec Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Wolfgang=20Hu=C3=9F?= Date: Wed, 30 Oct 2019 19:01:54 +0100 Subject: [PATCH] Get decide mutation to work - Tests are not fully working yet! --- .../validation/validationMiddleware.js | 2 +- backend/src/schema/resolvers/moderation.js | 68 +++++++++---------- .../src/schema/resolvers/moderation.spec.js | 27 +++++--- backend/src/schema/types/type/REPORTED.gql | 2 + webapp/pages/moderation/index.vue | 6 +- 5 files changed, 60 insertions(+), 45 deletions(-) diff --git a/backend/src/middleware/validation/validationMiddleware.js b/backend/src/middleware/validation/validationMiddleware.js index 740f25ec4..1a31fe891 100644 --- a/backend/src/middleware/validation/validationMiddleware.js +++ b/backend/src/middleware/validation/validationMiddleware.js @@ -89,7 +89,7 @@ const validateReport = async (resolve, root, args, context, info) => { // const session = driver.session() // const reportQueryRes = await session.run( // ` -// MATCH (:User {id:$submitterId})-[:REPORTED]->(resource {id:$resourceId}) +// MATCH (:User {id:$submitterId})-[:REPORTED]->(resource {id:$resourceId}) // RETURN labels(resource)[0] as label // `, // { diff --git a/backend/src/schema/resolvers/moderation.js b/backend/src/schema/resolvers/moderation.js index 33d021656..e5e2c580c 100644 --- a/backend/src/schema/resolvers/moderation.js +++ b/backend/src/schema/resolvers/moderation.js @@ -37,9 +37,6 @@ export default { SET decision.disabled = false, decision.updatedAt = toString(datetime()) RETURN resource {.id} ` - // Wolle - // DELETE decision - const session = driver.session() const res = await session.run(cypher, { resourceId }) session.close() @@ -51,7 +48,8 @@ export default { }, decide: async (object, params, context, _resolveInfo) => { let createdRelationshipWithNestedAttributes = null - const { resourceId, disabled, closed } = params + const { resourceId } = params + let { disabled, closed } = params const { user: moderator, driver } = context const session = driver.session() @@ -62,7 +60,8 @@ export default { MATCH (moderator:User)-[decision:DECIDED {closed: false}]->(resource {id: $resourceId}) WHERE resource:User OR resource:Comment OR resource:Post RETURN decision, moderator {.id} AS decisionModerator - `, { resourceId }, + `, + { resourceId }, ) return decisionRelationshipTransactionResponse.records.map(record => ({ decision: record.get('decision'), @@ -71,31 +70,32 @@ export default { }) try { - const cypherHeader = '' + let cypherHeader = '' // is there an open decision? const [existingDecisionTxResult] = await existingDecisionWriteTxResultPromise if (!existingDecisionTxResult) { // no open decision, then create one - if (!disabled) disabled = false // default for creation - if (!disabled) closed = false // default for creation + if (disabled === undefined) disabled = false // default for creation + if (closed === undefined) closed = false // default for creation cypherHeader = ` MATCH (moderator:User {id: $moderatorId}) MATCH (resource {id: $resourceId}) WHERE resource:User OR resource:Comment OR resource:Post CREATE (resource)<-[decision:DECIDED]-(moderator) SET decision.last = true + WITH decision, resource, moderator OPTIONAL MATCH (:User)-[lastDecision:DECIDED {last: true}]->(resource) SET ( CASE WHEN lastDecision IS NOT NULL THEN lastDecision END).last = false - ` + ` } else { // an open decision … - - if (!disabled) disabled = existingDecisionTxResult.decision.properties.disabled // default set to existing - if (!disabled) closed = existingDecisionTxResult.decision.properties.closed // default set to existing + if (disabled === undefined) + disabled = existingDecisionTxResult.decision.properties.disabled // default set to existing + if (closed === undefined) closed = existingDecisionTxResult.decision.properties.closed // default set to existing // current moderator is not the same as old if (moderator.id !== existingDecisionTxResult.decisionModerator.id) { // an open decision from different moderator, then change relation and properties @@ -108,37 +108,37 @@ export default { WHERE resource:User OR resource:Comment OR resource:Post CREATE (resource)<-[decision:DECIDED]-(moderator) SET decision = oldDecision - ` + ` } else { // an open decision from same moderator, then change properties cypherHeader = ` MATCH (moderator:User)-[decision:DECIDED {closed: false}]->(resource {id: $resourceId}) WHERE resource:User OR resource:Comment OR resource:Post - ` + ` } } + const cypher = + cypherHeader + + `SET ( + CASE + WHEN decision.createdAt IS NOT NULL + THEN decision END).updatedAt = toString(datetime()) + SET ( + CASE + WHEN decision.createdAt IS NULL + THEN decision END).createdAt = toString(datetime()) + SET decision.disabled = $disabled, decision.closed = $closed + SET resource.disabled = $disabled + RETURN decision, resource, moderator, labels(resource)[0] AS type + ` const newDecisionWriteTxResultPromise = session.writeTransaction(async txc => { - const decisionRelationshipTransactionResponse = await txc.run( - cypherHeader + ` - SET ( - CASE - WHEN decision.createdAt IS NOT NULL - THEN decision END).updatedAt = toString(datetime()) - SET ( - CASE - WHEN decision.createdAt IS NULL - THEN decision END).createdAt = toString(datetime()) - SET decision.disabled = $disabled, decision.closed = $closed - SET resource.disabled = $disabled - RETURN decision, resource, moderator, labels(resource)[0] AS type - `, { - resourceId, - moderatorId: moderator.id, - disabled, - closed, - }, - ) + const decisionRelationshipTransactionResponse = await txc.run(cypher, { + resourceId, + moderatorId: moderator.id, + disabled, + closed, + }) return decisionRelationshipTransactionResponse.records.map(record => ({ decision: record.get('decision'), resource: record.get('resource'), diff --git a/backend/src/schema/resolvers/moderation.spec.js b/backend/src/schema/resolvers/moderation.spec.js index 531bec693..242a8c3fa 100644 --- a/backend/src/schema/resolvers/moderation.spec.js +++ b/backend/src/schema/resolvers/moderation.spec.js @@ -12,7 +12,14 @@ let query, mutate, authenticatedUser, variables, moderator, nonModerator const disableMutation = gql` mutation($id: ID!) { - disable(id: $id) + decide(resourceId: $id, disabled: true, closed: false) { + post { + id + } + comment { + id + } + } } ` const enableMutation = gql` @@ -123,7 +130,7 @@ describe('moderate resources', () => { it('returns null', async () => { await expect(mutate({ mutation: disableMutation, variables })).resolves.toMatchObject({ - data: { disable: null }, + data: { decide: null }, }) }) }) @@ -139,7 +146,7 @@ describe('moderate resources', () => { it('returns disabled resource id', async () => { variables = { id: 'comment-id' } await expect(mutate({ mutation: disableMutation, variables })).resolves.toMatchObject({ - data: { disable: 'comment-id' }, + data: { decide: { comment: { id: 'comment-id' } } }, errors: undefined, }) }) @@ -152,7 +159,7 @@ describe('moderate resources', () => { } await expect(query({ query: commentQuery, variables })).resolves.toMatchObject(before) await expect(mutate({ mutation: disableMutation, variables })).resolves.toMatchObject({ - data: { disable: 'comment-id' }, + data: { decide: { comment: { id: 'comment-id' } } }, }) await expect(query({ query: commentQuery, variables })).resolves.toMatchObject(expected) }) @@ -164,7 +171,7 @@ describe('moderate resources', () => { await expect(query({ query: commentQuery, variables })).resolves.toMatchObject(before) await expect(mutate({ mutation: disableMutation, variables })).resolves.toMatchObject({ - data: { disable: 'comment-id' }, + data: { decide: { comment: { id: 'comment-id' } } }, }) await expect(query({ query: commentQuery, variables })).resolves.toMatchObject(expected) }) @@ -181,7 +188,7 @@ describe('moderate resources', () => { it('returns disabled resource id', async () => { variables = { id: 'sample-post-id' } await expect(mutate({ mutation: disableMutation, variables })).resolves.toMatchObject({ - data: { disable: 'sample-post-id' }, + data: { decide: { post: { id: 'sample-post-id' } } }, }) }) @@ -189,12 +196,14 @@ describe('moderate resources', () => { variables = { id: 'sample-post-id' } const before = { data: { Post: [{ id: 'sample-post-id', decidedByModerator: null }] } } const expected = { - data: { Post: [{ id: 'sample-post-id', decidedByModerator: { id: 'moderator-id' } }] }, + data: { + Post: [{ id: 'sample-post-id', decidedByModerator: { id: 'moderator-id' } }], + }, } await expect(query({ query: postQuery, variables })).resolves.toMatchObject(before) await expect(mutate({ mutation: disableMutation, variables })).resolves.toMatchObject({ - data: { disable: 'sample-post-id' }, + data: { decide: { post: { id: 'sample-post-id' } } }, }) await expect(query({ query: postQuery, variables })).resolves.toMatchObject(expected) }) @@ -206,7 +215,7 @@ describe('moderate resources', () => { await expect(query({ query: postQuery, variables })).resolves.toMatchObject(before) await expect(mutate({ mutation: disableMutation, variables })).resolves.toMatchObject({ - data: { disable: 'sample-post-id' }, + data: { decide: { post: { id: 'sample-post-id' } } }, }) await expect(query({ query: postQuery, variables })).resolves.toMatchObject(expected) }) diff --git a/backend/src/schema/types/type/REPORTED.gql b/backend/src/schema/types/type/REPORTED.gql index 5042672a7..abbf2a7d4 100644 --- a/backend/src/schema/types/type/REPORTED.gql +++ b/backend/src/schema/types/type/REPORTED.gql @@ -12,6 +12,8 @@ type REPORTED { user: User post: Post comment: Comment + + # decided: DECIDED } # this list equals the strings of an array in file "webapp/constants/modals.js" diff --git a/webapp/pages/moderation/index.vue b/webapp/pages/moderation/index.vue index 13db490ff..35c8addfb 100644 --- a/webapp/pages/moderation/index.vue +++ b/webapp/pages/moderation/index.vue @@ -91,7 +91,11 @@
{{ $t('moderation.reports.disabledBy') }}
- +
{{ $t('moderation.reports.noDecision') }}