Merge pull request #449 from Human-Connection/219-refactor-follow-shout-mutations

refactor follow, unfollow, shout, unshout to custom mutations, but don't add date-time to relation yet
This commit is contained in:
Robert Schäfer 2019-04-17 11:35:47 +02:00 committed by GitHub
commit 2d85e5496d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 150 additions and 55 deletions

View File

@ -6,6 +6,8 @@ import statistics from './resolvers/statistics.js'
import reports from './resolvers/reports.js' import reports from './resolvers/reports.js'
import posts from './resolvers/posts.js' import posts from './resolvers/posts.js'
import moderation from './resolvers/moderation.js' import moderation from './resolvers/moderation.js'
import follow from './resolvers/follow.js'
import shout from './resolvers/shout.js'
import rewards from './resolvers/rewards.js' import rewards from './resolvers/rewards.js'
import socialMedia from './resolvers/socialMedia.js' import socialMedia from './resolvers/socialMedia.js'
import notifications from './resolvers/notifications' import notifications from './resolvers/notifications'
@ -27,6 +29,8 @@ export const resolvers = {
...reports.Mutation, ...reports.Mutation,
...posts.Mutation, ...posts.Mutation,
...moderation.Mutation, ...moderation.Mutation,
...follow.Mutation,
...shout.Mutation,
...rewards.Mutation, ...rewards.Mutation,
...socialMedia.Mutation, ...socialMedia.Mutation,
...notifications.Mutation ...notifications.Mutation

View File

@ -0,0 +1,51 @@
export default {
Mutation: {
follow: async (_object, params, context, _resolveInfo) => {
const { id, type } = params
const session = context.driver.session()
let transactionRes = await session.run(
`MATCH (node {id: $id}), (user:User {id: $userId})
WHERE $type IN labels(node) AND NOT $id = $userId
MERGE (user)-[relation:FOLLOWS]->(node)
RETURN COUNT(relation) > 0 as isFollowed`,
{
id,
type,
userId: context.user.id
}
)
const [isFollowed] = transactionRes.records.map(record => {
return record.get('isFollowed')
})
session.close()
return isFollowed
},
unfollow: async (_object, params, context, _resolveInfo) => {
const { id, type } = params
const session = context.driver.session()
let transactionRes = await session.run(
`MATCH (user:User {id: $userId})-[relation:FOLLOWS]->(node {id: $id})
WHERE $type IN labels(node)
DELETE relation
RETURN COUNT(relation) > 0 as isFollowed`,
{
id,
type,
userId: context.user.id
}
)
const [isFollowed] = transactionRes.records.map(record => {
return record.get('isFollowed')
})
session.close()
return isFollowed
}
}
}

View File

@ -4,7 +4,7 @@ export default {
const { fromBadgeId, toUserId } = params const { fromBadgeId, toUserId } = params
const session = context.driver.session() const session = context.driver.session()
let sessionRes = await session.run( let transactionRes = await session.run(
`MATCH (badge:Badge {id: $badgeId}), (rewardedUser:User {id: $rewardedUserId}) `MATCH (badge:Badge {id: $badgeId}), (rewardedUser:User {id: $rewardedUserId})
MERGE (badge)-[:REWARDED]->(rewardedUser) MERGE (badge)-[:REWARDED]->(rewardedUser)
RETURN rewardedUser {.id}`, RETURN rewardedUser {.id}`,
@ -14,7 +14,7 @@ export default {
} }
) )
const [rewardedUser] = sessionRes.records.map(record => { const [rewardedUser] = transactionRes.records.map(record => {
return record.get('rewardedUser') return record.get('rewardedUser')
}) })
@ -27,7 +27,7 @@ export default {
const { fromBadgeId, toUserId } = params const { fromBadgeId, toUserId } = params
const session = context.driver.session() const session = context.driver.session()
let sessionRes = await session.run( let transactionRes = await session.run(
`MATCH (badge:Badge {id: $badgeId})-[reward:REWARDED]->(rewardedUser:User {id: $rewardedUserId}) `MATCH (badge:Badge {id: $badgeId})-[reward:REWARDED]->(rewardedUser:User {id: $rewardedUserId})
DELETE reward DELETE reward
RETURN rewardedUser {.id}`, RETURN rewardedUser {.id}`,
@ -36,7 +36,7 @@ export default {
rewardedUserId: toUserId rewardedUserId: toUserId
} }
) )
const [rewardedUser] = sessionRes.records.map(record => { const [rewardedUser] = transactionRes.records.map(record => {
return record.get('rewardedUser') return record.get('rewardedUser')
}) })
session.close() session.close()

View File

@ -0,0 +1,51 @@
export default {
Mutation: {
shout: async (_object, params, context, _resolveInfo) => {
const { id, type } = params
const session = context.driver.session()
let transactionRes = await session.run(
`MATCH (node {id: $id})<-[:WROTE]-(userWritten:User), (user:User {id: $userId})
WHERE $type IN labels(node) AND NOT userWritten.id = $userId
MERGE (user)-[relation:SHOUTED]->(node)
RETURN COUNT(relation) > 0 as isShouted`,
{
id,
type,
userId: context.user.id
}
)
const [isShouted] = transactionRes.records.map(record => {
return record.get('isShouted')
})
session.close()
return isShouted
},
unshout: async (_object, params, context, _resolveInfo) => {
const { id, type } = params
const session = context.driver.session()
let transactionRes = await session.run(
`MATCH (user:User {id: $userId})-[relation:SHOUTED]->(node {id: $id})
WHERE $type IN labels(node)
DELETE relation
RETURN COUNT(relation) > 0 as isShouted`,
{
id,
type,
userId: context.user.id
}
)
const [isShouted] = transactionRes.records.map(record => {
return record.get('isShouted')
})
session.close()
return isShouted
}
}
}

View File

@ -1,8 +1,8 @@
type Query { type Query {
isLoggedIn: Boolean! isLoggedIn: Boolean!
"Get the currently logged in User based on the given JWT Token" # Get the currently logged in User based on the given JWT Token
currentUser: User currentUser: User
"Get the latest Network Statistics" # Get the latest Network Statistics
statistics: Statistics! statistics: Statistics!
findPosts(filter: String!, limit: Int = 10): [Post]! @cypher( findPosts(filter: String!, limit: Int = 10): [Post]! @cypher(
statement: """ statement: """
@ -18,7 +18,7 @@ type Query {
) )
} }
type Mutation { type Mutation {
"Get a JWT Token for the given Email and password" # Get a JWT Token for the given Email and password
login(email: String!, password: String!): String! login(email: String!, password: String!): String!
signup(email: String!, password: String!): Boolean! signup(email: String!, password: String!): Boolean!
changePassword(oldPassword:String!, newPassword: String!): String! changePassword(oldPassword:String!, newPassword: String!): String!
@ -27,34 +27,14 @@ type Mutation {
enable(id: ID!): ID enable(id: ID!): ID
reward(fromBadgeId: ID!, toUserId: ID!): ID reward(fromBadgeId: ID!, toUserId: ID!): ID
unreward(fromBadgeId: ID!, toUserId: ID!): ID unreward(fromBadgeId: ID!, toUserId: ID!): ID
"Shout the given Type and ID" # Shout the given Type and ID
shout(id: ID!, type: ShoutTypeEnum): Boolean! @cypher(statement: """ shout(id: ID!, type: ShoutTypeEnum): Boolean!
MATCH (n {id: $id})<-[:WROTE]-(wu:User), (u:User {id: $cypherParams.currentUserId}) # Unshout the given Type and ID
WHERE $type IN labels(n) AND NOT wu.id = $cypherParams.currentUserId unshout(id: ID!, type: ShoutTypeEnum): Boolean!
MERGE (u)-[r:SHOUTED]->(n) # Follow the given Type and ID
RETURN COUNT(r) > 0 follow(id: ID!, type: FollowTypeEnum): Boolean!
""") # Unfollow the given Type and ID
"Unshout the given Type and ID" unfollow(id: ID!, type: FollowTypeEnum): Boolean!
unshout(id: ID!, type: ShoutTypeEnum): Boolean! @cypher(statement: """
MATCH (:User {id: $cypherParams.currentUserId})-[r:SHOUTED]->(n {id: $id})
WHERE $type IN labels(n)
DELETE r
RETURN COUNT(r) > 0
""")
"Follow the given Type and ID"
follow(id: ID!, type: FollowTypeEnum): Boolean! @cypher(statement: """
MATCH (n {id: $id}), (u:User {id: $cypherParams.currentUserId})
WHERE $type IN labels(n) AND NOT $id = $cypherParams.currentUserId
MERGE (u)-[r:FOLLOWS]->(n)
RETURN COUNT(r) > 0
""")
"Unfollow the given Type and ID"
unfollow(id: ID!, type: FollowTypeEnum): Boolean! @cypher(statement: """
MATCH (:User {id: $cypherParams.currentUserId})-[r:FOLLOWS]->(n {id: $id})
WHERE $type IN labels(n)
DELETE r
RETURN COUNT(r) > 0
""")
} }
type Statistics { type Statistics {
@ -144,11 +124,13 @@ type User {
followedBy: [User]! @relation(name: "FOLLOWS", direction: "IN") followedBy: [User]! @relation(name: "FOLLOWS", direction: "IN")
followedByCount: Int! @cypher(statement: "MATCH (this)<-[:FOLLOWS]-(r:User) RETURN COUNT(DISTINCT r)") followedByCount: Int! @cypher(statement: "MATCH (this)<-[:FOLLOWS]-(r:User) RETURN COUNT(DISTINCT r)")
"Is the currently logged in user following that user?" # Is the currently logged in user following that user?
followedByCurrentUser: Boolean! @cypher(statement: """ followedByCurrentUser: Boolean! @cypher(
MATCH (this)<-[:FOLLOWS]-(u:User {id: $cypherParams.currentUserId}) statement: """
RETURN COUNT(u) >= 1 MATCH (this)<-[:FOLLOWS]-(u:User {id: $cypherParams.currentUserId})
""") RETURN COUNT(u) >= 1
"""
)
#contributions: [WrittenPost]! #contributions: [WrittenPost]!
#contributions2(first: Int = 10, offset: Int = 0): [WrittenPost2]! #contributions2(first: Int = 10, offset: Int = 0): [WrittenPost2]!
@ -156,11 +138,13 @@ type User {
# statement: "MATCH (this)-[w:WROTE]->(p:Post) RETURN p as Post, w.timestamp as timestamp" # statement: "MATCH (this)-[w:WROTE]->(p:Post) RETURN p as Post, w.timestamp as timestamp"
# ) # )
contributions: [Post]! @relation(name: "WROTE", direction: "OUT") contributions: [Post]! @relation(name: "WROTE", direction: "OUT")
contributionsCount: Int! @cypher(statement: """ contributionsCount: Int! @cypher(
MATCH (this)-[:WROTE]->(r:Post) statement: """
WHERE (NOT exists(r.deleted) OR r.deleted = false) MATCH (this)-[:WROTE]->(r:Post)
AND (NOT exists(r.disabled) OR r.disabled = false) WHERE (NOT exists(r.deleted) OR r.deleted = false)
RETURN COUNT(r)""" AND (NOT exists(r.disabled) OR r.disabled = false)
RETURN COUNT(r)
"""
) )
comments: [Comment]! @relation(name: "WROTE", direction: "OUT") comments: [Comment]! @relation(name: "WROTE", direction: "OUT")
@ -197,11 +181,13 @@ type Post {
createdAt: String createdAt: String
updatedAt: String updatedAt: String
relatedContributions: [Post]! @cypher(statement: """ relatedContributions: [Post]! @cypher(
MATCH (this)-[:TAGGED|CATEGORIZED]->(categoryOrTag)<-[:TAGGED|CATEGORIZED]-(post:Post) statement: """
RETURN DISTINCT post MATCH (this)-[:TAGGED|CATEGORIZED]->(categoryOrTag)<-[:TAGGED|CATEGORIZED]-(post:Post)
LIMIT 10 RETURN DISTINCT post
""") LIMIT 10
"""
)
tags: [Tag]! @relation(name: "TAGGED", direction: "OUT") tags: [Tag]! @relation(name: "TAGGED", direction: "OUT")
categories: [Category]! @relation(name: "CATEGORIZED", direction: "OUT") categories: [Category]! @relation(name: "CATEGORIZED", direction: "OUT")
@ -212,11 +198,13 @@ type Post {
shoutedBy: [User]! @relation(name: "SHOUTED", direction: "IN") shoutedBy: [User]! @relation(name: "SHOUTED", direction: "IN")
shoutedCount: Int! @cypher(statement: "MATCH (this)<-[:SHOUTED]-(r:User) WHERE NOT r.deleted = true AND NOT r.disabled = true RETURN COUNT(DISTINCT r)") shoutedCount: Int! @cypher(statement: "MATCH (this)<-[:SHOUTED]-(r:User) WHERE NOT r.deleted = true AND NOT r.disabled = true RETURN COUNT(DISTINCT r)")
"Has the currently logged in user shouted that post?" # Has the currently logged in user shouted that post?
shoutedByCurrentUser: Boolean! @cypher(statement: """ shoutedByCurrentUser: Boolean! @cypher(
MATCH (this)<-[:SHOUTED]-(u:User {id: $cypherParams.currentUserId}) statement: """
RETURN COUNT(u) >= 1 MATCH (this)<-[:SHOUTED]-(u:User {id: $cypherParams.currentUserId})
""") RETURN COUNT(u) >= 1
"""
)
} }
type Comment { type Comment {
@ -315,6 +303,7 @@ type Tag {
deleted: Boolean deleted: Boolean
disabled: Boolean disabled: Boolean
} }
type SharedInboxEndpoint { type SharedInboxEndpoint {
id: ID! id: ID!
uri: String uri: String