Skip to content

Node.js based forum software built for the modern web

refactor: use topics.post, topics.reply, and posts.edit in Create(Note), Create(Note), and Update(Note), respectively

Julian Lamf7c8967ee2ed
diff --git a/src/activitypub/inbox.js b/src/activitypub/inbox.js
index 38bfee26ce..c629926d34 100644
--- a/src/activitypub/inbox.js
+++ b/src/activitypub/inbox.js
@@ -42,13 +42,11 @@ inbox.update = async (req) => {
switch (object.type) {
case 'Note': {
- const [exists, allowed] = await Promise.all([
- posts.exists(object.id),
- privileges.posts.can('posts:edit', object.id, activitypub._constants.uid),
- ]);
- if (!exists || !allowed) {
- winston.warn(`[activitypub/inbox.update] ${object.id} not allowed to be edited.`);
- return activitypub.send('uid', 0, actor, {
+ const postData = await activitypub.mocks.post(object);
+ try {
+ await posts.edit(postData);
+ } catch (e) {
+ activitypub.send('uid', 0, actor, {
type: 'Reject',
object: {
type: 'Update',
@@ -57,15 +55,6 @@ inbox.update = async (req) => {
},
});
}
-
- const postData = await activitypub.mocks.post(object);
-
- if (postData) {
- await activitypub.notes.assert(0, [postData], { update: true });
- winston.verbose(`[activitypub/inbox.update] Updating note ${postData.pid}`);
- } else {
- winston.warn(`[activitypub/inbox.update] Received note did not parse properly (id: ${object.id})`);
- }
break;
}
diff --git a/src/activitypub/notes.js b/src/activitypub/notes.js
index 83c26b8cb7..5bd414edae 100644
--- a/src/activitypub/notes.js
+++ b/src/activitypub/notes.js
@@ -192,16 +192,6 @@ Notes.getParentChain = async (uid, input) => {
return chain;
};
-Notes.assertParentChain = async (chain) => {
- const data = [];
- chain.reduce((child, parent) => {
- data.push([`pid:${parent.pid}:replies`, child.timestamp, child.pid]);
- return parent;
- });
-
- await db.sortedSetAddBulk(data);
-};
-
Notes.assertTopic = async (uid, id) => {
/**
* Given the id of any post, traverses up to cache the entire threaded context
@@ -275,21 +265,17 @@ Notes.assertTopic = async (uid, id) => {
.filter(o => o.type === 'Hashtag')
.map(o => o.name.slice(1));
- try {
- await topics.post({
- tid,
- uid: authorId,
- cid,
- pid: mainPid,
- title,
- timestamp,
- tags,
- content: mainPost.content,
- _activitypub: mainPost._activitypub,
- });
- } catch (e) {
- console.log(e);
- }
+ await topics.post({
+ tid,
+ uid: authorId,
+ cid,
+ pid: mainPid,
+ title,
+ timestamp,
+ tags,
+ content: mainPost.content,
+ _activitypub: mainPost._activitypub,
+ });
unprocessed.pop();
}
@@ -300,34 +286,10 @@ Notes.assertTopic = async (uid, id) => {
}
await Notes.syncUserInboxes(tid);
- // await Promise.all([
- // db.sortedSetAdd(`tid:${tid}:posts`, timestamps, ids),
- // Notes.assert(uid, unprocessed),
- // ]);
- // await Promise.all([ // must be done after .assert()
- // Notes.assertParentChain(chain),
- // Notes.updateTopicCounts(tid),
- // Notes.syncUserInboxes(tid),
- // topics.updateLastPostTimeFromLastPid(tid),
- // topics.updateTeaser(tid),
- // ]);
return tid;
};
-Notes.updateTopicCounts = async function (tid) {
- const mainPid = await topics.getTopicField(tid, 'mainPid');
- const pids = await db.getSortedSetMembers(`tid:${tid}:posts`);
- pids.unshift(mainPid);
- let uids = await db.getObjectsFields(pids.map(p => `post:${p}`), ['uid']);
- uids = new Set(uids.map(o => o.uid));
-
- db.setObject(`topic:${tid}`, {
- postercount: uids.size,
- postcount: pids.length,
- });
-};
-
Notes.syncUserInboxes = async function (tid) {
const [pids, { cid, mainPid }] = await Promise.all([
db.getSortedSetMembers(`tid:${tid}:posts`),
@@ -343,8 +305,3 @@ Notes.syncUserInboxes = async function (tid) {
winston.verbose(`[activitypub/syncUserInboxes] Syncing tid ${tid} with ${uids.size} inboxes`);
await db.sortedSetsAdd(keys, keys.map(() => score || Date.now()), tid);
};
-
-Notes.getTopicPosts = async (tid, uid, start, stop) => {
- const pids = await db.getSortedSetRange(`tid:${tid}:posts`, start, stop);
- return await posts.getPostsByPids(pids, uid);
-};
diff --git a/src/posts/index.js b/src/posts/index.js
index aaa520ed1f..d2bb6cb360 100644
--- a/src/posts/index.js
+++ b/src/posts/index.js
@@ -7,7 +7,6 @@ const utils = require('../utils');
const user = require('../user');
const privileges = require('../privileges');
const plugins = require('../plugins');
-const activitypub = require('../activitypub');
const Posts = module.exports;
@@ -46,8 +45,9 @@ Posts.getPostsByPids = async function (pids, uid) {
return [];
}
- const remotePids = pids.filter(pid => !utils.isNumber(pid));
- await activitypub.notes.assert(uid, remotePids);
+ // todo: remove if not needed (and if this is still commented out after March 2024, then it wasn't needed)
+ // const remotePids = pids.filter(pid => !utils.isNumber(pid));
+ // await activitypub.notes.assert(uid, remotePids);
let posts = await Posts.getPostsData(pids);
posts = await Promise.all(posts.map(Posts.parsePost));
const data = await plugins.hooks.fire('filter:post.getPosts', { posts: posts, uid: uid });
diff --git a/src/posts/user.js b/src/posts/user.js
index 850ed4c613..a224a960c7 100644
--- a/src/posts/user.js
+++ b/src/posts/user.js
@@ -11,6 +11,7 @@ const groups = require('../groups');
const meta = require('../meta');
const plugins = require('../plugins');
const privileges = require('../privileges');
+const utils = require('../utils');
module.exports = function (Posts) {
Posts.getUserInfoForPosts = async function (uids, uid) {
@@ -115,10 +116,10 @@ module.exports = function (Posts) {
}
Posts.isOwner = async function (pids, uid) {
- uid = parseInt(uid, 10);
+ uid = utils.isNumber(uid) ? parseInt(uid, 10) : uid;
const isArray = Array.isArray(pids);
pids = isArray ? pids : [pids];
- if (uid <= 0) {
+ if (utils.isNumber(uid) && uid <= 0) {
return isArray ? pids.map(() => false) : false;
}
const postData = await Posts.getPostsFields(pids, ['uid']);
diff --git a/src/privileges/posts.js b/src/privileges/posts.js
index fbd6858282..bc699e7801 100644
--- a/src/privileges/posts.js
+++ b/src/privileges/posts.js
@@ -7,6 +7,7 @@ const meta = require('../meta');
const posts = require('../posts');
const topics = require('../topics');
const user = require('../user');
+const activitypub = require('../activitypub');
const helpers = require('./helpers');
const plugins = require('../plugins');
const utils = require('../utils');
@@ -115,6 +116,7 @@ privsPosts.filter = async function (privilege, pids, uid) {
};
privsPosts.canEdit = async function (pid, uid) {
+ const isRemote = activitypub.helpers.isUri(pid);
const results = await utils.promiseParallel({
isAdmin: user.isAdministrator(uid),
isMod: posts.isModerator([pid], uid),
@@ -130,14 +132,14 @@ privsPosts.canEdit = async function (pid, uid) {
}
if (
- !results.isMod &&
+ !isRemote && !results.isMod &&
meta.config.postEditDuration &&
(Date.now() - results.postData.timestamp > meta.config.postEditDuration * 1000)
) {
return { flag: false, message: `[[error:post-edit-duration-expired, ${meta.config.postEditDuration}]]` };
}
if (
- !results.isMod &&
+ !isRemote && !results.isMod &&
meta.config.newbiePostEditDuration > 0 &&
meta.config.newbieReputationThreshold > results.userData.reputation &&
Date.now() - results.postData.timestamp > meta.config.newbiePostEditDuration * 1000
@@ -154,7 +156,7 @@ privsPosts.canEdit = async function (pid, uid) {
return { flag: false, message: '[[error:post-deleted]]' };
}
- results.pid = parseInt(pid, 10);
+ results.pid = utils.isNumber(pid) ? parseInt(pid, 10) : pid;
results.uid = uid;
const result = await plugins.hooks.fire('filter:privileges.posts.edit', results);