enigma-bbs/core/activitypub/activity.js

113 lines
3.2 KiB
JavaScript
Raw Normal View History

const { localActorId } = require('./util');
const { WellKnownActivityTypes } = require('./const');
2023-01-20 05:31:14 +00:00
const ActivityPubObject = require('./object');
2023-01-13 06:19:52 +00:00
const { Errors } = require('../enig_error');
const UserProps = require('../user_property');
const { postJson } = require('../http_util');
2023-01-25 01:11:28 +00:00
const { getISOTimestampString } = require('../database');
// deps
const _ = require('lodash');
2023-01-20 05:31:14 +00:00
module.exports = class Activity extends ActivityPubObject {
constructor(obj) {
2023-01-20 05:31:14 +00:00
super(obj);
}
static get ActivityTypes() {
2023-01-20 05:31:14 +00:00
return WellKnownActivityTypes;
}
static fromJsonString(s) {
const obj = ActivityPubObject.fromJsonString(s);
return new Activity(obj);
}
static makeFollow(webServer, localActor, remoteActor) {
2023-01-22 20:51:32 +00:00
return new Activity({
id: Activity.activityObjectId(webServer),
2023-01-22 20:51:32 +00:00
type: 'Follow',
actor: localActor,
object: remoteActor.id,
});
}
// https://www.w3.org/TR/activitypub/#accept-activity-inbox
static makeAccept(webServer, localActor, followRequest) {
return new Activity({
id: Activity.activityObjectId(webServer),
type: 'Accept',
actor: localActor,
object: followRequest, // previous request Activity
});
}
static makeCreate(webServer, actor, obj) {
return new Activity({
id: Activity.activityObjectId(webServer),
type: 'Create',
actor,
object: obj,
});
}
2023-01-25 01:11:28 +00:00
static makeTombstone(obj) {
const deleted = getISOTimestampString();
return new Activity({
id: obj.id,
type: 'Tombstone',
deleted,
published: deleted,
updated: deleted,
});
}
sendTo(actorUrl, fromUser, webServer, cb) {
const privateKey = fromUser.getProperty(UserProps.PrivateActivityPubSigningKey);
if (_.isEmpty(privateKey)) {
return cb(
Errors.MissingProperty(
`User "${fromUser.username}" is missing the '${UserProps.PrivateActivityPubSigningKey}' property`
)
);
}
const reqOpts = {
headers: {
'Content-Type': 'application/activity+json',
},
sign: {
// :TODO: Make a helper for this
key: privateKey,
keyId: localActorId(webServer, fromUser) + '#main-key',
authorizationHeaderName: 'Signature',
headers: ['(request-target)', 'host', 'date', 'digest', 'content-type'],
},
};
const activityJson = JSON.stringify(this);
return postJson(actorUrl, activityJson, reqOpts, cb);
}
recipientIds() {
const ids = [];
// :TODO: bto, bcc?
['to', 'cc', 'audience'].forEach(field => {
let v = this[field];
if (v) {
if (!Array.isArray(v)) {
v = [v];
}
ids.push(...v);
}
});
return ids;
}
static activityObjectId(webServer) {
return ActivityPubObject.makeObjectId(webServer, 'activity');
}
};