Skip to content

Commit

Permalink
Cleanup, DRY, logging
Browse files Browse the repository at this point in the history
  • Loading branch information
NuSkooler committed Jan 21, 2023
1 parent 9517b29 commit d9e4b66
Show file tree
Hide file tree
Showing 5 changed files with 133 additions and 101 deletions.
40 changes: 9 additions & 31 deletions core/activitypub/actor.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@ const ActivityPubSettings = require('./settings');

// deps
const _ = require('lodash');
const https = require('https');
const isString = require('lodash/isString');
const mimeTypes = require('mime-types');
const { getJson } = require('../http_util.js');

// https://www.w3.org/TR/activitypub/#actor-objects
module.exports = class Actor {
Expand Down Expand Up @@ -141,40 +141,18 @@ module.exports = class Actor {
};

// :TODO: use getJson()

https.get(url, { headers }, res => {
if (res.statusCode !== 200) {
return cb(Errors.Invalid(`Bad HTTP status code: ${res.statusCode}`));
}

const contentType = res.headers['content-type'];
if (
!_.isString(contentType) ||
!contentType.startsWith('application/activity+json')
) {
return cb(Errors.Invalid(`Invalid Content-Type: ${contentType}`));
getJson(url, { headers }, (err, actor) => {
if (err) {
return cb(err);
}

res.setEncoding('utf8');
let body = '';
res.on('data', data => {
body += data;
});
actor = new Actor(actor);

res.on('end', () => {
let actor;
try {
actor = Actor.fromJsonString(body);
} catch (e) {
return cb(e);
}

if (!actor.isValid()) {
return cb(Errors.Invalid('Invalid Actor'));
}
if (!actor.isValid()) {
return cb(Errors.Invalid('Invalid Actor'));
}

return cb(null, actor);
});
return cb(null, actor);
});
}

Expand Down
79 changes: 53 additions & 26 deletions core/activitypub/collection.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,42 @@ const { makeUserUrl } = require('./util');
const ActivityPubObject = require('./object');
const apDb = require('../database').dbs.activitypub;
const { getISOTimestampString } = require('../database');
const { isString } = require('lodash');

const { isString, get } = require('lodash');

module.exports = class Collection extends ActivityPubObject {
constructor(obj) {
super(obj);
}

static followers(owningUser, page, webServer, cb) {
return Collection.getOrdered(
'followers',
owningUser,
false,
page,
e => e.id,
webServer,
cb
);
}

static following(owningUser, page, webServer, cb) {
return Collection.getOrdered(
'following',
owningUser,
false,
page,
e => get(e, 'object.id'),
webServer,
cb
);
}

static addFollower(owningUser, followingActor, cb) {
return Collection.addToCollection('followers', owningUser, followingActor, cb);
}

static getOrdered(name, owningUser, includePrivate, page, mapper, webServer, cb) {
// :TODD: |includePrivate| handling
const followersUrl =
Expand All @@ -24,12 +53,22 @@ module.exports = class Collection extends ActivityPubObject {
return cb(err);
}

const obj = {
id: followersUrl,
type: 'OrderedCollection',
first: `${followersUrl}?page=1`,
totalItems: row.count,
};
let obj;
if (row.count > 0) {
obj = {
id: followersUrl,
type: 'OrderedCollection',
first: `${followersUrl}?page=1`,
totalItems: row.count,
};
} else {
obj = {
id: followersUrl,
type: 'OrderedCollection',
totalItems: 0,
orderedItems: [],
};
}

return cb(null, new Collection(obj));
}
Expand All @@ -48,7 +87,8 @@ module.exports = class Collection extends ActivityPubObject {
return cb(err);
}

if (mapper) {
entries = entries || [];
if (mapper && entries.length > 0) {
entries = entries.map(mapper);
}

Expand All @@ -65,35 +105,22 @@ module.exports = class Collection extends ActivityPubObject {
);
}

static followers(owningUser, page, webServer, cb) {
return Collection.getOrdered(
'followers',
owningUser,
false,
page,
e => e.id,
webServer,
cb
);
}

static addToCollection(name, owningUser, entry, cb) {
if (!isString(entry)) {
entry = JSON.stringify(entry);
}

apDb.run(
`INSERT INTO collection_entry (name, timestamp, user_id, entry_json)
VALUES (?, ?, ?, ?);`,
`INSERT OR IGNORE INTO collection_entry (name, timestamp, user_id, entry_json)
VALUES (?, ?, ?, ?);`,
[name, getISOTimestampString(), owningUser.userId, entry],
function res(err) {
// non-arrow for 'this' scope
if (err) {
return cb(err);
}
return cb(err, this.lastID);
}
);
}

static addFollower(owningUser, followingActor, cb) {
return Collection.addToCollection('followers', owningUser, followingActor, cb);
}
};
9 changes: 7 additions & 2 deletions core/database.js
Original file line number Diff line number Diff line change
Expand Up @@ -542,8 +542,13 @@ dbs.message.run(
);

dbs.activitypub.run(
`CREATE INDEX IF NOT EXISTS collection_entry_unique_index0
ON collection_entry (name, user_id, json_extract(entry_json, '$.id'))`
`CREATE INDEX IF NOT EXISTS collection_entry_by_user_index0
ON collection_entry (name, user_id);`
);

dbs.activitypub.run(
`CREATE UNIQUE INDEX IF NOT EXISTS collection_entry_unique_index0
ON collection_entry (name, user_id, json_extract(entry_json, '$.id'));`
);

return cb(null);
Expand Down
5 changes: 4 additions & 1 deletion core/servers/content/web.js
Original file line number Diff line number Diff line change
Expand Up @@ -334,7 +334,10 @@ exports.getModule = class WebServerModule extends ServerModule {
);
}

internalServerError(resp) {
internalServerError(resp, err) {
if (err) {
this.log.error({ error: err.message }, 'Internal server error');
}
return this.respondWithError(
resp,
500,
Expand Down
Loading

0 comments on commit d9e4b66

Please sign in to comment.