2023-02-06 21:34:18 +00:00
|
|
|
const { parseTimestampOrNow } = require('./util');
|
|
|
|
const Endpoints = require('./endpoint');
|
2023-01-20 23:03:27 +00:00
|
|
|
const ActivityPubObject = require('./object');
|
|
|
|
const apDb = require('../database').dbs.activitypub;
|
|
|
|
const { getISOTimestampString } = require('../database');
|
2023-01-23 21:45:56 +00:00
|
|
|
const { Errors } = require('../enig_error.js');
|
2023-02-05 06:16:44 +00:00
|
|
|
const {
|
2023-02-27 04:29:07 +00:00
|
|
|
PublicCollectionId,
|
2023-02-05 06:16:44 +00:00
|
|
|
ActivityStreamMediaType,
|
2023-02-18 04:46:24 +00:00
|
|
|
Collections,
|
2023-02-20 23:01:16 +00:00
|
|
|
ActorCollectionId,
|
2023-02-05 06:16:44 +00:00
|
|
|
} = require('./const');
|
2023-01-28 18:55:31 +00:00
|
|
|
const UserProps = require('../user_property');
|
2023-02-05 06:16:44 +00:00
|
|
|
const { getJson } = require('../http_util');
|
2023-01-21 05:15:59 +00:00
|
|
|
|
2023-01-23 21:45:56 +00:00
|
|
|
// deps
|
2023-01-28 18:55:31 +00:00
|
|
|
const { isString } = require('lodash');
|
2023-03-15 03:58:03 +00:00
|
|
|
const Log = require('../logger').log;
|
2023-02-20 23:01:16 +00:00
|
|
|
const async = require('async');
|
2023-01-14 04:27:02 +00:00
|
|
|
|
2023-01-20 23:03:27 +00:00
|
|
|
module.exports = class Collection extends ActivityPubObject {
|
2023-01-14 04:27:02 +00:00
|
|
|
constructor(obj) {
|
2023-01-20 23:03:27 +00:00
|
|
|
super(obj);
|
2023-01-14 04:27:02 +00:00
|
|
|
}
|
|
|
|
|
2023-02-05 06:16:44 +00:00
|
|
|
static getRemoteCollectionStats(collectionUrl, cb) {
|
|
|
|
const headers = {
|
|
|
|
Accept: ActivityStreamMediaType,
|
|
|
|
};
|
2023-02-25 18:50:30 +00:00
|
|
|
|
2023-02-05 17:42:30 +00:00
|
|
|
getJson(
|
|
|
|
collectionUrl,
|
|
|
|
{ headers, validContentTypes: [ActivityStreamMediaType] },
|
|
|
|
(err, collection) => {
|
|
|
|
if (err) {
|
|
|
|
return cb(err);
|
|
|
|
}
|
2023-02-05 06:16:44 +00:00
|
|
|
|
2023-02-05 17:42:30 +00:00
|
|
|
collection = new Collection(collection);
|
|
|
|
if (!collection.isValid()) {
|
|
|
|
return cb(Errors.Invalid('Invalid Collection'));
|
|
|
|
}
|
2023-02-05 06:16:44 +00:00
|
|
|
|
2023-02-05 17:42:30 +00:00
|
|
|
const { totalItems, type, id, summary } = collection;
|
2023-02-05 06:16:44 +00:00
|
|
|
|
2023-02-05 17:42:30 +00:00
|
|
|
return cb(null, {
|
|
|
|
totalItems,
|
|
|
|
type,
|
|
|
|
id,
|
|
|
|
summary,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
);
|
2023-02-05 06:16:44 +00:00
|
|
|
}
|
|
|
|
|
2023-01-28 18:55:31 +00:00
|
|
|
static followers(collectionId, page, cb) {
|
|
|
|
return Collection.publicOrderedById(
|
2023-02-18 04:46:24 +00:00
|
|
|
Collections.Followers,
|
2023-01-28 18:55:31 +00:00
|
|
|
collectionId,
|
2023-01-21 05:15:59 +00:00
|
|
|
page,
|
|
|
|
e => e.id,
|
|
|
|
cb
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2023-01-28 18:55:31 +00:00
|
|
|
static following(collectionId, page, cb) {
|
|
|
|
return Collection.publicOrderedById(
|
2023-02-18 04:46:24 +00:00
|
|
|
Collections.Following,
|
2023-01-28 18:55:31 +00:00
|
|
|
collectionId,
|
2023-01-21 05:15:59 +00:00
|
|
|
page,
|
2023-01-28 18:55:31 +00:00
|
|
|
e => e.id,
|
2023-01-21 05:15:59 +00:00
|
|
|
cb
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2023-02-08 19:53:56 +00:00
|
|
|
static outbox(collectionId, page, cb) {
|
2023-02-18 04:46:24 +00:00
|
|
|
return Collection.publicOrderedById(
|
|
|
|
Collections.Outbox,
|
|
|
|
collectionId,
|
|
|
|
page,
|
|
|
|
null,
|
|
|
|
cb
|
|
|
|
);
|
2023-02-08 19:53:56 +00:00
|
|
|
}
|
|
|
|
|
2023-02-03 22:14:27 +00:00
|
|
|
static addFollower(owningUser, followingActor, webServer, ignoreDupes, cb) {
|
2023-02-06 21:34:18 +00:00
|
|
|
const collectionId = Endpoints.followers(webServer, owningUser);
|
2023-01-21 08:19:19 +00:00
|
|
|
return Collection.addToCollection(
|
2023-02-18 04:46:24 +00:00
|
|
|
Collections.Followers,
|
2023-01-21 08:19:19 +00:00
|
|
|
owningUser,
|
2023-01-28 18:55:31 +00:00
|
|
|
collectionId,
|
2023-02-05 17:42:30 +00:00
|
|
|
followingActor.id, // Actor following owningUser
|
2023-01-21 08:19:19 +00:00
|
|
|
followingActor,
|
2023-02-05 17:42:30 +00:00
|
|
|
false, // we'll check dynamically when queried
|
2023-02-03 22:14:27 +00:00
|
|
|
ignoreDupes,
|
2023-01-22 01:51:54 +00:00
|
|
|
cb
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2023-02-03 22:14:27 +00:00
|
|
|
static addFollowRequest(owningUser, requestingActor, webServer, ignoreDupes, cb) {
|
2023-01-28 18:55:31 +00:00
|
|
|
const collectionId =
|
2023-02-06 21:34:18 +00:00
|
|
|
Endpoints.makeUserUrl(webServer, owningUser) + 'follow-requests';
|
2023-01-25 01:11:28 +00:00
|
|
|
return Collection.addToCollection(
|
2023-02-18 04:46:24 +00:00
|
|
|
Collections.FollowRequests,
|
2023-01-25 01:11:28 +00:00
|
|
|
owningUser,
|
2023-01-28 18:55:31 +00:00
|
|
|
collectionId,
|
2023-02-05 17:42:30 +00:00
|
|
|
requestingActor.id, // Actor requesting to follow owningUser
|
2023-01-25 01:11:28 +00:00
|
|
|
requestingActor,
|
|
|
|
true,
|
2023-02-03 22:14:27 +00:00
|
|
|
ignoreDupes,
|
2023-01-25 01:11:28 +00:00
|
|
|
cb
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2023-02-05 17:42:30 +00:00
|
|
|
static addFollowing(owningUser, followingActor, webServer, ignoreDupes, cb) {
|
2023-02-06 21:34:18 +00:00
|
|
|
const collectionId = Endpoints.following(webServer, owningUser);
|
2023-02-05 17:42:30 +00:00
|
|
|
return Collection.addToCollection(
|
2023-02-18 04:46:24 +00:00
|
|
|
Collections.Following,
|
2023-02-05 17:42:30 +00:00
|
|
|
owningUser,
|
|
|
|
collectionId,
|
|
|
|
followingActor.id, // Actor owningUser is following
|
|
|
|
followingActor,
|
|
|
|
false, // we'll check dynamically when queried
|
|
|
|
ignoreDupes,
|
|
|
|
cb
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2023-02-03 22:14:27 +00:00
|
|
|
static addOutboxItem(owningUser, outboxItem, isPrivate, webServer, ignoreDupes, cb) {
|
2023-02-06 21:34:18 +00:00
|
|
|
const collectionId = Endpoints.outbox(webServer, owningUser);
|
2023-01-22 01:51:54 +00:00
|
|
|
return Collection.addToCollection(
|
2023-02-18 04:46:24 +00:00
|
|
|
Collections.Outbox,
|
2023-01-22 01:51:54 +00:00
|
|
|
owningUser,
|
2023-01-28 18:55:31 +00:00
|
|
|
collectionId,
|
2023-01-22 01:51:54 +00:00
|
|
|
outboxItem.id,
|
|
|
|
outboxItem,
|
2023-01-22 18:02:45 +00:00
|
|
|
isPrivate,
|
2023-02-03 22:14:27 +00:00
|
|
|
ignoreDupes,
|
2023-01-21 08:19:19 +00:00
|
|
|
cb
|
|
|
|
);
|
2023-01-21 05:15:59 +00:00
|
|
|
}
|
|
|
|
|
2023-02-03 22:14:27 +00:00
|
|
|
static addInboxItem(inboxItem, owningUser, webServer, ignoreDupes, cb) {
|
2023-02-06 21:34:18 +00:00
|
|
|
const collectionId = Endpoints.inbox(webServer, owningUser);
|
2023-01-25 04:40:12 +00:00
|
|
|
return Collection.addToCollection(
|
2023-02-18 04:46:24 +00:00
|
|
|
Collections.Inbox,
|
2023-01-25 04:40:12 +00:00
|
|
|
owningUser,
|
2023-01-28 18:55:31 +00:00
|
|
|
collectionId,
|
2023-01-25 04:40:12 +00:00
|
|
|
inboxItem.id,
|
|
|
|
inboxItem,
|
|
|
|
true,
|
2023-02-03 22:14:27 +00:00
|
|
|
ignoreDupes,
|
2023-01-25 04:40:12 +00:00
|
|
|
cb
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2023-02-05 05:55:11 +00:00
|
|
|
static addSharedInboxItem(inboxItem, ignoreDupes, cb) {
|
2023-01-22 20:51:32 +00:00
|
|
|
return Collection.addToCollection(
|
2023-02-18 04:46:24 +00:00
|
|
|
Collections.SharedInbox,
|
2023-01-28 18:55:31 +00:00
|
|
|
null, // N/A
|
2023-02-27 04:29:07 +00:00
|
|
|
PublicCollectionId,
|
2023-01-22 20:51:32 +00:00
|
|
|
inboxItem.id,
|
|
|
|
inboxItem,
|
|
|
|
false,
|
2023-02-03 22:14:27 +00:00
|
|
|
ignoreDupes,
|
2023-01-22 20:51:32 +00:00
|
|
|
cb
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2023-02-20 23:01:16 +00:00
|
|
|
// Actors is a special collection
|
|
|
|
static actor(actorIdOrSubject, cb) {
|
|
|
|
// We always store subjects prefixed with '@'
|
|
|
|
if (!/^https?:\/\//.test(actorIdOrSubject) && '@' !== actorIdOrSubject[0]) {
|
|
|
|
actorIdOrSubject = `@${actorIdOrSubject}`;
|
|
|
|
}
|
|
|
|
|
|
|
|
apDb.get(
|
|
|
|
`SELECT c.name, c.timestamp, c.owner_actor_id, c.is_private, c.object_json, m.meta_value
|
|
|
|
FROM collection c, collection_object_meta m
|
2023-02-21 00:09:34 +00:00
|
|
|
WHERE c.collection_id = ? AND c.name = ? AND m.object_id = c.object_id AND (c.object_id LIKE ? OR (m.meta_name = ? AND m.meta_value LIKE ?))
|
2023-02-20 23:01:16 +00:00
|
|
|
LIMIT 1;`,
|
|
|
|
[
|
|
|
|
ActorCollectionId,
|
|
|
|
Collections.Actors,
|
|
|
|
actorIdOrSubject,
|
|
|
|
'actor_subject',
|
|
|
|
actorIdOrSubject,
|
|
|
|
],
|
|
|
|
(err, row) => {
|
|
|
|
if (err) {
|
|
|
|
return cb(err);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!row) {
|
|
|
|
return cb(
|
|
|
|
Errors.DoesNotExist(`No Actor found for "${actorIdOrSubject}"`)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
const obj = ActivityPubObject.fromJsonString(row.object_json);
|
|
|
|
if (!obj) {
|
|
|
|
return cb(Errors.Invalid('Failed to parse Object JSON'));
|
|
|
|
}
|
|
|
|
|
|
|
|
const info = Collection._rowToObjectInfo(row);
|
|
|
|
if (row.meta_value) {
|
|
|
|
info.subject = row.meta_value;
|
|
|
|
} else {
|
|
|
|
info.subject = obj.id;
|
|
|
|
}
|
|
|
|
|
|
|
|
return cb(null, obj, info);
|
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
static addActor(actor, subject, cb) {
|
|
|
|
async.waterfall(
|
|
|
|
[
|
|
|
|
callback => {
|
|
|
|
return apDb.beginTransaction(callback);
|
|
|
|
},
|
|
|
|
(trans, callback) => {
|
|
|
|
trans.run(
|
|
|
|
`REPLACE INTO collection (collection_id, name, timestamp, owner_actor_id, object_id, object_json, is_private)
|
|
|
|
VALUES(?, ?, ?, ?, ?, ?, ?);`,
|
|
|
|
[
|
|
|
|
ActorCollectionId,
|
|
|
|
Collections.Actors,
|
|
|
|
getISOTimestampString(),
|
2023-02-27 04:29:07 +00:00
|
|
|
PublicCollectionId,
|
2023-02-20 23:01:16 +00:00
|
|
|
actor.id,
|
|
|
|
JSON.stringify(actor),
|
|
|
|
false,
|
|
|
|
],
|
|
|
|
err => {
|
|
|
|
return callback(err, trans);
|
|
|
|
}
|
|
|
|
);
|
|
|
|
},
|
|
|
|
(trans, callback) => {
|
|
|
|
trans.run(
|
|
|
|
`REPLACE INTO collection_object_meta (collection_id, name, object_id, meta_name, meta_value)
|
|
|
|
VALUES(?, ?, ?, ?, ?);`,
|
|
|
|
[
|
|
|
|
ActorCollectionId,
|
|
|
|
Collections.Actors,
|
|
|
|
actor.id,
|
|
|
|
'actor_subject',
|
|
|
|
subject,
|
|
|
|
],
|
|
|
|
err => {
|
|
|
|
return callback(err, trans);
|
|
|
|
}
|
|
|
|
);
|
|
|
|
},
|
|
|
|
],
|
|
|
|
(err, trans) => {
|
|
|
|
if (err) {
|
|
|
|
trans.rollback(err => {
|
|
|
|
return cb(err);
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
trans.commit(err => {
|
|
|
|
return cb(err);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2023-02-21 04:49:41 +00:00
|
|
|
static removeExpiredActors(maxAgeDays, cb) {
|
2023-02-20 23:01:16 +00:00
|
|
|
apDb.run(
|
|
|
|
`DELETE FROM collection
|
|
|
|
WHERE collection_id = ? AND name = ? AND DATETIME(timestamp, "+${maxAgeDays} days") > DATETIME("now");`,
|
|
|
|
[ActorCollectionId, Collections.Actors],
|
|
|
|
err => {
|
|
|
|
return cb(err);
|
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2023-02-18 04:46:24 +00:00
|
|
|
// Get Object(s) by ID; There may be multiples as they may be
|
|
|
|
// e.g. Actors belonging to multiple followers collections.
|
|
|
|
// This method also returns information about the objects
|
|
|
|
// and any items that can't be parsed
|
|
|
|
static objectsById(objectId, cb) {
|
|
|
|
apDb.all(
|
|
|
|
`SELECT name, timestamp, owner_actor_id, object_json, is_private
|
|
|
|
FROM collection
|
|
|
|
WHERE object_id = ?;`,
|
|
|
|
[objectId],
|
|
|
|
(err, rows) => {
|
|
|
|
if (err) {
|
|
|
|
return cb(err);
|
|
|
|
}
|
|
|
|
|
|
|
|
const results = (rows || []).map(r => {
|
|
|
|
const info = {
|
|
|
|
info: this._rowToObjectInfo(r),
|
|
|
|
object: ActivityPubObject.fromJsonString(r.object_json),
|
|
|
|
};
|
|
|
|
if (!info.object) {
|
|
|
|
info.raw = r.object_json;
|
|
|
|
}
|
|
|
|
return info;
|
|
|
|
});
|
|
|
|
|
|
|
|
return cb(null, results);
|
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
static ownedObjectByNameAndId(collectionName, owningUser, objectId, cb) {
|
|
|
|
const actorId = owningUser.getProperty(UserProps.ActivityPubActorId);
|
|
|
|
if (!actorId) {
|
|
|
|
return cb(
|
|
|
|
Errors.MissingProperty(
|
|
|
|
`User "${owningUser.username}" is missing property '${UserProps.ActivityPubActorId}'`
|
|
|
|
)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
apDb.get(
|
|
|
|
`SELECT name, timestamp, owner_actor_id, object_json, is_private
|
|
|
|
FROM collection
|
|
|
|
WHERE name = ? AND owner_actor_id = ? AND object_id = ?
|
|
|
|
LIMIT 1;`,
|
|
|
|
[collectionName, actorId, objectId],
|
|
|
|
(err, row) => {
|
|
|
|
if (err) {
|
|
|
|
return cb(err);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!row) {
|
|
|
|
return cb(null, null);
|
|
|
|
}
|
|
|
|
|
|
|
|
const obj = ActivityPubObject.fromJsonString(row.object_json);
|
|
|
|
if (!obj) {
|
|
|
|
return cb(Errors.Invalid('Failed to parse Object JSON'));
|
|
|
|
}
|
|
|
|
|
|
|
|
return cb(null, obj, Collection._rowToObjectInfo(row));
|
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
static objectByNameAndId(collectionName, objectId, cb) {
|
2023-02-05 05:55:11 +00:00
|
|
|
apDb.get(
|
|
|
|
`SELECT name, timestamp, owner_actor_id, object_json, is_private
|
|
|
|
FROM collection
|
|
|
|
WHERE name = ? AND object_id = ?
|
|
|
|
LIMIT 1;`,
|
2023-02-18 04:46:24 +00:00
|
|
|
[collectionName, objectId],
|
2023-02-05 05:55:11 +00:00
|
|
|
(err, row) => {
|
|
|
|
if (err) {
|
|
|
|
return cb(err);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!row) {
|
|
|
|
return cb(null, null);
|
|
|
|
}
|
2023-01-23 21:45:56 +00:00
|
|
|
|
2023-02-05 05:55:11 +00:00
|
|
|
const obj = ActivityPubObject.fromJsonString(row.object_json);
|
|
|
|
if (!obj) {
|
|
|
|
return cb(Errors.Invalid('Failed to parse Object JSON'));
|
|
|
|
}
|
|
|
|
|
|
|
|
return cb(null, obj, Collection._rowToObjectInfo(row));
|
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
static objectByEmbeddedId(objectId, cb) {
|
2023-01-23 21:45:56 +00:00
|
|
|
apDb.get(
|
2023-02-05 05:55:11 +00:00
|
|
|
`SELECT name, timestamp, owner_actor_id, object_json, is_private
|
2023-01-23 21:45:56 +00:00
|
|
|
FROM collection
|
2023-02-05 05:55:11 +00:00
|
|
|
WHERE json_extract(object_json, '$.object.id') = ?
|
|
|
|
LIMIT 1;`,
|
|
|
|
[objectId],
|
2023-01-23 21:45:56 +00:00
|
|
|
(err, row) => {
|
|
|
|
if (err) {
|
|
|
|
return cb(err);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!row) {
|
2023-02-05 05:55:11 +00:00
|
|
|
// no match
|
|
|
|
return cb(null, null);
|
2023-01-23 21:45:56 +00:00
|
|
|
}
|
|
|
|
|
2023-01-28 18:55:31 +00:00
|
|
|
const obj = ActivityPubObject.fromJsonString(row.object_json);
|
2023-01-23 21:45:56 +00:00
|
|
|
if (!obj) {
|
|
|
|
return cb(Errors.Invalid('Failed to parse Object JSON'));
|
|
|
|
}
|
|
|
|
|
2023-02-05 05:55:11 +00:00
|
|
|
return cb(null, obj, Collection._rowToObjectInfo(row));
|
2023-01-23 21:45:56 +00:00
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2023-01-28 18:55:31 +00:00
|
|
|
static publicOrderedById(collectionName, collectionId, page, mapper, cb) {
|
|
|
|
if (!page) {
|
|
|
|
return apDb.get(
|
|
|
|
`SELECT COUNT(collection_id) AS count
|
|
|
|
FROM collection
|
|
|
|
WHERE name = ? AND collection_id = ? AND is_private = FALSE;`,
|
|
|
|
[collectionName, collectionId],
|
|
|
|
(err, row) => {
|
|
|
|
if (err) {
|
|
|
|
return cb(err);
|
|
|
|
}
|
|
|
|
|
|
|
|
let obj;
|
|
|
|
if (row.count > 0) {
|
|
|
|
obj = {
|
|
|
|
id: collectionId,
|
|
|
|
type: 'OrderedCollection',
|
|
|
|
first: `${collectionId}?page=1`,
|
|
|
|
totalItems: row.count,
|
|
|
|
};
|
|
|
|
} else {
|
|
|
|
obj = {
|
|
|
|
id: collectionId,
|
|
|
|
type: 'OrderedCollection',
|
|
|
|
totalItems: 0,
|
|
|
|
orderedItems: [],
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
return cb(null, new Collection(obj));
|
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
// :TODO: actual paging...
|
|
|
|
apDb.all(
|
|
|
|
`SELECT object_json
|
|
|
|
FROM collection
|
|
|
|
WHERE name = ? AND collection_id = ? AND is_private = FALSE
|
|
|
|
ORDER BY timestamp;`,
|
|
|
|
[collectionName, collectionId],
|
|
|
|
(err, entries) => {
|
|
|
|
if (err) {
|
|
|
|
return cb(err);
|
|
|
|
}
|
|
|
|
|
2023-02-06 04:10:51 +00:00
|
|
|
try {
|
|
|
|
entries = (entries || []).map(e => JSON.parse(e.object_json));
|
|
|
|
} catch (e) {
|
|
|
|
Log.error(`Collection "${collectionId}" error: ${e.message}`);
|
|
|
|
}
|
|
|
|
|
2023-01-28 18:55:31 +00:00
|
|
|
if (mapper && entries.length > 0) {
|
|
|
|
entries = entries.map(mapper);
|
|
|
|
}
|
|
|
|
|
2023-02-06 04:10:51 +00:00
|
|
|
let obj;
|
|
|
|
if ('all' === page) {
|
|
|
|
obj = {
|
|
|
|
id: collectionId,
|
|
|
|
type: 'OrderedCollection',
|
|
|
|
totalItems: entries.length,
|
|
|
|
orderedItems: entries,
|
|
|
|
};
|
|
|
|
} else {
|
|
|
|
obj = {
|
|
|
|
id: `${collectionId}/page=${page}`,
|
|
|
|
type: 'OrderedCollectionPage',
|
|
|
|
totalItems: entries.length,
|
|
|
|
orderedItems: entries,
|
|
|
|
partOf: collectionId,
|
|
|
|
};
|
|
|
|
}
|
2023-01-28 18:55:31 +00:00
|
|
|
|
|
|
|
return cb(null, new Collection(obj));
|
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
static ownedOrderedByUser(
|
2023-01-23 21:45:56 +00:00
|
|
|
collectionName,
|
|
|
|
owningUser,
|
|
|
|
includePrivate,
|
|
|
|
page,
|
|
|
|
mapper,
|
|
|
|
webServer,
|
|
|
|
cb
|
|
|
|
) {
|
2023-01-22 01:51:54 +00:00
|
|
|
const privateQuery = includePrivate ? '' : ' AND is_private = FALSE';
|
2023-01-28 18:55:31 +00:00
|
|
|
const actorId = owningUser.getProperty(UserProps.ActivityPubActorId);
|
|
|
|
if (!actorId) {
|
|
|
|
return cb(
|
|
|
|
Errors.MissingProperty(
|
|
|
|
`User "${owningUser.username}" is missing property '${UserProps.ActivityPubActorId}'`
|
|
|
|
)
|
|
|
|
);
|
|
|
|
}
|
2023-01-22 01:51:54 +00:00
|
|
|
|
2023-02-05 17:42:30 +00:00
|
|
|
// e.g. http://somewhere.com/_enig/ap/users/NuSkooler/followers
|
2023-01-28 18:55:31 +00:00
|
|
|
const collectionId =
|
2023-02-06 21:34:18 +00:00
|
|
|
Endpoints.makeUserUrl(webServer, owningUser) + `/${collectionName}`;
|
2023-01-23 21:45:56 +00:00
|
|
|
|
2023-01-14 04:27:02 +00:00
|
|
|
if (!page) {
|
2023-01-20 23:03:27 +00:00
|
|
|
return apDb.get(
|
2023-01-28 18:55:31 +00:00
|
|
|
`SELECT COUNT(collection_id) AS count
|
2023-01-21 08:19:19 +00:00
|
|
|
FROM collection
|
2023-01-28 18:55:31 +00:00
|
|
|
WHERE owner_actor_id = ? AND name = ?${privateQuery};`,
|
|
|
|
[actorId, collectionName],
|
2023-01-20 23:03:27 +00:00
|
|
|
(err, row) => {
|
|
|
|
if (err) {
|
|
|
|
return cb(err);
|
|
|
|
}
|
2023-01-14 04:27:02 +00:00
|
|
|
|
2023-01-22 01:51:54 +00:00
|
|
|
//
|
|
|
|
// Mastodon for instance, will never follow up for the
|
|
|
|
// actual data from some Collections such as 'followers';
|
|
|
|
// Instead, they only use the |totalItems| to form an
|
|
|
|
// approximate follower count.
|
|
|
|
//
|
2023-01-21 05:15:59 +00:00
|
|
|
let obj;
|
|
|
|
if (row.count > 0) {
|
|
|
|
obj = {
|
2023-01-28 18:55:31 +00:00
|
|
|
id: collectionId,
|
2023-01-21 05:15:59 +00:00
|
|
|
type: 'OrderedCollection',
|
2023-01-28 18:55:31 +00:00
|
|
|
first: `${collectionId}?page=1`,
|
2023-01-21 05:15:59 +00:00
|
|
|
totalItems: row.count,
|
|
|
|
};
|
|
|
|
} else {
|
|
|
|
obj = {
|
2023-01-28 18:55:31 +00:00
|
|
|
id: collectionId,
|
2023-01-21 05:15:59 +00:00
|
|
|
type: 'OrderedCollection',
|
|
|
|
totalItems: 0,
|
|
|
|
orderedItems: [],
|
|
|
|
};
|
|
|
|
}
|
2023-01-14 04:27:02 +00:00
|
|
|
|
2023-01-20 23:03:27 +00:00
|
|
|
return cb(null, new Collection(obj));
|
|
|
|
}
|
|
|
|
);
|
2023-01-14 04:27:02 +00:00
|
|
|
}
|
|
|
|
|
2023-01-20 23:03:27 +00:00
|
|
|
// :TODO: actual paging...
|
|
|
|
apDb.all(
|
2023-01-28 18:55:31 +00:00
|
|
|
`SELECT object_json
|
2023-01-21 08:19:19 +00:00
|
|
|
FROM collection
|
2023-01-28 18:55:31 +00:00
|
|
|
WHERE owner_actor_id = ? AND name = ?${privateQuery}
|
2023-01-20 23:03:27 +00:00
|
|
|
ORDER BY timestamp;`,
|
2023-01-28 18:55:31 +00:00
|
|
|
[actorId, collectionName],
|
2023-01-20 23:03:27 +00:00
|
|
|
(err, entries) => {
|
|
|
|
if (err) {
|
|
|
|
return cb(err);
|
|
|
|
}
|
|
|
|
|
2023-01-21 05:15:59 +00:00
|
|
|
entries = entries || [];
|
|
|
|
if (mapper && entries.length > 0) {
|
2023-01-20 23:03:27 +00:00
|
|
|
entries = entries.map(mapper);
|
|
|
|
}
|
|
|
|
|
|
|
|
const obj = {
|
2023-01-28 18:55:31 +00:00
|
|
|
id: `${collectionId}/page=${page}`,
|
2023-01-20 23:03:27 +00:00
|
|
|
type: 'OrderedCollectionPage',
|
|
|
|
totalItems: entries.length,
|
|
|
|
orderedItems: entries,
|
2023-01-28 18:55:31 +00:00
|
|
|
partOf: collectionId,
|
2023-01-20 23:03:27 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
return cb(null, new Collection(obj));
|
2023-01-14 04:27:02 +00:00
|
|
|
}
|
2023-01-20 23:03:27 +00:00
|
|
|
);
|
|
|
|
}
|
2023-01-14 04:27:02 +00:00
|
|
|
|
2023-01-25 01:11:28 +00:00
|
|
|
// https://www.w3.org/TR/activitypub/#update-activity-inbox
|
|
|
|
static updateCollectionEntry(collectionName, objectId, obj, cb) {
|
|
|
|
if (!isString(obj)) {
|
|
|
|
obj = JSON.stringify(obj);
|
|
|
|
}
|
|
|
|
|
|
|
|
apDb.run(
|
|
|
|
`UPDATE collection
|
2023-01-28 18:55:31 +00:00
|
|
|
SET object_json = ?, timestamp = ?
|
|
|
|
WHERE name = ? AND object_id = ?;`,
|
2023-01-25 01:11:28 +00:00
|
|
|
[obj, collectionName, getISOTimestampString(), objectId],
|
|
|
|
err => {
|
|
|
|
return cb(err);
|
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2023-01-28 18:55:31 +00:00
|
|
|
static addToCollection(
|
|
|
|
collectionName,
|
|
|
|
owningUser,
|
|
|
|
collectionId,
|
|
|
|
objectId,
|
|
|
|
obj,
|
|
|
|
isPrivate,
|
2023-02-03 22:14:27 +00:00
|
|
|
ignoreDupes,
|
2023-01-28 18:55:31 +00:00
|
|
|
cb
|
|
|
|
) {
|
2023-01-21 08:19:19 +00:00
|
|
|
if (!isString(obj)) {
|
|
|
|
obj = JSON.stringify(obj);
|
2023-01-20 23:03:27 +00:00
|
|
|
}
|
2023-01-14 04:27:02 +00:00
|
|
|
|
2023-01-28 18:55:31 +00:00
|
|
|
let actorId;
|
|
|
|
if (owningUser) {
|
|
|
|
actorId = owningUser.getProperty(UserProps.ActivityPubActorId);
|
|
|
|
if (!actorId) {
|
|
|
|
return cb(
|
|
|
|
Errors.MissingProperty(
|
|
|
|
`User "${owningUser.username}" is missing property '${UserProps.ActivityPubActorId}'`
|
|
|
|
)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
} else {
|
2023-02-27 04:29:07 +00:00
|
|
|
actorId = PublicCollectionId;
|
2023-01-28 18:55:31 +00:00
|
|
|
}
|
|
|
|
|
2023-01-22 01:51:54 +00:00
|
|
|
isPrivate = isPrivate ? 1 : 0;
|
2023-01-28 18:55:31 +00:00
|
|
|
|
2023-01-20 23:03:27 +00:00
|
|
|
apDb.run(
|
2023-01-28 18:55:31 +00:00
|
|
|
`INSERT OR IGNORE INTO collection (name, timestamp, collection_id, owner_actor_id, object_id, object_json, is_private)
|
|
|
|
VALUES (?, ?, ?, ?, ?, ?, ?);`,
|
2023-01-23 21:45:56 +00:00
|
|
|
[
|
|
|
|
collectionName,
|
|
|
|
getISOTimestampString(),
|
2023-01-28 18:55:31 +00:00
|
|
|
collectionId,
|
|
|
|
actorId,
|
2023-01-23 21:45:56 +00:00
|
|
|
objectId,
|
|
|
|
obj,
|
|
|
|
isPrivate,
|
|
|
|
],
|
2023-01-20 23:03:27 +00:00
|
|
|
function res(err) {
|
|
|
|
// non-arrow for 'this' scope
|
2023-02-03 22:14:27 +00:00
|
|
|
if (err && 'SQLITE_CONSTRAINT' === err.code) {
|
|
|
|
if (ignoreDupes) {
|
2023-01-28 18:55:31 +00:00
|
|
|
err = null; // ignore
|
|
|
|
}
|
2023-01-21 05:15:59 +00:00
|
|
|
return cb(err);
|
|
|
|
}
|
2023-01-20 23:03:27 +00:00
|
|
|
return cb(err, this.lastID);
|
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|
2023-01-21 08:19:19 +00:00
|
|
|
|
2023-02-07 05:45:01 +00:00
|
|
|
static removeOwnedById(collectionName, owningUser, objectId, cb) {
|
2023-01-28 18:55:31 +00:00
|
|
|
const actorId = owningUser.getProperty(UserProps.ActivityPubActorId);
|
|
|
|
if (!actorId) {
|
|
|
|
return cb(
|
|
|
|
Errors.MissingProperty(
|
|
|
|
`User "${owningUser.username}" is missing property '${UserProps.ActivityPubActorId}'`
|
|
|
|
)
|
|
|
|
);
|
|
|
|
}
|
2023-01-21 08:19:19 +00:00
|
|
|
apDb.run(
|
|
|
|
`DELETE FROM collection
|
2023-01-28 18:55:31 +00:00
|
|
|
WHERE name = ? AND owner_actor_id = ? AND object_id = ?;`,
|
|
|
|
[collectionName, actorId, objectId],
|
2023-01-21 08:19:19 +00:00
|
|
|
err => {
|
|
|
|
return cb(err);
|
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|
2023-02-05 05:55:11 +00:00
|
|
|
|
2023-02-07 05:45:01 +00:00
|
|
|
static removeById(collectionName, objectId, cb) {
|
|
|
|
apDb.run(
|
|
|
|
`DELETE FROM collection
|
|
|
|
WHERE name = ? AND object_id = ?;`,
|
|
|
|
[collectionName, objectId],
|
|
|
|
err => {
|
|
|
|
return cb(err);
|
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2023-03-15 03:58:03 +00:00
|
|
|
static removeByMaxCount(collectionName, maxCount, cb) {
|
|
|
|
apDb.run(
|
|
|
|
`DELETE FROM collection
|
|
|
|
WHERE _rowid_ IN (
|
|
|
|
SELECT _rowid_
|
|
|
|
FROM collection
|
|
|
|
WHERE name = ?
|
|
|
|
ORDER BY _rowid_ DESC
|
|
|
|
LIMIT -1 OFFSET ${maxCount}
|
|
|
|
);`,
|
|
|
|
[maxCount],
|
|
|
|
function res(err) {
|
|
|
|
// non-arrow function for 'this'
|
|
|
|
Collection._removeByLogHelper(
|
|
|
|
collectionName,
|
|
|
|
'MaxCount',
|
|
|
|
err,
|
|
|
|
maxCount,
|
|
|
|
this.changes
|
|
|
|
);
|
|
|
|
return cb(err);
|
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
static removeByMaxAgeDays(collectionName, maxAgeDays, cb) {
|
|
|
|
apDb.run(
|
|
|
|
`DELETE FROM collection
|
|
|
|
WHERE name = ? AND timestamp < DATE('now', '-${maxAgeDays} days');`,
|
|
|
|
[maxAgeDays],
|
|
|
|
function res(err) {
|
|
|
|
// non-arrow function for 'this'
|
|
|
|
Collection._removeByLogHelper(
|
|
|
|
collectionName,
|
|
|
|
'MaxAgeDays',
|
|
|
|
err,
|
|
|
|
maxAgeDays,
|
|
|
|
this.changes
|
|
|
|
);
|
|
|
|
return cb(err);
|
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
static _removeByLogHelper(collectionName, type, err, value, deletedCount) {
|
|
|
|
if (err) {
|
|
|
|
Log.error(
|
|
|
|
{ collectionName, error: err.message, type, value },
|
|
|
|
'Error trimming collection'
|
|
|
|
);
|
|
|
|
} else {
|
|
|
|
Log.debug(
|
|
|
|
{ collectionName, type, value, deletedCount },
|
|
|
|
'Collection trimmed successfully'
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-02-05 05:55:11 +00:00
|
|
|
static _rowToObjectInfo(row) {
|
|
|
|
return {
|
|
|
|
name: row.name,
|
|
|
|
timestamp: parseTimestampOrNow(row.timestamp),
|
|
|
|
ownerActorId: row.owner_actor_id,
|
|
|
|
isPrivate: row.is_private,
|
|
|
|
};
|
|
|
|
}
|
2023-01-14 04:27:02 +00:00
|
|
|
};
|