Files
discord-decomp/modules/game_profile/AnnouncementMessageUtils.tsx
T
2026-11-01 12:12:55 +02:00

211 lines
7.0 KiB
TypeScript

// Metro module 8651 (f8652).
import { isMessageComponentsV2 } from "../../records/MessageRecord";
import { ComponentType } from "../../flow/Server";
import { getUnfurledMediaItemType, messageAttachmentToMediaItem, embedMediaToMediaItem } from "../media/MediaTypes";
import { isImageContentType, isVideoContentType } from "../messages/MediaFormatTesters";
import maybeCreateMessageRecordFromSnapshot from "../forwarding/maybeCreateMessageRecordFromSnapshot";
import { createMessageRecord } from "../messages/MessageRecordUtils";
import { formatExpirationLabel } from "../polls/chat/useFormattedExpirationLabel";
import { intl, t } from "../../intl/index.native";
import URLUtils from "../../utils/URLUtils";
let c5628_4 = /^#{1,3}\s+(.+)$/;
let c5628_5 = /^https?:\/\/\S+$/;
export function toAnnouncementMessages(a0) {
return a0.map((item) => {
let r185, body2, content2, media, message;
L16: {
message = maybeCreateMessageRecordFromSnapshot(createMessageRecord(item));
content2 = message.content;
if (isMessageComponentsV2(message)) {
body2 = message.components.filter((component) => component.type === ComponentType.TEXT_DISPLAY).map((item) => item.content).join("\n");
} else {
const content = message.content;
if (content.length !== 0) {
const r41 = c5628_5.test(content.trim());
body2 = content;
if (!r41) {
break L16;
}
}
const r44 = message.embeds[0];
const r3 = r44 == null;
let r45 = null;
if (!r3) {
const r17 = [];
if (r44.rawTitle != null) {
r17.push(`# ${r44.rawTitle}`);
}
if (r44.rawDescription != null) {
r17.push(r44.rawDescription);
}
r45 = r17.length > 0 ? r17.join("\n") : null;
}
if (r45 == null) {
r45 = content;
}
body2 = r45;
}
}
L40: {
if (isMessageComponentsV2(message)) {
const r73 = message.components.find((component) => component.type === ComponentType.MEDIA_GALLERY);
const r76 = r73 == null ? void 0 : r73.items[0]?.media;
if (r76 != null) {
const unfurledMediaItemType = getUnfurledMediaItemType(r76);
if (unfurledMediaItemType !== "INVALID") {
media = {
...r76,
type: unfurledMediaItemType,
sourceMetadata: {
message
}
};
break L40;
}
}
}
const r88 = message.attachments.find((attachment) => isImageContentType(attachment.content_type));
if (r88 != null) {
media = messageAttachmentToMediaItem(r88, message);
} else {
const r93 = message.attachments.find((attachment) => isVideoContentType(attachment.content_type));
if (r93 != null) {
media = messageAttachmentToMediaItem(r93, message);
} else {
let c26253_0, c26253_1, c26253_2;
const r97 = message.embeds.find((embed) => embed.video != null && embed.thumbnail != null);
c26253_0 = r97;
if (r97?.thumbnail != null) {
media = embedMediaToMediaItem(r97.thumbnail, {
message,
identifier: {
type: "embed",
embedIndex: message.embeds.findIndex((embed) => embed === c26253_0)
}
}, "IMAGE");
} else {
const r103 = message.embeds.find((embed) => embed.image != null);
c26253_1 = r103;
if (r103?.image != null) {
media = embedMediaToMediaItem(r103.image, {
message,
identifier: {
type: "embed",
embedIndex: message.embeds.findIndex((embed) => embed === c26253_1)
}
}, "IMAGE");
} else {
const r108 = message.embeds.find((embed) => embed.thumbnail != null);
c26253_2 = r108;
media = r108?.thumbnail != null ? embedMediaToMediaItem(r108.thumbnail, {
message,
identifier: {
type: "embed",
embedIndex: message.embeds.findIndex((embed) => embed === c26253_2)
}
}, "IMAGE") : void 0;
}
}
}
}
}
const r151 = body2.indexOf("\n");
const r153 = r151 === -1;
const r159 = (r153 ? body2 : body2.slice(0, r151)).match(c5628_4);
const r161 = r159 != null ? {
title: r159[1].trim(),
body: r153 ? "" : body2.slice(r151 + 1).trimStart()
} : {
body: body2
};
const { title, body } = r161;
const reactions = item.reactions;
let reactionCount = reactions == null ? void 0 : reactions.reduce((acc, reaction) => acc + reaction.count, 0);
if (reactionCount == null) {
reactionCount = 0;
}
let embedSource = void 0;
if (body2 !== content2) {
const isMessageComponentsV22 = isMessageComponentsV2(message);
embedSource = void 0;
if (!isMessageComponentsV22) {
L80: {
const r183 = message.embeds[0];
const r184 = r183 != null;
r185 = void 0;
if (r184) {
const name = r183.author?.name;
let authorIconUrl = r183.author?.iconProxyURL;
if (authorIconUrl == null) {
authorIconUrl = r183.author?.iconURL;
}
let providerName = r183.footer?.text;
if (providerName == null) {
providerName = r183.provider?.name;
}
let providerIconUrl = r183.footer?.iconProxyURL;
if (providerIconUrl == null) {
providerIconUrl = r183.footer?.iconURL;
}
const url = r183.url;
let color = r183.color;
if (color == null) {
color = void 0;
}
if (name == null && providerName == null) {
const r209 = url != null;
r185 = void 0;
if (!r209) {
break L80;
}
}
r185 = {
authorName: name,
authorIconUrl,
providerName,
providerIconUrl,
url,
color
};
}
}
embedSource = r185;
}
}
return {
id: message.id,
media,
title,
body,
content: body2,
timestamp: item.timestamp,
reactionCount,
embedSource,
poll: message.poll
};
});
}
export function getPollExpiryLabel(a0) {
let r10 = formatExpirationLabel(a0.expiry);
if (r10 == null) {
r10 = intl.string(t["e+J3JZ"]);
}
return r10;
}
export function getPosterUrl(a0, a1, a2) {
const r13 = URLUtils.toURLSafe(a0);
const r1 = r13 == null;
let r15 = null;
if (!r1) {
r13.searchParams.append("format", "webp");
if (a1 != null) {
r13.searchParams.append("width", a1.toString());
}
if (a2 != null) {
r13.searchParams.append("height", a2.toString());
}
r15 = r13.toString();
}
return r15;
}