feat: Like(Note) and Undo(Like); federating likes

This commit is contained in:
Julian Lam
2024-02-01 15:59:29 -05:00
parent 94361721b1
commit 607c4623c7
9 changed files with 108 additions and 20 deletions

View File

@@ -28,6 +28,7 @@ activitypubApi.follow = async (caller, { uid } = {}) => {
});
};
// should be .undo.follow
activitypubApi.unfollow = async (caller, { uid }) => {
const result = await activitypub.helpers.query(uid);
if (!result) {
@@ -112,3 +113,45 @@ activitypubApi.update.note = async (caller, { post }) => {
await activitypub.send(caller.uid, Array.from(targets), payload);
};
activitypubApi.like = {};
activitypubApi.like.note = async (caller, { pid }) => {
if (!activitypub.helpers.isUri(pid)) {
return;
}
const uid = await posts.getPostField(pid, 'uid');
if (!activitypub.helpers.isUri(uid)) {
return;
}
await activitypub.send(caller.uid, [uid], {
type: 'Like',
object: pid,
});
};
activitypubApi.undo = {};
// activitypubApi.undo.follow =
activitypubApi.undo.like = async (caller, { pid }) => {
if (!activitypub.helpers.isUri(pid)) {
return;
}
const uid = await posts.getPostField(pid, 'uid');
if (!activitypub.helpers.isUri(uid)) {
return;
}
await activitypub.send(caller.uid, [uid], {
type: 'Undo',
object: {
actor: `${nconf.get('url')}/uid/${caller.uid}`,
type: 'Like',
object: pid,
},
});
};

View File

@@ -129,6 +129,7 @@ exports.postCommand = async function (caller, command, eventName, notification,
};
async function executeCommand(caller, command, eventName, notification, data) {
const api = require('.');
const result = await posts[command](data.pid, caller.uid);
if (result && eventName) {
websockets.in(`uid_${caller.uid}`).emit(`posts.${command}`, result);
@@ -136,10 +137,12 @@ async function executeCommand(caller, command, eventName, notification, data) {
}
if (result && command === 'upvote') {
socketHelpers.upvote(result, notification);
api.activitypub.like.note(caller, { pid: data.pid });
} else if (result && notification) {
socketHelpers.sendNotificationToPostOwner(data.pid, caller.uid, command, notification);
} else if (result && command === 'unvote') {
socketHelpers.rescindUpvoteNotification(data.pid, caller.uid);
api.activitypub.undo.like(caller, { pid: data.pid });
}
return result;
}