Work on shifting code to generic collections, etc.
This commit is contained in:
parent
930308e07f
commit
9517b292a4
|
@ -11,7 +11,6 @@ const { getOutboxEntries } = require('./db');
|
|||
const { WellKnownLocations } = require('../servers/content/web');
|
||||
|
||||
// deps
|
||||
//const { isString, isObject } = require('lodash');
|
||||
const { v4: UUIDv4 } = require('uuid');
|
||||
const async = require('async');
|
||||
const _ = require('lodash');
|
||||
|
@ -30,22 +29,6 @@ module.exports = class Activity extends ActivityPubObject {
|
|||
return new Activity(parsed);
|
||||
}
|
||||
|
||||
// isValid() {
|
||||
// if (
|
||||
// this['@context'] !== ActivityStreamsContext ||
|
||||
// !isString(this.id) ||
|
||||
// !isString(this.actor) ||
|
||||
// (!isString(this.object) && !isObject(this.object)) ||
|
||||
// !Activity.ActivityTypes.includes(this.type)
|
||||
// ) {
|
||||
// return false;
|
||||
// }
|
||||
|
||||
// // :TODO: Additional validation
|
||||
|
||||
// return true;
|
||||
// }
|
||||
|
||||
// https://www.w3.org/TR/activitypub/#accept-activity-inbox
|
||||
static makeAccept(webServer, localActor, followRequest, id = null) {
|
||||
id = id || Activity._makeFullId(webServer, 'accept');
|
||||
|
|
|
@ -1,49 +1,99 @@
|
|||
const { makeUserUrl } = require('./util');
|
||||
const { ActivityStreamsContext } = require('./const');
|
||||
const { FollowerEntryStatus, getFollowerEntries } = require('./db');
|
||||
const ActivityPubObject = require('./object');
|
||||
const apDb = require('../database').dbs.activitypub;
|
||||
const { getISOTimestampString } = require('../database');
|
||||
const { isString } = require('lodash');
|
||||
|
||||
module.exports = class Collection {
|
||||
module.exports = class Collection extends ActivityPubObject {
|
||||
constructor(obj) {
|
||||
this['@context'] = ActivityStreamsContext;
|
||||
Object.assign(this, obj);
|
||||
super(obj);
|
||||
}
|
||||
|
||||
static getOrdered(name, owningUser, includePrivate, page, mapper, webServer, cb) {
|
||||
// :TODD: |includePrivate| handling
|
||||
const followersUrl =
|
||||
makeUserUrl(webServer, owningUser, '/ap/users/') + `/${name}`;
|
||||
if (!page) {
|
||||
return apDb.get(
|
||||
`SELECT COUNT(id) AS count
|
||||
FROM collection_entry
|
||||
WHERE name = ?;`,
|
||||
[name],
|
||||
(err, row) => {
|
||||
if (err) {
|
||||
return cb(err);
|
||||
}
|
||||
|
||||
const obj = {
|
||||
id: followersUrl,
|
||||
type: 'OrderedCollection',
|
||||
first: `${followersUrl}?page=1`,
|
||||
totalItems: row.count,
|
||||
};
|
||||
|
||||
return cb(null, new Collection(obj));
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
// :TODO: actual paging...
|
||||
apDb.all(
|
||||
`SELECT entry_json
|
||||
FROM collection_entry
|
||||
WHERE user_id = ? AND name = ?
|
||||
ORDER BY timestamp;`,
|
||||
[owningUser.userId, name],
|
||||
(err, entries) => {
|
||||
if (err) {
|
||||
return cb(err);
|
||||
}
|
||||
|
||||
if (mapper) {
|
||||
entries = entries.map(mapper);
|
||||
}
|
||||
|
||||
const obj = {
|
||||
id: `${followersUrl}/page=${page}`,
|
||||
type: 'OrderedCollectionPage',
|
||||
totalItems: entries.length,
|
||||
orderedItems: entries,
|
||||
partOf: followersUrl,
|
||||
};
|
||||
|
||||
return cb(null, new Collection(obj));
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
static followers(owningUser, page, webServer, cb) {
|
||||
if (!page) {
|
||||
const followersUrl =
|
||||
makeUserUrl(webServer, owningUser, '/ap/users/') + '/followers';
|
||||
return Collection.getOrdered(
|
||||
'followers',
|
||||
owningUser,
|
||||
false,
|
||||
page,
|
||||
e => e.id,
|
||||
webServer,
|
||||
cb
|
||||
);
|
||||
}
|
||||
|
||||
const obj = {
|
||||
id: followersUrl,
|
||||
type: 'OrderedCollection',
|
||||
first: `${followersUrl}?page=1`,
|
||||
totalItems: 1,
|
||||
};
|
||||
|
||||
return cb(null, new Collection(obj));
|
||||
static addToCollection(name, owningUser, entry, cb) {
|
||||
if (!isString(entry)) {
|
||||
entry = JSON.stringify(entry);
|
||||
}
|
||||
|
||||
// :TODO: actually support paging...
|
||||
page = parseInt(page);
|
||||
const getOpts = {
|
||||
status: FollowerEntryStatus.Accepted,
|
||||
};
|
||||
getFollowerEntries(owningUser, getOpts, (err, followers) => {
|
||||
if (err) {
|
||||
return cb(err);
|
||||
apDb.run(
|
||||
`INSERT INTO collection_entry (name, timestamp, user_id, entry_json)
|
||||
VALUES (?, ?, ?, ?);`,
|
||||
[name, getISOTimestampString(), owningUser.userId, entry],
|
||||
function res(err) {
|
||||
// non-arrow for 'this' scope
|
||||
return cb(err, this.lastID);
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
const baseId = makeUserUrl(webServer, owningUser, '/ap/users') + '/followers';
|
||||
|
||||
const obj = {
|
||||
id: `${baseId}/page=${page}`,
|
||||
type: 'OrderedCollectionPage',
|
||||
totalItems: followers.length,
|
||||
orderedItems: followers,
|
||||
partOf: baseId,
|
||||
};
|
||||
|
||||
return cb(null, new Collection(obj));
|
||||
});
|
||||
static addFollower(owningUser, followingActor, cb) {
|
||||
return Collection.addToCollection('followers', owningUser, followingActor, cb);
|
||||
}
|
||||
};
|
||||
|
|
|
@ -2,8 +2,6 @@ const apDb = require('../database').dbs.activitypub;
|
|||
|
||||
exports.persistToOutbox = persistToOutbox;
|
||||
exports.getOutboxEntries = getOutboxEntries;
|
||||
exports.persistFollower = persistFollower;
|
||||
exports.getFollowerEntries = getFollowerEntries;
|
||||
|
||||
const FollowerEntryStatus = {
|
||||
Invalid: 0, // Invalid
|
||||
|
@ -65,36 +63,3 @@ function getOutboxEntries(owningUser, options, cb) {
|
|||
}
|
||||
);
|
||||
}
|
||||
|
||||
function persistFollower(localUser, remoteActor, options, cb) {
|
||||
const status = options.status || FollowerEntryStatus.Requested;
|
||||
|
||||
apDb.run(
|
||||
`INSERT OR IGNORE INTO followers (user_id, follower_id, status)
|
||||
VALUES (?, ?, ?);`,
|
||||
[localUser.userId, remoteActor.id, status],
|
||||
function res(err) {
|
||||
// non-arrow for 'this' scope
|
||||
return cb(err, this.lastID);
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
function getFollowerEntries(localUser, options, cb) {
|
||||
const status = options.status || FollowerEntryStatus.Accepted;
|
||||
|
||||
apDb.all(
|
||||
`SELECT follower_id
|
||||
FROM followers
|
||||
WHERE user_id = ? AND status = ?;`,
|
||||
[localUser.userId, status],
|
||||
(err, rows) => {
|
||||
if (err) {
|
||||
return cb(err);
|
||||
}
|
||||
|
||||
const entries = rows.map(r => r.follower_id);
|
||||
return cb(null, entries);
|
||||
}
|
||||
);
|
||||
}
|
||||
|
|
|
@ -532,16 +532,20 @@ dbs.message.run(
|
|||
);
|
||||
|
||||
dbs.activitypub.run(
|
||||
`CREATE TABLE IF NOT EXISTS followers (
|
||||
id INTEGER PRIMARY KEY, -- Local ID
|
||||
user_id INTEGER NOT NULL, -- Local user ID
|
||||
follower_id VARCHAR NOT NULL, -- Actor ID of follower
|
||||
status INTEGER NOT NULL, -- Status: See FollowerEntryStatus
|
||||
|
||||
UNIQUE(user_id, follower_id)
|
||||
`CREATE TABLE IF NOT EXISTS collection_entry (
|
||||
id INTEGER PRIMARY KEY, -- Auto-generated key
|
||||
name VARCHAR NOT NULL, -- examples: followers, follows, ...
|
||||
timestamp DATETIME NOT NULL, -- Timestamp in which this entry was created
|
||||
user_id INTEGER NOT NULL, -- Local, owning user ID
|
||||
entry_json VARCHAR NOT NULL -- Varies by collection
|
||||
);`
|
||||
);
|
||||
|
||||
dbs.activitypub.run(
|
||||
`CREATE INDEX IF NOT EXISTS collection_entry_unique_index0
|
||||
ON collection_entry (name, user_id, json_extract(entry_json, '$.id'))`
|
||||
);
|
||||
|
||||
return cb(null);
|
||||
},
|
||||
};
|
||||
|
|
|
@ -10,7 +10,6 @@ const Activity = require('../../../activitypub/activity');
|
|||
const ActivityPubSettings = require('../../../activitypub/settings');
|
||||
const Actor = require('../../../activitypub/actor');
|
||||
const Collection = require('../../../activitypub/collection');
|
||||
const { persistFollower, FollowerEntryStatus } = require('../../../activitypub/db');
|
||||
|
||||
// deps
|
||||
const _ = require('lodash');
|
||||
|
@ -383,10 +382,7 @@ exports.getModule = class ActivityPubWebHandler extends WebHandlerModule {
|
|||
async.series(
|
||||
[
|
||||
callback => {
|
||||
const persistOpts = {
|
||||
status: FollowerEntryStatus.Accepted,
|
||||
};
|
||||
return persistFollower(localUser, remoteActor, persistOpts, callback);
|
||||
return Collection.addFollower(localUser, remoteActor, callback);
|
||||
},
|
||||
callback => {
|
||||
Actor.fromLocalUser(localUser, this.webServer, (err, localActor) => {
|
||||
|
@ -444,6 +440,7 @@ exports.getModule = class ActivityPubWebHandler extends WebHandlerModule {
|
|||
],
|
||||
err => {
|
||||
if (err) {
|
||||
// :TODO: move this request to the "Request queue" for the user to try later
|
||||
this.log.error(
|
||||
{ error: err.message },
|
||||
'Failed processing Follow request'
|
||||
|
|
Loading…
Reference in New Issue