pl-api: infer group domain from url
Signed-off-by: nicole mikołajczyk <git@mkljczk.pl>
This commit is contained in:
@ -1,31 +1,13 @@
|
||||
import pick from 'lodash.pick';
|
||||
import * as v from 'valibot';
|
||||
|
||||
import { guessFqn } from '../utils/domain';
|
||||
|
||||
import { customEmojiSchema } from './custom-emoji';
|
||||
import { relationshipSchema } from './relationship';
|
||||
import { roleSchema } from './role';
|
||||
import { coerceObject, datetimeSchema, filteredArray } from './utils';
|
||||
|
||||
const getDomainFromURL = (account: Pick<Account, 'url'>): string => {
|
||||
try {
|
||||
const url = account.url;
|
||||
return new URL(url).host;
|
||||
} catch {
|
||||
return '';
|
||||
}
|
||||
};
|
||||
|
||||
const guessFqn = (account: Pick<Account, 'acct' | 'url'>): string => {
|
||||
const acct = account.acct;
|
||||
const [user, domain] = acct.split('@');
|
||||
|
||||
if (domain) {
|
||||
return acct;
|
||||
} else {
|
||||
return [user, getDomainFromURL(account)].join('@');
|
||||
}
|
||||
};
|
||||
|
||||
const filterBadges = (tags?: string[]) =>
|
||||
tags?.filter(tag => tag.startsWith('badge:')).map(tag => v.parse(roleSchema, { id: tag, name: tag.replace(/^badge:/, '') }));
|
||||
|
||||
|
||||
@ -1,5 +1,7 @@
|
||||
import * as v from 'valibot';
|
||||
|
||||
import { getDomainFromURL } from '../utils/domain';
|
||||
|
||||
import { customEmojiSchema } from './custom-emoji';
|
||||
import { groupRelationshipSchema } from './group-relationship';
|
||||
import { datetimeSchema, filteredArray } from './utils';
|
||||
@ -8,8 +10,11 @@ import { datetimeSchema, filteredArray } from './utils';
|
||||
* @category Schemas
|
||||
*/
|
||||
const groupSchema = v.pipe(v.any(), v.transform((group: any) => {
|
||||
const domain = getDomainFromURL(group);
|
||||
|
||||
if (group?.config) {
|
||||
return {
|
||||
domain,
|
||||
display_name: group.name,
|
||||
members_count: group.member_count,
|
||||
note: group.short_description,
|
||||
@ -25,7 +30,7 @@ const groupSchema = v.pipe(v.any(), v.transform((group: any) => {
|
||||
...group,
|
||||
};
|
||||
}
|
||||
return group;
|
||||
return { domain, ...group };
|
||||
}), v.object({
|
||||
avatar: v.fallback(v.string(), ''),
|
||||
avatar_static: v.fallback(v.string(), ''),
|
||||
|
||||
Reference in New Issue
Block a user