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');
|
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-26 15:10:35 -05:00
|
|
|
activitypubApi.follow = async (caller, { uid } = {}) => {
|
|
|
|
|
const result = await activitypub.helpers.query(uid);
|
|
|
|
|
if (!result) {
|
2024-01-08 14:30:09 -05:00
|
|
|
throw new Error('[[error:activitypub.invalid-id]]');
|
2023-12-08 10:55:16 -05:00
|
|
|
}
|
|
|
|
|
|
2024-01-26 22:35:02 -05:00
|
|
|
await activitypub.send(caller.uid, [result.actorUri], {
|
2023-12-08 10:55:16 -05:00
|
|
|
type: 'Follow',
|
2024-01-26 15:10:35 -05:00
|
|
|
object: result.actorUri,
|
2023-12-08 10:55:16 -05:00
|
|
|
});
|
|
|
|
|
};
|
|
|
|
|
|
2024-01-26 15:10:35 -05:00
|
|
|
activitypubApi.unfollow = async (caller, { uid }) => {
|
|
|
|
|
const result = await activitypub.helpers.query(uid);
|
|
|
|
|
if (!result) {
|
2024-01-08 14:30:09 -05:00
|
|
|
throw new Error('[[error:activitypub.invalid-id]]');
|
2023-12-08 10:55:16 -05:00
|
|
|
}
|
|
|
|
|
|
2024-01-26 22:35:02 -05:00
|
|
|
await activitypub.send(caller.uid, [result.actorUri], {
|
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',
|
2024-01-26 22:35:02 -05:00
|
|
|
actor: `${nconf.get('url')}/uid/${caller.uid}`,
|
2024-01-26 15:10:35 -05:00
|
|
|
object: result.actorUri,
|
2023-12-08 10:55:16 -05:00
|
|
|
},
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
await Promise.all([
|
2024-01-26 15:10:35 -05:00
|
|
|
db.sortedSetRemove(`followingRemote:${caller.uid}`, result.actorUri),
|
2023-12-08 10:55:16 -05:00
|
|
|
db.decrObjectField(`user:${caller.uid}`, 'followingRemoteCount'),
|
|
|
|
|
]);
|
|
|
|
|
};
|
2024-01-24 11:44:10 -05:00
|
|
|
|
|
|
|
|
activitypubApi.create = {};
|
|
|
|
|
|
2024-01-30 11:25:45 -05:00
|
|
|
// this might be better genericised... tbd. some of to/cc is built in mocks.
|
|
|
|
|
async function buildRecipients(object, uid) {
|
|
|
|
|
const followers = await db.getSortedSetMembers(`followersRemote:${uid}`);
|
|
|
|
|
const { to } = object;
|
|
|
|
|
const targets = new Set(followers);
|
|
|
|
|
const parentId = await posts.getPostField(object.inReplyTo, 'uid');
|
|
|
|
|
if (activitypub.helpers.isUri(parentId)) {
|
|
|
|
|
to.unshift(parentId);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return { targets };
|
|
|
|
|
}
|
|
|
|
|
|
2024-01-25 15:35:45 -05:00
|
|
|
activitypubApi.create.post = async (caller, { pid }) => {
|
|
|
|
|
const post = (await posts.getPostSummaryByPids([pid], caller.uid, { stripTags: false })).pop();
|
|
|
|
|
if (!post) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2024-01-30 11:25:45 -05:00
|
|
|
const object = await activitypub.mocks.note(post);
|
|
|
|
|
const { targets } = await buildRecipients(object, post.user.uid);
|
2024-01-24 11:44:10 -05:00
|
|
|
|
|
|
|
|
const payload = {
|
|
|
|
|
type: 'Create',
|
2024-01-30 11:25:45 -05:00
|
|
|
to: object.to,
|
|
|
|
|
cc: object.cc,
|
2024-01-24 11:44:10 -05:00
|
|
|
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,
|
|
|
|
|
});
|
|
|
|
|
};
|
2024-01-30 11:25:45 -05:00
|
|
|
|
|
|
|
|
activitypubApi.update.note = async (caller, { post }) => {
|
|
|
|
|
const object = await activitypub.mocks.note(post);
|
|
|
|
|
const { targets } = await buildRecipients(object, post.user.uid);
|
|
|
|
|
|
|
|
|
|
const payload = {
|
|
|
|
|
type: 'Update',
|
|
|
|
|
to: object.to,
|
|
|
|
|
cc: object.cc,
|
|
|
|
|
object,
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
await activitypub.send(caller.uid, Array.from(targets), payload);
|
|
|
|
|
};
|