From 8d4fd9c0f8d0b77ee41d370bf34f819a0984fe5f Mon Sep 17 00:00:00 2001 From: Julian Lam Date: Thu, 4 Jan 2024 14:04:34 -0500 Subject: [PATCH] refactor: move profile mocking logic to discrete method in main activitypub lib --- src/activitypub/index.js | 35 +++++++++++++++++++++++++ src/controllers/activitypub/profiles.js | 33 ++--------------------- 2 files changed, 37 insertions(+), 31 deletions(-) diff --git a/src/activitypub/index.js b/src/activitypub/index.js index 17abe9b282..cad7c5efaf 100644 --- a/src/activitypub/index.js +++ b/src/activitypub/index.js @@ -52,6 +52,41 @@ ActivityPub.getActor = async (input) => { return actor; }; +ActivityPub.mockProfile = async (callerUid, actor) => { + // Accepts an actor object; the output of getActor() + const uid = actor.id; + const { preferredUsername, published, icon, image, name, summary, hostname } = actor; + const isFollowing = await db.isSortedSetMember(`followingRemote:${callerUid}`, uid); + + let picture; + if (icon) { + picture = typeof icon === 'string' ? icon : icon.url; + } + const iconBackgrounds = await user.getIconBackgrounds(); + let bgColor = Array.prototype.reduce.call(preferredUsername, (cur, next) => cur + next.charCodeAt(), 0); + bgColor = iconBackgrounds[bgColor % iconBackgrounds.length]; + + const payload = { + uid, + username: `${preferredUsername}@${hostname}`, + userslug: `${preferredUsername}@${hostname}`, + fullname: name, + joindate: new Date(published).getTime(), + picture, + 'icon:text': (preferredUsername[0] || '').toUpperCase(), + 'icon:bgColor': bgColor, + uploadedpicture: undefined, + 'cover:url': !image || typeof image === 'string' ? image : image.url, + 'cover:position': '50% 50%', + aboutme: summary, + aboutmeParsed: summary, + + isFollowing, + }; + + return payload; +}; + ActivityPub.resolveInboxes = async ids => await Promise.all(ids.map(async (id) => { const actor = await ActivityPub.getActor(id); return actor.inbox; diff --git a/src/controllers/activitypub/profiles.js b/src/controllers/activitypub/profiles.js index 738902998c..45a0280c66 100644 --- a/src/controllers/activitypub/profiles.js +++ b/src/controllers/activitypub/profiles.js @@ -1,8 +1,6 @@ 'use strict'; -const db = require('../../database'); -const user = require('../../user'); -const { getActor } = require('../../activitypub'); +const { getActor, mockProfile } = require('../../activitypub'); const controller = module.exports; @@ -12,34 +10,7 @@ controller.get = async function (req, res, next) { if (!actor) { return next(); } - const { preferredUsername, published, icon, image, name, summary, hostname } = actor; - const isFollowing = await db.isSortedSetMember(`followingRemote:${req.uid}`, uid); - - let picture; - if (icon) { - picture = typeof icon === 'string' ? icon : icon.url; - } - const iconBackgrounds = await user.getIconBackgrounds(); - let bgColor = Array.prototype.reduce.call(preferredUsername, (cur, next) => cur + next.charCodeAt(), 0); - bgColor = iconBackgrounds[bgColor % iconBackgrounds.length]; - - const payload = { - uid, - username: `${preferredUsername}@${hostname}`, - userslug: `${preferredUsername}@${hostname}`, - fullname: name, - joindate: new Date(published).getTime(), - picture, - 'icon:text': (preferredUsername[0] || '').toUpperCase(), - 'icon:bgColor': bgColor, - uploadedpicture: undefined, - 'cover:url': !image || typeof image === 'string' ? image : image.url, - 'cover:position': '50% 50%', - aboutme: summary, - aboutmeParsed: summary, - - isFollowing, - }; + const payload = await mockProfile(req.uid, actor); res.render('account/profile', payload); };