From 62046b4bddb737f407b15d421e894e7268dbfae7 Mon Sep 17 00:00:00 2001 From: Evelynn Chen Date: Sun, 6 Oct 2024 21:05:53 -0400 Subject: [PATCH 1/2] added bool endorsed as a field to posts --- public/openapi/components/schemas/PostObject.yaml | 2 ++ public/openapi/read/topic/topic_id.yaml | 2 ++ public/openapi/write/posts/pid.yaml | 2 ++ public/openapi/write/posts/pid/replies.yaml | 2 ++ src/posts/create.js | 2 ++ src/posts/data.js | 6 +++++- src/posts/delete.js | 2 +- src/posts/summary.js | 3 ++- src/posts/user.js | 2 +- src/search.js | 2 +- src/topics/create.js | 3 +++ src/topics/fork.js | 2 +- 12 files changed, 24 insertions(+), 6 deletions(-) diff --git a/public/openapi/components/schemas/PostObject.yaml b/public/openapi/components/schemas/PostObject.yaml index ea91579cc6..78e53d85d4 100644 --- a/public/openapi/components/schemas/PostObject.yaml +++ b/public/openapi/components/schemas/PostObject.yaml @@ -22,6 +22,8 @@ PostObject: type: number votes: type: number + endorsed: + type: boolean timestampISO: type: string description: An ISO 8601 formatted date string (complementing `timestamp`) diff --git a/public/openapi/read/topic/topic_id.yaml b/public/openapi/read/topic/topic_id.yaml index f0fde1b6c0..0c9b3cba01 100644 --- a/public/openapi/read/topic/topic_id.yaml +++ b/public/openapi/read/topic/topic_id.yaml @@ -71,6 +71,8 @@ get: type: number downvotes: type: number + endorsed: + type: number bookmarks: type: number deleterUid: diff --git a/public/openapi/write/posts/pid.yaml b/public/openapi/write/posts/pid.yaml index 593a7acd01..d538000827 100644 --- a/public/openapi/write/posts/pid.yaml +++ b/public/openapi/write/posts/pid.yaml @@ -54,6 +54,8 @@ get: type: number votes: type: number + endorsed: + type: number timestampISO: type: string description: An ISO 8601 formatted date string (complementing `timestamp`) diff --git a/public/openapi/write/posts/pid/replies.yaml b/public/openapi/write/posts/pid/replies.yaml index b021eec14e..0adc11f762 100644 --- a/public/openapi/write/posts/pid/replies.yaml +++ b/public/openapi/write/posts/pid/replies.yaml @@ -51,6 +51,8 @@ get: type: number bookmarks: type: number + endorsed: + type: number deleterUid: type: number edited: diff --git a/src/posts/create.js b/src/posts/create.js index d541564c2e..2f66238d63 100644 --- a/src/posts/create.js +++ b/src/posts/create.js @@ -19,6 +19,7 @@ module.exports = function (Posts) { const content = data.content.toString(); const timestamp = data.timestamp || Date.now(); const isMain = data.isMain || false; + const endorsed = data.endorsed || false; if (!uid && parseInt(uid, 10) !== 0) { throw new Error('[[error:invalid-uid]]'); @@ -35,6 +36,7 @@ module.exports = function (Posts) { tid: tid, content: content, timestamp: timestamp, + endorsed: endorsed }; if (data.toPid) { diff --git a/src/posts/data.js b/src/posts/data.js index 3a4d303ff5..99d551fdf8 100644 --- a/src/posts/data.js +++ b/src/posts/data.js @@ -6,7 +6,7 @@ const utils = require('../utils'); const intFields = [ 'uid', 'pid', 'tid', 'deleted', 'timestamp', - 'upvotes', 'downvotes', 'deleterUid', 'edited', + 'upvotes', 'downvotes', 'endorsed', 'deleterUid', 'edited', 'replies', 'bookmarks', ]; @@ -67,5 +67,9 @@ function modifyPost(post, fields) { if (post.hasOwnProperty('edited')) { post.editedISO = post.edited !== 0 ? utils.toISOString(post.edited) : ''; } + + if (post.hasOwnProperty('endorsed')) { + post.endorsed = post.endorsed ? post.endorsed : false; + } } } diff --git a/src/posts/delete.js b/src/posts/delete.js index 94f73cf494..0481621dd4 100644 --- a/src/posts/delete.js +++ b/src/posts/delete.js @@ -26,7 +26,7 @@ module.exports = function (Posts) { deleted: isDeleting ? 1 : 0, deleterUid: isDeleting ? uid : 0, }); - const postData = await Posts.getPostFields(pid, ['pid', 'tid', 'uid', 'content', 'timestamp']); + const postData = await Posts.getPostFields(pid, ['pid', 'tid', 'uid', 'content', 'endorsed', 'timestamp']); const topicData = await topics.getTopicFields(postData.tid, ['tid', 'cid', 'pinned']); postData.cid = topicData.cid; await Promise.all([ diff --git a/src/posts/summary.js b/src/posts/summary.js index 364baad1f7..cbaa5c591e 100644 --- a/src/posts/summary.js +++ b/src/posts/summary.js @@ -20,7 +20,7 @@ module.exports = function (Posts) { options.parse = options.hasOwnProperty('parse') ? options.parse : true; options.extraFields = options.hasOwnProperty('extraFields') ? options.extraFields : []; - const fields = ['pid', 'tid', 'content', 'uid', 'timestamp', 'deleted', 'upvotes', 'downvotes', 'replies', 'handle'].concat(options.extraFields); + const fields = ['pid', 'tid', 'content', 'uid', 'timestamp', 'deleted', 'upvotes', 'downvotes', 'endorsed', 'replies', 'handle'].concat(options.extraFields); let posts = await Posts.getPostsFields(pids, fields); posts = posts.filter(Boolean); @@ -51,6 +51,7 @@ module.exports = function (Posts) { post.category = post.topic && cidToCategory[post.topic.cid]; post.isMainPost = post.topic && post.pid === post.topic.mainPid; post.deleted = post.deleted === 1; + post.endorsed = post.endorsed; post.timestampISO = utils.toISOString(post.timestamp); }); diff --git a/src/posts/user.js b/src/posts/user.js index 1fd8fa7e2c..53b3e056c4 100644 --- a/src/posts/user.js +++ b/src/posts/user.js @@ -140,7 +140,7 @@ module.exports = function (Posts) { throw new Error('[[error:no-user]]'); } let postData = await Posts.getPostsFields(pids, [ - 'pid', 'tid', 'uid', 'content', 'deleted', 'timestamp', 'upvotes', 'downvotes', + 'pid', 'tid', 'uid', 'content', 'deleted', 'endorsed', 'timestamp', 'upvotes', 'downvotes', ]); postData = postData.filter(p => p.pid && p.uid !== parseInt(toUid, 10)); pids = postData.map(p => p.pid); diff --git a/src/search.js b/src/search.js index df249ec1f6..053eec783c 100644 --- a/src/search.js +++ b/src/search.js @@ -177,7 +177,7 @@ async function filterAndSort(pids, data) { } async function getMatchedPosts(pids, data) { - const postFields = ['pid', 'uid', 'tid', 'timestamp', 'deleted', 'upvotes', 'downvotes']; + const postFields = ['pid', 'uid', 'tid', 'timestamp', 'deleted', 'upvotes', 'downvotes', 'endorsed']; let postsData = await posts.getPostsFields(pids, postFields); postsData = postsData.filter(post => post && !post.deleted); diff --git a/src/topics/create.js b/src/topics/create.js index 0d6ee1bc19..f0da010fa0 100644 --- a/src/topics/create.js +++ b/src/topics/create.js @@ -120,6 +120,9 @@ module.exports = function (Topics) { const tid = await Topics.create(data); let postData = data; + if (postData.endorsed === undefined) { + postData.endorsed = false; + } postData.tid = tid; postData.ip = data.req ? data.req.ip : null; postData.isMain = true; diff --git a/src/topics/fork.js b/src/topics/fork.js index e94da7f1c3..263e9d3f09 100644 --- a/src/topics/fork.js +++ b/src/topics/fork.js @@ -92,7 +92,7 @@ module.exports = function (Topics) { if (!forceScheduled && topicData.scheduled) { throw new Error('[[error:cant-move-posts-to-scheduled]]'); } - const postData = await posts.getPostFields(pid, ['tid', 'uid', 'timestamp', 'upvotes', 'downvotes']); + const postData = await posts.getPostFields(pid, ['tid', 'uid', 'timestamp', 'upvotes', 'downvotes', 'endorsed']); if (!postData || !postData.tid) { throw new Error('[[error:no-post]]'); } From aad6d865f7287cc71ec86e21ee55e339947a0bfb Mon Sep 17 00:00:00 2001 From: Evelynn Chen Date: Sun, 6 Oct 2024 21:20:50 -0400 Subject: [PATCH 2/2] fixed trailing comma and self assignment error --- src/posts/create.js | 2 +- src/posts/summary.js | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/posts/create.js b/src/posts/create.js index 2f66238d63..c035f86013 100644 --- a/src/posts/create.js +++ b/src/posts/create.js @@ -36,7 +36,7 @@ module.exports = function (Posts) { tid: tid, content: content, timestamp: timestamp, - endorsed: endorsed + endorsed: endorsed, }; if (data.toPid) { diff --git a/src/posts/summary.js b/src/posts/summary.js index cbaa5c591e..db3bdc20c1 100644 --- a/src/posts/summary.js +++ b/src/posts/summary.js @@ -51,7 +51,7 @@ module.exports = function (Posts) { post.category = post.topic && cidToCategory[post.topic.cid]; post.isMainPost = post.topic && post.pid === post.topic.mainPid; post.deleted = post.deleted === 1; - post.endorsed = post.endorsed; + post.endorsed = post.endorsed === true; post.timestampISO = utils.toISOString(post.timestamp); });