2022-12-11 12:37:00 -08:00
|
|
|
import escapeTextContentForBrowser from 'escape-html';
|
|
|
|
|
2023-02-23 08:42:31 -08:00
|
|
|
import emojify from 'soapbox/features/emoji';
|
2022-12-11 12:37:00 -08:00
|
|
|
import { unescapeHTML } from 'soapbox/utils/html';
|
|
|
|
import { makeEmojiMap } from 'soapbox/utils/normalizers';
|
|
|
|
|
2024-08-11 01:48:58 -07:00
|
|
|
import type { Group as BaseGroup } from 'pl-api';
|
2022-12-11 12:37:00 -08:00
|
|
|
|
2024-08-11 01:48:58 -07:00
|
|
|
const getDomainFromURL = (group: Pick<BaseGroup, 'url'>): string => {
|
2022-12-11 12:37:00 -08:00
|
|
|
try {
|
2024-08-11 01:48:58 -07:00
|
|
|
const url = group.url;
|
2022-12-11 12:37:00 -08:00
|
|
|
return new URL(url).host;
|
|
|
|
} catch {
|
|
|
|
return '';
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2024-08-11 01:48:58 -07:00
|
|
|
const normalizeGroup = (group: BaseGroup) => {
|
|
|
|
const missingAvatar = require('soapbox/assets/images/avatar-missing.png');
|
|
|
|
const missingHeader = require('soapbox/assets/images/header-missing.png');
|
2022-12-11 12:37:00 -08:00
|
|
|
|
2024-08-11 01:48:58 -07:00
|
|
|
const domain = getDomainFromURL(group);
|
|
|
|
const note = group.note === '<p></p>' ? '' : group.note;
|
2022-12-11 12:37:00 -08:00
|
|
|
|
2024-08-11 01:48:58 -07:00
|
|
|
const emojiMap = makeEmojiMap(group.emojis);
|
2022-12-11 12:37:00 -08:00
|
|
|
|
2024-08-11 01:48:58 -07:00
|
|
|
return {
|
|
|
|
...group,
|
|
|
|
avatar: group.avatar || group.avatar_static || missingAvatar,
|
|
|
|
avatar_static: group.avatar_static || group.avatar || missingAvatar,
|
|
|
|
header: group.header || group.header_static || missingHeader,
|
|
|
|
header_static: group.header_static || group.header || missingHeader,
|
|
|
|
domain,
|
|
|
|
note,
|
|
|
|
display_name_html: emojify(escapeTextContentForBrowser(group.display_name), emojiMap),
|
|
|
|
note_emojified: emojify(group.note, emojiMap),
|
|
|
|
note_plain: unescapeHTML(group.note),
|
|
|
|
};
|
2023-02-28 07:26:27 -08:00
|
|
|
};
|
|
|
|
|
2024-08-11 01:48:58 -07:00
|
|
|
type Group = ReturnType<typeof normalizeGroup>;
|
2022-12-11 12:37:00 -08:00
|
|
|
|
2024-08-11 01:48:58 -07:00
|
|
|
export { normalizeGroup, type Group };
|