2023-01-12 05:37:09 +00:00
|
|
|
const {
|
|
|
|
ActivityStreamsContext,
|
|
|
|
messageBodyToHtml,
|
|
|
|
selfUrl,
|
2023-01-13 06:19:52 +00:00
|
|
|
makeUserUrl,
|
|
|
|
} = require('./util');
|
2023-01-13 01:49:13 +00:00
|
|
|
const User = require('../user');
|
2023-01-13 06:19:52 +00:00
|
|
|
const Actor = require('./actor');
|
|
|
|
const { Errors } = require('../enig_error');
|
2023-01-13 01:49:13 +00:00
|
|
|
const { getISOTimestampString } = require('../database');
|
|
|
|
const UserProps = require('../user_property');
|
|
|
|
const { postJson } = require('../http_util');
|
2023-01-13 06:19:52 +00:00
|
|
|
const { getOutboxEntries } = require('./db');
|
|
|
|
const { WellKnownLocations } = require('../servers/content/web');
|
2023-01-12 05:37:09 +00:00
|
|
|
|
|
|
|
// deps
|
2023-01-13 06:19:52 +00:00
|
|
|
const { isString, isObject } = require('lodash');
|
|
|
|
const { v4: UUIDv4 } = require('uuid');
|
2023-01-12 05:37:09 +00:00
|
|
|
const async = require('async');
|
|
|
|
const _ = require('lodash');
|
2023-01-08 20:18:50 +00:00
|
|
|
|
|
|
|
module.exports = class Activity {
|
|
|
|
constructor(obj) {
|
2023-01-09 00:11:49 +00:00
|
|
|
this['@context'] = ActivityStreamsContext;
|
2023-01-08 20:18:50 +00:00
|
|
|
Object.assign(this, obj);
|
|
|
|
}
|
|
|
|
|
|
|
|
static get ActivityTypes() {
|
|
|
|
return [
|
|
|
|
'Create',
|
|
|
|
'Update',
|
|
|
|
'Delete',
|
|
|
|
'Follow',
|
|
|
|
'Accept',
|
|
|
|
'Reject',
|
|
|
|
'Add',
|
|
|
|
'Remove',
|
|
|
|
'Like',
|
|
|
|
'Announce',
|
|
|
|
'Undo',
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
static fromJson(json) {
|
|
|
|
const parsed = JSON.parse(json);
|
|
|
|
return new Activity(parsed);
|
|
|
|
}
|
|
|
|
|
|
|
|
isValid() {
|
|
|
|
if (
|
2023-01-09 00:11:49 +00:00
|
|
|
this['@context'] !== ActivityStreamsContext ||
|
2023-01-08 20:18:50 +00:00
|
|
|
!isString(this.id) ||
|
|
|
|
!isString(this.actor) ||
|
|
|
|
(!isString(this.object) && !isObject(this.object)) ||
|
|
|
|
!Activity.ActivityTypes.includes(this.type)
|
|
|
|
) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2023-01-13 01:26:44 +00:00
|
|
|
// :TODO: Additional validation
|
2023-01-08 20:18:50 +00:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
2023-01-09 00:11:49 +00:00
|
|
|
|
|
|
|
// https://www.w3.org/TR/activitypub/#accept-activity-inbox
|
|
|
|
static makeAccept(webServer, localActor, followRequest, id = null) {
|
2023-01-13 01:26:44 +00:00
|
|
|
id = id || Activity._makeFullId(webServer, 'accept');
|
2023-01-09 00:11:49 +00:00
|
|
|
|
|
|
|
return new Activity({
|
|
|
|
type: 'Accept',
|
|
|
|
actor: localActor,
|
|
|
|
object: followRequest, // previous request Activity
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2023-01-12 05:37:09 +00:00
|
|
|
static noteFromLocalMessage(webServer, message, cb) {
|
|
|
|
const localUserId = message.getLocalFromUserId();
|
|
|
|
if (!localUserId) {
|
|
|
|
return cb(Errors.UnexpectedState('Invalid user ID for local user!'));
|
|
|
|
}
|
|
|
|
|
|
|
|
async.waterfall(
|
|
|
|
[
|
|
|
|
callback => {
|
|
|
|
return User.getUser(localUserId, callback);
|
|
|
|
},
|
|
|
|
(localUser, callback) => {
|
|
|
|
const remoteActorAccount = message.getRemoteToUser();
|
|
|
|
if (!remoteActorAccount) {
|
|
|
|
return callback(
|
|
|
|
Errors.UnexpectedState(
|
|
|
|
'Message does not contain a remote address'
|
|
|
|
)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
const opts = {};
|
|
|
|
Actor.fromAccountName(
|
|
|
|
remoteActorAccount,
|
|
|
|
opts,
|
|
|
|
(err, remoteActor) => {
|
|
|
|
return callback(err, localUser, remoteActor);
|
|
|
|
}
|
|
|
|
);
|
|
|
|
},
|
|
|
|
(localUser, remoteActor, callback) => {
|
|
|
|
Actor.fromLocalUser(localUser, webServer, (err, localActor) => {
|
|
|
|
return callback(err, localUser, localActor, remoteActor);
|
|
|
|
});
|
|
|
|
},
|
|
|
|
(localUser, localActor, remoteActor, callback) => {
|
|
|
|
// we'll need the entire |activityId| as a linked reference later
|
2023-01-13 01:26:44 +00:00
|
|
|
const activityId = Activity._makeFullId(webServer, 'create');
|
2023-01-12 05:37:09 +00:00
|
|
|
|
|
|
|
const obj = {
|
|
|
|
'@context': ActivityStreamsContext,
|
|
|
|
id: activityId,
|
|
|
|
type: 'Create',
|
|
|
|
actor: localActor.id,
|
|
|
|
object: {
|
2023-01-13 01:26:44 +00:00
|
|
|
id: Activity._makeFullId(webServer, 'note'),
|
2023-01-12 05:37:09 +00:00
|
|
|
type: 'Note',
|
|
|
|
published: getISOTimestampString(message.modTimestamp),
|
|
|
|
attributedTo: localActor.id,
|
2023-01-13 06:19:52 +00:00
|
|
|
audience: [message.isPrivate() ? 'as:Private' : 'as:Public'],
|
2023-01-12 05:37:09 +00:00
|
|
|
// :TODO: inReplyto if this is a reply; we need this store in message meta.
|
|
|
|
|
|
|
|
content: messageBodyToHtml(message.message.trim()),
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
|
|
|
// :TODO: this probably needs to change quite a bit based on "groups"
|
|
|
|
// :TODO: verify we need both 'to' fields: https://socialhub.activitypub.rocks/t/problems-posting-to-mastodon-inbox/801/4
|
|
|
|
if (message.isPrivate()) {
|
2023-01-13 01:26:44 +00:00
|
|
|
//obj.to = remoteActor.id;
|
2023-01-12 05:37:09 +00:00
|
|
|
obj.object.to = remoteActor.id;
|
|
|
|
} else {
|
|
|
|
const publicInbox = `${ActivityStreamsContext}#Public`;
|
2023-01-13 01:26:44 +00:00
|
|
|
//obj.to = publicInbox;
|
2023-01-12 05:37:09 +00:00
|
|
|
obj.object.to = publicInbox;
|
|
|
|
}
|
|
|
|
|
|
|
|
const activity = new Activity(obj);
|
|
|
|
return callback(null, activity, localUser, remoteActor);
|
|
|
|
},
|
|
|
|
],
|
|
|
|
(err, activity, fromUser, remoteActor) => {
|
|
|
|
return cb(err, { activity, fromUser, remoteActor });
|
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2023-01-13 06:19:52 +00:00
|
|
|
static fromOutboxEntries(owningUser, webServer, cb) {
|
|
|
|
// :TODO: support paging
|
|
|
|
const getOpts = {
|
|
|
|
create: true, // items marked 'Create'
|
|
|
|
};
|
|
|
|
getOutboxEntries(owningUser, getOpts, (err, entries) => {
|
|
|
|
if (err) {
|
|
|
|
return cb(err);
|
|
|
|
}
|
|
|
|
|
|
|
|
const obj = {
|
|
|
|
'@context': ActivityStreamsContext,
|
|
|
|
// :TODO: makeOutboxUrl() and use elsewhere also
|
|
|
|
id: makeUserUrl(webServer, owningUser, '/ap/users') + '/outbox',
|
|
|
|
type: 'OrderedCollection',
|
|
|
|
totalItems: entries.length,
|
|
|
|
orderedItems: entries.map(e => {
|
|
|
|
return {
|
|
|
|
'@context': ActivityStreamsContext,
|
|
|
|
id: e.activity.id,
|
|
|
|
type: 'Create',
|
|
|
|
actor: e.activity.actor,
|
|
|
|
object: e.activity.object,
|
|
|
|
};
|
|
|
|
}),
|
|
|
|
};
|
|
|
|
|
|
|
|
return cb(null, new Activity(obj));
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2023-01-12 05:37:09 +00:00
|
|
|
sendTo(actorUrl, fromUser, webServer, cb) {
|
|
|
|
const privateKey = fromUser.getProperty(UserProps.PrivateKeyMain);
|
|
|
|
if (_.isEmpty(privateKey)) {
|
|
|
|
return cb(
|
|
|
|
Errors.MissingProperty(
|
|
|
|
`User "${fromUser.username}" is missing the '${UserProps.PrivateKeyMain}' property`
|
|
|
|
)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
const reqOpts = {
|
|
|
|
headers: {
|
|
|
|
'Content-Type': 'application/activity+json',
|
|
|
|
},
|
|
|
|
sign: {
|
|
|
|
// :TODO: Make a helper for this
|
|
|
|
key: privateKey,
|
|
|
|
keyId: selfUrl(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);
|
|
|
|
}
|
|
|
|
|
2023-01-13 06:19:52 +00:00
|
|
|
static _makeFullId(webServer, prefix) {
|
|
|
|
// e.g. http://some.host/_enig/ap/note/bf81a22e-cb3e-41c8-b114-21f375b61124
|
|
|
|
return webServer.buildUrl(
|
|
|
|
WellKnownLocations.Internal + `/ap/${prefix}/${UUIDv4()}`
|
|
|
|
);
|
2023-01-09 00:11:49 +00:00
|
|
|
}
|
2023-01-08 20:18:50 +00:00
|
|
|
};
|