From 7b16ddc91a6c58fb05d8322f791391dec2e486a9 Mon Sep 17 00:00:00 2001 From: RemixDev Date: Thu, 13 May 2021 11:40:32 +0200 Subject: [PATCH] Implemented saveSettings websocket module --- server/src/main.ts | 10 ++++++++-- server/src/websocket/index.ts | 17 +++++++++++++++-- .../{accountChanged.ts => analyzeLink.ts} | 0 .../modules/{addedToQueue.ts => applogin.ts} | 0 ...{alreadyInQueue.ts => cancelAllDownloads.ts} | 0 .../{apploginArl.ts => changeAccount.ts} | 0 server/src/websocket/modules/connection.ts | 9 --------- .../modules/finishConvertingSpotifyPlaylist.ts | 0 server/src/websocket/modules/finishDownload.ts | 0 .../websocket/modules/finishGeneratingItems.ts | 0 server/src/websocket/modules/index.ts | 4 ++-- .../src/websocket/modules/initDownloadQueue.ts | 0 .../websocket/modules/loginNeededToDownload.ts | 0 .../{cancellingCurrentItem.ts => logout.ts} | 0 ...tItemCancelled.ts => openDownloadsFolder.ts} | 0 server/src/websocket/modules/queueError.ts | 0 ...wnloadFolderSelected.ts => queueRestored.ts} | 0 ...rorMessage.ts => removeFinishedDownloads.ts} | 0 .../{familyAccounts.ts => removeFromQueue.ts} | 0 .../websocket/modules/removedAllDownloads.ts | 0 .../modules/removedFinishedDownloads.ts | 0 .../src/websocket/modules/removedFromQueue.ts | 0 server/src/websocket/modules/restoringQueue.ts | 0 server/src/websocket/modules/saveSettings.ts | 15 +++++++++++++++ ...hAddingArtist.ts => selectDownloadFolder.ts} | 0 .../src/websocket/modules/startAddingArtist.ts | 0 server/src/websocket/modules/startConversion.ts | 0 .../modules/startConvertingSpotifyPlaylist.ts | 0 server/src/websocket/modules/startDownload.ts | 0 .../websocket/modules/startGeneratingItems.ts | 0 server/src/websocket/modules/toast.ts | 0 server/src/websocket/modules/updateQueue.ts | 0 server/src/websocket/modules/updateSettings.ts | 0 webui | 2 +- 34 files changed, 41 insertions(+), 16 deletions(-) rename server/src/websocket/modules/{accountChanged.ts => analyzeLink.ts} (100%) rename server/src/websocket/modules/{addedToQueue.ts => applogin.ts} (100%) rename server/src/websocket/modules/{alreadyInQueue.ts => cancelAllDownloads.ts} (100%) rename server/src/websocket/modules/{apploginArl.ts => changeAccount.ts} (100%) delete mode 100644 server/src/websocket/modules/connection.ts delete mode 100644 server/src/websocket/modules/finishConvertingSpotifyPlaylist.ts delete mode 100644 server/src/websocket/modules/finishDownload.ts delete mode 100644 server/src/websocket/modules/finishGeneratingItems.ts delete mode 100644 server/src/websocket/modules/initDownloadQueue.ts delete mode 100644 server/src/websocket/modules/loginNeededToDownload.ts rename server/src/websocket/modules/{cancellingCurrentItem.ts => logout.ts} (100%) rename server/src/websocket/modules/{currentItemCancelled.ts => openDownloadsFolder.ts} (100%) delete mode 100644 server/src/websocket/modules/queueError.ts rename server/src/websocket/modules/{downloadFolderSelected.ts => queueRestored.ts} (100%) rename server/src/websocket/modules/{errorMessage.ts => removeFinishedDownloads.ts} (100%) rename server/src/websocket/modules/{familyAccounts.ts => removeFromQueue.ts} (100%) delete mode 100644 server/src/websocket/modules/removedAllDownloads.ts delete mode 100644 server/src/websocket/modules/removedFinishedDownloads.ts delete mode 100644 server/src/websocket/modules/removedFromQueue.ts delete mode 100644 server/src/websocket/modules/restoringQueue.ts create mode 100644 server/src/websocket/modules/saveSettings.ts rename server/src/websocket/modules/{finishAddingArtist.ts => selectDownloadFolder.ts} (100%) delete mode 100644 server/src/websocket/modules/startAddingArtist.ts delete mode 100644 server/src/websocket/modules/startConversion.ts delete mode 100644 server/src/websocket/modules/startConvertingSpotifyPlaylist.ts delete mode 100644 server/src/websocket/modules/startDownload.ts delete mode 100644 server/src/websocket/modules/startGeneratingItems.ts delete mode 100644 server/src/websocket/modules/toast.ts delete mode 100644 server/src/websocket/modules/updateQueue.ts delete mode 100644 server/src/websocket/modules/updateSettings.ts diff --git a/server/src/main.ts b/server/src/main.ts index 766ec8b..5c58977 100644 --- a/server/src/main.ts +++ b/server/src/main.ts @@ -1,6 +1,12 @@ // @ts-expect-error import deemix from 'deemix' -export const settings: any = deemix.settings.load() +export const loadSettings = deemix.settings.load export const defaultSettings: any = deemix.settings.DEFAULTS -export const sessionDZ: any = {} +export let settings: any = loadSettings() +export let sessionDZ: any = {} + +export function saveSettings(newSettings: any) { + deemix.settings.save(newSettings) + settings = newSettings +} diff --git a/server/src/websocket/index.ts b/server/src/websocket/index.ts index ea7638c..29541a3 100644 --- a/server/src/websocket/index.ts +++ b/server/src/websocket/index.ts @@ -1,3 +1,4 @@ +import WebSocket from 'ws' import { Server as WsServer } from 'ws' import { consoleError, consoleInfo } from '../helpers/errors' import wsModules from './modules' @@ -5,10 +6,22 @@ import wsModules from './modules' // ? Is this needed? // ? https://github.com/websockets/ws#how-to-detect-and-close-broken-connections +export const broadcast = function(wss:WsServer, key:string, data:any) { + wss.clients.forEach(client => { + if (client.readyState === WebSocket.OPEN) { + client.send(JSON.stringify({key, data})) + } + }) +} + export const registerWebsocket = (wss: WsServer) => { wss.on('connection', ws => { - wsModules.forEach(module => { - ws.on(module.eventName, module.cb) + ws.on('message', (message)=>{ + consoleInfo(`received: ${message}`) + const data = JSON.parse(message.toString()) + wsModules.forEach(module => { + if (data.key === module.eventName) module.cb(data.data, ws, wss) + }) }) }) diff --git a/server/src/websocket/modules/accountChanged.ts b/server/src/websocket/modules/analyzeLink.ts similarity index 100% rename from server/src/websocket/modules/accountChanged.ts rename to server/src/websocket/modules/analyzeLink.ts diff --git a/server/src/websocket/modules/addedToQueue.ts b/server/src/websocket/modules/applogin.ts similarity index 100% rename from server/src/websocket/modules/addedToQueue.ts rename to server/src/websocket/modules/applogin.ts diff --git a/server/src/websocket/modules/alreadyInQueue.ts b/server/src/websocket/modules/cancelAllDownloads.ts similarity index 100% rename from server/src/websocket/modules/alreadyInQueue.ts rename to server/src/websocket/modules/cancelAllDownloads.ts diff --git a/server/src/websocket/modules/apploginArl.ts b/server/src/websocket/modules/changeAccount.ts similarity index 100% rename from server/src/websocket/modules/apploginArl.ts rename to server/src/websocket/modules/changeAccount.ts diff --git a/server/src/websocket/modules/connection.ts b/server/src/websocket/modules/connection.ts deleted file mode 100644 index 1e05a57..0000000 --- a/server/src/websocket/modules/connection.ts +++ /dev/null @@ -1,9 +0,0 @@ -import { consoleInfo } from '../../helpers/errors' - -const eventName = 'message' - -const cb = (message: string) => { - consoleInfo(`received: ${message}`) -} - -export default { eventName, cb } diff --git a/server/src/websocket/modules/finishConvertingSpotifyPlaylist.ts b/server/src/websocket/modules/finishConvertingSpotifyPlaylist.ts deleted file mode 100644 index e69de29..0000000 diff --git a/server/src/websocket/modules/finishDownload.ts b/server/src/websocket/modules/finishDownload.ts deleted file mode 100644 index e69de29..0000000 diff --git a/server/src/websocket/modules/finishGeneratingItems.ts b/server/src/websocket/modules/finishGeneratingItems.ts deleted file mode 100644 index e69de29..0000000 diff --git a/server/src/websocket/modules/index.ts b/server/src/websocket/modules/index.ts index cee690d..d9dd639 100644 --- a/server/src/websocket/modules/index.ts +++ b/server/src/websocket/modules/index.ts @@ -1,3 +1,3 @@ -import connection from './connection' +import saveSettings from './saveSettings' -export default [connection] +export default [saveSettings] diff --git a/server/src/websocket/modules/initDownloadQueue.ts b/server/src/websocket/modules/initDownloadQueue.ts deleted file mode 100644 index e69de29..0000000 diff --git a/server/src/websocket/modules/loginNeededToDownload.ts b/server/src/websocket/modules/loginNeededToDownload.ts deleted file mode 100644 index e69de29..0000000 diff --git a/server/src/websocket/modules/cancellingCurrentItem.ts b/server/src/websocket/modules/logout.ts similarity index 100% rename from server/src/websocket/modules/cancellingCurrentItem.ts rename to server/src/websocket/modules/logout.ts diff --git a/server/src/websocket/modules/currentItemCancelled.ts b/server/src/websocket/modules/openDownloadsFolder.ts similarity index 100% rename from server/src/websocket/modules/currentItemCancelled.ts rename to server/src/websocket/modules/openDownloadsFolder.ts diff --git a/server/src/websocket/modules/queueError.ts b/server/src/websocket/modules/queueError.ts deleted file mode 100644 index e69de29..0000000 diff --git a/server/src/websocket/modules/downloadFolderSelected.ts b/server/src/websocket/modules/queueRestored.ts similarity index 100% rename from server/src/websocket/modules/downloadFolderSelected.ts rename to server/src/websocket/modules/queueRestored.ts diff --git a/server/src/websocket/modules/errorMessage.ts b/server/src/websocket/modules/removeFinishedDownloads.ts similarity index 100% rename from server/src/websocket/modules/errorMessage.ts rename to server/src/websocket/modules/removeFinishedDownloads.ts diff --git a/server/src/websocket/modules/familyAccounts.ts b/server/src/websocket/modules/removeFromQueue.ts similarity index 100% rename from server/src/websocket/modules/familyAccounts.ts rename to server/src/websocket/modules/removeFromQueue.ts diff --git a/server/src/websocket/modules/removedAllDownloads.ts b/server/src/websocket/modules/removedAllDownloads.ts deleted file mode 100644 index e69de29..0000000 diff --git a/server/src/websocket/modules/removedFinishedDownloads.ts b/server/src/websocket/modules/removedFinishedDownloads.ts deleted file mode 100644 index e69de29..0000000 diff --git a/server/src/websocket/modules/removedFromQueue.ts b/server/src/websocket/modules/removedFromQueue.ts deleted file mode 100644 index e69de29..0000000 diff --git a/server/src/websocket/modules/restoringQueue.ts b/server/src/websocket/modules/restoringQueue.ts deleted file mode 100644 index e69de29..0000000 diff --git a/server/src/websocket/modules/saveSettings.ts b/server/src/websocket/modules/saveSettings.ts new file mode 100644 index 0000000..af75b97 --- /dev/null +++ b/server/src/websocket/modules/saveSettings.ts @@ -0,0 +1,15 @@ +import { Server as WsServer } from 'ws' +import { consoleInfo } from '../../helpers/errors' +import { saveSettings } from '../../main' +import { broadcast } from '../index' + +const eventName = 'saveSettings' + +const cb = (data: any, ws: any, wss: WsServer) => { + const {settings, spotifySettings} = data + saveSettings(settings) + consoleInfo('Settings saved') + broadcast(wss, 'updateSettings', {settings, spotifySettings}) +} + +export default { eventName, cb } diff --git a/server/src/websocket/modules/finishAddingArtist.ts b/server/src/websocket/modules/selectDownloadFolder.ts similarity index 100% rename from server/src/websocket/modules/finishAddingArtist.ts rename to server/src/websocket/modules/selectDownloadFolder.ts diff --git a/server/src/websocket/modules/startAddingArtist.ts b/server/src/websocket/modules/startAddingArtist.ts deleted file mode 100644 index e69de29..0000000 diff --git a/server/src/websocket/modules/startConversion.ts b/server/src/websocket/modules/startConversion.ts deleted file mode 100644 index e69de29..0000000 diff --git a/server/src/websocket/modules/startConvertingSpotifyPlaylist.ts b/server/src/websocket/modules/startConvertingSpotifyPlaylist.ts deleted file mode 100644 index e69de29..0000000 diff --git a/server/src/websocket/modules/startDownload.ts b/server/src/websocket/modules/startDownload.ts deleted file mode 100644 index e69de29..0000000 diff --git a/server/src/websocket/modules/startGeneratingItems.ts b/server/src/websocket/modules/startGeneratingItems.ts deleted file mode 100644 index e69de29..0000000 diff --git a/server/src/websocket/modules/toast.ts b/server/src/websocket/modules/toast.ts deleted file mode 100644 index e69de29..0000000 diff --git a/server/src/websocket/modules/updateQueue.ts b/server/src/websocket/modules/updateQueue.ts deleted file mode 100644 index e69de29..0000000 diff --git a/server/src/websocket/modules/updateSettings.ts b/server/src/websocket/modules/updateSettings.ts deleted file mode 100644 index e69de29..0000000 diff --git a/webui b/webui index 9710a5f..134c43a 160000 --- a/webui +++ b/webui @@ -1 +1 @@ -Subproject commit 9710a5f19f6614fefc760e1771cb67b410ee55de +Subproject commit 134c43a85589b64d44c55eca2f645bf4dbfb87bd