« 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
diff options
context:
space:
mode:
author(quasar) nebula <towerofnix@gmail.com>2021-04-23 11:59:59 -0300
committer(quasar) nebula <towerofnix@gmail.com>2021-04-23 11:59:59 -0300
commit90186fa7759c1d4fa08b41fa69a87fb45782f000 (patch)
tree528e269c876e508783988f059695bcd6c5aea0e6
parent533f2898871cc5c1a548308537fbcd4f8d4bbdf5 (diff)
announce joining party
-rw-r--r--backend.js10
-rwxr-xr-xindex.js1
-rw-r--r--socket.js34
3 files changed, 45 insertions, 0 deletions
diff --git a/backend.js b/backend.js
index 13287ec..b225564 100644
--- a/backend.js
+++ b/backend.js
@@ -671,6 +671,8 @@ class Backend extends EventEmitter {
     this.alwaysStartPaused = false
     this.waitWhenDonePlaying = false
 
+    this.hasAnnouncedJoin = false
+
     this.recordStore = new RecordStore()
     this.throttleMetadata = throttlePromise(10)
     this.metadataDictionary = {}
@@ -859,6 +861,14 @@ class Backend extends EventEmitter {
     this.emit('log message', messageInfo)
   }
 
+  announceJoinParty() {
+    this.emit('announce join party')
+  }
+
+  setHasAnnouncedJoin(hasAnnouncedJoin) {
+    this.hasAnnouncedJoin = hasAnnouncedJoin
+  }
+
   loadPartyGrouplike(socketId, partyGrouplike) {
     this.emit('got party grouplike', socketId, partyGrouplike)
   }
diff --git a/index.js b/index.js
index d212a82..c3e4a22 100755
--- a/index.js
+++ b/index.js
@@ -180,6 +180,7 @@ async function main() {
       nickname = options['socket-name']
     }
     socketClient.setNickname(nickname)
+    backend.announceJoinParty()
   }
 
   if (options['stress-test']) {
diff --git a/socket.js b/socket.js
index 3db0f98..53dd4b9 100644
--- a/socket.js
+++ b/socket.js
@@ -118,6 +118,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':
@@ -259,6 +261,7 @@ function makeSocketServer() {
 
     socketMap[socketId] = socket
 
+    let hasAnnouncedJoin = false
     let nickname = DEFAULT_NICKNAME
 
     socket.on('close', () => {
@@ -285,6 +288,18 @@ 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.
 
@@ -364,6 +379,13 @@ 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)
@@ -446,6 +468,8 @@ 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,
@@ -454,6 +478,7 @@ function attachBackendToSocketClient(backend, client, {
 
   const partyGrouplikeMap = Object.create(null)
 
+  backend.setHasAnnouncedJoin(false)
   backend.setAlwaysStartPaused(true)
   backend.setWaitWhenDonePlaying(true)
 
@@ -474,6 +499,9 @@ 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
@@ -784,6 +812,12 @@ function attachBackendToSocketClient(backend, client, {
     })
   })
 
+  backend.on('announce join party', () => {
+    client.sendCommand({
+      code: 'announce-join'
+    })
+  })
+
   backend.on('share with party', origItem => {
     let newItem = {
       ...origItem,