Skip to content

Add slack scrobbler #3242

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -256,6 +256,7 @@
"@skyra/jaro-winkler": "1.1.1",
"@xhayper/discord-rpc": "1.2.1",
"async-mutex": "0.5.0",
"axios": "^1.8.4",
"bgutils-js": "3.2.0",
"butterchurn": "3.0.0-beta.4",
"butterchurn-presets": "3.0.0-beta.4",
Expand All @@ -273,6 +274,7 @@
"fast-average-color": "9.5.0",
"fast-equals": "5.2.2",
"filenamify": "6.0.0",
"form-data": "^4.0.2",
"hanja": "1.1.4",
"happy-dom": "17.4.4",
"hono": "4.7.6",
Expand Down Expand Up @@ -311,6 +313,7 @@
"@stylistic/eslint-plugin-js": "4.2.0",
"@total-typescript/ts-reset": "0.6.1",
"@types/electron-localshortcut": "3.1.3",
"@types/form-data": "^2.5.2",
"@types/howler": "2.2.12",
"@types/html-to-text": "9.0.4",
"@types/semver": "7.7.0",
Expand Down
44 changes: 44 additions & 0 deletions pnpm-lock.yaml

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

28 changes: 28 additions & 0 deletions src/plugins/scrobbler/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,28 @@ export interface ScrobblerPluginConfig {
*/
apiRoot: string;
};
slack: {
/**
* Enable Slack scrobbling
*
* @default false
*/
enabled: boolean;
/**
* Slack OAuth token
*/
token: string | undefined;
/**
* Slack cookie token (d cookie value)
*/
cookieToken: string | undefined;
/**
* Name to use for the custom emoji in Slack
*
* @default 'my-album-art'
*/
emojiName: string;
};
};
}

Expand All @@ -92,6 +114,12 @@ export const defaultConfig: ScrobblerPluginConfig = {
token: undefined,
apiRoot: 'https://api.listenbrainz.org/1/',
},
slack: {
enabled: false,
token: undefined,
cookieToken: undefined,
emojiName: 'my-album-art',
},
},
};

Expand Down
7 changes: 7 additions & 0 deletions src/plugins/scrobbler/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { createBackend } from '@/utils';

import { LastFmScrobbler } from './services/lastfm';
import { ListenbrainzScrobbler } from './services/listenbrainz';
import { SlackScrobbler } from './services/slack';

import type { ScrobblerPluginConfig } from './index';
import type { ScrobblerBase } from './services/base';
Expand Down Expand Up @@ -51,6 +52,12 @@ export const backend = createBackend<
} else {
this.enabledScrobblers.delete('listenbrainz');
}

if (config.scrobblers.slack && config.scrobblers.slack.enabled) {
this.enabledScrobblers.set('slack', new SlackScrobbler(window));
} else {
this.enabledScrobblers.delete('slack');
}
},

async createSessions(config: ScrobblerPluginConfig, setConfig: SetConfType) {
Expand Down
78 changes: 78 additions & 0 deletions src/plugins/scrobbler/menu.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,63 @@ async function promptListenbrainzOptions(
}
}

async function promptSlackOptions(
options: ScrobblerPluginConfig,
setConfig: SetConfType,
window: BrowserWindow,
) {
const output = await prompt(
{
title: 'Slack Settings',
label: 'Slack Settings',
type: 'multiInput',
multiInputOptions: [
{
label: 'Slack OAuth Token',
value: options.scrobblers.slack?.token,
inputAttrs: {
type: 'text',
},
},
{
label: 'Slack Cookie Token (d cookie value)',
value: options.scrobblers.slack?.cookieToken,
inputAttrs: {
type: 'text',
},
},
{
label: 'Emoji Name (for album art upload)',
value: options.scrobblers.slack?.emojiName,
inputAttrs: {
type: 'text',
},
},
],
resizable: true,
height: 360,
...promptOptions(),
},
window,
);

if (output) {
if (output[0]) {
options.scrobblers.slack.token = output[0];
}

if (output[1]) {
options.scrobblers.slack.cookieToken = output[1];
}

if (output[2]) {
options.scrobblers.slack.emojiName = output[2];
}

setConfig(options);
}
}

export const onMenu = async ({
window,
getConfig,
Expand Down Expand Up @@ -147,5 +204,26 @@ export const onMenu = async ({
},
],
},
{
label: 'Slack',
submenu: [
{
label: t('main.menu.plugins.enabled'),
type: 'checkbox',
checked: Boolean(config.scrobblers.slack?.enabled),
click(item) {
backend.toggleScrobblers(config, window);
config.scrobblers.slack.enabled = item.checked;
setConfig(config);
},
},
{
label: 'Slack Settings',
click() {
promptSlackOptions(config, setConfig, window);
},
},
],
},
];
};
51 changes: 51 additions & 0 deletions src/plugins/scrobbler/services/slack-api-client.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
import axios, { AxiosResponse } from 'axios';

/**
* Centralized Slack API client for all requests
*/
export class SlackApiClient {
readonly token: string;
readonly cookie: string;

constructor(token: string, cookie: string) {
this.token = token;
this.cookie = cookie;
}

private getBaseHeaders(): Record<string, string> {
return {
'Cookie': `d=${this.cookie}`,
};
}

/**
* POST to a Slack API endpoint
*/
async post(endpoint: string, data: any, formData = false): Promise<AxiosResponse> {
const url = `https://slack.com/api/${endpoint}`;
let headers = this.getBaseHeaders();
let payload = data;
if (formData) {
headers = { ...headers, ...data.getHeaders() };
} else {
headers['Content-Type'] = 'application/x-www-form-urlencoded';
payload = new URLSearchParams(data).toString();
}
return axios.post(url, payload, { headers, maxBodyLength: Infinity, validateStatus: () => true });
}

/**
* GET from a Slack API endpoint
*/
async get(endpoint: string, params: Record<string, any> = {}): Promise<AxiosResponse> {
const url = `https://slack.com/api/${endpoint}`;
const headers = this.getBaseHeaders();
return axios.get(url, { headers, params, validateStatus: () => true });
}
}

export interface SlackApiResponse {
ok: boolean;
error?: string;
[key: string]: any;
}
Loading