Skip to content

Commit

Permalink
Combines IPC Listeners & moves big sur check to post checks
Browse files Browse the repository at this point in the history
  • Loading branch information
zlshames committed Dec 21, 2021
1 parent 0c14c72 commit 1b10f74
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 68 deletions.
31 changes: 13 additions & 18 deletions src/main/server/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,9 @@ class BlueBubblesServer extends EventEmitter {
// Initialize and connect to the server database
await this.initDatabase();

this.log("Starting IPC Listeners..");
IPCService.startIpcListeners();

// Do some pre-flight checks
// Make sure settings are correct and all things are a go
await this.preChecks();
Expand All @@ -236,9 +239,11 @@ class BlueBubblesServer extends EventEmitter {
async initDatabase(): Promise<void> {
this.log("Initializing server database...");
this.repo = new ServerRepository();
this.repo.on("config-update", (args: ServerConfigChange) => this.handleConfigUpdate(args));
await this.repo.initialize();

// Handle when something in the config changes
this.repo.on("config-update", (args: ServerConfigChange) => this.handleConfigUpdate(args));

try {
this.log("Connecting to iMessage database...");
this.iMessageRepo = new MessageRepository();
Expand Down Expand Up @@ -448,13 +453,6 @@ class BlueBubblesServer extends EventEmitter {
this.log("Starting Services...");
await this.startServices();

// Start the rest of the IPC listeners
// This handles all the listeners that depend on services
if (this.hasDiskAccess) {
this.log("Starting IPC Listeners..");
IPCService.startIpcListener();
}

// Perform any post-setup tasks/checks
await this.postChecks();

Expand Down Expand Up @@ -483,11 +481,6 @@ class BlueBubblesServer extends EventEmitter {
private async initServerComponents(): Promise<void> {
this.log("Initializing Server Components...");

if (this.hasDiskAccess) {
this.log("Starting Configuration IPC Listeners..");
IPCService.startConfigIpcListeners();
}

// Load notification count
try {
this.log("Initializing alert service...");
Expand Down Expand Up @@ -584,14 +577,10 @@ class BlueBubblesServer extends EventEmitter {
this.log(`Server Metadata -> macOS Version: v${osVersion}`, "debug");
this.log(`Server Metadata -> Local Timezone: ${Intl.DateTimeFormat().resolvedOptions().timeZone}`, "debug");

// Check if on Big Sur. If we are, then create a log/alert saying that
if (isMinBigSur) {
this.log("Warning: macOS Big Sur does NOT support creating group chats due to API limitations!", "warn");
}

// If the user is on el capitan, we need to force cloudflare
const proxyService = this.repo.getConfig("proxy_service") as string;
if (!isMinSierra && proxyService === "Ngrok") {
this.log("El Capitan detected. Forcing Cloudflare Proxy");
await this.repo.setConfig("proxy_service", "Cloudflare");
}

Expand Down Expand Up @@ -635,6 +624,12 @@ class BlueBubblesServer extends EventEmitter {
}

this.setDockIcon();

// Check if on Big Sur. If we are, then create a log/alert saying that
if (isMinBigSur) {
this.log("Warning: macOS Big Sur does NOT support creating group chats due to API limitations!", "warn");
}

this.log("Finished post-start checks...");
}

Expand Down
94 changes: 44 additions & 50 deletions src/main/server/services/ipcService/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,59 +8,10 @@ import { fixServerUrl } from "@server/helpers/utils";
import { BlueBubblesHelperService } from "../privateApi";

export class IPCService {
/**
* Starts the inter-process-communication handlers. Basically, a router
* for all requests sent by the Electron front-end
*/
static startIpcListener() {
ipcMain.handle("get-message-count", async (event, args) => {
if (!Server().iMessageRepo?.db) return 0;
const count = await Server().iMessageRepo.getMessageCount(args?.after, args?.before, args?.isFromMe);
return count;
});

ipcMain.handle("get-chat-image-count", async (event, args) => {
if (!Server().iMessageRepo?.db) return 0;
const count = await Server().iMessageRepo.getMediaCountsByChat({ mediaType: "image" });
return count;
});

ipcMain.handle("get-chat-video-count", async (event, args) => {
if (!Server().iMessageRepo?.db) return 0;
const count = await Server().iMessageRepo.getMediaCountsByChat({ mediaType: "video" });
return count;
});

ipcMain.handle("get-group-message-counts", async (event, args) => {
if (!Server().iMessageRepo?.db) return 0;
const count = await Server().iMessageRepo.getChatMessageCounts("group");
return count;
});

ipcMain.handle("get-individual-message-counts", async (event, args) => {
if (!Server().iMessageRepo?.db) return 0;
const count = await Server().iMessageRepo.getChatMessageCounts("individual");
return count;
});

ipcMain.handle("get-devices", async (event, args) => {
// eslint-disable-next-line no-return-await
return await Server().repo.devices().find();
});

ipcMain.handle("get-fcm-server", (event, args) => {
return FileSystem.getFCMServer();
});

ipcMain.handle("get-fcm-client", (event, args) => {
return FileSystem.getFCMClient();
});
}

/**
* Starts configuration related inter-process-communication handlers.
*/
static startConfigIpcListeners() {
static startIpcListeners() {
ipcMain.handle("set-config", async (_, args) => {
// Make sure that the server address being sent is using https
if (args.server_address) {
Expand Down Expand Up @@ -107,6 +58,19 @@ export class IPCService {
await Server().fcm.start();
});

ipcMain.handle("get-devices", async (event, args) => {
// eslint-disable-next-line no-return-await
return await Server().repo.devices().find();
});

ipcMain.handle("get-fcm-server", (event, args) => {
return FileSystem.getFCMServer();
});

ipcMain.handle("get-fcm-client", (event, args) => {
return FileSystem.getFCMClient();
});

ipcMain.handle("toggle-tutorial", async (_, toggle) => {
await Server().repo.setConfig("tutorial_is_done", toggle);

Expand All @@ -116,6 +80,36 @@ export class IPCService {
}
});

ipcMain.handle("get-message-count", async (event, args) => {
if (!Server().iMessageRepo?.db) return 0;
const count = await Server().iMessageRepo.getMessageCount(args?.after, args?.before, args?.isFromMe);
return count;
});

ipcMain.handle("get-chat-image-count", async (event, args) => {
if (!Server().iMessageRepo?.db) return 0;
const count = await Server().iMessageRepo.getMediaCountsByChat({ mediaType: "image" });
return count;
});

ipcMain.handle("get-chat-video-count", async (event, args) => {
if (!Server().iMessageRepo?.db) return 0;
const count = await Server().iMessageRepo.getMediaCountsByChat({ mediaType: "video" });
return count;
});

ipcMain.handle("get-group-message-counts", async (event, args) => {
if (!Server().iMessageRepo?.db) return 0;
const count = await Server().iMessageRepo.getChatMessageCounts("group");
return count;
});

ipcMain.handle("get-individual-message-counts", async (event, args) => {
if (!Server().iMessageRepo?.db) return 0;
const count = await Server().iMessageRepo.getChatMessageCounts("individual");
return count;
});

ipcMain.handle("check_perms", async (_, __) => {
return {
abPerms: systemPreferences.isTrustedAccessibilityClient(false) ? "authorized" : "denied",
Expand Down

0 comments on commit 1b10f74

Please sign in to comment.