Added liking and security measures
This commit is contained in:
@@ -0,0 +1,72 @@
|
||||
const driver = require("../../graph_db");
|
||||
const { graphIdToNumber } = require("../relations/friend_graph");
|
||||
|
||||
const GRAPH = { database: process.env.GRAPH_DB_NAME };
|
||||
|
||||
async function likePost(userId, postId) {
|
||||
const { records } = await driver.executeQuery(
|
||||
`MATCH (me:Person {db_id: $me})
|
||||
MERGE (p:Post {db_id: $postId})
|
||||
MERGE (me)-[l:LIKES]->(p)
|
||||
SET l.created_at = coalesce(l.created_at, datetime())
|
||||
RETURN me`,
|
||||
{ me: Number(userId), postId: Number(postId) },
|
||||
GRAPH,
|
||||
);
|
||||
return records.length > 0;
|
||||
}
|
||||
|
||||
async function unlikePost(userId, postId) {
|
||||
await driver.executeQuery(
|
||||
`MATCH (me:Person {db_id: $me})-[l:LIKES]->(p:Post {db_id: $postId})
|
||||
DELETE l`,
|
||||
{ me: Number(userId), postId: Number(postId) },
|
||||
GRAPH,
|
||||
);
|
||||
}
|
||||
|
||||
async function likeDataForPosts(userId, postIds) {
|
||||
const ids = postIds.map(Number);
|
||||
if (ids.length === 0) return new Map();
|
||||
const { records } = await driver.executeQuery(
|
||||
`MATCH (p:Post) WHERE p.db_id IN $ids
|
||||
OPTIONAL MATCH (p)<-[l:LIKES]-(:Person)
|
||||
WITH p, count(l) AS like_count
|
||||
OPTIONAL MATCH (me:Person {db_id: $me})-[:LIKES]->(p)
|
||||
RETURN p.db_id AS id, like_count, count(me) > 0 AS liked_by_me`,
|
||||
{ ids, me: Number(userId) },
|
||||
GRAPH,
|
||||
);
|
||||
const result = new Map();
|
||||
for (const record of records) {
|
||||
result.set(graphIdToNumber(record.get("id")), {
|
||||
like_count: Number(record.get("like_count")),
|
||||
liked_by_me: Boolean(record.get("liked_by_me")),
|
||||
});
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
async function deletePostNode(postId) {
|
||||
await driver.executeQuery(
|
||||
"MATCH (p:Post {db_id: $postId}) DETACH DELETE p",
|
||||
{ postId: Number(postId) },
|
||||
GRAPH,
|
||||
);
|
||||
}
|
||||
|
||||
async function deletePostsByAuthor(authorId) {
|
||||
await driver.executeQuery(
|
||||
"MATCH (p:Post {author_id: $authorId}) DETACH DELETE p",
|
||||
{ authorId: Number(authorId) },
|
||||
GRAPH,
|
||||
);
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
likePost,
|
||||
unlikePost,
|
||||
likeDataForPosts,
|
||||
deletePostNode,
|
||||
deletePostsByAuthor,
|
||||
};
|
||||
Reference in New Issue
Block a user