Cleanup and placeholder

This commit is contained in:
Bryan Ashby 2023-01-24 18:11:28 -07:00
parent d7df066ab0
commit d5446cdb51
No known key found for this signature in database
GPG Key ID: C2C1B501E4EFD994
5 changed files with 90 additions and 6 deletions

View File

@ -4,6 +4,7 @@ const ActivityPubObject = require('./object');
const { Errors } = require('../enig_error'); const { Errors } = require('../enig_error');
const UserProps = require('../user_property'); const UserProps = require('../user_property');
const { postJson } = require('../http_util'); const { postJson } = require('../http_util');
const { getISOTimestampString } = require('../database');
// deps // deps
const _ = require('lodash'); const _ = require('lodash');
@ -45,6 +46,17 @@ module.exports = class Activity extends ActivityPubObject {
}); });
} }
static makeTombstone(obj) {
const deleted = getISOTimestampString();
return new Activity({
id: obj.id,
type: 'Tombstone',
deleted,
published: deleted,
updated: deleted,
});
}
sendTo(actorUrl, fromUser, webServer, cb) { sendTo(actorUrl, fromUser, webServer, cb) {
const privateKey = fromUser.getProperty(UserProps.PrivateActivityPubSigningKey); const privateKey = fromUser.getProperty(UserProps.PrivateActivityPubSigningKey);
if (_.isEmpty(privateKey)) { if (_.isEmpty(privateKey)) {

View File

@ -54,6 +54,17 @@ module.exports = class Collection extends ActivityPubObject {
); );
} }
static addFollowRequest(owningUser, requestingActor, cb) {
return Collection.addToCollection(
'follow_requests',
owningUser,
requestingActor.id,
requestingActor,
true,
cb
);
}
static outbox(owningUser, page, webServer, cb) { static outbox(owningUser, page, webServer, cb) {
return Collection.getOrdered( return Collection.getOrdered(
'outbox', 'outbox',
@ -207,6 +218,25 @@ module.exports = class Collection extends ActivityPubObject {
); );
} }
// https://www.w3.org/TR/activitypub/#update-activity-inbox
static updateCollectionEntry(collectionName, objectId, obj, cb) {
if (!isString(obj)) {
obj = JSON.stringify(obj);
}
// :TODO: The receiving server MUST take care to be sure that the Update is authorized to modify its object. At minimum, this may be done by ensuring that the Update and its object are of same origin.
apDb.run(
`UPDATE collection
SET obj_json = ?, timestamp = ?
WHERE name = ? AND obj_id = ?;`,
[obj, collectionName, getISOTimestampString(), objectId],
err => {
return cb(err);
}
);
}
static addToCollection(collectionName, owningUser, objectId, obj, isPrivate, cb) { static addToCollection(collectionName, owningUser, objectId, obj, isPrivate, cb) {
if (!isString(obj)) { if (!isString(obj)) {
obj = JSON.stringify(obj); obj = JSON.stringify(obj);

19
core/activitypub/const.js Normal file
View File

@ -0,0 +1,19 @@
exports.ActivityStreamsContext = 'https://www.w3.org/ns/activitystreams';
const WellKnownActivity = {
Create: 'Create',
Update: 'Update',
Delete: 'Delete',
Follow: 'Follow',
Accept: 'Accept',
Reject: 'Reject',
Add: 'Add',
Remove: 'Remove',
Like: 'Like',
Announce: 'Announce',
Undo: 'Undo',
};
exports.WellKnownActivity = WellKnownActivity;
const WellKnownActivityTypes = Object.values(WellKnownActivity);
exports.WellKnownActivityTypes = WellKnownActivityTypes;

View File

@ -203,6 +203,9 @@ exports.getModule = class ActivityPubWebHandler extends WebHandlerModule {
resp resp
); );
case 'Update':
return this._inboxUpdateRequestHandler(activity, req, resp);
case 'Undo': case 'Undo':
return this._inboxUndoRequestHandler(activity, req, resp); return this._inboxUndoRequestHandler(activity, req, resp);
@ -378,6 +381,8 @@ exports.getModule = class ActivityPubWebHandler extends WebHandlerModule {
return this.webServer.internalServerError(resp, err); return this.webServer.internalServerError(resp, err);
} }
// :TODO: support a template here
resp.writeHead(200, { 'Content-Type': 'text/html' }); resp.writeHead(200, { 'Content-Type': 'text/html' });
return resp.end(note.content); return resp.end(note.content);
}); });
@ -431,6 +436,11 @@ exports.getModule = class ActivityPubWebHandler extends WebHandlerModule {
_inboxFollowRequestHandler(activity, remoteActor, user, resp) { _inboxFollowRequestHandler(activity, remoteActor, user, resp) {
this.log.info({ user_id: user.userId, actor: activity.actor }, 'Follow request'); this.log.info({ user_id: user.userId, actor: activity.actor }, 'Follow request');
const ok = () => {
resp.writeHead(200, { 'Content-Type': 'text/html' });
return resp.end('');
};
// //
// If the user blindly accepts Followers, we can persist // If the user blindly accepts Followers, we can persist
// and send an 'Accept' now. Otherwise, we need to queue this // and send an 'Accept' now. Otherwise, we need to queue this
@ -440,16 +450,29 @@ exports.getModule = class ActivityPubWebHandler extends WebHandlerModule {
const activityPubSettings = ActivityPubSettings.fromUser(user); const activityPubSettings = ActivityPubSettings.fromUser(user);
if (!activityPubSettings.manuallyApproveFollowers) { if (!activityPubSettings.manuallyApproveFollowers) {
this._recordAcceptedFollowRequest(user, remoteActor, activity); this._recordAcceptedFollowRequest(user, remoteActor, activity);
return ok();
} else { } else {
// :TODO: queue the request Collection.addFollowRequest(user, remoteActor, err => {
} if (err) {
return this.internalServerError(resp, err);
}
resp.writeHead(200, { 'Content-Type': 'text/html' }); return ok();
return resp.end(''); });
}
}
_inboxUpdateRequestHandler(activity, req, resp) {
this.log.info({ actor: activity.actor }, 'Update Activity request');
return this.webServer.notImplemented(resp);
// Collection.updateCollectionEntry('inbox', activity.id, activity, err => {
// });
} }
_inboxUndoRequestHandler(activity, req, resp) { _inboxUndoRequestHandler(activity, req, resp) {
this.log.info({ actor: activity.actor }, 'Undo request'); this.log.info({ actor: activity.actor }, 'Undo Activity request');
const url = new URL(req.url, `https://${req.headers.host}`); const url = new URL(req.url, `https://${req.headers.host}`);
const accountName = this._accountNameFromUserPath(url, 'inbox'); const accountName = this._accountNameFromUserPath(url, 'inbox');

View File

@ -69,7 +69,7 @@
} }
] ]
} }
actionKeys: @reference: common.escToPrev actionKeys: @reference:common.escToPrev
} }
1: { 1: {
mci: { mci: {