From 4c0a341ccff707a056e48736a0e9527baac9ab7a Mon Sep 17 00:00:00 2001 From: syuilo Date: Mon, 26 Mar 2018 17:54:10 +0900 Subject: [PATCH] Implement packForAp --- src/api/models/user.ts | 51 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 51 insertions(+) diff --git a/src/api/models/user.ts b/src/api/models/user.ts index 372e2c5da..545747b50 100644 --- a/src/api/models/user.ts +++ b/src/api/models/user.ts @@ -262,6 +262,57 @@ export const pack = ( resolve(_user); }); +/** + * Pack a user for ActivityPub + * + * @param user target + * @return Packed user + */ +export const packForAp = ( + user: string | mongo.ObjectID | IUser +) => new Promise(async (resolve, reject) => { + + let _user: any; + + const fields = { + // something + }; + + // Populate the user if 'user' is ID + if (mongo.ObjectID.prototype.isPrototypeOf(user)) { + _user = await User.findOne({ + _id: user + }, { fields }); + } else if (typeof user === 'string') { + _user = await User.findOne({ + _id: new mongo.ObjectID(user) + }, { fields }); + } else { + _user = deepcopy(user); + } + + if (!_user) return reject('invalid user arg.'); + + resolve({ + "@context": ["https://www.w3.org/ns/activitystreams", { + "@language": "ja" + }], + "type": "Person", + "id": `${config.url}/${_user.username}`, + "following": `${config.url}/${_user.username}/following.json`, + "followers": `${config.url}/${_user.username}/followers.json`, + "liked": `${config.url}/${_user.username}/liked.json`, + "inbox": `${config.url}/${_user.username}/inbox.json`, + "outbox": `${config.url}/${_user.username}/feed.json`, + "preferredUsername": _user.username, + "name": _user.name, + "summary": _user.description, + "icon": [ + `${config.drive_url}/${_user.avatar_id}` + ] + }); +}); + /* function img(url) { return {