From 45f804fef3766e3183610b93ac3d1ffb89f08408 Mon Sep 17 00:00:00 2001 From: "(quasar) nebula" Date: Thu, 22 Apr 2021 20:06:58 -0300 Subject: "party sources" ui (no socket functionality yet) --- socket.js | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) (limited to 'socket.js') diff --git a/socket.js b/socket.js index 19fecb9..92a48bf 100644 --- a/socket.js +++ b/socket.js @@ -24,6 +24,8 @@ // clients / the server otherwise. const DEFAULT_NICKNAME = '(Unnamed)' +const originalSymbol = Symbol('Original item') + const EventEmitter = require('events') const net = require('net') @@ -40,6 +42,14 @@ const { silenceEvents } = require('./general-util') +const { + parentSymbol, + updateGroupFormat, + updateTrackFormat, + isTrack, + isGroup +} = require('./playlist-utils') + function serializeCommandToData(command) { // Turn a command into a string/buffer that can be sent over a socket. return JSON.stringify(command) @@ -422,8 +432,11 @@ function attachBackendToSocketClient(backend, client, { // All actual logic for instances of the mtui backend interacting with each // other through commands lives here. + const partyGrouplike = {name: 'My Party Sources', isPartySources: true, items: []} + backend.setAlwaysStartPaused(true) backend.setWaitWhenDonePlaying(true) + backend.loadPartyGrouplike(partyGrouplike) function logCommand(command) { const nickToMessage = nickname => `\x1b[32;1m${nickname}\x1b[0m` @@ -744,6 +757,27 @@ function attachBackendToSocketClient(backend, client, { topItem: saveItemReference(topItem) }) }) + + backend.on('share with party', origItem => { + let newItem = { + ...origItem, + [parentSymbol]: partyGrouplike, + [originalSymbol]: origItem + } + + if (isGroup(newItem)) { + newItem = updateGroupFormat(newItem) + } else if (isTrack(newItem)) { + newItem = updateTrackFormat(newItem) + } else { + return + } + + if (partyGrouplike.items.every(x => x[originalSymbol] !== origItem)) { + partyGrouplike.items.push(newItem) + backend.partyGrouplikeUpdated(partyGrouplike) + } + }) } function attachSocketServerToBackend(server, backend) { -- cgit 1.3.0-6-gf8a5