enigma-bbs/core/activitypub/follow_util.js

84 lines
2.7 KiB
JavaScript
Raw Normal View History

const { Collections, WellKnownActivity } = require('./const');
const ActivityPubObject = require('./object');
const UserProps = require('../user_property');
const { Errors } = require('../enig_error');
const Collection = require('./collection');
exports.sendFollowRequest = sendFollowRequest;
exports.sendUnfollowRequest = sendUnfollowRequest;
function sendFollowRequest(fromUser, toActor, webServer, cb) {
const fromActorId = fromUser.getProperty(UserProps.ActivityPubActorId);
if (!fromActorId) {
return cb(
Errors.MissingProperty(
`User missing "${UserProps.ActivityPubActorId}" property`
)
);
}
// We always add to the following collection;
// We expect an async follow up request to our server of
// Accept or Reject but it's not guaranteed
const followRequest = new ActivityPubObject({
id: ActivityPubObject.makeObjectId(webServer, 'follow'),
type: WellKnownActivity.Follow,
actor: fromActorId,
object: toActor.id,
});
toActor._followRequest = followRequest;
Collection.addFollowing(fromUser, toActor, webServer, true, err => {
if (err) {
return cb(err);
}
return followRequest.sendTo(toActor.inbox, fromUser, webServer, cb);
});
}
function sendUnfollowRequest(fromUser, toActor, webServer, cb) {
const fromActorId = fromUser.getProperty(UserProps.ActivityPubActorId);
if (!fromActorId) {
return cb(
Errors.MissingProperty(
`User missing "${UserProps.ActivityPubActorId}" property`
)
);
}
// Fetch previously saved 'Follow'; We're going to Undo it &
// need a copy.
Collection.ownedObjectByNameAndId(
Collections.Following,
fromUser,
toActor.id,
(err, followedActor) => {
if (err) {
return cb(err);
}
// Always remove from the local collection, notify the remote server
Collection.removeOwnedById(
Collections.Following,
fromUser,
toActor.id,
err => {
if (err) {
return cb(err);
}
const undoRequest = new ActivityPubObject({
id: ActivityPubObject.makeObjectId(webServer, 'undo'),
type: WellKnownActivity.Undo,
actor: fromActorId,
object: followedActor._followRequest,
});
return undoRequest.sendTo(toActor.inbox, fromUser, webServer, cb);
}
);
}
);
}