Files
discord-decomp/modules/premium/gifting/shared/GiftIntentReconcilingManager.tsx
T
2026-11-01 12:12:55 +02:00

142 lines
5.6 KiB
TypeScript

// Metro module 16301 (f16302).
import EphemeralMessageStore from "../../../messages/EphemeralMessageStore";
import UserSettingsProtoStore from "../../../user_settings/UserSettingsProtoStore";
import ChannelStore from "../../../../stores/ChannelStore";
import PremiumGiftingIntentStore from "../PremiumGiftingIntentStore";
import { MessageTypes } from "../../../../Constants";
import Durations from "../../../../utils/Durations";
import AutomaticLifecycleManager from "../../../../lib/AutomaticLifecycleManager";
import Backoff from "../../../../_bundle/Backoff";
import { FriendshipAnniversaryBackendPersistenceExperiment } from "../../experiments/FriendshipAnniversaryBackendPersistenceExperiment";
import { fetchAndReconcileGiftIntentDismissals } from "../PremiumGiftingIntentActionCreators";
import Dispatcher from "../../../../Dispatcher";
import MessageActionCreators from "../../../../actions/MessageActionCreators";
let c11720_8 = 10 * Durations.Millis.SECOND;
let c11720_9 = 5 * Durations.Millis.MINUTE;
class GiftIntentReconcilingManager extends AutomaticLifecycleManager {
actions = {
POST_CONNECTION_OPEN: () => this.onPostConnectionOpen(),
CHANNEL_SELECT: (a0) => this.onChannelSelect(a0),
GIFT_INTENT_DISMISSALS_FETCH_SUCCESS: (a0) => this.onReconcileSuccess(a0),
GIFT_INTENT_DISMISSALS_FETCH_FAILURE: () => this.onReconcileSettled(false),
LOGOUT: () => this.onLogout()
};
stores = new Map([[PremiumGiftingIntentStore, () => this.onPremiumGiftingIntentStoreChange()]]);
reconcileBackoff = new Backoff(c11720_8, c11720_9);
isReconciling = false;
heldGiftingPromptSystemMessage = false;
lastReconciledDismissalAtMs = new Map();
retryReconcileServerDismissals = () => {
if (this.isReconcileEligible("retryReconcileServerDismissals")) {
this.attemptReconcileFetch();
}
};
onPostConnectionOpen() {
this.lastReconciledDismissalAtMs.clear();
this.sendGiftingPromptSystemMessagesIfEligible();
}
onPremiumGiftingIntentStoreChange() {
this.maybeReconcileServerDismissals();
this.maybeRetryHeldGiftingPromptSystemMessage();
}
maybeReconcileServerDismissals() {
if (this.isReconcileEligible("maybeReconcileServerDismissals") && !this.reconcileBackoff.pending) {
this.attemptReconcileFetch();
}
}
isReconcileEligible(a0) {
return PremiumGiftingIntentStore.getFriendAnniversaries().length !== 0 && FriendshipAnniversaryBackendPersistenceExperiment.getConfig({
location: a0
}).enabled;
}
getServerDismissalTimestampMs() {
const r4 = Number;
let r9 = UserSettingsProtoStore.settings.userContent?.lastGiftIntentDismissedAtMs;
if (r9 == null) {
r9 = "0";
}
return r4(r9);
}
attemptReconcileFetch() {
const serverDismissalTimestampMs = this.getServerDismissalTimestampMs();
if (!(PremiumGiftingIntentStore.getLastKnownGiftIntentDismissedAtMs() >= serverDismissalTimestampMs || this.isReconciling)) {
this.isReconciling = true;
fetchAndReconcileGiftIntentDismissals(serverDismissalTimestampMs);
}
}
onReconcileSuccess(a0) {
this.onReconcileSettled(true);
this.removeRemotelyDismissedGiftIntentCards(a0.dismissals);
}
onReconcileSettled(a0) {
this.isReconciling = false;
const reconcileBackoff = this.reconcileBackoff;
if (a0) {
reconcileBackoff.succeed();
} else {
reconcileBackoff.fail(this.retryReconcileServerDismissals);
}
}
removeRemotelyDismissedGiftIntentCards(a0) {
for (const item of a0) {
const { targetId, dismissedAtMs } = item;
const r39 = this.lastReconciledDismissalAtMs.get(targetId);
const lastReconciledDismissalAtMs = this.lastReconciledDismissalAtMs;
const r19 = Math;
let r17 = r39;
if (r17 == null) {
r17 = 0;
}
lastReconciledDismissalAtMs.set(targetId, r19.max(r17, dismissedAtMs));
if (r39 != null && !(dismissedAtMs <= r39)) {
const dMFromUserId = ChannelStore.getDMFromUserId(targetId);
const channelId = dMFromUserId;
if (dMFromUserId != null) {
for (const item2 of EphemeralMessageStore.getMessages(channelId)) {
if (item2.type === MessageTypes.GIFTING_PROMPT && item2.giftingPrompt?.recipientUserId === targetId) {
Dispatcher.dispatch({
type: "MESSAGE_DELETE",
id: item2.id,
channelId
});
}
}
}
}
}
}
onLogout() {
this.reconcileBackoff.cancel();
this.isReconciling = false;
this.heldGiftingPromptSystemMessage = false;
this.lastReconciledDismissalAtMs.clear();
}
maybeRetryHeldGiftingPromptSystemMessage() {
if (this.heldGiftingPromptSystemMessage && !(PremiumGiftingIntentStore.getLastKnownGiftIntentDismissedAtMs() < this.getServerDismissalTimestampMs())) {
this.heldGiftingPromptSystemMessage = false;
this.sendGiftingPromptSystemMessagesIfEligible();
}
}
shouldHoldGiftingPromptSystemMessageForServerReconcile(a0) {
return !!FriendshipAnniversaryBackendPersistenceExperiment.getConfig({
location: a0
}).enabled && PremiumGiftingIntentStore.getLastKnownGiftIntentDismissedAtMs() < this.getServerDismissalTimestampMs();
}
trySendGiftingPromptSystemMessage(a0, a1, a2, a3, a4) {
let r18;
if (this.shouldHoldGiftingPromptSystemMessageForServerReconcile(a4)) {
this.heldGiftingPromptSystemMessage = true;
r18 = false;
} else {
MessageActionCreators.sendGiftingPromptSystemMessage(a0, {
giftIntentType: a1,
recipientUserId: a2,
giftIntentSecondaryAction: a3
});
r18 = true;
}
return r18;
}
}
export default GiftIntentReconcilingManager;