mirror of
https://github.com/NodeBB/NodeBB.git
synced 2025-10-26 16:46:12 +01:00
refactor: use topics.post, topics.reply, and posts.edit in Create(Note), Create(Note), and Update(Note), respectively
This commit is contained in:
@@ -42,13 +42,11 @@ inbox.update = async (req) => {
|
|||||||
|
|
||||||
switch (object.type) {
|
switch (object.type) {
|
||||||
case 'Note': {
|
case 'Note': {
|
||||||
const [exists, allowed] = await Promise.all([
|
const postData = await activitypub.mocks.post(object);
|
||||||
posts.exists(object.id),
|
try {
|
||||||
privileges.posts.can('posts:edit', object.id, activitypub._constants.uid),
|
await posts.edit(postData);
|
||||||
]);
|
} catch (e) {
|
||||||
if (!exists || !allowed) {
|
activitypub.send('uid', 0, actor, {
|
||||||
winston.warn(`[activitypub/inbox.update] ${object.id} not allowed to be edited.`);
|
|
||||||
return activitypub.send('uid', 0, actor, {
|
|
||||||
type: 'Reject',
|
type: 'Reject',
|
||||||
object: {
|
object: {
|
||||||
type: 'Update',
|
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;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -192,16 +192,6 @@ Notes.getParentChain = async (uid, input) => {
|
|||||||
return chain;
|
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) => {
|
Notes.assertTopic = async (uid, id) => {
|
||||||
/**
|
/**
|
||||||
* Given the id of any post, traverses up to cache the entire threaded context
|
* Given the id of any post, traverses up to cache the entire threaded context
|
||||||
@@ -275,7 +265,6 @@ Notes.assertTopic = async (uid, id) => {
|
|||||||
.filter(o => o.type === 'Hashtag')
|
.filter(o => o.type === 'Hashtag')
|
||||||
.map(o => o.name.slice(1));
|
.map(o => o.name.slice(1));
|
||||||
|
|
||||||
try {
|
|
||||||
await topics.post({
|
await topics.post({
|
||||||
tid,
|
tid,
|
||||||
uid: authorId,
|
uid: authorId,
|
||||||
@@ -287,9 +276,6 @@ Notes.assertTopic = async (uid, id) => {
|
|||||||
content: mainPost.content,
|
content: mainPost.content,
|
||||||
_activitypub: mainPost._activitypub,
|
_activitypub: mainPost._activitypub,
|
||||||
});
|
});
|
||||||
} catch (e) {
|
|
||||||
console.log(e);
|
|
||||||
}
|
|
||||||
unprocessed.pop();
|
unprocessed.pop();
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -300,34 +286,10 @@ Notes.assertTopic = async (uid, id) => {
|
|||||||
}
|
}
|
||||||
|
|
||||||
await Notes.syncUserInboxes(tid);
|
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;
|
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) {
|
Notes.syncUserInboxes = async function (tid) {
|
||||||
const [pids, { cid, mainPid }] = await Promise.all([
|
const [pids, { cid, mainPid }] = await Promise.all([
|
||||||
db.getSortedSetMembers(`tid:${tid}:posts`),
|
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`);
|
winston.verbose(`[activitypub/syncUserInboxes] Syncing tid ${tid} with ${uids.size} inboxes`);
|
||||||
await db.sortedSetsAdd(keys, keys.map(() => score || Date.now()), tid);
|
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);
|
|
||||||
};
|
|
||||||
|
|||||||
@@ -7,7 +7,6 @@ const utils = require('../utils');
|
|||||||
const user = require('../user');
|
const user = require('../user');
|
||||||
const privileges = require('../privileges');
|
const privileges = require('../privileges');
|
||||||
const plugins = require('../plugins');
|
const plugins = require('../plugins');
|
||||||
const activitypub = require('../activitypub');
|
|
||||||
|
|
||||||
const Posts = module.exports;
|
const Posts = module.exports;
|
||||||
|
|
||||||
@@ -46,8 +45,9 @@ Posts.getPostsByPids = async function (pids, uid) {
|
|||||||
return [];
|
return [];
|
||||||
}
|
}
|
||||||
|
|
||||||
const remotePids = pids.filter(pid => !utils.isNumber(pid));
|
// todo: remove if not needed (and if this is still commented out after March 2024, then it wasn't needed)
|
||||||
await activitypub.notes.assert(uid, remotePids);
|
// const remotePids = pids.filter(pid => !utils.isNumber(pid));
|
||||||
|
// await activitypub.notes.assert(uid, remotePids);
|
||||||
let posts = await Posts.getPostsData(pids);
|
let posts = await Posts.getPostsData(pids);
|
||||||
posts = await Promise.all(posts.map(Posts.parsePost));
|
posts = await Promise.all(posts.map(Posts.parsePost));
|
||||||
const data = await plugins.hooks.fire('filter:post.getPosts', { posts: posts, uid: uid });
|
const data = await plugins.hooks.fire('filter:post.getPosts', { posts: posts, uid: uid });
|
||||||
|
|||||||
@@ -11,6 +11,7 @@ const groups = require('../groups');
|
|||||||
const meta = require('../meta');
|
const meta = require('../meta');
|
||||||
const plugins = require('../plugins');
|
const plugins = require('../plugins');
|
||||||
const privileges = require('../privileges');
|
const privileges = require('../privileges');
|
||||||
|
const utils = require('../utils');
|
||||||
|
|
||||||
module.exports = function (Posts) {
|
module.exports = function (Posts) {
|
||||||
Posts.getUserInfoForPosts = async function (uids, uid) {
|
Posts.getUserInfoForPosts = async function (uids, uid) {
|
||||||
@@ -115,10 +116,10 @@ module.exports = function (Posts) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
Posts.isOwner = async function (pids, uid) {
|
Posts.isOwner = async function (pids, uid) {
|
||||||
uid = parseInt(uid, 10);
|
uid = utils.isNumber(uid) ? parseInt(uid, 10) : uid;
|
||||||
const isArray = Array.isArray(pids);
|
const isArray = Array.isArray(pids);
|
||||||
pids = isArray ? pids : [pids];
|
pids = isArray ? pids : [pids];
|
||||||
if (uid <= 0) {
|
if (utils.isNumber(uid) && uid <= 0) {
|
||||||
return isArray ? pids.map(() => false) : false;
|
return isArray ? pids.map(() => false) : false;
|
||||||
}
|
}
|
||||||
const postData = await Posts.getPostsFields(pids, ['uid']);
|
const postData = await Posts.getPostsFields(pids, ['uid']);
|
||||||
|
|||||||
@@ -7,6 +7,7 @@ const meta = require('../meta');
|
|||||||
const posts = require('../posts');
|
const posts = require('../posts');
|
||||||
const topics = require('../topics');
|
const topics = require('../topics');
|
||||||
const user = require('../user');
|
const user = require('../user');
|
||||||
|
const activitypub = require('../activitypub');
|
||||||
const helpers = require('./helpers');
|
const helpers = require('./helpers');
|
||||||
const plugins = require('../plugins');
|
const plugins = require('../plugins');
|
||||||
const utils = require('../utils');
|
const utils = require('../utils');
|
||||||
@@ -115,6 +116,7 @@ privsPosts.filter = async function (privilege, pids, uid) {
|
|||||||
};
|
};
|
||||||
|
|
||||||
privsPosts.canEdit = async function (pid, uid) {
|
privsPosts.canEdit = async function (pid, uid) {
|
||||||
|
const isRemote = activitypub.helpers.isUri(pid);
|
||||||
const results = await utils.promiseParallel({
|
const results = await utils.promiseParallel({
|
||||||
isAdmin: user.isAdministrator(uid),
|
isAdmin: user.isAdministrator(uid),
|
||||||
isMod: posts.isModerator([pid], uid),
|
isMod: posts.isModerator([pid], uid),
|
||||||
@@ -130,14 +132,14 @@ privsPosts.canEdit = async function (pid, uid) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (
|
if (
|
||||||
!results.isMod &&
|
!isRemote && !results.isMod &&
|
||||||
meta.config.postEditDuration &&
|
meta.config.postEditDuration &&
|
||||||
(Date.now() - results.postData.timestamp > meta.config.postEditDuration * 1000)
|
(Date.now() - results.postData.timestamp > meta.config.postEditDuration * 1000)
|
||||||
) {
|
) {
|
||||||
return { flag: false, message: `[[error:post-edit-duration-expired, ${meta.config.postEditDuration}]]` };
|
return { flag: false, message: `[[error:post-edit-duration-expired, ${meta.config.postEditDuration}]]` };
|
||||||
}
|
}
|
||||||
if (
|
if (
|
||||||
!results.isMod &&
|
!isRemote && !results.isMod &&
|
||||||
meta.config.newbiePostEditDuration > 0 &&
|
meta.config.newbiePostEditDuration > 0 &&
|
||||||
meta.config.newbieReputationThreshold > results.userData.reputation &&
|
meta.config.newbieReputationThreshold > results.userData.reputation &&
|
||||||
Date.now() - results.postData.timestamp > meta.config.newbiePostEditDuration * 1000
|
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]]' };
|
return { flag: false, message: '[[error:post-deleted]]' };
|
||||||
}
|
}
|
||||||
|
|
||||||
results.pid = parseInt(pid, 10);
|
results.pid = utils.isNumber(pid) ? parseInt(pid, 10) : pid;
|
||||||
results.uid = uid;
|
results.uid = uid;
|
||||||
|
|
||||||
const result = await plugins.hooks.fire('filter:privileges.posts.edit', results);
|
const result = await plugins.hooks.fire('filter:privileges.posts.edit', results);
|
||||||
|
|||||||
Reference in New Issue
Block a user