2023-12-08 10:55:16 -05:00
|
|
|
'use strict';
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* DEVELOPMENT NOTE
|
|
|
|
|
*
|
|
|
|
|
* THIS FILE IS UNDER ACTIVE DEVELOPMENT AND IS EXPLICITLY EXCLUDED FROM IMMUTABILITY GUARANTEES
|
|
|
|
|
*
|
|
|
|
|
* If you use api methods in this file, be prepared that they may be removed or modified with no warning.
|
|
|
|
|
*/
|
|
|
|
|
|
2023-12-22 15:53:04 -05:00
|
|
|
const nconf = require('nconf');
|
|
|
|
|
|
2023-12-08 10:55:16 -05:00
|
|
|
const db = require('../database');
|
|
|
|
|
const activitypub = require('../activitypub');
|
2023-12-22 15:53:04 -05:00
|
|
|
const user = require('../user');
|
2024-01-24 11:44:10 -05:00
|
|
|
const posts = require('../posts');
|
2023-12-08 10:55:16 -05:00
|
|
|
|
|
|
|
|
const activitypubApi = module.exports;
|
|
|
|
|
|
2024-01-23 12:11:35 -05:00
|
|
|
activitypubApi.follow = async (caller, { uid: actorId } = {}) => {
|
2024-01-05 22:45:33 -05:00
|
|
|
const object = await activitypub.getActor(caller.uid, actorId);
|
2023-12-22 13:56:18 -05:00
|
|
|
if (!object) {
|
2024-01-08 14:30:09 -05:00
|
|
|
throw new Error('[[error:activitypub.invalid-id]]');
|
2023-12-08 10:55:16 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
await activitypub.send(caller.uid, actorId, {
|
|
|
|
|
type: 'Follow',
|
2023-12-22 15:53:04 -05:00
|
|
|
object: object.id,
|
2023-12-08 10:55:16 -05:00
|
|
|
});
|
|
|
|
|
};
|
|
|
|
|
|
2024-01-23 12:11:35 -05:00
|
|
|
activitypubApi.unfollow = async (caller, { uid: actorId }) => {
|
2024-01-05 22:45:33 -05:00
|
|
|
const object = await activitypub.getActor(caller.uid, actorId);
|
2023-12-22 15:53:04 -05:00
|
|
|
const userslug = await user.getUserField(caller.uid, 'userslug');
|
|
|
|
|
if (!object) {
|
2024-01-08 14:30:09 -05:00
|
|
|
throw new Error('[[error:activitypub.invalid-id]]');
|
2023-12-08 10:55:16 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
await activitypub.send(caller.uid, actorId, {
|
2023-12-22 15:53:04 -05:00
|
|
|
type: 'Undo',
|
2023-12-08 10:55:16 -05:00
|
|
|
object: {
|
2023-12-22 15:53:04 -05:00
|
|
|
type: 'Follow',
|
|
|
|
|
actor: `${nconf.get('url')}/user/${userslug}`,
|
|
|
|
|
object: object.id,
|
2023-12-08 10:55:16 -05:00
|
|
|
},
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
await Promise.all([
|
2024-01-03 13:54:17 -05:00
|
|
|
db.sortedSetRemove(`followingRemote:${caller.uid}`, object.id),
|
2023-12-08 10:55:16 -05:00
|
|
|
db.decrObjectField(`user:${caller.uid}`, 'followingRemoteCount'),
|
|
|
|
|
]);
|
|
|
|
|
};
|
2024-01-24 11:44:10 -05:00
|
|
|
|
|
|
|
|
activitypubApi.create = {};
|
|
|
|
|
|
|
|
|
|
activitypubApi.create.post = async (caller, { post }) => {
|
|
|
|
|
const id = `${nconf.get('url')}/post/${post.pid}`;
|
|
|
|
|
const published = new Date(post.timestamp).toISOString();
|
|
|
|
|
const [userslug, raw, followers] = await Promise.all([
|
|
|
|
|
user.getUserField(caller.uid, 'userslug'),
|
|
|
|
|
posts.getPostField(post.pid, 'content'),
|
|
|
|
|
db.getSortedSetMembers(`followersRemote:${caller.uid}`),
|
|
|
|
|
]);
|
|
|
|
|
|
|
|
|
|
// todo: post visibility, category privileges integration
|
|
|
|
|
const recipients = {
|
|
|
|
|
to: [activitypub._constants.publicAddress],
|
|
|
|
|
cc: [`${nconf.get('url')}/user/${userslug}/followers`],
|
|
|
|
|
};
|
|
|
|
|
const targets = new Set(followers);
|
|
|
|
|
|
|
|
|
|
let inReplyTo = null;
|
|
|
|
|
if (post.toPid) {
|
|
|
|
|
inReplyTo = activitypub.helpers.isUri(post.toPid) ? post.toPid : id;
|
|
|
|
|
const parentId = await posts.getPostField(post.toPid, 'uid');
|
|
|
|
|
if (activitypub.helpers.isUri(parentId)) {
|
|
|
|
|
recipients.to.unshift(parentId);
|
|
|
|
|
targets.add(parentId);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const object = {
|
|
|
|
|
id,
|
|
|
|
|
type: 'Note',
|
|
|
|
|
...recipients,
|
|
|
|
|
inReplyTo,
|
|
|
|
|
published,
|
|
|
|
|
url: id,
|
|
|
|
|
attributedTo: `${nconf.get('url')}/user/${post.user.userslug}`,
|
|
|
|
|
sensitive: false, // todo
|
|
|
|
|
content: post.content,
|
|
|
|
|
source: {
|
|
|
|
|
content: raw,
|
|
|
|
|
mediaType: 'text/markdown',
|
|
|
|
|
},
|
|
|
|
|
// replies: {} todo...
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const payload = {
|
|
|
|
|
type: 'Create',
|
|
|
|
|
...recipients,
|
|
|
|
|
object,
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
await activitypub.send(caller.uid, Array.from(targets), payload);
|
|
|
|
|
};
|
2024-01-24 20:10:22 -05:00
|
|
|
|
|
|
|
|
activitypubApi.update = {};
|
|
|
|
|
|
|
|
|
|
activitypubApi.update.profile = async (caller, { uid }) => {
|
|
|
|
|
const [object, followers] = await Promise.all([
|
|
|
|
|
activitypub.mocks.actor(uid),
|
|
|
|
|
db.getSortedSetMembers(`followersRemote:${caller.uid}`),
|
|
|
|
|
]);
|
|
|
|
|
|
|
|
|
|
await activitypub.send(caller.uid, followers, {
|
|
|
|
|
type: 'Update',
|
|
|
|
|
to: [activitypub._constants.publicAddress],
|
|
|
|
|
cc: [],
|
|
|
|
|
object,
|
|
|
|
|
});
|
|
|
|
|
};
|