Files
discord-decomp/modules/notifications/native/NotificationTokenManager.tsx
T
2026-11-01 12:12:55 +02:00

192 lines
8.6 KiB
TypeScript

// Metro module 13508 (f13509).
import { NativeModules } from "react-native";
import ApexExperimentStore from "../../experiments/apex/ApexExperimentStore";
import MultiAccountStore from "../../multi_account/MultiAccountStore";
import MultiAccountSwitchStore from "../../multi_account/MultiAccountSwitchStore";
import AuthenticationStore from "../../../stores/AuthenticationStore";
import { NOTIF_SETTINGS } from "../NotificationSettingsConstants";
import { AnalyticEvents } from "../../../Constants";
import LifecycleManager from "../../../lib/LifecycleManager";
import Dispatcher from "../../../Dispatcher";
import PushNotification from "../../../lib/pushnotification/PushNotification";
import { declarativeNotifSettingsExperiment } from "./NotifSettingsExperiments";
import NotifSettingsUtils_android from "./NotifSettingsUtils.android";
import SentryUtils_native from "../../../utils/SentryUtils.native";
import { intl, t } from "../../../intl/index.native";
import NotificationSettings_messages from "../NotificationSettings.messages";
import { isAndroid } from "../../../utils/PlatformUtils";
import Module13514 from "../../../_bundle/Module13514";
import AnalyticsUtils from "../../../utils/AnalyticsUtils";
import PushNotificationActionCreators from "../../../actions/native/PushNotificationActionCreators";
class NotificationTokenManager extends LifecycleManager {
_experimentUnsubscribe = null;
token = null;
hasRegisterEventListener = false;
hasTrackedDisabledAndroidNotifChannels = false;
postConnectionOpenTimeoutID = null;
_handleExperimentsUpdated = () => this.registerNotificationCategories();
handleToken = (a0) => {
this.token = a0;
if (this.canSync) {
this.registerToken();
}
};
registerToken = () => {
if (this.token != null) {
NativeModules.DCDNotificationCategoryUtils.registerNotificationReplyCategories(intl.string(t.TBA5Xg), intl.string(t.TXNS7S), intl.string(t.TBA5Xg), () => {});
if (NativeModules.PushNotificationAndroid) {
NativeModules.PushNotificationAndroid.onRegisterNotificationToken();
}
PushNotificationActionCreators.registerDevice(this.token);
}
};
handleSyncNoMultiAccountOnLoginSuccess = () => {
this.handleSyncNoMultiAccount();
};
handleSyncNoMultiAccountOnRegisterSuccess = () => {
this.postConnectionOpenTimeoutID = setTimeout(this.handleSyncNoMultiAccountOnPostConnectionOpen, 5000);
};
handleSyncNoMultiAccountOnPostConnectionOpen = () => {
if (this.postConnectionOpenTimeoutID != null) {
clearTimeout(this.postConnectionOpenTimeoutID);
this.postConnectionOpenTimeoutID = null;
this.handleSyncNoMultiAccount();
}
};
handleSyncNoMultiAccount = () => {
if (this.canSync && !MultiAccountStore.canUseMultiAccountNotifications) {
this.registerToken();
}
};
handleSyncWithMultiAccount = () => {
if (this.canSync && MultiAccountStore.canUseMultiAccountNotifications) {
this.registerToken();
}
};
_initialize() {
if (this.token != null) {
this.handleToken(this.token);
}
ApexExperimentStore.addChangeListener(this._handleExperimentsUpdated);
this._experimentUnsubscribe = () => {
ApexExperimentStore.removeChangeListener(this._handleExperimentsUpdated);
};
Dispatcher.subscribe("LOGIN_SUCCESS", this.handleSyncNoMultiAccountOnLoginSuccess);
Dispatcher.subscribe("REGISTER_SUCCESS", this.handleSyncNoMultiAccountOnRegisterSuccess);
Dispatcher.subscribe("POST_CONNECTION_OPEN", this.handleSyncNoMultiAccountOnPostConnectionOpen);
Dispatcher.subscribe("POST_CONNECTION_OPEN", this.handleSyncWithMultiAccount);
Dispatcher.subscribe("MULTI_ACCOUNT_REMOVE_ACCOUNT", this.handleSyncWithMultiAccount);
}
_terminate() {
Dispatcher.unsubscribe("LOGIN_SUCCESS", this.handleSyncNoMultiAccountOnLoginSuccess);
Dispatcher.unsubscribe("REGISTER_SUCCESS", this.handleSyncNoMultiAccountOnRegisterSuccess);
Dispatcher.unsubscribe("POST_CONNECTION_OPEN", this.handleSyncNoMultiAccountOnPostConnectionOpen);
Dispatcher.unsubscribe("POST_CONNECTION_OPEN", this.handleSyncWithMultiAccount);
Dispatcher.unsubscribe("MULTI_ACCOUNT_REMOVE_ACCOUNT", this.handleSyncWithMultiAccount);
if (this._experimentUnsubscribe != null) {
this._experimentUnsubscribe();
this._experimentUnsubscribe = null;
}
}
registerListener() {
if (this.hasRegisterEventListener) {
throw new Error("Device token listener already registered.");
}
this.hasRegisterEventListener = true;
PushNotification.addRegisterEventListener(this.handleToken);
}
registerNotificationCategories() {
const config = declarativeNotifSettingsExperiment.getConfig({
location: "registerNotificationCategories"
});
let r12 = false;
try {
if (config.enabled) {
r12 = NotifSettingsUtils_android.registerDeclarativeNotificationCategories();
}
} catch ($e) {
SentryUtils_native.captureException($e);
}
if (!r12) {
try {
if (config.clearDeclarative) {
SentryUtils_native.addBreadcrumb({
message: "Clearing declarative notification categories"
});
NotifSettingsUtils_android.clear();
}
} catch ($e) {
SentryUtils_native.captureException($e);
}
this.registerLegacyNotificationCategories();
}
this.trackDisabledAndroidNotifChannels();
}
registerLegacyNotificationCategories() {
const registerNotificationCategories2 = NativeModules.DCDNotificationCategoryUtils.registerNotificationCategories;
const registerNotificationCategoriesAndGroups = NativeModules.DCDNotificationCategoryUtils.registerNotificationCategoriesAndGroups;
if (registerNotificationCategoriesAndGroups != null) {
registerNotificationCategoriesAndGroups({
calls: intl.string(NotificationSettings_messages["IUH/Oe"]),
mediaConnections: intl.string(NotificationSettings_messages.VeBD1N),
messages: intl.string(NotificationSettings_messages["4qWUAO"]),
directMessages: intl.string(NotificationSettings_messages.NGdNZb),
friendRequests: intl.string(NotificationSettings_messages.NxgGZA),
polls: intl.string(NotificationSettings_messages.MOjygY),
social: intl.string(NotificationSettings_messages["UzRF+8"]),
stageLive: intl.string(NotificationSettings_messages["4n388K"]),
guildEventLive: intl.string(NotificationSettings_messages["40TIqW"]),
guildHighlights: intl.string(t.p5jg9S),
forumThreadCreated: intl.string(NotificationSettings_messages.HibKoy),
systemMessages: intl.string(NotificationSettings_messages.zJlwvV),
other: intl.string(NotificationSettings_messages.kIrLfg),
default: intl.string(NotificationSettings_messages["T+79Eo"]),
reactions: intl.string(t.gHp0C4)
}, {
realtime: intl.string(NotificationSettings_messages.S5cB9e),
social: intl.string(NotificationSettings_messages["UzRF+8"]),
server: intl.string(NotificationSettings_messages.zRKbpz),
other: intl.string(NotificationSettings_messages.q5M7HV)
});
} else if (registerNotificationCategories2 != null) {
registerNotificationCategories2({
calls: intl.string(t.JJogjm),
mediaConnections: intl.string(t.K3lovD),
messages: intl.string(t.OIgYlQ),
directMessages: intl.string(t.YUU0RF),
social: intl.string(t.TdEu5X),
gameDetection: intl.string(t["A/4saf"]),
stageLive: intl.string(t.qGRagm),
guildEventLive: intl.string(t.MfGr0a),
guildHighlights: intl.string(t.p5jg9S),
forumThreadCreated: intl.string(t.dl57ho),
other: intl.string(t.BcZTKu),
otherHighPriority: intl.string(t.bcv3rp)
});
}
}
trackDisabledAndroidNotifChannels() {
if (!this.hasTrackedDisabledAndroidNotifChannels) {
this.hasTrackedDisabledAndroidNotifChannels = true;
if (isAndroid()) {
const getAndroidNotifChannelStates = Module13514?.getAndroidNotifChannelStates;
if (getAndroidNotifChannelStates != null) {
const r8 = getAndroidNotifChannelStates();
let c20503_0 = new Set(NOTIF_SETTINGS.map((item) => item.string_id));
AnalyticsUtils.track(AnalyticEvents.ANDROID_NOTIFICATION_CHANNELS_SYNCED, {
disabled_channels: r8.filter((item) => item.importance === 0 && c20503_0.has(item.channelId)).map((item) => item.channelId)
});
}
}
}
}
getToken() {
return this.token;
}
get canSync() {
return this.isInitialized && AuthenticationStore.isAuthenticated() && !MultiAccountSwitchStore.getIsSwitchingAccount();
}
}
const notificationTokenManager = new NotificationTokenManager();
export default notificationTokenManager;