2023-01-20 05:31:14 +00:00
|
|
|
const { makeUserUrl } = require('./util');
|
2023-01-20 23:03:27 +00:00
|
|
|
const ActivityPubObject = require('./object');
|
|
|
|
const apDb = require('../database').dbs.activitypub;
|
|
|
|
const { getISOTimestampString } = require('../database');
|
2023-01-21 05:15:59 +00:00
|
|
|
|
|
|
|
const { isString, get } = require('lodash');
|
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-01-21 05:15:59 +00:00
|
|
|
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) {
|
2023-01-21 08:19:19 +00:00
|
|
|
return Collection.addToCollection(
|
|
|
|
'followers',
|
|
|
|
owningUser,
|
|
|
|
followingActor.id,
|
|
|
|
followingActor,
|
|
|
|
cb
|
|
|
|
);
|
2023-01-21 05:15:59 +00:00
|
|
|
}
|
|
|
|
|
2023-01-20 23:03:27 +00:00
|
|
|
static getOrdered(name, owningUser, includePrivate, page, mapper, webServer, cb) {
|
|
|
|
// :TODD: |includePrivate| handling
|
|
|
|
const followersUrl =
|
|
|
|
makeUserUrl(webServer, owningUser, '/ap/users/') + `/${name}`;
|
2023-01-14 04:27:02 +00:00
|
|
|
if (!page) {
|
2023-01-20 23:03:27 +00:00
|
|
|
return apDb.get(
|
|
|
|
`SELECT COUNT(id) AS count
|
2023-01-21 08:19:19 +00:00
|
|
|
FROM collection
|
2023-01-20 23:03:27 +00:00
|
|
|
WHERE name = ?;`,
|
|
|
|
[name],
|
|
|
|
(err, row) => {
|
|
|
|
if (err) {
|
|
|
|
return cb(err);
|
|
|
|
}
|
2023-01-14 04:27:02 +00:00
|
|
|
|
2023-01-21 05:15:59 +00:00
|
|
|
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: [],
|
|
|
|
};
|
|
|
|
}
|
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-21 08:19:19 +00:00
|
|
|
`SELECT obj_json
|
|
|
|
FROM collection
|
2023-01-20 23:03:27 +00:00
|
|
|
WHERE user_id = ? AND name = ?
|
|
|
|
ORDER BY timestamp;`,
|
|
|
|
[owningUser.userId, name],
|
|
|
|
(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 = {
|
|
|
|
id: `${followersUrl}/page=${page}`,
|
|
|
|
type: 'OrderedCollectionPage',
|
|
|
|
totalItems: entries.length,
|
|
|
|
orderedItems: entries,
|
|
|
|
partOf: followersUrl,
|
|
|
|
};
|
|
|
|
|
|
|
|
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-21 08:19:19 +00:00
|
|
|
static addToCollection(name, owningUser, objectId, obj, cb) {
|
|
|
|
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-20 23:03:27 +00:00
|
|
|
apDb.run(
|
2023-01-21 08:19:19 +00:00
|
|
|
`INSERT OR IGNORE INTO collection (name, timestamp, user_id, obj_id, obj_json)
|
|
|
|
VALUES (?, ?, ?, ?, ?);`,
|
|
|
|
[name, getISOTimestampString(), owningUser.userId, objectId, obj],
|
2023-01-20 23:03:27 +00:00
|
|
|
function res(err) {
|
|
|
|
// non-arrow for 'this' scope
|
2023-01-21 05:15:59 +00:00
|
|
|
if (err) {
|
|
|
|
return cb(err);
|
|
|
|
}
|
2023-01-20 23:03:27 +00:00
|
|
|
return cb(err, this.lastID);
|
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|
2023-01-21 08:19:19 +00:00
|
|
|
|
|
|
|
static remoteFromCollectionById(name, owningUser, objectId, cb) {
|
|
|
|
apDb.run(
|
|
|
|
`DELETE FROM collection
|
|
|
|
WHERE user_id = ? AND name = ? AND obj_id = ?;`,
|
|
|
|
[owningUser.userId, name, objectId],
|
|
|
|
err => {
|
|
|
|
return cb(err);
|
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|
2023-01-14 04:27:02 +00:00
|
|
|
};
|