OverviewFilesCommitsIssuesDocsReleasesPeers

godot-keet / tests / create-admin-channel.mjs

111 lines · javascript ref:
/**
 * create-admin-channel.mjs — Creates a Broadcast room for admin metadata
 *
 * Creates a roomType=1 (Broadcast) room where only moderators can post.
 * Outputs the roomId to hardcode in admin-channel.json.
 *
 * Usage: bare tests/create-admin-channel.mjs
 */

import Pipe from 'bare-pipe'
import FramedStream from 'framed-stream'
import b4a from 'b4a'

const SOCKET = '/tmp/keet-daemon.sock'
let rpcId = 0

function connect () {
  const pipe = new Pipe(SOCKET)
  const framed = new FramedStream(pipe)
  const pending = new Map()

  framed.on('data', (buf) => {
    try {
      const msg = JSON.parse(b4a.toString(buf))
      const cb = pending.get(msg.id)
      if (cb) {
        pending.delete(msg.id)
        if (msg.ok !== undefined && msg.data !== undefined) {
          msg.result = msg.data
        }
        cb(msg)
      }
    } catch {}
  })

  function call (method, params = {}, timeoutMs = 30000) {
    return new Promise((resolve, reject) => {
      const id = ++rpcId
      const timeout = setTimeout(() => {
        pending.delete(id)
        reject(new Error(`RPC timeout: ${method}`))
      }, timeoutMs)
      pending.set(id, (msg) => {
        clearTimeout(timeout)
        if (msg.error) reject(new Error(msg.error.message || msg.error))
        else resolve(msg.result)
      })
      const payload = JSON.stringify({ jsonrpc: '2.0', id, method, params })
      framed.write(b4a.from(payload))
    })
  }

  function close () {
    framed.destroy()
    pipe.destroy()
  }

  return { call, close }
}

async function run () {
  console.log('=== Create Admin Broadcast Channel ===\n')

  const client = connect()

  // Check if admin channel already exists
  try {
    const rooms = await client.call('rooms')
    const existing = rooms.find(r => r.title === '_admin-meta')
    if (existing) {
      const roomId = existing.roomId || existing.id
      console.log(`Admin channel already exists: ${roomId}`)
      console.log('Done.')
      client.close()
      setTimeout(() => Bare.exit(0), 500)
      return
    }
  } catch (err) {
    console.log('WARN: Could not list rooms:', err.message)
  }

  // Create Broadcast room (roomType=1)
  console.log('Creating Broadcast room "_admin-meta" (roomType=1)...')
  try {
    const result = await client.call('room.create', {
      title: '_admin-meta',
      roomType: '1'
    })

    console.log('Created:', JSON.stringify(result, null, 2))

    // Get the roomId from the result
    const roomId = result.roomId || result.id || result
    console.log(`\nAdmin channel roomId: ${roomId}`)
    console.log('\nSave this to ~/.config/keet-cli/admin-channel.json')
  } catch (err) {
    console.log('FAIL:', err.message)
    client.close()
    setTimeout(() => Bare.exit(1), 500)
    return
  }

  client.close()
  setTimeout(() => Bare.exit(0), 500)
}

run().catch(err => {
  console.log('FATAL:', err.message)
  Bare.exit(1)
})