feat: #12924, parse incoming custom fields from remote users

This commit is contained in:
Julian Lam
2024-11-20 12:15:40 -05:00
parent be6e8b0f48
commit 0f351ba70d
2 changed files with 28 additions and 0 deletions

View File

@@ -68,6 +68,11 @@ Mocks.profile = async (actors, hostMap) => {
let bgColor = Array.prototype.reduce.call(preferredUsername, (cur, next) => cur + next.charCodeAt(), 0);
bgColor = iconBackgrounds[bgColor % iconBackgrounds.length];
// Add custom fields into user hash
const customFields = actor.attachment
.filter(attachment => attachment.type === 'PropertyValue')
.reduce((map, { name, value }) => map.set(name, value), new Map());
const payload = {
uid,
username: `${preferredUsername}@${hostname}`,
@@ -90,6 +95,7 @@ Mocks.profile = async (actors, hostMap) => {
inbox,
sharedInbox: endpoints ? endpoints.sharedInbox : null,
followersUrl: followers,
customFields: new URLSearchParams(customFields).toString(),
};
return payload;

View File

@@ -16,6 +16,7 @@ const categories = require('../../categories');
const posts = require('../../posts');
const activitypub = require('../../activitypub');
const flags = require('../../flags');
const slugify = require('../../slugify');
const relative_path = nconf.get('relative_path');
@@ -144,6 +145,27 @@ helpers.getUserDataByUserSlug = async function (userslug, callerUID, query = {})
};
helpers.getCustomUserFields = async function (userData) {
// Remote users' fields are serialized in hash
if (!utils.isNumber(userData.uid)) {
const customFields = await user.getUserField(userData.uid, 'customFields');
const fields = Array
.from(new URLSearchParams(customFields))
.reduce((memo, [name, value]) => {
memo.push({
key: slugify(name),
name,
value,
type: 'input-text',
'min-rep': '',
icon: 'fa-solid fa-circle-info',
});
return memo;
}, []);
return fields;
}
const keys = await db.getSortedSetRange('user-custom-fields', 0, -1);
const allFields = (await db.getObjects(keys.map(k => `user-custom-field:${k}`))).filter(Boolean);