2023-01-28 18:55:31 +00:00
|
|
|
const { localActorId } = require('./util');
|
2023-01-29 23:52:01 +00:00
|
|
|
const {
|
|
|
|
ActivityStreamMediaType,
|
|
|
|
WellKnownActivityTypes,
|
|
|
|
WellKnownActivity,
|
|
|
|
WellKnownRecipientFields,
|
|
|
|
HttpSignatureSignHeaders,
|
|
|
|
} = 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');
|
2023-01-13 01:49:13 +00:00
|
|
|
const UserProps = require('../user_property');
|
|
|
|
const { postJson } = require('../http_util');
|
2023-01-25 01:11:28 +00:00
|
|
|
const { getISOTimestampString } = require('../database');
|
2023-01-12 05:37:09 +00:00
|
|
|
|
|
|
|
// deps
|
|
|
|
const _ = require('lodash');
|
2023-01-08 20:18:50 +00:00
|
|
|
|
2023-01-20 05:31:14 +00:00
|
|
|
module.exports = class Activity extends ActivityPubObject {
|
2023-01-08 20:18:50 +00:00
|
|
|
constructor(obj) {
|
2023-01-20 05:31:14 +00:00
|
|
|
super(obj);
|
2023-01-08 20:18:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static get ActivityTypes() {
|
2023-01-20 05:31:14 +00:00
|
|
|
return WellKnownActivityTypes;
|
2023-01-08 20:18:50 +00:00
|
|
|
}
|
|
|
|
|
2023-01-28 18:55:31 +00:00
|
|
|
static fromJsonString(s) {
|
|
|
|
const obj = ActivityPubObject.fromJsonString(s);
|
|
|
|
return new Activity(obj);
|
|
|
|
}
|
|
|
|
|
2023-01-23 21:45:56 +00:00
|
|
|
static makeFollow(webServer, localActor, remoteActor) {
|
2023-01-22 20:51:32 +00:00
|
|
|
return new Activity({
|
2023-01-23 21:45:56 +00:00
|
|
|
id: Activity.activityObjectId(webServer),
|
2023-01-29 23:52:01 +00:00
|
|
|
type: WellKnownActivity.Follow,
|
2023-01-22 20:51:32 +00:00
|
|
|
actor: localActor,
|
|
|
|
object: remoteActor.id,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2023-01-09 00:11:49 +00:00
|
|
|
// https://www.w3.org/TR/activitypub/#accept-activity-inbox
|
2023-01-23 21:45:56 +00:00
|
|
|
static makeAccept(webServer, localActor, followRequest) {
|
2023-01-09 00:11:49 +00:00
|
|
|
return new Activity({
|
2023-01-23 21:45:56 +00:00
|
|
|
id: Activity.activityObjectId(webServer),
|
2023-01-29 23:52:01 +00:00
|
|
|
type: WellKnownActivity.Accept,
|
2023-01-09 00:11:49 +00:00
|
|
|
actor: localActor,
|
|
|
|
object: followRequest, // previous request Activity
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2023-01-23 21:45:56 +00:00
|
|
|
static makeCreate(webServer, actor, obj) {
|
2023-02-06 04:10:51 +00:00
|
|
|
const activity = new Activity({
|
2023-01-23 21:45:56 +00:00
|
|
|
id: Activity.activityObjectId(webServer),
|
2023-02-06 04:10:51 +00:00
|
|
|
to: obj.to,
|
2023-01-29 23:52:01 +00:00
|
|
|
type: WellKnownActivity.Create,
|
2023-01-23 21:45:56 +00:00
|
|
|
actor,
|
|
|
|
object: obj,
|
|
|
|
});
|
2023-02-06 04:10:51 +00:00
|
|
|
|
|
|
|
const copy = n => {
|
|
|
|
if (obj[n]) {
|
|
|
|
activity[n] = obj[n];
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
copy('to');
|
|
|
|
copy('cc');
|
|
|
|
// :TODO: Others?
|
|
|
|
|
|
|
|
return activity;
|
2023-01-12 05:37:09 +00:00
|
|
|
}
|
|
|
|
|
2023-01-25 01:11:28 +00:00
|
|
|
static makeTombstone(obj) {
|
|
|
|
const deleted = getISOTimestampString();
|
|
|
|
return new Activity({
|
|
|
|
id: obj.id,
|
2023-01-29 23:52:01 +00:00
|
|
|
type: WellKnownActivity.Tombstone,
|
2023-01-25 01:11:28 +00:00
|
|
|
deleted,
|
|
|
|
published: deleted,
|
|
|
|
updated: deleted,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2023-02-06 04:10:51 +00:00
|
|
|
sendTo(inboxEndpoint, fromUser, webServer, cb) {
|
2023-01-14 04:27:02 +00:00
|
|
|
const privateKey = fromUser.getProperty(UserProps.PrivateActivityPubSigningKey);
|
2023-01-12 05:37:09 +00:00
|
|
|
if (_.isEmpty(privateKey)) {
|
|
|
|
return cb(
|
|
|
|
Errors.MissingProperty(
|
2023-01-14 04:27:02 +00:00
|
|
|
`User "${fromUser.username}" is missing the '${UserProps.PrivateActivityPubSigningKey}' property`
|
2023-01-12 05:37:09 +00:00
|
|
|
)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
const reqOpts = {
|
|
|
|
headers: {
|
2023-01-29 23:52:01 +00:00
|
|
|
'Content-Type': ActivityStreamMediaType,
|
2023-01-12 05:37:09 +00:00
|
|
|
},
|
|
|
|
sign: {
|
|
|
|
key: privateKey,
|
2023-01-28 18:55:31 +00:00
|
|
|
keyId: localActorId(webServer, fromUser) + '#main-key',
|
2023-01-12 05:37:09 +00:00
|
|
|
authorizationHeaderName: 'Signature',
|
2023-01-29 23:52:01 +00:00
|
|
|
headers: HttpSignatureSignHeaders,
|
2023-01-12 05:37:09 +00:00
|
|
|
},
|
|
|
|
};
|
|
|
|
|
|
|
|
const activityJson = JSON.stringify(this);
|
2023-02-06 04:10:51 +00:00
|
|
|
return postJson(inboxEndpoint, activityJson, reqOpts, cb);
|
2023-01-12 05:37:09 +00:00
|
|
|
}
|
|
|
|
|
2023-02-04 18:51:47 +00:00
|
|
|
// :TODO: we need dp/support a bit more here...
|
2023-01-28 18:55:31 +00:00
|
|
|
recipientIds() {
|
|
|
|
const ids = [];
|
|
|
|
|
2023-01-29 23:52:01 +00:00
|
|
|
WellKnownRecipientFields.forEach(field => {
|
2023-01-28 18:55:31 +00:00
|
|
|
let v = this[field];
|
|
|
|
if (v) {
|
|
|
|
if (!Array.isArray(v)) {
|
|
|
|
v = [v];
|
|
|
|
}
|
|
|
|
ids.push(...v);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2023-01-29 23:52:01 +00:00
|
|
|
return Array.from(new Set(ids));
|
2023-01-28 18:55:31 +00:00
|
|
|
}
|
|
|
|
|
2023-01-23 21:45:56 +00:00
|
|
|
static activityObjectId(webServer) {
|
|
|
|
return ActivityPubObject.makeObjectId(webServer, 'activity');
|
2023-01-09 00:11:49 +00:00
|
|
|
}
|
2023-01-08 20:18:50 +00:00
|
|
|
};
|