Added liking and security measures
This commit is contained in:
@@ -0,0 +1,39 @@
|
||||
const driver = require("../../graph_db");
|
||||
|
||||
function graphIdToNumber(value) {
|
||||
if (typeof value === "number") return value;
|
||||
if (value && typeof value.toNumber === "function") return value.toNumber();
|
||||
return Number(value);
|
||||
}
|
||||
|
||||
function graphParams(userId) {
|
||||
return { db_id: Number(userId) };
|
||||
}
|
||||
|
||||
async function getFriendIds(userId) {
|
||||
const { records } = await driver.executeQuery(
|
||||
"MATCH (me:Person {db_id: $db_id})-[:FRIENDS_WITH]-(f:Person) RETURN f.db_id AS id",
|
||||
graphParams(userId),
|
||||
{ database: process.env.GRAPH_DB_NAME },
|
||||
);
|
||||
return new Set(records.map((record) => graphIdToNumber(record.get("id"))));
|
||||
}
|
||||
|
||||
async function getFriendIdsAmong(userId, ids) {
|
||||
if (ids.length === 0) return new Set();
|
||||
const { records } = await driver.executeQuery(
|
||||
`MATCH (me:Person {db_id: $db_id})-[:FRIENDS_WITH]-(f:Person)
|
||||
WHERE f.db_id IN $ids
|
||||
RETURN f.db_id AS id`,
|
||||
{ ...graphParams(userId), ids: ids.map(Number) },
|
||||
{ database: process.env.GRAPH_DB_NAME },
|
||||
);
|
||||
return new Set(records.map((record) => graphIdToNumber(record.get("id"))));
|
||||
}
|
||||
|
||||
async function areFriends(userId, otherId) {
|
||||
const friendIds = await getFriendIdsAmong(userId, [otherId]);
|
||||
return friendIds.has(Number(otherId));
|
||||
}
|
||||
|
||||
module.exports = { getFriendIds, getFriendIdsAmong, areFriends, graphIdToNumber };
|
||||
Reference in New Issue
Block a user