Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Vickyc/5 us1 be exp2 #19

Open
wants to merge 4 commits into
base: f24
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
72 changes: 72 additions & 0 deletions src/api/posts.js
Original file line number Diff line number Diff line change
Expand Up @@ -512,3 +512,75 @@

return postData;
};

//This is mostly Gen AI generated code to get APIs for endorse and un-endorse

Check failure on line 516 in src/api/posts.js

View workflow job for this annotation

GitHub Actions / test

Expected exception block, space or tab after '//' in comment
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 };
};

24 changes: 24 additions & 0 deletions src/posts/data.js
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down Expand Up @@ -53,6 +54,29 @@
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

Check failure on line 58 in src/posts/data.js

View workflow job for this annotation

GitHub Actions / test

Expected exception block, space or tab after '//' in comment
Posts.addEndorsement = async function (pid, uid) {
const alreadyEndorsed = await dbSets.isSetMember(`post:${pid}:endorsements`, uid);
if (alreadyEndorsed) {
throw new Error('[[error:already-endorsed]]');

Check failure on line 62 in src/posts/data.js

View workflow job for this annotation

GitHub Actions / test

Mixed spaces and tabs

Check failure on line 62 in src/posts/data.js

View workflow job for this annotation

GitHub Actions / test

Trailing spaces not allowed
}
await dbSets.setAdd(`post:${pid}:endorsements`, uid);
};

Check failure on line 65 in src/posts/data.js

View workflow job for this annotation

GitHub Actions / test

Mixed spaces and tabs

Check failure on line 66 in src/posts/data.js

View workflow job for this annotation

GitHub Actions / test

Trailing spaces not allowed

Check failure on line 66 in src/posts/data.js

View workflow job for this annotation

GitHub Actions / test

Mixed spaces and tabs
Posts.removeEndorsement = async function (pid, uid) {
const alreadyEndorsed = await dbSets.isSetMember(`post:${pid}:endorsements`, uid);
if (!alreadyEndorsed) {
throw new Error('[[error:not-endorsed]]');

Check failure on line 70 in src/posts/data.js

View workflow job for this annotation

GitHub Actions / test

Mixed spaces and tabs

Check failure on line 70 in src/posts/data.js

View workflow job for this annotation

GitHub Actions / test

Trailing spaces not allowed
}
await dbSets.setRemove(`post:${pid}:endorsements`, uid);
};

Check failure on line 74 in src/posts/data.js

View workflow job for this annotation

GitHub Actions / test

Trailing spaces not allowed
Posts.getEndorsementCount = async function (pid) {
return await dbSets.setCount(`post:${pid}:endorsements`);
};


};

function modifyPost(post, fields) {
Expand Down