Chats: a shared highlight colour per chat, pushed live with star and rename
chat_color {session_id, color|null} stores a palette name (red, orange,
amber, green, teal, blue, purple, pink) on the operator-session row; rows
and replies carry it as color. Colour, star and rename changes are pushed
to every other connection as chat_session_changed.
This commit is contained in:
@@ -0,0 +1,87 @@
|
||||
package server
|
||||
|
||||
import (
|
||||
"context"
|
||||
"sync"
|
||||
|
||||
"git.a-wan.net/alan/pipelines/internal/protocol"
|
||||
)
|
||||
|
||||
// chatSessionChanges fans a chat's shared list metadata (colour, star, title)
|
||||
// out to every open connection, so a change on one device updates the chat
|
||||
// list on the others live. Unlike a chat room it is not keyed by session: every
|
||||
// connection shows the list. It holds each connection's serialized write
|
||||
// function, never the socket. The zero value is ready to use.
|
||||
type chatSessionChanges struct {
|
||||
mu sync.Mutex
|
||||
clients map[string]func(protocol.Message) error
|
||||
}
|
||||
|
||||
func (c *chatSessionChanges) attach(clientID string, write func(protocol.Message) error) {
|
||||
c.mu.Lock()
|
||||
defer c.mu.Unlock()
|
||||
if c.clients == nil {
|
||||
c.clients = map[string]func(protocol.Message) error{}
|
||||
}
|
||||
c.clients[clientID] = write
|
||||
}
|
||||
|
||||
func (c *chatSessionChanges) detach(clientID string) {
|
||||
c.mu.Lock()
|
||||
defer c.mu.Unlock()
|
||||
delete(c.clients, clientID)
|
||||
}
|
||||
|
||||
// changes returns the hub's connection fan-out; nil for a nil hub, which
|
||||
// broadcasts nothing.
|
||||
func (h *chatHub) changes() *chatSessionChanges {
|
||||
if h == nil {
|
||||
return nil
|
||||
}
|
||||
return &h.sessionChanges
|
||||
}
|
||||
|
||||
// broadcast pushes the updated row to every connection except the sender,
|
||||
// which already has it in its reply. Each write runs on its own goroutine
|
||||
// (bounded by the connection's write timeout), so a slow client cannot delay
|
||||
// the sender or the others.
|
||||
func (c *chatSessionChanges) broadcast(sender string, session protocol.ChatSession) {
|
||||
c.mu.Lock()
|
||||
targets := make([]func(protocol.Message) error, 0, len(c.clients))
|
||||
for id, write := range c.clients {
|
||||
if id != sender {
|
||||
targets = append(targets, write)
|
||||
}
|
||||
}
|
||||
c.mu.Unlock()
|
||||
for _, write := range targets {
|
||||
row := session
|
||||
go func() {
|
||||
_ = write(protocol.Message{Type: protocol.ChatSessionChanged, ChatSession: &row})
|
||||
}()
|
||||
}
|
||||
}
|
||||
|
||||
// echoOperatorSession answers a metadata change with the updated registry row
|
||||
// and pushes the same row to every other connection.
|
||||
func echoOperatorSession(ctx context.Context, request protocol.Message, sessionID, reply string, registry operatorRegistry, changes *chatSessionChanges, clientID string, write func(protocol.Message) error) {
|
||||
session, found, err := registry.OperatorSession(ctx, sessionID)
|
||||
if err != nil {
|
||||
_ = write(protocol.Message{Type: protocol.Error, ID: request.ID, Error: err.Error()})
|
||||
return
|
||||
}
|
||||
if !found {
|
||||
_ = write(protocol.Message{Type: protocol.Error, ID: request.ID, Error: "session " + sessionID + " is no longer an operator session"})
|
||||
return
|
||||
}
|
||||
wire := chatSessionFromOperator(session)
|
||||
_ = write(protocol.Message{
|
||||
Type: reply,
|
||||
ID: request.ID,
|
||||
ChatSession: &wire,
|
||||
ChatSessions: []protocol.ChatSession{wire},
|
||||
})
|
||||
if changes != nil {
|
||||
changes.broadcast(clientID, wire)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user