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 }; +}; + diff --git a/src/posts/data.js b/src/posts/data.js index 3a4d303ff5..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', @@ -53,6 +54,29 @@ 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) { + const alreadyEndorsed = await dbSets.isSetMember(`post:${pid}:endorsements`, uid); + if (alreadyEndorsed) { + throw new Error('[[error:already-endorsed]]'); + } + await dbSets.setAdd(`post:${pid}:endorsements`, uid); + }; + + Posts.removeEndorsement = async function (pid, uid) { + const alreadyEndorsed = await dbSets.isSetMember(`post:${pid}:endorsements`, uid); + if (!alreadyEndorsed) { + throw new Error('[[error:not-endorsed]]'); + } + await dbSets.setRemove(`post:${pid}:endorsements`, uid); + }; + + Posts.getEndorsementCount = async function (pid) { + return await dbSets.setCount(`post:${pid}:endorsements`); + }; + + }; function modifyPost(post, fields) {