feat: stub lib for retrieving and mocking posts

This commit is contained in:
Julian Lam
2024-01-10 14:14:44 -05:00
parent 91a509c4eb
commit f1e5e5a0ad
2 changed files with 80 additions and 0 deletions

View File

@@ -14,6 +14,7 @@ const ActivityPub = module.exports;
ActivityPub.helpers = require('./helpers');
ActivityPub.inbox = require('./inbox');
ActivityPub.notes = require('./notes');
ActivityPub.getActor = async (uid, input) => {
// 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) => {
// Accepts an array containing actor objects (the output of getActor()), or uris
let single = false;

78
src/activitypub/notes.js Normal file
View 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;
};