2023-01-13 01:49:13 +00:00
|
|
|
const Activity = require('../activitypub/activity');
|
2023-01-12 05:37:09 +00:00
|
|
|
const Message = require('../message');
|
|
|
|
const { MessageScanTossModule } = require('../msg_scan_toss_module');
|
|
|
|
const { getServer } = require('../listening_server');
|
2023-01-13 01:26:44 +00:00
|
|
|
const Log = require('../logger').log;
|
2023-02-06 04:10:51 +00:00
|
|
|
const { WellKnownAreaTags, AddressFlavor } = require('../message_const');
|
|
|
|
const { Errors } = require('../enig_error');
|
|
|
|
const Collection = require('../activitypub/collection');
|
|
|
|
const Note = require('../activitypub/note');
|
2023-02-06 21:34:18 +00:00
|
|
|
const Endpoints = require('../activitypub/endpoint');
|
2023-02-06 04:10:51 +00:00
|
|
|
const { getAddressedToInfo } = require('../mail_util');
|
|
|
|
const { PublicCollectionId } = require('../activitypub/const');
|
|
|
|
const Actor = require('../activitypub/actor');
|
2023-01-13 01:26:44 +00:00
|
|
|
|
|
|
|
// deps
|
|
|
|
const async = require('async');
|
|
|
|
const _ = require('lodash');
|
2023-01-12 05:37:09 +00:00
|
|
|
|
|
|
|
exports.moduleInfo = {
|
|
|
|
name: 'ActivityPub',
|
|
|
|
desc: 'Provides ActivityPub scanner/tosser integration',
|
|
|
|
author: 'NuSkooler',
|
|
|
|
};
|
|
|
|
|
|
|
|
exports.getModule = class ActivityPubScannerTosser extends MessageScanTossModule {
|
|
|
|
constructor() {
|
|
|
|
super();
|
2023-01-13 01:26:44 +00:00
|
|
|
|
|
|
|
this.log = Log.child({ module: 'ActivityPubScannerTosser' });
|
2023-01-12 05:37:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
startup(cb) {
|
|
|
|
return cb(null);
|
|
|
|
}
|
|
|
|
|
|
|
|
shutdown(cb) {
|
|
|
|
return cb(null);
|
|
|
|
}
|
|
|
|
|
|
|
|
record(message) {
|
2023-01-13 01:38:42 +00:00
|
|
|
if (!this._shouldExportMessage(message)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2023-01-12 05:37:09 +00:00
|
|
|
if (!this._isEnabled()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2023-02-06 04:10:51 +00:00
|
|
|
//
|
|
|
|
// Private:
|
|
|
|
// Send Note directly to another remote Actor's inbox
|
|
|
|
//
|
|
|
|
// Public:
|
|
|
|
// - The original message may be addressed to a non-ActivityPub address
|
|
|
|
// or something like "All" or "Public"; In this case, ignore that entry
|
|
|
|
// - Additionally, we need to send to the local Actor's followers via their sharedInbox
|
|
|
|
//
|
|
|
|
// To achieve the above for Public, we'll collect the followers from the local
|
|
|
|
// user, query their unique shared inboxes's, update the Note's addressing,
|
|
|
|
// then deliver and store.
|
|
|
|
//
|
|
|
|
|
2023-01-13 01:26:44 +00:00
|
|
|
async.waterfall(
|
|
|
|
[
|
|
|
|
callback => {
|
2023-02-06 04:10:51 +00:00
|
|
|
Note.fromLocalMessage(message, this._webServer(), (err, noteInfo) => {
|
|
|
|
return callback(err, noteInfo);
|
|
|
|
});
|
|
|
|
},
|
|
|
|
(noteInfo, callback) => {
|
|
|
|
if (message.isPrivate()) {
|
|
|
|
if (!noteInfo.remoteActor) {
|
|
|
|
return callback(
|
|
|
|
Errors.UnexpectedState(
|
|
|
|
'Private messages should contain a remote Actor!'
|
|
|
|
)
|
|
|
|
);
|
|
|
|
}
|
2023-02-09 03:10:50 +00:00
|
|
|
return callback(null, noteInfo, [noteInfo.remoteActor.inbox]);
|
2023-02-06 04:10:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// public: we need to build a list of sharedInbox's
|
|
|
|
this._collectDeliveryEndpoints(
|
2023-01-13 01:26:44 +00:00
|
|
|
message,
|
2023-02-06 04:10:51 +00:00
|
|
|
noteInfo.fromUser,
|
|
|
|
(err, deliveryEndpoints) => {
|
|
|
|
return callback(err, noteInfo, deliveryEndpoints);
|
2023-01-23 21:45:56 +00:00
|
|
|
}
|
2023-01-13 01:26:44 +00:00
|
|
|
);
|
|
|
|
},
|
2023-02-06 04:10:51 +00:00
|
|
|
(noteInfo, deliveryEndpoints, callback) => {
|
2023-02-07 01:27:12 +00:00
|
|
|
const { note, fromUser, context } = noteInfo;
|
2023-02-06 04:10:51 +00:00
|
|
|
|
|
|
|
//
|
|
|
|
// Update the Note's addressing:
|
|
|
|
// - Private:
|
2023-02-10 03:36:11 +00:00
|
|
|
// to: Directly to addressed-to Actor inbox
|
|
|
|
//
|
2023-02-06 04:10:51 +00:00
|
|
|
// - Public:
|
|
|
|
// to: https://www.w3.org/ns/activitystreams#Public
|
|
|
|
// ... and the message.getRemoteToUser() value *if*
|
|
|
|
// the flavor is deemed ActivityPub
|
|
|
|
// cc: [sharedInboxEndpoints]
|
|
|
|
//
|
|
|
|
if (message.isPrivate()) {
|
2023-02-09 03:10:50 +00:00
|
|
|
note.to = deliveryEndpoints;
|
2023-02-06 04:10:51 +00:00
|
|
|
} else {
|
|
|
|
if (deliveryEndpoints.additionalTo) {
|
|
|
|
note.to = [
|
|
|
|
PublicCollectionId,
|
|
|
|
deliveryEndpoints.additionalTo,
|
|
|
|
];
|
|
|
|
} else {
|
|
|
|
note.to = PublicCollectionId;
|
|
|
|
}
|
|
|
|
note.cc = [
|
|
|
|
deliveryEndpoints.followers,
|
|
|
|
...deliveryEndpoints.sharedInboxes,
|
|
|
|
];
|
|
|
|
|
|
|
|
if (note.to.length < 2 && note.cc.length < 2) {
|
|
|
|
// If we only have a generic 'followers' endpoint, there is no where to send to
|
|
|
|
return callback(null, activity, fromUser);
|
|
|
|
}
|
|
|
|
}
|
2023-01-23 21:45:56 +00:00
|
|
|
|
|
|
|
const activity = Activity.makeCreate(
|
|
|
|
note.attributedTo,
|
2023-02-07 01:27:12 +00:00
|
|
|
note,
|
|
|
|
context
|
2023-01-23 21:45:56 +00:00
|
|
|
);
|
2023-01-12 05:37:09 +00:00
|
|
|
|
2023-02-09 03:10:50 +00:00
|
|
|
let allEndpoints = Array.isArray(deliveryEndpoints)
|
|
|
|
? deliveryEndpoints
|
|
|
|
: deliveryEndpoints.sharedInboxes;
|
2023-02-06 04:10:51 +00:00
|
|
|
if (deliveryEndpoints.additionalTo) {
|
|
|
|
allEndpoints.push(deliveryEndpoints.additionalTo);
|
|
|
|
}
|
|
|
|
allEndpoints = Array.from(new Set(allEndpoints)); // unique again
|
2023-01-23 21:45:56 +00:00
|
|
|
|
2023-02-06 04:10:51 +00:00
|
|
|
async.eachLimit(
|
|
|
|
allEndpoints,
|
|
|
|
4,
|
|
|
|
(inbox, nextInbox) => {
|
2023-03-18 20:58:37 +00:00
|
|
|
activity.sendTo(inbox, fromUser, (err, respBody, res) => {
|
|
|
|
if (err) {
|
|
|
|
this.log.warn(
|
|
|
|
{
|
|
|
|
inbox,
|
|
|
|
error: err.message,
|
|
|
|
},
|
|
|
|
'Failed to send "Note" Activity to Inbox'
|
|
|
|
);
|
|
|
|
} else if (
|
|
|
|
res.statusCode === 200 ||
|
|
|
|
res.statusCode === 202
|
|
|
|
) {
|
|
|
|
this.log.debug(
|
|
|
|
{ inbox, uuid: message.uuid },
|
|
|
|
'Message delivered to Inbox'
|
|
|
|
);
|
|
|
|
} else {
|
|
|
|
this.log.warn(
|
|
|
|
{
|
|
|
|
inbox,
|
|
|
|
statusCode: res.statusCode,
|
|
|
|
body: _.truncate(respBody, 128),
|
|
|
|
},
|
|
|
|
'Unexpected status code'
|
|
|
|
);
|
2023-02-06 04:10:51 +00:00
|
|
|
}
|
2023-03-18 20:58:37 +00:00
|
|
|
|
|
|
|
// If we can't send now, no harm, we'll record to the outbox
|
|
|
|
return nextInbox(null);
|
|
|
|
});
|
2023-02-06 04:10:51 +00:00
|
|
|
},
|
|
|
|
() => {
|
2023-02-06 04:17:57 +00:00
|
|
|
return callback(null, activity, fromUser, note);
|
2023-01-13 01:38:42 +00:00
|
|
|
}
|
|
|
|
);
|
|
|
|
},
|
2023-02-06 04:17:57 +00:00
|
|
|
(activity, fromUser, note, callback) => {
|
2023-01-22 18:02:45 +00:00
|
|
|
Collection.addOutboxItem(
|
|
|
|
fromUser,
|
|
|
|
activity,
|
|
|
|
message.isPrivate(),
|
2023-02-03 22:14:27 +00:00
|
|
|
false, // do not ignore dupes
|
2023-01-22 18:02:45 +00:00
|
|
|
(err, localId) => {
|
|
|
|
if (!err) {
|
|
|
|
this.log.debug(
|
2023-02-06 04:17:57 +00:00
|
|
|
{ localId, activityId: activity.id, noteId: note.id },
|
2023-01-22 18:02:45 +00:00
|
|
|
'Note Activity persisted to "outbox" collection"'
|
|
|
|
);
|
|
|
|
}
|
|
|
|
return callback(err, activity);
|
2023-01-13 01:26:44 +00:00
|
|
|
}
|
2023-01-22 18:02:45 +00:00
|
|
|
);
|
2023-01-13 01:26:44 +00:00
|
|
|
},
|
|
|
|
(activity, callback) => {
|
|
|
|
// mark exported
|
|
|
|
return message.persistMetaValue(
|
|
|
|
Message.WellKnownMetaCategories.System,
|
|
|
|
Message.SystemMetaNames.StateFlags0,
|
|
|
|
Message.StateFlags0.Exported.toString(),
|
|
|
|
err => {
|
|
|
|
return callback(err, activity);
|
|
|
|
}
|
|
|
|
);
|
|
|
|
},
|
|
|
|
(activity, callback) => {
|
|
|
|
// message -> Activity ID relation
|
|
|
|
return message.persistMetaValue(
|
|
|
|
Message.WellKnownMetaCategories.ActivityPub,
|
|
|
|
Message.ActivityPubPropertyNames.ActivityId,
|
|
|
|
activity.id,
|
|
|
|
err => {
|
|
|
|
return callback(err, activity);
|
|
|
|
}
|
|
|
|
);
|
|
|
|
},
|
2023-01-26 05:22:45 +00:00
|
|
|
(activity, callback) => {
|
|
|
|
return message.persistMetaValue(
|
|
|
|
Message.WellKnownMetaCategories.ActivityPub,
|
|
|
|
Message.ActivityPubPropertyNames.NoteId,
|
|
|
|
activity.object.id,
|
|
|
|
err => {
|
|
|
|
return callback(err, activity);
|
|
|
|
}
|
|
|
|
);
|
|
|
|
},
|
2023-01-13 01:26:44 +00:00
|
|
|
],
|
|
|
|
(err, activity) => {
|
2023-02-03 22:14:27 +00:00
|
|
|
// dupes aren't considered failure
|
2023-01-13 01:26:44 +00:00
|
|
|
if (err) {
|
2023-02-03 22:14:27 +00:00
|
|
|
if (err.code === 'SQLITE_CONSTRAINT') {
|
|
|
|
this.log.debug({ id: activity.id }, 'Ignoring duplicate');
|
|
|
|
} else {
|
|
|
|
this.log.error(
|
|
|
|
{ error: err.message, messageId: message.messageId },
|
|
|
|
'Failed to export message to ActivityPub'
|
|
|
|
);
|
|
|
|
}
|
2023-01-13 01:26:44 +00:00
|
|
|
} else {
|
2023-01-13 01:40:43 +00:00
|
|
|
this.log.info(
|
2023-02-09 03:10:50 +00:00
|
|
|
{ activityId: activity.id, noteId: activity.object.id },
|
|
|
|
'Note Activity published successfully'
|
2023-01-13 01:40:43 +00:00
|
|
|
);
|
2023-01-12 05:37:09 +00:00
|
|
|
}
|
2023-01-13 01:26:44 +00:00
|
|
|
}
|
|
|
|
);
|
2023-01-12 05:37:09 +00:00
|
|
|
}
|
|
|
|
|
2023-02-06 04:10:51 +00:00
|
|
|
_collectDeliveryEndpoints(message, localUser, cb) {
|
|
|
|
this._collectFollowersSharedInboxEndpoints(
|
|
|
|
localUser,
|
|
|
|
(err, endpoints, followersEndpoint) => {
|
|
|
|
if (err) {
|
|
|
|
return cb(err);
|
|
|
|
}
|
|
|
|
|
|
|
|
//
|
|
|
|
// Don't inspect the remote address/remote to
|
|
|
|
// Here; We already know this in a public
|
|
|
|
// area. Instead, see if the user typed in
|
|
|
|
// a reasonable AP address here. If so, we'll
|
|
|
|
// try to send directly to them as well.
|
|
|
|
//
|
|
|
|
const addrInfo = getAddressedToInfo(message.toUserName);
|
|
|
|
if (
|
|
|
|
!message.isPrivate() &&
|
|
|
|
AddressFlavor.ActivityPub === addrInfo.flavor
|
|
|
|
) {
|
|
|
|
Actor.fromId(addrInfo.remote, (err, actor) => {
|
|
|
|
if (err) {
|
|
|
|
return cb(err);
|
|
|
|
}
|
|
|
|
|
|
|
|
return cb(null, {
|
2023-02-09 04:32:54 +00:00
|
|
|
additionalTo: actor.inbox,
|
2023-02-06 04:10:51 +00:00
|
|
|
sharedInboxes: endpoints,
|
|
|
|
followers: followersEndpoint,
|
|
|
|
});
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
return cb(null, {
|
|
|
|
sharedInboxes: endpoints,
|
|
|
|
followers: followersEndpoint,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
_collectFollowersSharedInboxEndpoints(localUser, cb) {
|
2023-03-18 20:30:37 +00:00
|
|
|
const localFollowersEndpoint = Endpoints.followers(localUser);
|
2023-02-06 04:10:51 +00:00
|
|
|
|
|
|
|
Collection.followers(localFollowersEndpoint, 'all', (err, collection) => {
|
|
|
|
if (err) {
|
|
|
|
return cb(err);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!collection.orderedItems || collection.orderedItems.length < 1) {
|
|
|
|
// no followers :(
|
|
|
|
return cb(null, []);
|
|
|
|
}
|
|
|
|
|
|
|
|
async.mapLimit(
|
|
|
|
collection.orderedItems,
|
|
|
|
4,
|
|
|
|
(actorId, nextActorId) => {
|
|
|
|
Actor.fromId(actorId, (err, actor) => {
|
|
|
|
return nextActorId(err, actor);
|
|
|
|
});
|
|
|
|
},
|
|
|
|
(err, followerActors) => {
|
|
|
|
if (err) {
|
|
|
|
return cb(err);
|
|
|
|
}
|
|
|
|
|
|
|
|
const sharedInboxEndpoints = Array.from(
|
|
|
|
new Set(
|
|
|
|
followerActors
|
|
|
|
.map(actor => {
|
|
|
|
return _.get(actor, 'endpoints.sharedInbox');
|
|
|
|
})
|
|
|
|
.filter(inbox => inbox) // drop nulls
|
|
|
|
)
|
|
|
|
);
|
|
|
|
|
|
|
|
return cb(null, sharedInboxEndpoints, localFollowersEndpoint);
|
|
|
|
}
|
|
|
|
);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2023-01-12 05:37:09 +00:00
|
|
|
_isEnabled() {
|
|
|
|
// :TODO: check config to see if AP integration is enabled/etc.
|
|
|
|
return this._webServer();
|
|
|
|
}
|
|
|
|
|
|
|
|
_shouldExportMessage(message) {
|
|
|
|
//
|
|
|
|
// - Private messages: Must be ActivityPub flavor
|
|
|
|
// - Public messages: Must be in area mapped for ActivityPub import/export
|
|
|
|
//
|
|
|
|
if (
|
|
|
|
Message.AddressFlavor.ActivityPub === message.getAddressFlavor() &&
|
2023-01-13 01:38:42 +00:00
|
|
|
message.isPrivate()
|
2023-01-12 05:37:09 +00:00
|
|
|
) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2023-02-06 04:10:51 +00:00
|
|
|
// Public items do not need a specific 'to'; we'll record to the
|
|
|
|
// local Actor's outbox and send to any followers we know about
|
|
|
|
if (message.areaTag === WellKnownAreaTags.ActivityPubShared) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// :TODO: Implement the area mapping check for public 'groups'
|
2023-01-12 05:37:09 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
_exportToActivityPub(message, cb) {
|
|
|
|
return cb(null);
|
|
|
|
}
|
|
|
|
|
|
|
|
_webServer() {
|
|
|
|
// we have to lazy init
|
|
|
|
if (undefined === this.webServer) {
|
|
|
|
this.webServer = getServer('codes.l33t.enigma.web.server') || null;
|
|
|
|
}
|
|
|
|
|
|
|
|
return this.webServer ? this.webServer.instance : null;
|
|
|
|
}
|
|
|
|
};
|