« get me outta code hell

announce joining party - 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-23 11:59:59 -0300
committer(quasar) nebula <qznebula@protonmail.com>2024-05-16 22:00:46 -0300
commit8a44b3c4088a1d3a6720f1d9c47627d50d5ef39c (patch)
treecbb6ff07575b7d65390e817e53118718f01c6b0a /socket.js
parentb64d923daa2aad9ca848dd8ff57c016a51ac5383 (diff)
announce joining party
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 42f4057..1cd6c35 100644
--- a/socket.js
+++ b/socket.js
@@ -111,6 +111,8 @@ function validateCommand(command) {
       // clients too.
     case 'client':
       switch (command.code) {
+        case 'announce-join':
+          return true
         case 'clear-queue':
           return typeof command.queuePlayer === 'string'
         case 'clear-queue-past':
@@ -252,6 +254,7 @@ export function makeSocketServer() {
 
     socketMap[socketId] = socket
 
+    let hasAnnouncedJoin = false
     let nickname = DEFAULT_NICKNAME
 
     socket.on('close', () => {
@@ -278,6 +281,18 @@ export function makeSocketServer() {
         return
       }
 
+      // If the socket hasn't announced its joining yet, it only has access to
+      // a few commands.
+
+      if (!hasAnnouncedJoin) {
+        if (![
+          'announce-join',
+          'set-nickname'
+        ].includes(command.code)) {
+          return
+        }
+      }
+
       // If it's a status command, respond appropriately, and return so that it
       // is not relayed.
 
@@ -357,6 +372,13 @@ export function makeSocketServer() {
         nickname = command.nickname
       }
 
+      // If the socket hasn't announced its joining yet - and this isn't the
+      // command where it does so - don't relay the command.
+
+      if (!hasAnnouncedJoin && command.code !== 'announce-join') {
+        return
+      }
+
       // Relay the command to client sockets besides the sender.
 
       const otherSockets = Object.values(socketMap).filter(s => s !== socket)
@@ -439,6 +461,8 @@ export function attachBackendToSocketClient(backend, client, {
   // All actual logic for instances of the mtui backend interacting with each
   // other through commands lives here.
 
+  let hasAnnouncedJoin = false
+
   const partyGrouplike = {
     name: `Party Sources - ${client.nickname}`,
     isPartySources: true,
@@ -447,6 +471,7 @@ export function attachBackendToSocketClient(backend, client, {
 
   const partyGrouplikeMap = Object.create(null)
 
+  backend.setHasAnnouncedJoin(false)
   backend.setAlwaysStartPaused(true)
   backend.setWaitWhenDonePlaying(true)
 
@@ -467,6 +492,9 @@ export function attachBackendToSocketClient(backend, client, {
     let isVerbose = false
 
     switch (command.code) {
+      case 'announce-join':
+        actionmsg = `joined the party`
+        break
       case 'clear-queue':
         actionmsg = 'cleared the queue'
         break
@@ -777,6 +805,12 @@ export function attachBackendToSocketClient(backend, client, {
     })
   })
 
+  backend.on('announce join party', () => {
+    client.sendCommand({
+      code: 'announce-join'
+    })
+  })
+
   backend.on('share with party', origItem => {
     let newItem = {
       ...origItem,