From d47da91e930f44b62d3728b3c4d66f9cc6eaf2ab Mon Sep 17 00:00:00 2001 From: Rick van Lieshout Date: Mon, 20 May 2024 14:24:47 +0200 Subject: [PATCH] Added an API to add & delete entries from the skippedArtists list in the settings. fixes [#405] --- .../settings/skipped-artists/addArtists.http | 5 +++ .../settings/skipped-artists/addCurrent.http | 2 + .../skipped-artists/removeArtists.http | 5 +++ .../skipped-artists/removeCurrent.http | 2 + CHANGELOG.md | 6 +++ .../api/features/settings/settings.ts | 39 +++++++++++++++++++ src/features/api/index.ts | 3 ++ src/scripts/settings.ts | 22 +++++++++++ 8 files changed, 84 insertions(+) create mode 100644 .vscode/http/settings/skipped-artists/addArtists.http create mode 100644 .vscode/http/settings/skipped-artists/addCurrent.http create mode 100644 .vscode/http/settings/skipped-artists/removeArtists.http create mode 100644 .vscode/http/settings/skipped-artists/removeCurrent.http create mode 100644 src/features/api/features/settings/settings.ts diff --git a/.vscode/http/settings/skipped-artists/addArtists.http b/.vscode/http/settings/skipped-artists/addArtists.http new file mode 100644 index 0000000..73f3cd2 --- /dev/null +++ b/.vscode/http/settings/skipped-artists/addArtists.http @@ -0,0 +1,5 @@ +POST /settings/skipped-artists HTTP/1.1 +Host: localhost:47836 +Content-Type: application/json + +["abc", "def"] diff --git a/.vscode/http/settings/skipped-artists/addCurrent.http b/.vscode/http/settings/skipped-artists/addCurrent.http new file mode 100644 index 0000000..7f36ac0 --- /dev/null +++ b/.vscode/http/settings/skipped-artists/addCurrent.http @@ -0,0 +1,2 @@ +POST /settings/skipped-artists/current HTTP/1.1 +Host: localhost:47836 diff --git a/.vscode/http/settings/skipped-artists/removeArtists.http b/.vscode/http/settings/skipped-artists/removeArtists.http new file mode 100644 index 0000000..fcf415b --- /dev/null +++ b/.vscode/http/settings/skipped-artists/removeArtists.http @@ -0,0 +1,5 @@ +POST /settings/skipped-artists/delete HTTP/1.1 +Host: localhost:47836 +Content-Type: application/json + +["abc", "def"] diff --git a/.vscode/http/settings/skipped-artists/removeCurrent.http b/.vscode/http/settings/skipped-artists/removeCurrent.http new file mode 100644 index 0000000..c1634f0 --- /dev/null +++ b/.vscode/http/settings/skipped-artists/removeCurrent.http @@ -0,0 +1,2 @@ +DELETE /settings/skipped-artists/current HTTP/1.1 +Host: localhost:47836 diff --git a/CHANGELOG.md b/CHANGELOG.md index f19db6e..f6d0845 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,12 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ## [next] - Fixed [#403](https://github.com/Mastermindzh/tidal-hifi/issues/403) "cannot read shuffle of undefined" error +- Added an API to add & delete entries from the skippedArtists list in the settings. fixes [#405](https://github.com/Mastermindzh/tidal-hifi/issues/405) + - `GET /settings/skipped-artists` -> get list of skipped artists + - `POST /settings/skipped-artists` -> add to the list of skipped artists + - `POST /settings/skipped-artists/delete` -> delete from the list of skipped artists + - `POST /settings/skipped-artists/current` -> skip the current artist + - `DELETE /settings/skipped-artists/current` -> delete the current artist from the skip list ## [5.12.0] diff --git a/src/features/api/features/settings/settings.ts b/src/features/api/features/settings/settings.ts new file mode 100644 index 0000000..ea3c592 --- /dev/null +++ b/src/features/api/features/settings/settings.ts @@ -0,0 +1,39 @@ +import { Request, Router } from "express"; +import { settings } from "../../../../constants/settings"; +import { mediaInfo } from "../../../../scripts/mediaInfo"; +import { + addSkippedArtists, + removeSkippedArtists, + settingsStore, +} from "../../../../scripts/settings"; +import { BrowserWindow } from "electron"; +import { globalEvents } from "../../../../constants/globalEvents"; + +export const addSettingsAPI = (expressApp: Router, mainWindow: BrowserWindow) => { + expressApp.get("/settings/skipped-artists", (req, res) => { + res.json(settingsStore.get(settings.skippedArtists)); + }); + + expressApp.post("/settings/skipped-artists", (req: Request, res) => { + addSkippedArtists(req.body); + res.sendStatus(200); + }); + + expressApp.post( + "/settings/skipped-artists/delete", + (req: Request, res) => { + removeSkippedArtists(req.body); + res.sendStatus(200); + } + ); + + expressApp.post("/settings/skipped-artists/current", (req, res) => { + addSkippedArtists([mediaInfo.artists]); + mainWindow.webContents.send("globalEvent", globalEvents.next); + res.sendStatus(200); + }); + expressApp.delete("/settings/skipped-artists/current", (req, res) => { + removeSkippedArtists([mediaInfo.artists]); + res.sendStatus(200); + }); +}; diff --git a/src/features/api/index.ts b/src/features/api/index.ts index a2f1547..bc47373 100644 --- a/src/features/api/index.ts +++ b/src/features/api/index.ts @@ -4,6 +4,7 @@ import { settings } from "../../constants/settings"; import { settingsStore } from "../../scripts/settings"; import { addCurrentInfo } from "./features/current"; import { addPlaybackControl } from "./features/player"; +import { addSettingsAPI } from "./features/settings/settings"; import { addLegacyApi } from "./legacy"; /** @@ -11,12 +12,14 @@ import { addLegacyApi } from "./legacy"; */ export const startApi = (mainWindow: BrowserWindow) => { const expressApp = express(); + expressApp.use(express.json()); expressApp.get("/", (req, res) => res.send("Hello World!")); // add features addLegacyApi(expressApp, mainWindow); addPlaybackControl(expressApp, mainWindow); addCurrentInfo(expressApp); + addSettingsAPI(expressApp, mainWindow); const port = settingsStore.get(settings.apiSettings.port); const expressInstance = expressApp.listen(port, "127.0.0.1"); diff --git a/src/scripts/settings.ts b/src/scripts/settings.ts index 7c0536d..f77b591 100644 --- a/src/scripts/settings.ts +++ b/src/scripts/settings.ts @@ -159,3 +159,25 @@ export const hideSettingsWindow = function () { export const closeSettingsWindow = function () { settingsWindow = null; }; + +/** + * add artists to the list of skipped artists + * @param artists list of artists to append + */ +export const addSkippedArtists = (artists: string[]) => { + const { skippedArtists } = settings; + const previousStoreValue = settingsStore.get(skippedArtists); + settingsStore.set(skippedArtists, Array.from(new Set([...previousStoreValue, ...artists]))); +}; + +/** + * Remove artists from the list of skipped artists + * @param artists list of artists to remove + */ +export const removeSkippedArtists = (artists: string[]) => { + const { skippedArtists } = settings; + const previousStoreValue = settingsStore.get(skippedArtists); + const filteredArtists = previousStoreValue.filter((value) => !artists.includes(value)); + + settingsStore.set(skippedArtists, filteredArtists); +};