2023-02-09 04:32:54 +00:00
|
|
|
const {
|
|
|
|
ActivityStreamsContext,
|
|
|
|
ActivityStreamMediaType,
|
|
|
|
HttpSignatureSignHeaders,
|
|
|
|
} = require('./const');
|
2023-02-07 01:27:12 +00:00
|
|
|
const Endpoints = require('./endpoint');
|
2023-02-09 04:32:54 +00:00
|
|
|
const UserProps = require('../user_property');
|
|
|
|
const { Errors } = require('../enig_error');
|
|
|
|
const { postJson } = require('../http_util');
|
2023-01-23 21:45:56 +00:00
|
|
|
|
|
|
|
// deps
|
2023-02-09 04:32:54 +00:00
|
|
|
const { isString, isObject, isEmpty } = require('lodash');
|
2023-01-23 21:45:56 +00:00
|
|
|
|
2023-02-07 05:18:09 +00:00
|
|
|
const Context = '@context';
|
|
|
|
|
2023-01-23 21:45:56 +00:00
|
|
|
module.exports = class ActivityPubObject {
|
2023-02-07 01:27:12 +00:00
|
|
|
constructor(obj, withContext = [ActivityStreamsContext]) {
|
|
|
|
if (withContext) {
|
|
|
|
this.setContext(withContext);
|
|
|
|
}
|
2023-01-23 21:45:56 +00:00
|
|
|
Object.assign(this, obj);
|
|
|
|
}
|
|
|
|
|
2023-02-07 01:27:12 +00:00
|
|
|
static get DefaultContext() {
|
|
|
|
return [ActivityStreamsContext];
|
|
|
|
}
|
|
|
|
|
|
|
|
static makeContext(namespaceUrls, aliases = null) {
|
|
|
|
const context = [ActivityStreamsContext];
|
|
|
|
if (Array.isArray(namespaceUrls)) {
|
|
|
|
context.push(...namespaceUrls);
|
|
|
|
}
|
|
|
|
if (isObject(aliases)) {
|
|
|
|
context.push(aliases);
|
|
|
|
}
|
|
|
|
return context;
|
|
|
|
}
|
|
|
|
|
2023-01-23 21:45:56 +00:00
|
|
|
static fromJsonString(s) {
|
|
|
|
let obj;
|
|
|
|
try {
|
|
|
|
obj = JSON.parse(s);
|
|
|
|
obj = new ActivityPubObject(obj);
|
|
|
|
} catch (e) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
return obj;
|
|
|
|
}
|
|
|
|
|
|
|
|
isValid() {
|
2023-02-07 05:18:09 +00:00
|
|
|
//
|
|
|
|
// If @context is present, it must be valid;
|
|
|
|
// child objects generally inherit, so they may not have one
|
|
|
|
//
|
|
|
|
if (this[Context]) {
|
|
|
|
if (!this.isContextValid()) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const checkString = s => isString(s) && s.length > 1;
|
|
|
|
return checkString(this.id) && checkString(this.type);
|
|
|
|
}
|
|
|
|
|
|
|
|
isContextValid() {
|
|
|
|
if (Array.isArray(this[Context])) {
|
|
|
|
if (this[Context][0] === ActivityStreamsContext) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
} else if (isString(this[Context])) {
|
|
|
|
if (ActivityStreamsContext === this[Context]) {
|
|
|
|
return true;
|
|
|
|
}
|
2023-01-23 21:45:56 +00:00
|
|
|
}
|
2023-02-07 05:18:09 +00:00
|
|
|
|
2023-01-23 21:45:56 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2023-02-07 01:27:12 +00:00
|
|
|
setContext(context) {
|
|
|
|
if (!Array.isArray(context)) {
|
|
|
|
context = [context];
|
|
|
|
}
|
|
|
|
this['@context'] = context;
|
|
|
|
}
|
|
|
|
|
|
|
|
static makeObjectId(webServer, objectType) {
|
|
|
|
return Endpoints.objectId(webServer, objectType);
|
2023-01-23 21:45:56 +00:00
|
|
|
}
|
2023-02-09 04:32:54 +00:00
|
|
|
|
|
|
|
sendTo(inboxEndpoint, 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': ActivityStreamMediaType,
|
|
|
|
},
|
|
|
|
sign: {
|
|
|
|
key: privateKey,
|
|
|
|
keyId: Endpoints.actorId(webServer, fromUser) + '#main-key',
|
|
|
|
authorizationHeaderName: 'Signature',
|
|
|
|
headers: HttpSignatureSignHeaders,
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
|
|
|
const activityJson = JSON.stringify(this);
|
|
|
|
return postJson(inboxEndpoint, activityJson, reqOpts, cb);
|
|
|
|
}
|
2023-01-23 21:45:56 +00:00
|
|
|
};
|