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

319 lines
12 KiB
TypeScript

// Metro module 16645 (f16646).
import { NativeModules } from "react-native";
import GuildReadStateStore from "../../../stores/GuildReadStateStore";
import ReadStateStore from "../../../stores/ReadStateStore";
import { AnalyticEvents } from "../../../Constants";
import Logger from "../../debug/Logger";
import { shouldClearChannelNotificationsOnAppForeground } from "./ClearChannelNotificationsOnAppForegroundExperiment";
import PushNotification from "../../../lib/pushnotification/PushNotification";
import SnowflakeUtils from "../../../utils/SnowflakeUtils";
import AutomaticLifecycleManager from "../../../lib/AutomaticLifecycleManager";
import { isIOS } from "../../../utils/PlatformUtils";
import { removeFile, moveFile, readFile } from "../../../utils/FileManagerUtils.android";
import AnalyticsUtils from "../../../utils/AnalyticsUtils";
function $mk117431($env) {
return function clearStaleNotifications() {
const r2 = $env;
if (shouldClearChannelNotificationsOnAppForeground({
location: "clearStaleNotifications"
})) {
const r16 = c11984_9.filter($mk118578(r2)).map((item) => item.identifier);
if (r16.length > 0) {
PushNotification.removeDeliveredNotifications(r16);
(0, getDeliveredNotifications)();
}
}
};
}
function $mk118578($env) {
return (a0) => {
if (a0.userInfo != null && typeof a0.userInfo === "object") {
if (typeof a0.userInfo.channel_id !== "string") {
return false;
}
if (typeof a0.userInfo.notif_instance_id !== "string") {
return false;
}
let notif_instance_id = a0.userInfo.notif_instance_id;
if (a0.userInfo.type === "MESSAGE_CREATE") {
if (typeof a0.userInfo.message_id !== "string") {
return false;
}
notif_instance_id = a0.userInfo.message_id;
} else {
if (a0.userInfo.type !== "GENERIC_PUSH_NOTIFICATION_SENT") {
return false;
}
if (a0.userInfo.notification_type !== "REACTIONS_PUSH_NOTIFICATION") {
return false;
}
}
if (notif_instance_id != null && typeof notif_instance_id === "string") {
const r30 = ReadStateStore.ackMessageId(a0.userInfo.channel_id);
return r30 != null && !(SnowflakeUtils.compare(r30, notif_instance_id) <= 0);
}
return false;
}
return false;
};
}
function $mk118583($env) {
return (a0) => {
const r6 = JSON.parse(a0);
$env[4].set(r6._local_uuid, r6.app_state);
};
}
let updateAndClearStaleNotifications2, c11984_8, c11984_9, c11984_14;
let env11984 = {};
const getDeliveredNotifications = async function getDeliveredNotifications() {
let c28480_0;
L13: {
c28480_0 = env11984;
if (shouldClearChannelNotificationsOnAppForeground({
location: "getDeliveredNotifications"
})) {
env11984[9] = await PushNotification.getDeliveredNotifications();
break L13;
}
return void 0;
}
};
updateAndClearStaleNotifications2 = async function updateAndClearStaleNotifications() {
let c28481_0 = env11984;
await getDeliveredNotifications();
$mk117431(c28481_0)();
};
c11984_8 = new Logger("NativeNotificationsManager");
c11984_9 = [];
class NativeNotificationsManager extends AutomaticLifecycleManager {
handleAck = ({ channelId }) => {
if (isIOS()) {
PushNotification.setApplicationIconBadgeNumber(GuildReadStateStore.getTotalMentionCount());
}
if (channelId != null) {
const DCDNotificationManager = NativeModules.DCDNotificationManager;
if (DCDNotificationManager != null) {
DCDNotificationManager.clearNotificationsForChannel(channelId);
}
}
};
constructor() {
super(...arguments);
const _this = this;
this.handlePostConnectionOpen = async () => {
let r45;
let c31088_0, c31088_1, app_state, c31088_3, c31088_4, c31088_5;
let c31088_6 = 0;
const r18 = void 0;
const r17 = env11984;
L39: {
L34: {
try {
let env31835;
c31088_4 = r18;
const r28 = env31835 = {
$parent: r17
};
c31088_3 = r28;
env31835[0] = void 0;
env31835[1] = void 0;
env31835[2] = void 0;
env31835[3] = void 0;
env31835[4] = void 0;
env31835[5] = void 0;
env31835[6] = void 0;
env31835[7] = void 0;
env31835[8] = void 0;
if (isIOS()) {
updateAndClearStaleNotifications2();
} else {
_this.handleSetCallNotificationExperiment();
}
c31088_6 = 1;
const moveAndReadData = function moveAndReadData() {
return env31835[10].apply(this, arguments);
};
env31835[9] = moveAndReadData;
env31835[10] = function _moveAndReadData() {
const r9 = async (c32149_0, c32149_1) => {
let r104;
let c32180_0, c32180_1;
L15: {
c32180_0 = c32149_0;
c32180_1 = c32149_1;
await removeFile(env31835[0], c32149_1);
const r19 = await moveFile(env31835[0], c32180_0, c32180_1);
const r76 = null;
if (r19) {
r104 = await readFile(env31835[0], c32180_1, "utf8");
break L15;
}
return r76;
}
return r104;
};
env31835[10] = r9;
return r9.apply(this, arguments);
};
env31835[11] = function normalizeTimestampToMs(a0) {
if (a0 != null) {
return typeof a0 === "number" ? Math.round(1000 * a0) : typeof a0 === "string" ? parseInt(a0, 10) : void 0;
}
};
env31835[0] = isIOS() ? "shared" : "cache";
env31835[1] = "processing_notifications";
env31835[2] = "processing_notification_states";
env31835[3] = await moveAndReadData("notifications_to_track", "processing_notifications");
if (env31835[3] == null) {
c31088_6 = 0;
break L39;
}
env31835[4] = new Map();
if (isIOS()) {
const r309 = await (0, env31835[9])("notification_states_to_track", env31835[2]);
const r334 = c31088_3;
env31835[5] = r309;
if (env31835[5] !== null) {
env31835[5].trim().split("\n").forEach($mk118583(r334));
}
}
env31835[6] = env31835[3].trim().split("\n");
const r136 = env31835[6];
c31088_1 = r136;
const r138 = r136[Symbol.iterator]();
c31088_1 = r138.next;
c31088_0 = r138;
while (true) {
const r140 = c31088_1;
let r141 = c31088_0;
const r142 = r141 === void 0 ? void 0 : (r45 = r140.call(r141)).done ? r141 = void 0 : r45.value;
c31088_0 = r141;
if (r141 === void 0) {
await removeFile(env31835[0], env31835[1]);
if (isIOS()) {
await removeFile(env31835[0], env31835[2]);
}
c31088_6 = 0;
break;
}
c31088_6 = 2;
env31835[7] = r142;
env31835[8] = JSON.parse(env31835[7]);
if (isIOS() && env31835[8]._local_uuid !== void 0) {
const r208 = env31835[8];
const r213 = env31835[4].get(env31835[8]._local_uuid);
app_state = r213;
if (r213 == null) {
app_state = "background";
}
r208.app_state = app_state;
}
const PUSH_NOTIFICATION_RECEIVED = AnalyticEvents.PUSH_NOTIFICATION_RECEIVED;
const r182 = {
notification_received_timestamp: (0, env31835[11])(env31835[8].timestamp),
push_action_type: env31835[8].push_action_type,
notif_instance_id: env31835[8].notif_instance_id,
notif_type_id: env31835[8].notif_type_id,
join_id: env31835[8].join_id,
notif_user_id: env31835[8].notif_user_id,
receiving_user_id: env31835[8].receiving_user_id,
message_id: env31835[8].message_id,
message_type: env31835[8].message_type,
guild_id: env31835[8].guild_id,
channel_id: env31835[8].channel_id,
channel_type: null,
rel_type: null,
mention_type: null,
app_state: null,
os_enabled: null
};
const channel_type = env31835[8].channel_type;
r182.channel_type = channel_type == null ? void 0 : channel_type.toString();
r182.rel_type = env31835[8].rel_type;
r182.mention_type = env31835[8].mention_type;
r182.app_state = env31835[8].app_state;
r182.os_enabled = env31835[8].os_enabled;
AnalyticsUtils.track(PUSH_NOTIFICATION_RECEIVED, r182);
c31088_6 = 1;
}
} catch ($e) {
const r57 = $e;
let r7 = r57;
c31088_5 = r57;
let r58 = c31088_6;
if (r58 !== 0) {
while (true) {
if (r58 === 1) {
try {
const r356 = c31088_5;
c31088_6 = 0;
env31835[12] = r356;
c11984_8.error("Error tracking push notifications", env31835[12]);
break L34;
} catch (r353) {
r7 = r353;
c31088_5 = r353;
r58 = c31088_6;
if (r58 === 0) {
break;
}
}
} else {
try {
const r348 = c31088_5;
c31088_6 = 1;
const r350 = c31088_0;
if (r350 !== void 0 && typeof r350.return === "function") {
try {
r350.return();
} catch {}
}
throw r348;
} catch (r347) {
r7 = r347;
c31088_5 = r347;
r58 = c31088_6;
if (r58 === 0) {
break;
}
}
}
}
}
throw r7;
}
}
return void 0;
}
};
this.handleSetCallNotificationExperiment = () => {
if (!isIOS()) {
const DCDNotificationManager = NativeModules.DCDNotificationManager;
const setShowMissedCallNotifications = DCDNotificationManager.setShowMissedCallNotifications;
if (setShowMissedCallNotifications != null) {
setShowMissedCallNotifications.call(DCDNotificationManager, true);
}
const DCDNotificationManager2 = NativeModules.DCDNotificationManager;
const setShowFullscreenCallUI = DCDNotificationManager2.setShowFullscreenCallUI;
if (setShowFullscreenCallUI != null) {
setShowFullscreenCallUI.call(DCDNotificationManager2, true);
}
}
};
this.updateAndClearStaleNotifications = () => {
updateAndClearStaleNotifications2();
};
this.actions = {
MESSAGE_ACK: this.handleAck,
CHANNEL_SELECT: this.handleAck,
POST_CONNECTION_OPEN: this.handlePostConnectionOpen,
EXPERIMENT_OVERRIDE_BUCKET: this.handleSetCallNotificationExperiment,
EXPERIMENTS_FETCH_SUCCESS: this.handleSetCallNotificationExperiment,
APP_STATE_UPDATE: this.updateAndClearStaleNotifications
};
}
}
c11984_14 = NativeNotificationsManager;
const r42 = new NativeNotificationsManager();
export default r42;