From 44e2c1adf7ba8aa640feaef630e255a9d4c9e80c Mon Sep 17 00:00:00 2001 From: Moriz Wahl Date: Thu, 8 Jun 2023 14:00:04 +0200 Subject: [PATCH 1/2] feat(backend): migration to add postType property to existing posts --- .../20230608130637-add-postType-property.js | 53 +++++++++++++++++++ 1 file changed, 53 insertions(+) create mode 100644 backend/src/db/migrations/20230608130637-add-postType-property.js diff --git a/backend/src/db/migrations/20230608130637-add-postType-property.js b/backend/src/db/migrations/20230608130637-add-postType-property.js new file mode 100644 index 000000000..433577715 --- /dev/null +++ b/backend/src/db/migrations/20230608130637-add-postType-property.js @@ -0,0 +1,53 @@ +import { getDriver } from '../../db/neo4j' + +export const description = 'Add postType property Article to all posts' + +export async function up(next) { + const driver = getDriver() + const session = driver.session() + const transaction = session.beginTransaction() + + try { + await transaction.run(` + MATCH (post:Post) + SET post.postType = 'Article' + RETURN post + `) + await transaction.commit() + next() + } catch (error) { + // eslint-disable-next-line no-console + console.log(error) + await transaction.rollback() + // eslint-disable-next-line no-console + console.log('rolled back') + throw new Error(error) + } finally { + session.close() + } +} + +export async function down(next) { + const driver = getDriver() + const session = driver.session() + const transaction = session.beginTransaction() + + try { + await transaction.run(` + MATCH (post:Post) + REMOVE post.postType + RETURN post + `) + await transaction.commit() + next() + } catch (error) { + // eslint-disable-next-line no-console + console.log(error) + await transaction.rollback() + // eslint-disable-next-line no-console + console.log('rolled back') + throw new Error(error) + } finally { + session.close() + } +} From 1d903798cde1bf44fe5dd8df1fbc0a7297bead67 Mon Sep 17 00:00:00 2001 From: Moriz Wahl Date: Thu, 8 Jun 2023 16:01:27 +0200 Subject: [PATCH 2/2] ensure that __typename of Post is a known value. labels(resource)[0] may return Article or Event in the case of Post --- .../middleware/notifications/notificationsMiddleware.js | 2 +- backend/src/middleware/validation/validationMiddleware.js | 2 +- backend/src/schema/resolvers/moderation.js | 2 +- backend/src/schema/resolvers/notifications.js | 6 +++--- backend/src/schema/resolvers/reports.js | 4 ++-- backend/src/schema/resolvers/searches.js | 8 ++++---- 6 files changed, 12 insertions(+), 12 deletions(-) diff --git a/backend/src/middleware/notifications/notificationsMiddleware.js b/backend/src/middleware/notifications/notificationsMiddleware.js index 1c97e9591..abb8abdc1 100644 --- a/backend/src/middleware/notifications/notificationsMiddleware.js +++ b/backend/src/middleware/notifications/notificationsMiddleware.js @@ -238,7 +238,7 @@ const notifyUsersOfMention = async (label, id, idsOfUsers, reason, context) => { [(resource)<-[:WROTE]-(author:User) | author {.*}] AS authors, [(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 + resource {.*, __typename: filter(l IN labels(resource) WHERE l IN ['Post', 'Comment', 'Group'])[0], author: authors[0], post: posts[0]} AS finalResource SET notification.read = FALSE SET notification.createdAt = COALESCE(notification.createdAt, toString(datetime())) SET notification.updatedAt = toString(datetime()) diff --git a/backend/src/middleware/validation/validationMiddleware.js b/backend/src/middleware/validation/validationMiddleware.js index 20933a777..9a56536e7 100644 --- a/backend/src/middleware/validation/validationMiddleware.js +++ b/backend/src/middleware/validation/validationMiddleware.js @@ -64,7 +64,7 @@ const validateReview = async (resolve, root, args, context, info) => { WHERE resource:User OR resource:Post OR resource:Comment OPTIONAL MATCH (:User)-[filed:FILED]->(:Report {closed: false})-[:BELONGS_TO]->(resource) OPTIONAL MATCH (resource)<-[:WROTE]-(author:User) - RETURN labels(resource)[0] AS label, author, filed + RETURN filter(l IN labels(resource) WHERE l IN ['Post', 'Comment', 'User'])[0] AS label, author, filed `, { resourceId, diff --git a/backend/src/schema/resolvers/moderation.js b/backend/src/schema/resolvers/moderation.js index c261d187e..b0472070f 100644 --- a/backend/src/schema/resolvers/moderation.js +++ b/backend/src/schema/resolvers/moderation.js @@ -18,7 +18,7 @@ export default { SET report.updatedAt = $dateTime, report.disable = review.disable, report.closed = $params.closed SET resource.disabled = report.disable - WITH review, report, resource {.*, __typename: labels(resource)[0]} AS finalResource + WITH review, report, resource {.*, __typename: filter(l IN labels(resource) WHERE l IN ['Post', 'Comment', 'User'])[0]} AS finalResource RETURN review {.*, report: properties(report), resource: properties(finalResource)} ` const reviewWriteTxResultPromise = session.writeTransaction(async (txc) => { diff --git a/backend/src/schema/resolvers/notifications.js b/backend/src/schema/resolvers/notifications.js index c3b882146..83c4da11b 100644 --- a/backend/src/schema/resolvers/notifications.js +++ b/backend/src/schema/resolvers/notifications.js @@ -54,7 +54,7 @@ export default { [(resource)-[:COMMENTS]->(post:Post)<-[:WROTE]-(author:User) | post {.*, author: properties(author), postType: filter(l IN labels(post) WHERE NOT l = "Post")} ] AS posts WITH resource, user, notification, authors, posts, relatedUser, membership, resource {.*, - __typename: labels(resource)[0], + __typename: filter(l IN labels(resource) WHERE l IN ['Post', 'Comment', 'Group'])[0], author: authors[0], post: posts[0], myRole: membership.role } AS finalResource @@ -93,7 +93,7 @@ export default { [(resource)-[:COMMENTS]->(post:Post)<-[:WROTE]-(author:User) | post{.*, author: properties(author), postType: filter(l IN labels(post) WHERE NOT l = "Post")} ] AS posts OPTIONAL MATCH (resource)<-[membership:MEMBER_OF]-(user) WITH resource, user, notification, authors, posts, membership, - resource {.*, __typename: labels(resource)[0], author: authors[0], post: posts[0], myRole: membership.role } AS finalResource + resource {.*, __typename: filter(l IN labels(resource) WHERE l IN ['Post', 'Comment', 'Group'])[0], author: authors[0], post: posts[0], myRole: membership.role } AS finalResource RETURN notification {.*, from: finalResource, to: properties(user)} `, { resourceId: args.id, id: currentUser.id }, @@ -123,7 +123,7 @@ export default { [(resource)-[:COMMENTS]->(post:Post)<-[:WROTE]-(author:User) | post{.*, author: properties(author), postType: filter(l IN labels(post) WHERE NOT l = "Post")} ] AS posts OPTIONAL MATCH (resource)<-[membership:MEMBER_OF]-(user) WITH resource, user, notification, authors, posts, membership, - resource {.*, __typename: labels(resource)[0], author: authors[0], post: posts[0], myRole: membership.role} AS finalResource + resource {.*, __typename: filter(l IN labels(resource) WHERE l IN ['Post', 'Comment', 'Group'])[0], author: authors[0], post: posts[0], myRole: membership.role} AS finalResource RETURN notification {.*, from: finalResource, to: properties(user)} `, { id: currentUser.id }, diff --git a/backend/src/schema/resolvers/reports.js b/backend/src/schema/resolvers/reports.js index 431bbaefc..414291acb 100644 --- a/backend/src/schema/resolvers/reports.js +++ b/backend/src/schema/resolvers/reports.js @@ -17,7 +17,7 @@ export default { WITH submitter, resource, report CREATE (report)<-[filed:FILED {createdAt: $createdAt, reasonCategory: $reasonCategory, reasonDescription: $reasonDescription}]-(submitter) - WITH filed, report, resource {.*, __typename: labels(resource)[0]} AS finalResource + WITH filed, report, resource {.*, __typename: filter(l IN labels(resource) WHERE l IN ['Post', 'Comment', 'User'])[0]} AS finalResource RETURN filed {.*, reportId: report.id, resource: properties(finalResource)} AS filedReport `, { @@ -93,7 +93,7 @@ export default { [(moderator:User)-[reviewed:REVIEWED]->(report) | reviewed {.*, moderator: properties(moderator)} ] as reviewed, [(resource)<-[:WROTE]-(author:User) | author {.*} ] as optionalAuthors, [(resource)-[:COMMENTS]->(post:Post)<-[:WROTE]-(author:User) | post {.*, author: properties(author), postType: filter(l IN labels(post) WHERE NOT l = "Post")} ] as optionalCommentedPosts, - resource {.*, __typename: labels(resource)[0] } as resourceWithType + resource {.*, __typename: filter(l IN labels(resource) WHERE l IN ['Post', 'Comment', 'User'])[0] } as resourceWithType WITH report, optionalAuthors, optionalCommentedPosts, reviewed, filed, resourceWithType {.*, post: optionalCommentedPosts[0], author: optionalAuthors[0] } as finalResource RETURN report {.*, resource: finalResource, filed: filed, reviewed: reviewed } diff --git a/backend/src/schema/resolvers/searches.js b/backend/src/schema/resolvers/searches.js index a67abe90d..ccbbb3b8e 100644 --- a/backend/src/schema/resolvers/searches.js +++ b/backend/src/schema/resolvers/searches.js @@ -38,7 +38,7 @@ const searchPostsSetup = { [(resource)<-[:SHOUTED]-(user:User) | user] AS shouter`, returnClause: `resource { .*, - __typename: labels(resource)[0], + __typename: 'Post', author: properties(author), commentsCount: toString(size(comments)), shoutedCount: toString(size(shouter)), @@ -53,7 +53,7 @@ const searchUsersSetup = { match: 'MATCH (resource:User)', whereClause: simpleWhereClause, withClause: '', - returnClause: 'resource {.*, __typename: labels(resource)[0]}', + returnClause: `resource {.*, __typename: 'User'}`, limit: 'LIMIT $limit', } @@ -62,7 +62,7 @@ const searchHashtagsSetup = { match: 'MATCH (resource:Tag)', whereClause: simpleWhereClause, withClause: '', - returnClause: 'resource {.*, __typename: labels(resource)[0]}', + returnClause: `resource {.*, __typename: 'Tag'}`, limit: 'LIMIT $limit', } @@ -77,7 +77,7 @@ const searchGroupsSetup = { AND (resource.groupType IN ['public', 'closed'] OR membership.role IN ['usual', 'admin', 'owner'])`, withClause: 'WITH resource, membership', - returnClause: 'resource { .*, myRole: membership.role, __typename: labels(resource)[0] }', + returnClause: `resource { .*, myRole: membership.role, __typename: 'Group' }`, limit: 'LIMIT $limit', }