enigma-bbs/core/activitypub/activity.js

83 lines
2.2 KiB
JavaScript
Raw Normal View History

const { WellKnownActivityTypes, WellKnownActivity } = require('./const');
const { recipientIdsFromObject } = require('./util');
2023-01-20 05:31:14 +00:00
const ActivityPubObject = require('./object');
2023-01-25 01:11:28 +00:00
const { getISOTimestampString } = require('../database');
2023-01-20 05:31:14 +00:00
module.exports = class Activity extends ActivityPubObject {
2023-02-07 01:27:12 +00:00
constructor(obj, withContext = ActivityPubObject.DefaultContext) {
super(obj, withContext);
}
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(localActor, remoteActor) {
2023-01-22 20:51:32 +00:00
return new Activity({
id: Activity.activityObjectId(),
type: WellKnownActivity.Follow,
2023-01-22 20:51:32 +00:00
actor: localActor,
object: remoteActor.id,
});
}
// https://www.w3.org/TR/activitypub/#accept-activity-inbox
static makeAccept(localActor, followRequest) {
return new Activity({
id: Activity.activityObjectId(),
type: WellKnownActivity.Accept,
actor: localActor,
object: followRequest, // previous request Activity
});
}
static makeCreate(actor, obj, context) {
2023-02-07 01:27:12 +00:00
const activity = new Activity(
{
id: Activity.activityObjectId(),
2023-02-07 01:27:12 +00:00
to: obj.to,
type: WellKnownActivity.Create,
actor,
object: obj,
},
context
);
const copy = n => {
if (obj[n]) {
activity[n] = obj[n];
}
};
copy('to');
copy('cc');
// :TODO: Others?
return activity;
}
2023-01-25 01:11:28 +00:00
static makeTombstone(obj) {
const deleted = getISOTimestampString();
return new Activity({
id: obj.id,
type: WellKnownActivity.Tombstone,
2023-01-25 01:11:28 +00:00
deleted,
published: deleted,
updated: deleted,
});
}
recipientIds() {
return recipientIdsFromObject(this);
}
static activityObjectId() {
return ActivityPubObject.makeObjectId('activity');
}
};