From c4b436f6cb750e640eb37745757330c0e84cb571 Mon Sep 17 00:00:00 2001 From: Moriz Wahl Date: Mon, 26 Oct 2020 10:43:22 +0100 Subject: [PATCH] spinner added to download button, only the posts written by the user are exported --- backend/src/schema/resolvers/userData.js | 49 ++++++++++--- backend/src/schema/resolvers/userData.spec.js | 68 ------------------- webapp/pages/settings/data-download.vue | 2 +- 3 files changed, 41 insertions(+), 78 deletions(-) diff --git a/backend/src/schema/resolvers/userData.js b/backend/src/schema/resolvers/userData.js index 654f847fa..b6b7cce72 100644 --- a/backend/src/schema/resolvers/userData.js +++ b/backend/src/schema/resolvers/userData.js @@ -5,14 +5,34 @@ export default { const cypher = ` MATCH (user:User { id: $id }) WITH user - OPTIONAL MATCH (p:Post) - WHERE ((p)<-[:COMMENTS]-(:Comment)<-[:WROTE]-(user) - OR (user)-[:WROTE]->(p)) - AND p.deleted = FALSE - AND p.disabled = FALSE - RETURN { user: properties(user), posts: collect(properties(p)) } - AS result - ` + OPTIONAL MATCH (posts:Post) + WHERE (user)-[:WROTE]->(posts) + AND posts.deleted = FALSE + AND posts.disabled = FALSE + RETURN { user: properties(user), + posts: collect( + posts { + .*, + author: [ + (posts)<-[:WROTE]-(author:User) | + author { + .* + } + ][0], + comments: [ + (posts)<-[:COMMENTS]-(comment:Comment) + WHERE comment.disabled = FALSE + AND comment.deleted = FALSE | + comment { + .*, + author: [ (comment)<-[:WROTE]-(commentator:User) | + commentator { .name, .slug, .id } ][0] + } + ], + categories: [ (posts)-[:CATEGORIZED]->(category:Category) | + category { .name, .id } ] + }) + } AS result` const session = context.driver.session() const resultPromise = session.readTransaction(async (transaction) => { const transactionResponse = transaction.run(cypher, { @@ -23,10 +43,21 @@ export default { try { const result = await resultPromise - return result.records[0].get('result') + const userData = result.records[0].get('result') + userData.posts.sort(byCreationDate) + if (userData.posts.comments) { + userData.posts.each((post) => post.comments.sort(byCreationDate)) + } + return userData } finally { session.close() } }, }, } + +const byCreationDate = (a, b) => { + if (a.createdAt < b.createdAt) return -1 + if (a.createdAt > b.createdAt) return 1 + return 0 +} diff --git a/backend/src/schema/resolvers/userData.spec.js b/backend/src/schema/resolvers/userData.spec.js index db235f5e4..972248d50 100644 --- a/backend/src/schema/resolvers/userData.spec.js +++ b/backend/src/schema/resolvers/userData.spec.js @@ -113,63 +113,6 @@ describe('resolvers/userData', () => { }, }) }) - - describe('the user comments another post', () => { - beforeAll(async () => { - await Factory.build( - 'post', - { - id: 'b-post', - title: 'B post', - content: 'B post', - }, - { authorId: 'o-user' }, - ) - await Factory.build( - 'comment', - { - content: 'A comment to post B', - }, - { - postId: 'b-post', - authorId: 'a-user', - }, - ) - }) - - it('returns the written post and the commented post', async () => { - await expect(query({ query: userDataQuery, variables })).resolves.toMatchObject({ - data: { - userData: { - user: { - id: 'a-user', - name: 'John Doe', - slug: 'john-doe', - }, - posts: expect.arrayContaining([ - { - id: 'a-post', - title: 'A post', - content: 'A post', - comments: [], - }, - { - id: 'b-post', - title: 'B post', - content: 'B post', - comments: [ - { - content: 'A comment to post B', - author: { slug: 'john-doe' }, - }, - ], - }, - ]), - }, - }, - }) - }) - }) }) }) @@ -191,17 +134,6 @@ describe('resolvers/userData', () => { content: 'A post', comments: [], }, - { - id: 'b-post', - title: 'B post', - content: 'B post', - comments: [ - { - content: 'A comment to post B', - author: { slug: 'john-doe' }, - }, - ], - }, ]), }, }, diff --git a/webapp/pages/settings/data-download.vue b/webapp/pages/settings/data-download.vue index 95a42d644..6db6fca99 100644 --- a/webapp/pages/settings/data-download.vue +++ b/webapp/pages/settings/data-download.vue @@ -1,7 +1,7 @@