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-01-24 20:10:22 -05:00
|
|
|
|
2024-01-10 14:19:57 -05:00
|
|
|
const db = require('../database');
|
|
|
|
|
const user = require('../user');
|
|
|
|
|
|
|
|
|
|
const activitypub = module.parent.exports;
|
|
|
|
|
const Mocks = module.exports;
|
|
|
|
|
|
|
|
|
|
Mocks.profile = async (actors, callerUid = 0) => {
|
|
|
|
|
// Accepts an array containing actor objects (the output of getActor()), or uris
|
|
|
|
|
let single = false;
|
|
|
|
|
if (!Array.isArray(actors)) {
|
|
|
|
|
single = true;
|
|
|
|
|
actors = [actors];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const profiles = (await Promise.all(actors.map(async (actor) => {
|
|
|
|
|
// convert uri to actor object
|
|
|
|
|
if (typeof actor === 'string' && activitypub.helpers.isUri(actor)) {
|
|
|
|
|
actor = await activitypub.getActor(callerUid, actor);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!actor) {
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const uid = actor.id;
|
|
|
|
|
const { preferredUsername, published, icon, image, name, summary, hostname, followerCount, followingCount } = actor;
|
|
|
|
|
const isFollowing = await db.isSortedSetMember(`followingRemote:${callerUid}`, uid);
|
|
|
|
|
|
|
|
|
|
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,
|
|
|
|
|
aboutmeParsed: summary,
|
|
|
|
|
|
|
|
|
|
isFollowing,
|
|
|
|
|
counts: {
|
|
|
|
|
following: followingCount,
|
|
|
|
|
followers: followerCount,
|
|
|
|
|
},
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
return payload;
|
|
|
|
|
}))).filter(Boolean);
|
|
|
|
|
|
|
|
|
|
return single ? profiles.pop() : profiles;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
|
Mocks.actor = 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 {
|
|
|
|
|
'@context': [
|
|
|
|
|
'https://www.w3.org/ns/activitystreams',
|
|
|
|
|
'https://w3id.org/security/v1',
|
|
|
|
|
],
|
|
|
|
|
id: `${nconf.get('url')}/user/${userslug}`,
|
|
|
|
|
url: `${nconf.get('url')}/user/${userslug}`,
|
|
|
|
|
followers: `${nconf.get('url')}/user/${userslug}/followers`,
|
|
|
|
|
following: `${nconf.get('url')}/user/${userslug}/following`,
|
|
|
|
|
inbox: `${nconf.get('url')}/user/${userslug}/inbox`,
|
|
|
|
|
outbox: `${nconf.get('url')}/user/${userslug}/outbox`,
|
|
|
|
|
|
|
|
|
|
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: {
|
|
|
|
|
id: `${nconf.get('url')}/user/${userslug}#key`,
|
|
|
|
|
owner: `${nconf.get('url')}/user/${userslug}`,
|
|
|
|
|
publicKeyPem: publicKey,
|
|
|
|
|
},
|
|
|
|
|
};
|
|
|
|
|
};
|