feat(INDA-383): daily stats.
[ProtonMail-WebClient.git] / applications / inbox-desktop / src / ipc / main.ts
blob6f64de1c89bda2cd8a636edcd3dc2e24c7a4d58d
1 import { IpcMainEvent, ipcMain, shell } from "electron";
2 import { setReleaseCategory } from "../store/settingsStore";
3 import { cachedLatestVersion } from "../update";
4 import type {
5     IPCInboxClientUpdateMessage,
6     IPCInboxGetInfoMessage,
7     IPCInboxGetUserInfoMessage,
8 } from "@proton/shared/lib/desktop/desktopTypes";
9 import { clearStorage } from "../utils/helpers";
10 import { ipcLogger } from "../utils/log";
11 import { getColorScheme, getTheme, isEqualTheme, setTheme } from "../utils/themes";
12 import {
13     reloadHiddenViews,
14     resetHiddenViews,
15     showEndOfTrial,
16     showView,
17     getMailView,
18 } from "../utils/view/viewManagement";
19 import { DESKTOP_FEATURES } from "./ipcConstants";
20 import { handleIPCBadge, resetBadge, showNotification } from "./notification";
21 import { setInstallSourceReported, getInstallSource } from "../store/installInfoStore";
22 import { getESUserChoice, setESUserChoice } from "../store/userSettingsStore";
23 import { checkDefaultMailto, getDefaultMailto, setDefaultMailtoTelemetryReported } from "../utils/protocol/default";
24 import { getAllAppVersions, storeAppVersion } from "../utils/appVersions";
25 import metrics from "../utils/metrics";
26 import telemetry from "../utils/telemetry";
28 function isValidClientUpdateMessage(message: unknown): message is IPCInboxClientUpdateMessage {
29     return Boolean(message && typeof message === "object" && "type" in message && "payload" in message);
32 export const handleIPCCalls = () => {
33     ipcMain.on("hasFeature", (event: IpcMainEvent, message: keyof typeof DESKTOP_FEATURES) => {
34         event.returnValue = !!DESKTOP_FEATURES[message];
35     });
37     ipcMain.on("getUserInfo", (event: IpcMainEvent, message: IPCInboxGetUserInfoMessage["type"], userID: string) => {
38         switch (message) {
39             case "esUserChoice":
40                 event.returnValue = getESUserChoice(userID);
41                 break;
42             default:
43                 ipcLogger.error(`Invalid getUserInfo message: ${message}`);
44                 break;
45         }
46     });
48     ipcMain.on("getInfo", (event: IpcMainEvent, message: IPCInboxGetInfoMessage["type"]) => {
49         switch (message) {
50             case "theme":
51                 event.returnValue = getTheme();
52                 break;
53             case "latestVersion":
54                 event.returnValue = cachedLatestVersion;
55                 break;
56             case "installSource": {
57                 const installSource = getInstallSource();
58                 event.returnValue = installSource;
59                 setInstallSourceReported();
60                 break;
61             }
62             case "defaultMailto": {
63                 event.returnValue = getDefaultMailto();
64                 break;
65             }
66             case "dailyStats": {
67                 event.returnValue = telemetry.getDailyStats();
68                 break;
69             }
70             case "colorScheme":
71                 event.returnValue = getColorScheme();
72                 break;
73             case "getAllAppVersions":
74                 event.returnValue = getAllAppVersions();
75                 break;
76             default:
77                 ipcLogger.error(`Invalid getInfo message: ${message}`);
78                 break;
79         }
80     });
82     ipcMain.on("clientUpdate", (_e, message: unknown) => {
83         if (!isValidClientUpdateMessage(message)) {
84             ipcLogger.error(`Invalid clientUpdate message: ${message}`);
85             return;
86         }
88         const { type, payload } = message;
90         switch (type) {
91             case "updateNotification":
92                 handleIPCBadge(payload);
93                 break;
94             case "userLogin":
95                 resetHiddenViews();
96                 telemetry.userLogin();
97                 break;
98             case "userLogout":
99                 resetHiddenViews();
100                 resetBadge();
101                 telemetry.userLogout();
102                 break;
103             case "clearAppData":
104                 clearStorage(true, 500);
105                 resetBadge();
106                 break;
107             case "oauthPopupOpened": {
108                 const enabled = payload === "oauthPopupStarted";
109                 global.oauthProcess = enabled;
110                 ipcLogger.debug("oauthProcess", enabled ? "enabled" : "disabled");
111                 break;
112             }
113             case "subscriptionModalOpened": {
114                 const enabled = payload === "subscriptionModalStarted";
115                 global.subscriptionProcess = enabled;
116                 ipcLogger.debug("subscriptionProcess", enabled ? "enabled" : "disabled");
117                 break;
118             }
119             case "openExternal":
120                 shell.openExternal(payload);
121                 break;
122             case "trialEnd":
123                 resetBadge();
124                 showEndOfTrial();
125                 break;
126             case "changeView":
127                 showView(payload);
128                 break;
129             case "showNotification":
130                 showNotification(payload);
131                 break;
132             case "updateLocale":
133                 reloadHiddenViews();
134                 break;
135             case "setTheme": {
136                 if (!isEqualTheme(getTheme(), payload)) {
137                     setTheme(payload);
138                     reloadHiddenViews();
139                 }
140                 break;
141             }
142             case "earlyAccess": {
143                 setReleaseCategory(payload);
144                 break;
145             }
146             case "checkDefaultMailtoAndSignal": {
147                 checkDefaultMailto();
148                 const defaultMailto = getDefaultMailto();
150                 getMailView()?.webContents?.send("hostUpdate", {
151                     type: "defaultMailtoChecked",
152                     payload: defaultMailto,
153                 });
154                 break;
155             }
156             case "defaultMailtoTelemetryReported": {
157                 setDefaultMailtoTelemetryReported(payload);
158                 break;
159             }
160             case "checkDailyStatsAndSignal": {
161                 telemetry.checkDailyStats();
162                 const dailyStatsReport = telemetry.getDailyStatsReport();
164                 getMailView()?.webContents?.send("hostUpdate", {
165                     type: "dailyStatsChecked",
166                     payload: dailyStatsReport,
167                 });
168                 break;
169             }
170             case "dailyStatsReported": {
171                 telemetry.dailyStatsReported(payload);
172                 break;
173             }
174             case "setESUserChoice": {
175                 setESUserChoice(payload.userID, payload.userChoice);
176                 break;
177             }
178             case "storeAppVersion":
179                 storeAppVersion(payload);
180                 break;
181             case "triggerCrash":
182                 throw new Error("Crash bandicoot");
183             case "metricsListenerChanged":
184                 if (payload === "ready") {
185                     metrics.listenerReady();
186                 }
187                 if (payload === "removed") {
188                     metrics.listenerRemoved();
189                 }
190                 break;
191             default:
192                 ipcLogger.error(`unknown message type: ${type}`);
193                 break;
194         }
195     });