mirror of
https://github.com/NodeBB/NodeBB.git
synced 2025-10-26 16:46:12 +01:00
feat: stub lib for retrieving and mocking posts
This commit is contained in:
@@ -14,6 +14,7 @@ const ActivityPub = module.exports;
|
|||||||
|
|
||||||
ActivityPub.helpers = require('./helpers');
|
ActivityPub.helpers = require('./helpers');
|
||||||
ActivityPub.inbox = require('./inbox');
|
ActivityPub.inbox = require('./inbox');
|
||||||
|
ActivityPub.notes = require('./notes');
|
||||||
|
|
||||||
ActivityPub.getActor = async (uid, input) => {
|
ActivityPub.getActor = async (uid, input) => {
|
||||||
// Can be a webfinger id, uri, or object, handle as appropriate
|
// Can be a webfinger id, uri, or object, handle as appropriate
|
||||||
@@ -55,6 +56,7 @@ ActivityPub.getActor = async (uid, input) => {
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
// todo: move to mocks.js
|
||||||
ActivityPub.mockProfile = async (actors, callerUid = 0) => {
|
ActivityPub.mockProfile = async (actors, callerUid = 0) => {
|
||||||
// Accepts an array containing actor objects (the output of getActor()), or uris
|
// Accepts an array containing actor objects (the output of getActor()), or uris
|
||||||
let single = false;
|
let single = false;
|
||||||
|
|||||||
78
src/activitypub/notes.js
Normal file
78
src/activitypub/notes.js
Normal file
@@ -0,0 +1,78 @@
|
|||||||
|
'use strict';
|
||||||
|
|
||||||
|
const winston = require('winston');
|
||||||
|
|
||||||
|
const db = require('../database');
|
||||||
|
|
||||||
|
const activitypub = module.parent.exports;
|
||||||
|
const Notes = module.exports;
|
||||||
|
|
||||||
|
Notes.get = async (uid, id) => {
|
||||||
|
try {
|
||||||
|
const object = await activitypub.get(uid, id);
|
||||||
|
return await Notes.mockPost(object);
|
||||||
|
} catch (e) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
// todo: move to mocks.js
|
||||||
|
Notes.mockPost = async (objects) => {
|
||||||
|
const postCache = require('../posts/cache');
|
||||||
|
|
||||||
|
let single = false;
|
||||||
|
if (!Array.isArray(objects)) {
|
||||||
|
single = true;
|
||||||
|
objects = [objects];
|
||||||
|
}
|
||||||
|
|
||||||
|
const posts = (await Promise.all(objects.map(async (object) => {
|
||||||
|
if (object.type !== 'Note') {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
const {
|
||||||
|
id: pid,
|
||||||
|
published,
|
||||||
|
updated,
|
||||||
|
attributedTo: uid,
|
||||||
|
// conversation,
|
||||||
|
content,
|
||||||
|
sourceContent,
|
||||||
|
inReplyTo: toPid,
|
||||||
|
} = object;
|
||||||
|
|
||||||
|
const timestamp = new Date(published);
|
||||||
|
let edited = new Date(updated);
|
||||||
|
edited = Number.isNaN(edited.valueOf()) ? 0 : edited;
|
||||||
|
|
||||||
|
// If no source content, then `content` is pre-parsed and should be HTML, so cache it
|
||||||
|
if (!sourceContent) {
|
||||||
|
winston.verbose(`[activitypub/mockPost] pid ${pid} already has pre-parsed HTML content, adding to post cache...`);
|
||||||
|
postCache.set(pid, content);
|
||||||
|
}
|
||||||
|
|
||||||
|
const payload = {
|
||||||
|
uid,
|
||||||
|
pid,
|
||||||
|
timestamp: timestamp.getTime(),
|
||||||
|
timestampISO: timestamp.toISOString(),
|
||||||
|
content: sourceContent || content,
|
||||||
|
toPid,
|
||||||
|
|
||||||
|
edited,
|
||||||
|
editor: edited ? uid : undefined,
|
||||||
|
editedISO: edited ? edited.toISOString() : '',
|
||||||
|
|
||||||
|
deleted: 0,
|
||||||
|
deleterUid: 0,
|
||||||
|
replies: 0, // todo
|
||||||
|
bookmarks: 0,
|
||||||
|
votes: 0, // todo
|
||||||
|
};
|
||||||
|
|
||||||
|
return payload;
|
||||||
|
}))).filter(Boolean);
|
||||||
|
|
||||||
|
return single ? posts.pop() : posts;
|
||||||
|
};
|
||||||
Reference in New Issue
Block a user