2024-01-10 14:19:57 -05:00
|
|
|
'use strict';
|
|
|
|
|
|
2024-01-24 20:10:22 -05:00
|
|
|
const nconf = require('nconf');
|
2024-01-24 22:59:09 -05:00
|
|
|
const mime = require('mime');
|
2024-02-02 17:19:59 -05:00
|
|
|
const path = require('path');
|
2024-01-24 20:10:22 -05:00
|
|
|
|
2024-01-10 14:19:57 -05:00
|
|
|
const user = require('../user');
|
2024-02-02 17:19:59 -05:00
|
|
|
const categories = require('../categories');
|
2024-01-25 15:35:45 -05:00
|
|
|
const posts = require('../posts');
|
2024-01-26 21:39:20 -05:00
|
|
|
const topics = require('../topics');
|
2024-02-02 17:19:59 -05:00
|
|
|
const utils = require('../utils');
|
2024-01-10 14:19:57 -05:00
|
|
|
|
|
|
|
|
const activitypub = module.parent.exports;
|
|
|
|
|
const Mocks = module.exports;
|
|
|
|
|
|
2024-01-26 15:10:35 -05:00
|
|
|
Mocks.profile = async (actors) => {
|
|
|
|
|
// Should only ever be called by activitypub.actors.assert
|
2024-01-10 14:19:57 -05:00
|
|
|
const profiles = (await Promise.all(actors.map(async (actor) => {
|
|
|
|
|
if (!actor) {
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const uid = actor.id;
|
2024-01-26 15:10:35 -05:00
|
|
|
const {
|
|
|
|
|
preferredUsername, published, icon, image,
|
|
|
|
|
name, summary, followerCount, followingCount,
|
|
|
|
|
postcount, inbox, endpoints,
|
|
|
|
|
} = actor;
|
|
|
|
|
const { hostname } = new URL(actor.id);
|
2024-01-10 14:19:57 -05:00
|
|
|
|
|
|
|
|
let picture;
|
|
|
|
|
if (icon) {
|
|
|
|
|
picture = typeof icon === 'string' ? icon : icon.url;
|
|
|
|
|
}
|
|
|
|
|
const iconBackgrounds = await user.getIconBackgrounds();
|
|
|
|
|
let bgColor = Array.prototype.reduce.call(preferredUsername, (cur, next) => cur + next.charCodeAt(), 0);
|
|
|
|
|
bgColor = iconBackgrounds[bgColor % iconBackgrounds.length];
|
|
|
|
|
|
|
|
|
|
const payload = {
|
|
|
|
|
uid,
|
|
|
|
|
username: `${preferredUsername}@${hostname}`,
|
|
|
|
|
userslug: `${preferredUsername}@${hostname}`,
|
|
|
|
|
displayname: name,
|
|
|
|
|
fullname: name,
|
|
|
|
|
joindate: new Date(published).getTime(),
|
|
|
|
|
picture,
|
2024-01-10 20:51:23 -05:00
|
|
|
status: 'offline',
|
2024-01-10 14:19:57 -05:00
|
|
|
'icon:text': (preferredUsername[0] || '').toUpperCase(),
|
|
|
|
|
'icon:bgColor': bgColor,
|
|
|
|
|
uploadedpicture: undefined,
|
|
|
|
|
'cover:url': !image || typeof image === 'string' ? image : image.url,
|
|
|
|
|
'cover:position': '50% 50%',
|
|
|
|
|
aboutme: summary,
|
2024-01-26 15:10:35 -05:00
|
|
|
postcount,
|
|
|
|
|
followerCount,
|
|
|
|
|
followingCount,
|
2024-01-10 14:19:57 -05:00
|
|
|
|
2024-01-26 15:10:35 -05:00
|
|
|
inbox,
|
2024-01-26 21:39:20 -05:00
|
|
|
sharedInbox: endpoints ? endpoints.sharedInbox : null,
|
2024-01-10 14:19:57 -05:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
return payload;
|
2024-01-26 15:10:35 -05:00
|
|
|
})));
|
2024-01-10 14:19:57 -05:00
|
|
|
|
2024-01-26 15:10:35 -05:00
|
|
|
return profiles;
|
2024-01-10 14:19:57 -05:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
Mocks.post = async (objects) => {
|
|
|
|
|
let single = false;
|
|
|
|
|
if (!Array.isArray(objects)) {
|
|
|
|
|
single = true;
|
|
|
|
|
objects = [objects];
|
|
|
|
|
}
|
|
|
|
|
|
2024-01-12 11:29:08 -05:00
|
|
|
const posts = await Promise.all(objects.map(async (object) => {
|
2024-01-18 16:20:37 -05:00
|
|
|
const acceptedTypes = ['Note', 'Page', 'Article'];
|
|
|
|
|
if (!acceptedTypes.includes(object.type)) {
|
2024-01-10 14:19:57 -05:00
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const {
|
|
|
|
|
id: pid,
|
|
|
|
|
published,
|
|
|
|
|
updated,
|
|
|
|
|
attributedTo: uid,
|
|
|
|
|
// conversation,
|
2024-01-18 16:20:37 -05:00
|
|
|
name,
|
2024-01-10 14:19:57 -05:00
|
|
|
content,
|
|
|
|
|
sourceContent,
|
|
|
|
|
inReplyTo: toPid,
|
|
|
|
|
} = object;
|
|
|
|
|
|
2024-01-10 20:51:23 -05:00
|
|
|
const timestamp = new Date(published).getTime();
|
2024-01-10 14:19:57 -05:00
|
|
|
let edited = new Date(updated);
|
2024-01-10 20:51:23 -05:00
|
|
|
edited = Number.isNaN(edited.valueOf()) ? undefined : edited;
|
2024-01-10 14:19:57 -05:00
|
|
|
|
|
|
|
|
const payload = {
|
|
|
|
|
uid,
|
|
|
|
|
pid,
|
2024-01-18 16:20:37 -05:00
|
|
|
// tid, --> purposely omitted
|
|
|
|
|
name,
|
2024-01-10 20:51:23 -05:00
|
|
|
content,
|
|
|
|
|
sourceContent,
|
|
|
|
|
timestamp,
|
2024-01-10 14:19:57 -05:00
|
|
|
toPid,
|
|
|
|
|
|
|
|
|
|
edited,
|
|
|
|
|
editor: edited ? uid : undefined,
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
return payload;
|
2024-01-12 11:29:08 -05:00
|
|
|
}));
|
2024-01-10 14:19:57 -05:00
|
|
|
|
|
|
|
|
return single ? posts.pop() : posts;
|
|
|
|
|
};
|
2024-01-24 20:10:22 -05:00
|
|
|
|
2024-02-02 17:19:59 -05:00
|
|
|
Mocks.actors = {};
|
|
|
|
|
|
|
|
|
|
Mocks.actors.user = async (uid) => {
|
2024-01-24 22:59:09 -05:00
|
|
|
let { username, userslug, displayname: name, aboutme, picture, 'cover:url': cover } = await user.getUserData(uid);
|
2024-01-24 20:10:22 -05:00
|
|
|
const publicKey = await activitypub.getPublicKey(uid);
|
|
|
|
|
|
2024-01-24 22:59:09 -05:00
|
|
|
if (picture) {
|
|
|
|
|
const imagePath = await user.getLocalAvatarPath(uid);
|
|
|
|
|
picture = {
|
|
|
|
|
type: 'Image',
|
|
|
|
|
mediaType: mime.getType(imagePath),
|
|
|
|
|
url: `${nconf.get('url')}${picture}`,
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (cover) {
|
|
|
|
|
const imagePath = await user.getLocalCoverPath(uid);
|
|
|
|
|
cover = {
|
|
|
|
|
type: 'Image',
|
|
|
|
|
mediaType: mime.getType(imagePath),
|
|
|
|
|
url: `${nconf.get('url')}${cover}`,
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
2024-01-24 20:10:22 -05:00
|
|
|
return {
|
2024-01-29 16:33:41 -05:00
|
|
|
'@context': 'https://www.w3.org/ns/activitystreams',
|
2024-01-25 14:23:59 -05:00
|
|
|
id: `${nconf.get('url')}/uid/${uid}`,
|
2024-01-24 20:10:22 -05:00
|
|
|
url: `${nconf.get('url')}/user/${userslug}`,
|
2024-01-29 16:59:13 -05:00
|
|
|
followers: `${nconf.get('url')}/uid/${uid}/followers`,
|
|
|
|
|
following: `${nconf.get('url')}/uid/${uid}/following`,
|
|
|
|
|
inbox: `${nconf.get('url')}/uid/${uid}/inbox`,
|
|
|
|
|
outbox: `${nconf.get('url')}/uid/${uid}/outbox`,
|
2024-01-24 20:10:22 -05:00
|
|
|
|
|
|
|
|
type: 'Person',
|
|
|
|
|
name,
|
|
|
|
|
preferredUsername: username,
|
|
|
|
|
summary: aboutme,
|
2024-01-24 22:59:09 -05:00
|
|
|
icon: picture,
|
|
|
|
|
image: cover,
|
2024-01-24 20:10:22 -05:00
|
|
|
|
|
|
|
|
publicKey: {
|
2024-01-29 16:59:13 -05:00
|
|
|
id: `${nconf.get('url')}/uid/${uid}#key`,
|
|
|
|
|
owner: `${nconf.get('url')}/uid/${uid}`,
|
2024-01-24 20:10:22 -05:00
|
|
|
publicKeyPem: publicKey,
|
|
|
|
|
},
|
|
|
|
|
};
|
|
|
|
|
};
|
2024-01-25 15:35:45 -05:00
|
|
|
|
2024-02-02 17:19:59 -05:00
|
|
|
Mocks.actors.category = async (cid) => {
|
|
|
|
|
let { name, slug, description: summary, backgroundImage } = await categories.getCategoryData(cid);
|
|
|
|
|
|
|
|
|
|
if (backgroundImage) {
|
|
|
|
|
const filename = utils.decodeHTMLEntities(backgroundImage).split('/').pop();
|
|
|
|
|
const imagePath = path.join(nconf.get('upload_path'), 'category', filename);
|
|
|
|
|
backgroundImage = {
|
|
|
|
|
type: 'Image',
|
|
|
|
|
mediaType: mime.getType(imagePath),
|
|
|
|
|
url: `${nconf.get('url')}${utils.decodeHTMLEntities(backgroundImage)}`,
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return {
|
|
|
|
|
'@context': 'https://www.w3.org/ns/activitystreams',
|
|
|
|
|
id: `${nconf.get('url')}/category/${cid}`,
|
|
|
|
|
url: `${nconf.get('url')}/category/${slug}`,
|
|
|
|
|
// followers: ,
|
|
|
|
|
// following: ,
|
|
|
|
|
inbox: `${nconf.get('url')}/category/${cid}/inbox`,
|
|
|
|
|
outbox: `${nconf.get('url')}/category/${cid}/outbox`,
|
|
|
|
|
|
|
|
|
|
type: 'Group',
|
|
|
|
|
name,
|
|
|
|
|
preferredUsername: name,
|
|
|
|
|
summary,
|
|
|
|
|
icon: backgroundImage,
|
|
|
|
|
};
|
|
|
|
|
};
|
|
|
|
|
|
2024-01-25 15:35:45 -05:00
|
|
|
Mocks.note = async (post) => {
|
|
|
|
|
const id = `${nconf.get('url')}/post/${post.pid}`;
|
2024-01-25 16:33:31 -05:00
|
|
|
const published = new Date(parseInt(post.timestamp, 10)).toISOString();
|
2024-01-25 15:35:45 -05:00
|
|
|
|
2024-01-29 16:59:13 -05:00
|
|
|
const raw = await posts.getPostField(post.pid, 'content');
|
2024-01-25 15:35:45 -05:00
|
|
|
|
|
|
|
|
// todo: post visibility, category privileges integration
|
|
|
|
|
const to = [activitypub._constants.publicAddress];
|
2024-01-29 16:59:13 -05:00
|
|
|
const cc = [`${nconf.get('url')}/uid/${post.user.uid}/followers`];
|
2024-01-25 15:35:45 -05:00
|
|
|
|
|
|
|
|
let inReplyTo = null;
|
2024-01-26 21:39:20 -05:00
|
|
|
let name = null;
|
|
|
|
|
if (post.toPid) { // direct reply
|
2024-01-25 15:35:45 -05:00
|
|
|
inReplyTo = activitypub.helpers.isUri(post.toPid) ? post.toPid : `${nconf.get('url')}/post/${post.toPid}`;
|
|
|
|
|
const parentId = await posts.getPostField(post.toPid, 'uid');
|
2024-01-25 16:26:39 -05:00
|
|
|
to.unshift(activitypub.helpers.isUri(parentId) ? parentId : `${nconf.get('url')}/uid/${parentId}`);
|
2024-01-26 21:39:20 -05:00
|
|
|
} else if (!post.isMainPost) { // reply to OP
|
2024-01-25 16:26:39 -05:00
|
|
|
inReplyTo = `${nconf.get('url')}/post/${post.topic.mainPid}`;
|
|
|
|
|
to.unshift(activitypub.helpers.isUri(post.topic.uid) ? post.topic.uid : `${nconf.get('url')}/uid/${post.topic.uid}`);
|
2024-01-26 21:39:20 -05:00
|
|
|
} else { // new topic
|
|
|
|
|
name = await topics.getTitleByPid(post.pid);
|
2024-01-25 15:35:45 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const object = {
|
2024-01-29 16:33:41 -05:00
|
|
|
'@context': 'https://www.w3.org/ns/activitystreams',
|
2024-01-25 15:35:45 -05:00
|
|
|
id,
|
|
|
|
|
type: 'Note',
|
|
|
|
|
to,
|
|
|
|
|
cc,
|
|
|
|
|
inReplyTo,
|
|
|
|
|
published,
|
|
|
|
|
url: id,
|
|
|
|
|
attributedTo: `${nconf.get('url')}/uid/${post.user.uid}`,
|
|
|
|
|
sensitive: false, // todo
|
2024-01-25 16:26:39 -05:00
|
|
|
summary: null,
|
2024-01-26 21:39:20 -05:00
|
|
|
name,
|
2024-01-25 15:35:45 -05:00
|
|
|
content: post.content,
|
|
|
|
|
source: {
|
|
|
|
|
content: raw,
|
|
|
|
|
mediaType: 'text/markdown',
|
|
|
|
|
},
|
|
|
|
|
// replies: {} todo...
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
return object;
|
|
|
|
|
};
|