Files
discord-decomp/modules/messages/native/renderer/transformMessageComponents.tsx
T
2026-11-01 12:12:55 +02:00

473 lines
19 KiB
TypeScript

// Metro module 7889 (f7890).
import { processColor } from "react-native";
import { CheckpointVersions } from "../../../checkpoint/CheckpointConstants";
import { TEXT_DISPLAY_COMPONENT_MARKDOWN_RENDER_OPTIONS } from "../../../interaction_components/InteractionComponentConstants";
import { getInitialParserStateFromMessage } from "../../renderMessageMarkup";
import { isNotNullish } from "../../../../utils/GlobalUtils";
import { ComponentType, ButtonStyle } from "../../../../flow/Server";
import { getLayoutComponentErrorText, getSelectPlaceholder } from "../../../interaction_components/InteractionComponentUtils";
import { intl, t } from "../../../../intl/index.native";
import { getActionComponentState } from "../../../interaction_components/ComponentStateContext";
import { getInitialStringSelectOptions } from "../../../interaction_components/StringSelectActionComponentUtils";
import { getInitialSnowflakeSelectOptions } from "../../../interaction_components/SearchableSelectActionComponentUtils";
import { transformSearchableSelectOptions } from "../../../interaction_components/native/NativeSearchableSelectActionComponentUtils";
import { getUnfurledMediaItemObscurityProps } from "./ExplicitMediaUtils";
import { MediaGalleryItemType } from "./RowGeneratorTypes";
import { isVerifiedTeen } from "../../../age_assurance/AgeVerificationUtils";
import Module4728 from "../../../../_bundle/Module4728";
import { isSuspiciousDownload } from "../../../suspicious_downloads/SuspiciousDownloadUtils";
import { transformToRowGeneratedContentInventoryEntryComponent } from "./transformContentInventoryEntryMessageComponent";
import { shouldSkipAccessibilityLabels } from "../../../a11y/native/AccessibilityLabelOptimizationExperiment";
import { isImageContentType, isVideoContentType } from "../../MediaFormatTesters";
import { hasFlag } from "../../../../_bundle/Module1384";
import { UnfurledMediaItemFlags } from "../../../media/MediaTypes";
import RowGeneratorUtils from "./RowGeneratorUtils";
import { sanitizeMediaDimension } from "./sanitizeMediaDimension";
import URLUtils from "../../../../utils/URLUtils";
import LRUCache from "../../../../_bundle/LRUCache";
import Durations from "../../../../utils/Durations";
import MarkupUtils from "../../../markup/MarkupUtils";
import { getCardAssetUrl, getCheckpointPowerBarUnits, getVoiceDurationString } from "../../../checkpoint/CheckpointUtils";
import { isIOS } from "../../../../_bundle/Module501";
import UnicodeEmojis from "../../../emojis/UnicodeEmojis";
let _excluded, transformUnfurledMediaItem, c4986_10;
const transformToRowGeneratedComponent = function transformToRowGeneratedComponent(a0, a1) {
let r133, r449, r515, IMAGE, accessibilityLabel, accessibilityLabel2;
const { message, interaction, guildId, shouldDisableInteractiveComponents, shouldObscureSpoiler, enabledContentHarmTypeFlags, shouldShowMosaicMediaDescriptions, shouldAgeVerify, colors, markdownConfigs } = a0;
const type = a1.type;
if (ComponentType.ACTION_ROW === type) {
return {
...a1,
components: a1.components.map((component) => {
const r7 = transformToRowGeneratedComponent(a0, component);
return r7 ?? null;
}).filter(isNotNullish),
errorText: getLayoutComponentErrorText(interaction, message, a1)
};
}
if (ComponentType.BUTTON === type) {
const label = a1.style === ButtonStyle.PREMIUM ? intl.string(t.CHa0vN) : a1.label;
return {
...a1,
state: getActionComponentState(interaction, a1, shouldDisableInteractiveComponents),
label
};
}
if (ComponentType.STRING_SELECT === type) {
L101: {
const selectedOptions = getInitialStringSelectOptions(a1, message.id).map((c26053_0) => a1.options.findIndex((option) => option.value === c26053_0)).filter((item) => item !== -1);
r515 = {
...a1,
state: getActionComponentState(interaction, a1, shouldDisableInteractiveComponents),
selectedOptions,
placeholder: getSelectPlaceholder(a1)
};
if (shouldSkipAccessibilityLabels()) {
accessibilityLabel2 = getSelectPlaceholder(a1);
} else {
if (selectedOptions != null && selectedOptions.length !== 0) {
accessibilityLabel2 = intl.formatToPlainString(t["I/ROH+"], {
selections: selectedOptions.map((selectedOption) => a1.options[selectedOption].label).join(",")
});
break L101;
}
accessibilityLabel2 = intl.formatToPlainString(t["3aednA"], {
placeholder: getSelectPlaceholder(a1)
});
}
}
r515.accessibilityLabel = accessibilityLabel2;
return r515;
}
if (ComponentType.USER_SELECT !== type && ComponentType.ROLE_SELECT !== type && ComponentType.MENTIONABLE_SELECT !== type && ComponentType.CHANNEL_SELECT !== type) {
if (ComponentType.SECTION === type) {
const r421 = transformToRowGeneratedComponent(a0, a1.accessory);
const accessory = r421 == null ? null : r421;
return accessory == null ? null : {
...a1,
components: a1.components.map((component) => {
const r7 = transformToRowGeneratedComponent(a0, component);
return r7 ?? null;
}).filter(isNotNullish),
accessory,
errorText: getLayoutComponentErrorText(interaction, message, a1)
};
}
if (ComponentType.TEXT_DISPLAY === type) {
const textDisplayComponent = markdownConfigs.textDisplayComponent;
const id = message.id;
const content = a1.content;
const r401 = `${textDisplayComponent.type}-${id}-${content}`;
let content2 = c4986_10.get(r401);
if (content2 == null) {
const r415 = MarkupUtils.parseToAST(content, true, {
...textDisplayComponent.parserState
});
c4986_10.set(r401, r415);
content2 = r415;
}
return {
...a1,
content: content2
};
}
if (ComponentType.THUMBNAIL === type) {
L71: {
const media = a1.media;
const { width, height, contentType } = media;
if (width != null && width > 0 && height != null && height > 0) {
if (isImageContentType(contentType)) {
IMAGE = MediaGalleryItemType.IMAGE;
break L71;
}
IMAGE = isVideoContentType(contentType) ? MediaGalleryItemType.VIDEO : MediaGalleryItemType.VISUAL_PLACEHOLDER;
} else {
IMAGE = MediaGalleryItemType.VISUAL_PLACEHOLDER;
}
}
const type2 = IMAGE === MediaGalleryItemType.IMAGE ? "image" : "generic";
let spoiler2 = a1.spoiler;
if (spoiler2 == null) {
spoiler2 = false;
}
const unfurledMediaItemObscurityProps2 = getUnfurledMediaItemObscurityProps({
type: type2,
mediaItem: a1.media,
isSpoilered: spoiler2,
isAuthorBot: message.author.bot,
shouldObscureSpoiler,
shouldAgeVerify,
enabledContentHarmTypeFlags
});
const { isSpoiler: isSpoiler2, spoilerDescription: spoilerDescription2, isObscured: isObscured2, obscureDescription: obscureDescription2, obscureAwaitingScan: obscureAwaitingScan2 } = unfurledMediaItemObscurityProps2;
const obscureHideControls2 = isObscured2 ? isVerifiedTeen() : isObscured2;
return {
...a1,
media: transformUnfurledMediaItem(a1.media, a0),
isSpoiler: isSpoiler2,
spoilerDescription: spoilerDescription2,
isObscure: isObscured2,
isObscureAwaitingScan: obscureAwaitingScan2,
obscureDescription: obscureDescription2,
verifyAge: isObscured2 && shouldAgeVerify,
obscureHideControls: obscureHideControls2,
obscureIsOpaque: isObscured2,
descriptionHint: intl.string(t.IPzNKE),
accessibilityRole: intl.string(t.fKyfca)
};
}
if (ComponentType.MEDIA_GALLERY === type) {
const items = a1.items.map((item, index) => {
let r99, r100, IMAGE;
L9: {
const media = item.media;
const { width, height, contentType } = media;
if (width != null && width > 0 && height != null && height > 0) {
if (isImageContentType(contentType)) {
IMAGE = MediaGalleryItemType.IMAGE;
break L9;
}
IMAGE = isVideoContentType(contentType) ? MediaGalleryItemType.VIDEO : MediaGalleryItemType.VISUAL_PLACEHOLDER;
} else {
IMAGE = MediaGalleryItemType.VISUAL_PLACEHOLDER;
}
}
const type = IMAGE !== MediaGalleryItemType.IMAGE ? IMAGE === MediaGalleryItemType.VIDEO ? "video" : "generic" : "image";
let spoiler = item.spoiler;
if (spoiler == null) {
spoiler = false;
}
const unfurledMediaItemObscurityProps = getUnfurledMediaItemObscurityProps({
type,
mediaItem: item.media,
isSpoilered: spoiler,
isAuthorBot: message.author.bot,
shouldObscureSpoiler,
enabledContentHarmTypeFlags,
shouldAgeVerify
});
const { isSpoiler, spoilerDescription, isObscured, obscureDescription, obscureAwaitingScan } = unfurledMediaItemObscurityProps;
const obscureHideControls = isObscured ? isVerifiedTeen() : isObscured;
const VIDEO = MediaGalleryItemType.VIDEO;
let videoPreviewUrl = void 0;
if (IMAGE === VIDEO) {
const r92 = URLUtils.toURLSafe(item.media.proxyUrl);
const r93 = r92 == null;
let r94 = null;
if (!r93) {
r92.searchParams.set("format", "webp");
r94 = r92.toString();
}
const r98 = r94 == null;
videoPreviewUrl = r94;
r99 = null;
if (!r98) {
r100 = {
...item,
media: transformUnfurledMediaItem(item.media, a0),
mediaType: IMAGE,
videoPreviewUrl,
isSpoiler,
spoilerDescription,
isObscure: isObscured,
isObscureAwaitingScan: obscureAwaitingScan,
obscureDescription,
verifyAge: isObscured && shouldAgeVerify,
obscureHideControls,
obscureIsOpaque: isObscured,
showDescription: shouldShowMosaicMediaDescriptions,
descriptionHint: IMAGE === MediaGalleryItemType.VIDEO ? intl.string(t["BEWw/7"]) : intl.string(t.IPzNKE),
accessibilityRole: IMAGE === MediaGalleryItemType.VIDEO ? intl.string(t["/SCpvi"]) : intl.string(t.fKyfca),
portalId: IMAGE === MediaGalleryItemType.VIDEO ? `${message.id}_MediaGallery(${a1.id})_${index}` : null
};
r99 = r100;
}
} else {
r100 = {
...item,
media: transformUnfurledMediaItem(item.media, a0),
mediaType: IMAGE,
videoPreviewUrl,
isSpoiler,
spoilerDescription,
isObscure: isObscured,
isObscureAwaitingScan: obscureAwaitingScan,
obscureDescription,
verifyAge: isObscured && shouldAgeVerify,
obscureHideControls,
obscureIsOpaque: isObscured,
showDescription: shouldShowMosaicMediaDescriptions,
descriptionHint: IMAGE === MediaGalleryItemType.VIDEO ? intl.string(t["BEWw/7"]) : intl.string(t.IPzNKE),
accessibilityRole: IMAGE === MediaGalleryItemType.VIDEO ? intl.string(t["/SCpvi"]) : intl.string(t.fKyfca),
portalId: IMAGE === MediaGalleryItemType.VIDEO ? `${message.id}_MediaGallery(${a1.id})_${index}` : null
};
r99 = r100;
}
return r99;
}).filter(isNotNullish);
return items.length !== 0 ? {
...a1,
items
} : null;
}
if (ComponentType.FILE === type) {
let spoiler = a1.spoiler;
if (spoiler == null) {
spoiler = false;
}
const unfurledMediaItemObscurityProps = getUnfurledMediaItemObscurityProps({
type: "file",
mediaItem: a1.file,
isSpoilered: spoiler,
isAuthorBot: message.author.bot,
shouldObscureSpoiler,
shouldAgeVerify,
enabledContentHarmTypeFlags
});
const { isSpoiler, spoilerDescription, isObscured, obscureDescription, obscureAwaitingScan } = unfurledMediaItemObscurityProps;
const obscureHideControls = isObscured ? isVerifiedTeen() : isObscured;
const r268 = {
...a1,
file: transformUnfurledMediaItem(a1.file, a0)
};
let name = a1.name;
if (name == null) {
name = intl.string(t.GnuJ5u);
}
r268.name = name;
let size = a1.size;
if (size == null) {
size = 0;
}
r268.size = Module4728.filesize(size);
r268.isSuspiciousDownload = isSuspiciousDownload(a1.file.url) != null;
r268.isSpoiler = isSpoiler;
r268.spoilerDescription = spoilerDescription;
r268.isObscure = isObscured;
r268.isObscureAwaitingScan = obscureAwaitingScan;
r268.obscureDescription = obscureDescription;
r268.verifyAge = isObscured && shouldAgeVerify;
r268.obscureHideControls = obscureHideControls;
r268.obscureIsOpaque = isObscured;
return r268;
}
if (ComponentType.SEPARATOR === type) {
return a1;
}
if (ComponentType.TEXT_INPUT === type) {
return null;
}
if (ComponentType.CONTENT_INVENTORY_ENTRY === type) {
return {
type: a1.type,
id: a1.id,
contentInventoryEntry: transformToRowGeneratedContentInventoryEntryComponent({
component: a1,
message
})
};
}
if (ComponentType.CONTAINER === type) {
const r209 = {
...a1,
components: a1.components.map((component) => {
const r7 = transformToRowGeneratedComponent(a0, component);
return r7 ?? null;
}).filter(isNotNullish),
accentColor: a1.accentColor != null ? processColor(a1.accentColor) : null,
isSpoiler: a1.spoiler,
spoilerDescription: a1.spoiler ? intl.string(t.C8ci33) : null
};
let embedBackgroundColor = colors.embedBackgroundColor;
if (embedBackgroundColor == null) {
embedBackgroundColor = null;
}
r209.themedBackgroundColor = embedBackgroundColor;
return r209;
}
if (ComponentType.LABEL !== type && ComponentType.FILE_UPLOAD !== type) {
if (ComponentType.CHECKPOINT_CARD === type) {
const { checkpointData, ...rest } = a1;
if (checkpointData.version === CheckpointVersions.V2025) {
const numberFormat = new Intl.NumberFormat(intl.currentLocale, {
notation: "compact",
compactDisplay: "short"
});
const checkpointData2 = {
...checkpointData,
cardId: checkpointData.cardId.toString(),
cardAssetUrl: getCardAssetUrl(checkpointData.cardId),
authorId: message.author.id
};
let powerLevel = checkpointData.powerLevel;
if (powerLevel == null) {
powerLevel = 0;
}
checkpointData2.powerLevel = numberFormat.format(powerLevel);
const r161 = Math;
let powerLevelPercentile = checkpointData.powerLevelPercentile;
if (powerLevelPercentile == null) {
powerLevelPercentile = 0;
}
checkpointData2.powerLevelUnits = r161.min(getCheckpointPowerBarUnits(powerLevelPercentile), isIOS() ? 9 : 10);
checkpointData2.voiceString = getVoiceDurationString(checkpointData.totalVoiceMinutes);
checkpointData2.reactionString = checkpointData.numEmojisSent.toLocaleString(intl.currentLocale);
checkpointData2.messagesString = checkpointData.numMessagesSent.toLocaleString(intl.currentLocale);
checkpointData2.topEmoji = checkpointData.topEmoji != null ? {
...checkpointData.topEmoji,
emojiSurrogateName: checkpointData.topEmoji.emojiId == null ? UnicodeEmojis.convertSurrogateToName(checkpointData.topEmoji.emojiName) : void 0
} : void 0;
checkpointData2.clickable = void 0;
r133 = {
...rest,
checkpointData: checkpointData2
};
} else {
r133 = {
type: ComponentType.UNKNOWN,
id: a1.id
};
}
return r133;
}
if (ComponentType.RADIO_GROUP !== type && ComponentType.CHECKBOX_GROUP !== type) {
ComponentType.CHECKBOX;
}
return null;
}
return null;
}
L94: {
const initialSnowflakeSelectOptions = getInitialSnowflakeSelectOptions(a1, message.id, guildId);
r449 = {
...a1,
state: getActionComponentState(interaction, a1, shouldDisableInteractiveComponents),
selectedOptions: transformSearchableSelectOptions(initialSnowflakeSelectOptions, guildId),
placeholder: getSelectPlaceholder(a1)
};
if (shouldSkipAccessibilityLabels()) {
accessibilityLabel = getSelectPlaceholder(a1);
} else {
if (initialSnowflakeSelectOptions != null && initialSnowflakeSelectOptions.length !== 0) {
accessibilityLabel = intl.formatToPlainString(t["I/ROH+"], {
selections: initialSnowflakeSelectOptions.map((initialSnowflakeSelectOption) => initialSnowflakeSelectOption.label).join(",")
});
break L94;
}
accessibilityLabel = intl.formatToPlainString(t["3aednA"], {
placeholder: getSelectPlaceholder(a1)
});
}
}
r449.accessibilityLabel = accessibilityLabel;
return r449;
};
transformUnfurledMediaItem = function transformUnfurledMediaItem(a0, a1) {
let IMAGE;
L9: {
const { width, height, contentType } = a0;
if (width != null && width > 0 && height != null && height > 0) {
if (isImageContentType(contentType)) {
IMAGE = MediaGalleryItemType.IMAGE;
break L9;
}
IMAGE = isVideoContentType(contentType) ? MediaGalleryItemType.VIDEO : MediaGalleryItemType.VISUAL_PLACEHOLDER;
} else {
IMAGE = MediaGalleryItemType.VISUAL_PLACEHOLDER;
}
}
const IMAGE2 = MediaGalleryItemType.IMAGE;
const r49 = {
...a0,
srcIsAnimated: hasFlag(a0.flags, UnfurledMediaItemFlags.IS_ANIMATED)
};
if (!a1.shouldShowMedia) {
r49.height = 0;
r49.width = 0;
}
if (IMAGE === IMAGE2) {
const proxyUrl = r49.proxyUrl;
let width2 = r49.width;
if (width2 == null) {
width2 = 0;
}
let height2 = r49.height;
if (height2 == null) {
height2 = 0;
}
r49.proxyUrl = RowGeneratorUtils.getImageSrc(proxyUrl, width2, height2, !a1.shouldAutoPlayGifs);
}
r49.width = sanitizeMediaDimension(r49.width);
r49.height = sanitizeMediaDimension(r49.height);
return r49;
};
_excluded = ["checkpointData"];
c4986_10 = new LRUCache({
max: Infinity,
maxAge: 15 * Durations.Millis.MINUTE,
updateAgeOnGet: true
});
export default function transformMessageComponents(a0, a1) {
let c16077_0 = {
...a0,
markdownConfigs: {
textDisplayComponent: {
type: "textDisplayComponent",
parserState: getInitialParserStateFromMessage(a0.message, TEXT_DISPLAY_COMPONENT_MARKDOWN_RENDER_OPTIONS)
}
}
};
return a1.map((item) => transformToRowGeneratedComponent(c16077_0, item)).filter(isNotNullish);
};
export function getUnfurledMediaItemType({ width, height, contentType }) {
if (width != null && width > 0 && height != null && height > 0) {
if (isImageContentType(contentType)) {
return MediaGalleryItemType.IMAGE;
}
if (isVideoContentType(contentType)) {
return MediaGalleryItemType.VIDEO;
}
}
return MediaGalleryItemType.VISUAL_PLACEHOLDER;
}