Skip to content

Commit

Permalink
Bugfix/discord tray icon incompatibility (#741)
Browse files Browse the repository at this point in the history
* Fixes false positive virus detection problems with Windows build

* Initial fix

* Adds logging

* Refactoring

* More refactoring

* Smalle changes

* Some renames

* Works

* Works well

* Major rewrite
  • Loading branch information
digimezzo authored Dec 24, 2024
1 parent 2b0d5b8 commit b841f96
Show file tree
Hide file tree
Showing 120 changed files with 504 additions and 342 deletions.
51 changes: 30 additions & 21 deletions .github/workflows/merge.yml
Original file line number Diff line number Diff line change
Expand Up @@ -86,24 +86,33 @@ jobs:
runs-on: windows-2019
if: github.event.pull_request.merged == true
steps:
- uses: actions/checkout@v2
- uses: actions/setup-node@master
with:
node-version: '20.x'
- name: install dependencies
run: npm install
- name: run unit tests
run: npm run test
- name: build
run: npm run electron:windows
- name: fetch current version
id: ver_name
uses: notiz-dev/github-action-json-property@release
with:
path: 'package.json'
prop_path: 'version'
- name: upload build artifacts
uses: actions/upload-artifact@v4
with:
name: dopamine-merge-win
path: release\Dopamine-*.exe
- uses: actions/checkout@v2
- uses: actions/setup-node@master
with:
node-version: '20.x'
- name: Install dependencies
run: npm install
- name: Run unit tests
run: npm run test
- name: Build
run: npm run electron:windows
- name: Fetch current version
id: ver_name
uses: notiz-dev/github-action-json-property@release
with:
path: 'package.json'
prop_path: 'version'
- name: Download and Install 7-Zip
run: |
curl -L -o 7z.exe https://www.7-zip.org/a/7z2201-x64.exe
Start-Process -FilePath 7z.exe -ArgumentList '/S' -Wait
- name: Create 7z Archive With Only EXE Files
shell: cmd
run: |
cd release
"C:\Program Files\7-Zip\7z.exe" a ../dopamine-nightly-win.7z Dopamine-*.exe
- name: Upload Build Artifact
uses: actions/upload-artifact@v4
with:
name: dopamine-nightly-win
path: dopamine-nightly-win.7z
55 changes: 32 additions & 23 deletions .github/workflows/nightly.yml
Original file line number Diff line number Diff line change
Expand Up @@ -82,26 +82,35 @@ jobs:
path: release/Dopamine-*.dmg

nightly_build_on_win:
runs-on: windows-2019
steps:
- uses: actions/checkout@v2
- uses: actions/setup-node@master
with:
node-version: '20.x'
- name: install dependencies
run: npm install
- name: run unit tests
run: npm run test
- name: build
run: npm run electron:windows
- name: fetch current version
id: ver_name
uses: notiz-dev/github-action-json-property@release
with:
path: 'package.json'
prop_path: 'version'
- name: upload build artifacts
uses: actions/upload-artifact@v4
with:
name: dopamine-nightly-win
path: release\Dopamine-*.exe
runs-on: windows-2019
steps:
- uses: actions/checkout@v2
- uses: actions/setup-node@master
with:
node-version: '20.x'
- name: Install dependencies
run: npm install
- name: Run unit tests
run: npm run test
- name: Build
run: npm run electron:windows
- name: Fetch current version
id: ver_name
uses: notiz-dev/github-action-json-property@release
with:
path: 'package.json'
prop_path: 'version'
- name: Download and Install 7-Zip
run: |
curl -L -o 7z.exe https://www.7-zip.org/a/7z2201-x64.exe
Start-Process -FilePath 7z.exe -ArgumentList '/S' -Wait
- name: Create 7z Archive With Only EXE Files
shell: cmd
run: |
cd release
"C:\Program Files\7-Zip\7z.exe" a ../dopamine-nightly-win.7z Dopamine-*.exe
- name: Upload Build Artifact
uses: actions/upload-artifact@v4
with:
name: dopamine-nightly-win
path: dopamine-nightly-win.7z
17 changes: 16 additions & 1 deletion main.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion main.js.map

Large diffs are not rendered by default.

20 changes: 19 additions & 1 deletion main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@ import * as Store from 'electron-store';
import * as path from 'path';
import * as url from 'url';
import { Worker } from 'worker_threads';
import { DiscordApi } from './main/api/discord/discord-api';
import { SensitiveInformation } from './main/common/application/sensitive-information';
import { DiscordApiCommand } from './main/api/discord/discord-api-command';
import { DiscordApiCommandType } from './main/api/discord/discord-api-command-type';

/**
* Command line parameters
Expand All @@ -35,6 +39,8 @@ const globalAny: any = global; // Global does not allow setting custom propertie
const settings: Store<any> = new Store();
const args: string[] = process.argv.slice(1);
const isServing: boolean = args.some((val) => val === '--serve');
const discordApi = new DiscordApi(SensitiveInformation.discordClientId);

let mainWindow: BrowserWindow | undefined;
let tray: Tray;
let isQuitting: boolean;
Expand Down Expand Up @@ -480,6 +486,10 @@ try {
isQuit = true;
});

app.on('will-quit', () => {
discordApi.shutdown();
});

app.whenReady().then(() => {
// See: https://github.com/electron/electron/issues/23757
protocol.registerFileProtocol('file', (request, callback) => {
Expand Down Expand Up @@ -544,7 +554,7 @@ try {
});

ipcMain.on('indexing-worker', (event: any, arg: any) => {
const workerThread = new Worker(path.join(__dirname, 'main/workers/indexing-worker.js'), {
const workerThread = new Worker(path.join(__dirname, 'main/background-work/workers/indexing-worker.js'), {
workerData: { arg },
});

Expand Down Expand Up @@ -607,6 +617,14 @@ try {
log.info('[Main] [clear-file-queue] Clearing file queue');
globalAny.fileQueue = [];
});

ipcMain.on('discord-api-command', (event: any, command: DiscordApiCommand) => {
if (command.commandType === DiscordApiCommandType.SetPresence) {
discordApi.setPresence(command.args!);
} else if (command.commandType === DiscordApiCommandType.ClearPresence) {
discordApi.clearPresence();
}
});
}
} catch (e) {
log.error(`[Main] [Main] Could not start. Error: ${e.message}`);
Expand Down
4 changes: 4 additions & 0 deletions main/api/discord/discord-api-command-type.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export enum DiscordApiCommandType {
SetPresence,
ClearPresence,
}
9 changes: 9 additions & 0 deletions main/api/discord/discord-api-command.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { PresenceArgs } from './presence-args';
import { DiscordApiCommandType } from './discord-api-command-type';

export class DiscordApiCommand {
public constructor(
public commandType: DiscordApiCommandType,
public args: PresenceArgs | undefined,
) {}
}
100 changes: 100 additions & 0 deletions main/api/discord/discord-api.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
import { Client, Presence } from 'discord-rpc';
import log from 'electron-log';
import { PresenceArgs } from './presence-args';

export class DiscordApi {
private readonly _clientId: string;
private _client: Client;
private _isReady: boolean;
private _presenceToSetWhenReady: PresenceArgs | undefined;
private _isReconnecting: boolean = false;

public constructor(clientId: string) {
this._clientId = clientId;
}

private reconnect(): void {
if (this._isReconnecting) {
log.info('[DiscordApi] [reconnect] Already attempting to reconnect. Skipping...');
return;
}

this._isReconnecting = true;
this._isReady = false;
this._client = new Client({ transport: 'ipc' });

this._client.on('ready', () => {
log.info('[DiscordApi] [ready] Discord client is ready!');
this._isReady = true;

if (this._presenceToSetWhenReady) {
this.setPresence(this._presenceToSetWhenReady);
this._presenceToSetWhenReady = undefined;
}
});

this._client.on('disconnected', () => {
log.info('[DiscordApi] [disconnected] Discord client disconnected. Attempting to reconnect...');
this._isReady = false;
this.login();
});

this.login();
}

private async login(): Promise<void> {
try {
await this._client.login({ clientId: this._clientId });
log.info('[DiscordApi] [login] Successfully logged into Discord client');
} catch (error) {
log.error(`[DiscordApi] [login] Failed to log into Discord client: ${error}`);
} finally {
this._isReconnecting = false;
}
}

public setPresence(args: PresenceArgs): void {
if (!this._isReady) {
log.warn('[DiscordApi] [setPresence] Discord client is not ready. Attempting reconnect.');
this._presenceToSetWhenReady = args;
this.reconnect();

return;
}

const presence: Presence = {
details: `${args.title}`,
state: `${args.artists}`,
largeImageKey: args.largeImageKey,
largeImageText: args.largeImageText,
smallImageKey: args.smallImageKey,
smallImageText: args.smallImageText,
};

if (args.shouldSendTimestamps && args.startTime) {
presence.startTimestamp = Math.floor(args.startTime / 1000);
}

this._client.setActivity(presence);
log.info(`[DiscordApi] [setPresence] Rich Presence updated: ${presence.state} - ${presence.details} (${presence.smallImageKey})`);
}

public clearPresence(): void {
this._presenceToSetWhenReady = undefined;

if (!this._isReady) {
log.warn('[DiscordApi] [clearPresence] Discord client is not ready. Cannot clear presence.');
return;
}

this._client.clearActivity();
log.info('[DiscordApi] [clearPresence] Rich Presence cleared.');
}

public shutdown(): void {
this._presenceToSetWhenReady = undefined;

this._client.destroy();
log.info('[DiscordApi] [shutdown] Discord client destroyed.');
}
}
10 changes: 10 additions & 0 deletions main/api/discord/presence-args.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
export interface PresenceArgs {
title: string;
artists: string;
smallImageKey?: string;
smallImageText?: string;
largeImageKey?: string;
largeImageText?: string;
shouldSendTimestamps?: boolean;
startTime?: number;
}
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
2 changes: 1 addition & 1 deletion main/ioc/ioc.js → main/background-work/ioc/ioc.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
const { DateTime } = require('../common/date-time');
const { FileAccess } = require('../common/io/file-access');
const { WorkerProxy } = require('../workers/worker-proxy');
const { WorkerProxy } = require('../worker-proxy');
const { DatabaseFactory } = require('../data/database.factory');
const { FolderRepository } = require('../data/folder-repository');
const { FolderTrackRepository } = require('../data/folder-track-repository');
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
3 changes: 3 additions & 0 deletions main/common/application/sensitive-information.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export class SensitiveInformation {
public static readonly discordClientId: string = '826521040275636325';
}
2 changes: 1 addition & 1 deletion src/app/app.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ export class AppComponent implements OnInit {

this.audioVisualizer.initialize();
await this.addToPlaylistMenu.initializeAsync();
this.discordService.setRichPresenceFromSettings();
this.discordService.initialize();
await this.appearanceService.applyAppearanceAsync();
this.translatorService.applyLanguage();
this.trayService.updateTrayContextMenu();
Expand Down
3 changes: 0 additions & 3 deletions src/app/app.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -171,8 +171,6 @@ import { ArtistInformationService } from './services/artist-information/artist-i
import { ArtistService } from './services/artist/artist.service';
import { CollectionService } from './services/collection/collection.service';
import { DialogService } from './services/dialog/dialog.service';
import { DiscordService } from './services/discord/discord.service';
import { PresenceUpdater } from './services/discord/presence-updater';
import { ElectronService } from './services/electron.service';
import { FileService } from './services/file/file.service';
import { FolderService } from './services/folder/folder.service';
Expand Down Expand Up @@ -516,7 +514,6 @@ export function appInitializerFactory(translate: TranslateService, injector: Inj
WebSearchApi,
MetadataPatcher,
TracksColumnsOrdering,
PresenceUpdater,
SemanticZoomHeaderAdder,
DefaultThemesCreator,
ArtistsPersister,
Expand Down
1 change: 0 additions & 1 deletion src/app/common/application/sensitive-information.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,4 @@ export class SensitiveInformation {
public static readonly lastfmApiKey: string = '1be93666a6e8d3d79b05b1c74682cb62';
public static readonly lastfmSharedSecret: string = 'c71f3fbae4ee6be95664d5406261094e';
public static readonly fanartApiKey: string = '80d1c44e9b315f0c1963857e4d671a85';
public static readonly discordClientId: string = '826521040275636325';
}
4 changes: 4 additions & 0 deletions src/app/services/discord/discord-api-command-type.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export enum DiscordApiCommandType {
SetPresence,
ClearPresence,
}
Loading

0 comments on commit b841f96

Please sign in to comment.