« get me outta code hell

"party sources" ui (no socket functionality yet) - mtui - Music Text User Interface - user-friendly command line music player
about summary refs log tree commit diff
path: root/socket.js
diff options
context:
space:
mode:
author(quasar) nebula <towerofnix@gmail.com>2021-04-22 20:06:58 -0300
committer(quasar) nebula <towerofnix@gmail.com>2021-04-22 20:06:58 -0300
commit45f804fef3766e3183610b93ac3d1ffb89f08408 (patch)
treea8591c4886ee9109ae21a5ee18a9c282a7435998 /socket.js
parent1ca458abc3503ba951c5e19362a54208a285833c (diff)
"party sources" ui (no socket functionality yet)
Diffstat (limited to 'socket.js')
-rw-r--r--socket.js34
1 files changed, 34 insertions, 0 deletions
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) {