2023-02-18 04:46:24 +00:00
|
|
|
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
|
2023-02-25 05:54:19 +00:00
|
|
|
const followRequest = new ActivityPubObject({
|
|
|
|
id: ActivityPubObject.makeObjectId(webServer, 'follow'),
|
|
|
|
type: WellKnownActivity.Follow,
|
|
|
|
actor: fromActorId,
|
|
|
|
object: toActor.id,
|
|
|
|
});
|
|
|
|
|
|
|
|
toActor._followRequest = followRequest;
|
2023-02-18 04:46:24 +00:00
|
|
|
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`
|
|
|
|
)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2023-02-25 05:54:19 +00:00
|
|
|
// 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);
|
|
|
|
}
|
2023-02-18 04:46:24 +00:00
|
|
|
|
2023-02-25 05:54:19 +00:00
|
|
|
// Always remove from the local collection, notify the remote server
|
|
|
|
Collection.removeOwnedById(
|
|
|
|
Collections.Following,
|
|
|
|
fromUser,
|
|
|
|
toActor.id,
|
|
|
|
err => {
|
|
|
|
if (err) {
|
|
|
|
return cb(err);
|
|
|
|
}
|
2023-02-18 04:46:24 +00:00
|
|
|
|
2023-02-25 05:54:19 +00:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|
|
|
|
);
|
2023-02-18 04:46:24 +00:00
|
|
|
}
|