From 8382df1a539742a431182eb5dd6150a84b77215c Mon Sep 17 00:00:00 2001 From: Vicky Chen Date: Sun, 22 Sep 2024 01:06:22 -0400 Subject: [PATCH 1/4] attempted to add api endpoints into src/api/posts.js file --- src/api/posts.js | 72 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 72 insertions(+) diff --git a/src/api/posts.js b/src/api/posts.js index 4e3917a008..6b1c477d0a 100644 --- a/src/api/posts.js +++ b/src/api/posts.js @@ -512,3 +512,75 @@ postsAPI.getReplies = async (caller, { pid }) => { return postData; }; + +//This is mostly Gen AI generated code to get APIs for endorse and un-endorse +postsAPI.endorse = async function (caller, data) { + if (!data || !data.pid) { + throw new Error('[[error:invalid-data]]'); + } + + // Check if the user is logged in + if (!caller.uid) { + throw new Error('[[error:not-logged-in]]'); + } + + // Check if the user has the privilege to endorse the post + const canEndorse = await privileges.posts.can('endorse', data.pid, caller.uid); + if (!canEndorse) { + throw new Error('[[error:no-privileges]]'); + } + + // Add the endorsement + await posts.addEndorsement(data.pid, caller.uid); + + // Log the endorsement event + await events.log({ + type: 'post-endorse', + uid: caller.uid, + ip: caller.ip, + pid: data.pid, + }); + + // Notify the post owner + const postOwnerUid = await posts.getPostField(data.pid, 'uid'); + if (postOwnerUid && postOwnerUid !== caller.uid) { + await socketHelpers.sendNotificationToPostOwner(data.pid, caller.uid, 'endorse', 'notifications:endorsed-your-post'); + } + + // Return the updated endorsement count + const endorsementCount = await posts.getEndorsementCount(data.pid); + return { endorsementCount }; +}; + +postsAPI.unendorse = async function (caller, data) { + if (!data || !data.pid) { + throw new Error('[[error:invalid-data]]'); + } + + // Check if the user is logged in + if (!caller.uid) { + throw new Error('[[error:not-logged-in]]'); + } + + // Check if the user has the privilege to unendorse the post + const canUnendorse = await privileges.posts.can('endorse', data.pid, caller.uid); + if (!canUnendorse) { + throw new Error('[[error:no-privileges]]'); + } + + // Remove the endorsement + await posts.removeEndorsement(data.pid, caller.uid); + + // Log the unendorsement event + await events.log({ + type: 'post-unendorse', + uid: caller.uid, + ip: caller.ip, + pid: data.pid, + }); + + // Return the updated endorsement count + const endorsementCount = await posts.getEndorsementCount(data.pid); + return { endorsementCount }; +}; + From 2f423de8ea8f7cb569e56ea0cafe6dc4ba4083bc Mon Sep 17 00:00:00 2001 From: Vicky Chen Date: Sun, 22 Sep 2024 01:28:51 -0400 Subject: [PATCH 2/4] Added business logic for endorsement action of posts in src/posts/data.js --- src/posts/data.js | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/src/posts/data.js b/src/posts/data.js index 3a4d303ff5..13267c7394 100644 --- a/src/posts/data.js +++ b/src/posts/data.js @@ -53,6 +53,31 @@ module.exports = function (Posts) { await db.setObject(`post:${pid}`, data); plugins.hooks.fire('action:post.setFields', { data: { ...data, pid } }); }; + + //3 methods of these code and function ideas were suggested by ChatGPT + Posts.addEndorsement = async function (pid, uid) { + // Check if the user has already endorsed this post + const alreadyEndorsed = await db.isMember(`post:${pid}:endorsements`, uid); + if (alreadyEndorsed) { + throw new Error('[[error:already-endorsed]]'); + } + await db.setAdd(`post:${pid}:endorsements`, uid); + }; + + Posts.removeEndorsement = async function (pid, uid) { + // Check if the user has not endorsed this post + const alreadyEndorsed = await db.isMember(`post:${pid}:endorsements`, uid); + if (!alreadyEndorsed) { + throw new Error('[[error:not-endorsed]]'); + } + await db.setRemove(`post:${pid}:endorsements`, uid); + }; + + Posts.getEndorsementCount = async function (pid) { + return await db.setCount(`post:${pid}:endorsements`); + }; + + }; function modifyPost(post, fields) { From 4df30fa4ceb3ca0da1cf9f0800b0a96e805056a6 Mon Sep 17 00:00:00 2001 From: Vicky Chen Date: Sun, 22 Sep 2024 01:29:46 -0400 Subject: [PATCH 3/4] continued editing code for the business logic of adding, removing, and checking endorsement of a post in src/posts/data.js file --- src/posts/data.js | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/posts/data.js b/src/posts/data.js index 13267c7394..1eea457dd5 100644 --- a/src/posts/data.js +++ b/src/posts/data.js @@ -59,7 +59,7 @@ module.exports = function (Posts) { // Check if the user has already endorsed this post const alreadyEndorsed = await db.isMember(`post:${pid}:endorsements`, uid); if (alreadyEndorsed) { - throw new Error('[[error:already-endorsed]]'); + throw new Error('[[error:already-endorsed]]'); } await db.setAdd(`post:${pid}:endorsements`, uid); }; @@ -77,7 +77,6 @@ module.exports = function (Posts) { return await db.setCount(`post:${pid}:endorsements`); }; - }; function modifyPost(post, fields) { From 16c2f3bdde5d72d21717c86bb7553781605eda9c Mon Sep 17 00:00:00 2001 From: Vicky Chen Date: Mon, 23 Sep 2024 07:46:31 -0400 Subject: [PATCH 4/4] edited posts/data.js to add endorsement features --- src/posts/data.js | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/src/posts/data.js b/src/posts/data.js index 1eea457dd5..6b466cd1bd 100644 --- a/src/posts/data.js +++ b/src/posts/data.js @@ -3,6 +3,7 @@ const db = require('../database'); const plugins = require('../plugins'); const utils = require('../utils'); +const dbSets = require('../database/redis/sets'); const intFields = [ 'uid', 'pid', 'tid', 'deleted', 'timestamp', @@ -56,27 +57,26 @@ module.exports = function (Posts) { //3 methods of these code and function ideas were suggested by ChatGPT Posts.addEndorsement = async function (pid, uid) { - // Check if the user has already endorsed this post - const alreadyEndorsed = await db.isMember(`post:${pid}:endorsements`, uid); + const alreadyEndorsed = await dbSets.isSetMember(`post:${pid}:endorsements`, uid); if (alreadyEndorsed) { throw new Error('[[error:already-endorsed]]'); } - await db.setAdd(`post:${pid}:endorsements`, uid); - }; + await dbSets.setAdd(`post:${pid}:endorsements`, uid); + }; Posts.removeEndorsement = async function (pid, uid) { - // Check if the user has not endorsed this post - const alreadyEndorsed = await db.isMember(`post:${pid}:endorsements`, uid); + const alreadyEndorsed = await dbSets.isSetMember(`post:${pid}:endorsements`, uid); if (!alreadyEndorsed) { throw new Error('[[error:not-endorsed]]'); } - await db.setRemove(`post:${pid}:endorsements`, uid); + await dbSets.setRemove(`post:${pid}:endorsements`, uid); }; Posts.getEndorsementCount = async function (pid) { - return await db.setCount(`post:${pid}:endorsements`); + return await dbSets.setCount(`post:${pid}:endorsements`); }; + }; function modifyPost(post, fields) {