mirror of
https://github.com/NodeBB/NodeBB.git
synced 2025-10-26 16:46:12 +01:00
fix: missing awaits, more comprehensive 1b12 tests
This commit is contained in:
@@ -3,6 +3,7 @@
|
|||||||
const nconf = require('nconf');
|
const nconf = require('nconf');
|
||||||
|
|
||||||
const posts = require('../posts');
|
const posts = require('../posts');
|
||||||
|
const utils = require('../utils');
|
||||||
|
|
||||||
const activitypub = module.parent.exports;
|
const activitypub = module.parent.exports;
|
||||||
const Feps = module.exports;
|
const Feps = module.exports;
|
||||||
@@ -13,7 +14,7 @@ Feps.announce = async function announce(id, activity) {
|
|||||||
({ id: localId } = await activitypub.helpers.resolveLocalId(id));
|
({ id: localId } = await activitypub.helpers.resolveLocalId(id));
|
||||||
}
|
}
|
||||||
const cid = await posts.getCidByPid(localId || id);
|
const cid = await posts.getCidByPid(localId || id);
|
||||||
if (cid === -1) {
|
if (cid === -1 || !utils.isNumber(cid)) { // local cids only
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -49,7 +49,7 @@ inbox.create = async (req) => {
|
|||||||
|
|
||||||
const asserted = await activitypub.notes.assert(0, object, { cid });
|
const asserted = await activitypub.notes.assert(0, object, { cid });
|
||||||
if (asserted) {
|
if (asserted) {
|
||||||
activitypub.feps.announce(object.id, req.body);
|
await activitypub.feps.announce(object.id, req.body);
|
||||||
// api.activitypub.add(req, { pid: object.id });
|
// api.activitypub.add(req, { pid: object.id });
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
@@ -244,7 +244,7 @@ inbox.like = async (req) => {
|
|||||||
activitypub.helpers.log(`[activitypub/inbox/like] id ${id} via ${actor}`);
|
activitypub.helpers.log(`[activitypub/inbox/like] id ${id} via ${actor}`);
|
||||||
|
|
||||||
const result = await posts.upvote(id, actor);
|
const result = await posts.upvote(id, actor);
|
||||||
activitypub.feps.announce(object.id, req.body);
|
await activitypub.feps.announce(object.id, req.body);
|
||||||
socketHelpers.upvote(result, 'notifications:upvoted-your-post-in');
|
socketHelpers.upvote(result, 'notifications:upvoted-your-post-in');
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
@@ -310,7 +310,15 @@ activitypubApi.delete.note = enabledCheck(async (caller, { pid }) => {
|
|||||||
activitypubApi.like = {};
|
activitypubApi.like = {};
|
||||||
|
|
||||||
activitypubApi.like.note = enabledCheck(async (caller, { pid }) => {
|
activitypubApi.like.note = enabledCheck(async (caller, { pid }) => {
|
||||||
if (!activitypub.helpers.isUri(pid)) { // remote only
|
const payload = {
|
||||||
|
id: `${nconf.get('url')}/uid/${caller.uid}#activity/like/${encodeURIComponent(pid)}`,
|
||||||
|
type: 'Like',
|
||||||
|
actor: `${nconf.get('url')}/uid/${caller.uid}`,
|
||||||
|
object: utils.isNumber(pid) ? `${nconf.get('url')}/post/${pid}` : pid,
|
||||||
|
};
|
||||||
|
|
||||||
|
if (!activitypub.helpers.isUri(pid)) { // only 1b12 announce for local likes
|
||||||
|
await activitypub.feps.announce(pid, payload);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -319,13 +327,6 @@ activitypubApi.like.note = enabledCheck(async (caller, { pid }) => {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
const payload = {
|
|
||||||
id: `${nconf.get('url')}/uid/${caller.uid}#activity/like/${encodeURIComponent(pid)}`,
|
|
||||||
type: 'Like',
|
|
||||||
actor: `${nconf.get('url')}/uid/${caller.uid}`,
|
|
||||||
object: pid,
|
|
||||||
};
|
|
||||||
|
|
||||||
await Promise.all([
|
await Promise.all([
|
||||||
activitypub.send('uid', caller.uid, [uid], payload),
|
activitypub.send('uid', caller.uid, [uid], payload),
|
||||||
activitypub.feps.announce(pid, payload),
|
activitypub.feps.announce(pid, payload),
|
||||||
|
|||||||
@@ -137,12 +137,12 @@ async function executeCommand(caller, command, eventName, notification, data) {
|
|||||||
}
|
}
|
||||||
if (result && command === 'upvote') {
|
if (result && command === 'upvote') {
|
||||||
socketHelpers.upvote(result, notification);
|
socketHelpers.upvote(result, notification);
|
||||||
api.activitypub.like.note(caller, { pid: data.pid });
|
await api.activitypub.like.note(caller, { pid: data.pid });
|
||||||
} else if (result && notification) {
|
} else if (result && notification) {
|
||||||
socketHelpers.sendNotificationToPostOwner(data.pid, caller.uid, command, notification);
|
socketHelpers.sendNotificationToPostOwner(data.pid, caller.uid, command, notification);
|
||||||
} else if (result && command === 'unvote') {
|
} else if (result && command === 'unvote') {
|
||||||
socketHelpers.rescindUpvoteNotification(data.pid, caller.uid);
|
socketHelpers.rescindUpvoteNotification(data.pid, caller.uid);
|
||||||
api.activitypub.undo.like(caller, { pid: data.pid });
|
await api.activitypub.undo.like(caller, { pid: data.pid });
|
||||||
}
|
}
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -12,6 +12,7 @@ const user = require('../../src/user');
|
|||||||
const groups = require('../../src/groups');
|
const groups = require('../../src/groups');
|
||||||
const categories = require('../../src/categories');
|
const categories = require('../../src/categories');
|
||||||
const topics = require('../../src/topics');
|
const topics = require('../../src/topics');
|
||||||
|
const posts = require('../../src/posts');
|
||||||
const api = require('../../src/api');
|
const api = require('../../src/api');
|
||||||
|
|
||||||
const helpers = require('./helpers');
|
const helpers = require('./helpers');
|
||||||
@@ -47,6 +48,179 @@ describe('FEPs', () => {
|
|||||||
activitypub._sent.clear();
|
activitypub._sent.clear();
|
||||||
});
|
});
|
||||||
|
|
||||||
|
describe('local actions (create, reply, vote)', () => {
|
||||||
|
let topicData;
|
||||||
|
|
||||||
|
before(async () => {
|
||||||
|
topicData = await api.topics.create({ uid }, {
|
||||||
|
cid,
|
||||||
|
title: utils.generateUUID(),
|
||||||
|
content: utils.generateUUID(),
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
afterEach(() => {
|
||||||
|
activitypub._sent.clear();
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should have federated out both Announce(Create(Article)) and Announce(Article)', () => {
|
||||||
|
const activities = Array.from(activitypub._sent);
|
||||||
|
|
||||||
|
const test1 = activities.some((activity) => {
|
||||||
|
[, activity] = activity;
|
||||||
|
return activity.type === 'Announce' &&
|
||||||
|
activity.object && activity.object.type === 'Create' &&
|
||||||
|
activity.object.object && activity.object.object.type === 'Article';
|
||||||
|
});
|
||||||
|
|
||||||
|
const test2 = activities.some((activity) => {
|
||||||
|
[, activity] = activity;
|
||||||
|
return activity.type === 'Announce' &&
|
||||||
|
activity.object && activity.object.type === 'Article';
|
||||||
|
});
|
||||||
|
|
||||||
|
assert(test1 && test2);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should federate out Announce(Create(Note)) on local reply', async () => {
|
||||||
|
await api.topics.reply({ uid }, {
|
||||||
|
tid: topicData.tid,
|
||||||
|
content: utils.generateUUID(),
|
||||||
|
});
|
||||||
|
|
||||||
|
const activities = Array.from(activitypub._sent);
|
||||||
|
|
||||||
|
assert(activities.some((activity) => {
|
||||||
|
[, activity] = activity;
|
||||||
|
return activity.type === 'Announce' &&
|
||||||
|
activity.object && activity.object.type === 'Create' &&
|
||||||
|
activity.object.object && activity.object.object.type === 'Note';
|
||||||
|
}));
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should NOT federate out Announce(Note) on local reply', async () => {
|
||||||
|
await api.topics.reply({ uid }, {
|
||||||
|
tid: topicData.tid,
|
||||||
|
content: utils.generateUUID(),
|
||||||
|
});
|
||||||
|
|
||||||
|
const activities = Array.from(activitypub._sent);
|
||||||
|
|
||||||
|
assert(activities.every((activity) => {
|
||||||
|
[, activity] = activity;
|
||||||
|
if (activity.type === 'Announce' && activity.object && activity.object.type === 'Note') {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}));
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should federate out Announce(Like) on local vote', async () => {
|
||||||
|
activitypub._sent.clear();
|
||||||
|
await api.posts.upvote({ uid: adminUid }, { pid: topicData.mainPid, room_id: `topic_${topicData.tid}` });
|
||||||
|
const activities = Array.from(activitypub._sent);
|
||||||
|
|
||||||
|
assert(activities.some((activity) => {
|
||||||
|
[, activity] = activity;
|
||||||
|
return activity.type === 'Announce' &&
|
||||||
|
activity.object && activity.object.type === 'Like';
|
||||||
|
}));
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
describe('remote actions (create, reply, vote)', () => {
|
||||||
|
let activity;
|
||||||
|
let pid;
|
||||||
|
let topicData;
|
||||||
|
|
||||||
|
before(async () => {
|
||||||
|
topicData = await api.topics.create({ uid }, {
|
||||||
|
cid,
|
||||||
|
title: utils.generateUUID(),
|
||||||
|
content: utils.generateUUID(),
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
afterEach(() => {
|
||||||
|
activitypub._sent.clear();
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should have slotted the note into the test category', async () => {
|
||||||
|
const { id, note } = await helpers.mocks.note({
|
||||||
|
cc: [`${nconf.get('url')}/category/${cid}`],
|
||||||
|
});
|
||||||
|
pid = id;
|
||||||
|
({ activity } = await helpers.mocks.create(note));
|
||||||
|
await activitypub.inbox.create({ body: activity });
|
||||||
|
|
||||||
|
const noteCid = await posts.getCidByPid(pid);
|
||||||
|
assert.strictEqual(noteCid, cid);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should federate out an Announce(Create(Note)) and Announce(Note) on new topic', async () => {
|
||||||
|
const { id, note } = await helpers.mocks.note({
|
||||||
|
cc: [`${nconf.get('url')}/category/${cid}`],
|
||||||
|
});
|
||||||
|
pid = id;
|
||||||
|
({ activity } = await helpers.mocks.create(note));
|
||||||
|
await activitypub.inbox.create({ body: activity });
|
||||||
|
|
||||||
|
const activities = Array.from(activitypub._sent);
|
||||||
|
|
||||||
|
const test1 = activities.some((activity) => {
|
||||||
|
[, activity] = activity;
|
||||||
|
return activity.type === 'Announce' &&
|
||||||
|
activity.object && activity.object.type === 'Create' &&
|
||||||
|
activity.object.object && activity.object.object.type === 'Note';
|
||||||
|
});
|
||||||
|
|
||||||
|
const test2 = activities.some((activity) => {
|
||||||
|
[, activity] = activity;
|
||||||
|
return activity.type === 'Announce' &&
|
||||||
|
activity.object && activity.object.type === 'Note';
|
||||||
|
});
|
||||||
|
|
||||||
|
assert(test1 && test2);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should federate out an Announce(Create(Note)) on reply', async () => {
|
||||||
|
const { id, note } = await helpers.mocks.note({
|
||||||
|
cc: [`${nconf.get('url')}/category/${cid}`],
|
||||||
|
inReplyTo: `${nconf.get('url')}/post/${topicData.mainPid}`,
|
||||||
|
});
|
||||||
|
pid = id;
|
||||||
|
({ activity } = await helpers.mocks.create(note));
|
||||||
|
await activitypub.inbox.create({ body: activity });
|
||||||
|
|
||||||
|
const activities = Array.from(activitypub._sent);
|
||||||
|
|
||||||
|
assert(activities.some((activity) => {
|
||||||
|
[, activity] = activity;
|
||||||
|
return activity.type === 'Announce' &&
|
||||||
|
activity.object && activity.object.type === 'Create' &&
|
||||||
|
activity.object.object && activity.object.object.type === 'Note';
|
||||||
|
}));
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should federate out an Announce(Like) on vote', async () => {
|
||||||
|
const { activity } = await helpers.mocks.like({
|
||||||
|
object: {
|
||||||
|
id: `${nconf.get('url')}/post/${topicData.mainPid}`,
|
||||||
|
},
|
||||||
|
});
|
||||||
|
await activitypub.inbox.like({ body: activity });
|
||||||
|
|
||||||
|
const activities = Array.from(activitypub._sent);
|
||||||
|
assert(activities.some((activity) => {
|
||||||
|
[, activity] = activity;
|
||||||
|
return activity.type === 'Announce' &&
|
||||||
|
activity.object && activity.object.type === 'Like';
|
||||||
|
}));
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
describe('extended actions not explicitly specified in 1b12', () => {
|
||||||
it('should be called when a topic is moved from uncategorized to another category', async () => {
|
it('should be called when a topic is moved from uncategorized to another category', async () => {
|
||||||
const { topicData, postData } = await topics.post({
|
const { topicData, postData } = await topics.post({
|
||||||
uid,
|
uid,
|
||||||
@@ -128,4 +302,5 @@ describe('FEPs', () => {
|
|||||||
});
|
});
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -144,7 +144,7 @@ Helpers.mocks.like = (override = {}) => {
|
|||||||
|
|
||||||
const activity = {
|
const activity = {
|
||||||
'@context': 'https://www.w3.org/ns/activitystreams',
|
'@context': 'https://www.w3.org/ns/activitystreams',
|
||||||
id: `${Helpers.mocks._baseUrl}/like/${encodeURIComponent(object)}`,
|
id: `${Helpers.mocks._baseUrl}/like/${encodeURIComponent(object.id)}`,
|
||||||
type: 'Like',
|
type: 'Like',
|
||||||
actor,
|
actor,
|
||||||
object,
|
object,
|
||||||
|
|||||||
Reference in New Issue
Block a user