Add admin-only private chat messages and message deletion
Deploy with Docker Compose / deploy (push) Successful in 58s
Deploy with Docker Compose / deploy (push) Successful in 58s
Admins can flag a chat message as private so only other admin connections receive it, and can delete any message. Admin status is read from the auth cookies on the WebSocket upgrade request. Co-Authored-By: Claude Fable 5.1 <[email protected]>
This commit is contained in:
@@ -34,6 +34,7 @@ var Upgrader = websocket.Upgrader{
|
||||
}
|
||||
|
||||
var (
|
||||
// clients maps each open connection to whether it belongs to an admin.
|
||||
clients = make(map[*websocket.Conn]bool)
|
||||
mu sync.Mutex
|
||||
wsDB *gorm.DB
|
||||
@@ -45,21 +46,43 @@ const (
|
||||
rateLimitMaxMsgs = 10
|
||||
)
|
||||
|
||||
// wsIncoming is the envelope clients send over the socket. A plain chat
|
||||
// message carries text/fileUrl/private; an admin delete carries action+id.
|
||||
type wsIncoming struct {
|
||||
Action string `json:"action,omitempty"`
|
||||
ID uint `json:"id,omitempty"`
|
||||
Text string `json:"text"`
|
||||
FileURL string `json:"fileUrl,omitempty"`
|
||||
Private bool `json:"private,omitempty"`
|
||||
}
|
||||
|
||||
// wsDeleteEvent is broadcast to every client when a message is removed.
|
||||
type wsDeleteEvent struct {
|
||||
Action string `json:"action"`
|
||||
ID uint `json:"id"`
|
||||
}
|
||||
|
||||
func InitWebSocket(database *gorm.DB, domain string) {
|
||||
wsDB = database
|
||||
allowedDomain = domain
|
||||
}
|
||||
|
||||
func HandleWebSocket(conn *websocket.Conn) {
|
||||
// HandleWebSocket serves one chat connection. isAdmin controls whether the
|
||||
// connection may see private messages, send them, and delete messages.
|
||||
func HandleWebSocket(conn *websocket.Conn, isAdmin bool) {
|
||||
defer conn.Close()
|
||||
|
||||
mu.Lock()
|
||||
clients[conn] = true
|
||||
clients[conn] = isAdmin
|
||||
nextAuthorID++
|
||||
authorID := nextAuthorID
|
||||
|
||||
var history []models.Message
|
||||
wsDB.Order("created_at ASC").Limit(maxMessages).Find(&history)
|
||||
historyQuery := wsDB.Order("created_at ASC").Limit(maxMessages)
|
||||
if !isAdmin {
|
||||
historyQuery = historyQuery.Where("private = ?", false)
|
||||
}
|
||||
historyQuery.Find(&history)
|
||||
|
||||
for _, msg := range history {
|
||||
if err := conn.WriteJSON(msg); err != nil {
|
||||
@@ -73,7 +96,7 @@ func HandleWebSocket(conn *websocket.Conn) {
|
||||
windowStart := time.Now()
|
||||
|
||||
for {
|
||||
var incoming models.Message
|
||||
var incoming wsIncoming
|
||||
if err := conn.ReadJSON(&incoming); err != nil {
|
||||
break
|
||||
}
|
||||
@@ -88,16 +111,32 @@ func HandleWebSocket(conn *websocket.Conn) {
|
||||
continue
|
||||
}
|
||||
|
||||
incoming.AuthorID = authorID
|
||||
if incoming.Action == "delete" {
|
||||
if isAdmin && incoming.ID != 0 {
|
||||
deleteMessage(incoming.ID)
|
||||
}
|
||||
continue
|
||||
}
|
||||
|
||||
msg := models.Message{
|
||||
Content: incoming.Text,
|
||||
FileURL: incoming.FileURL,
|
||||
AuthorID: authorID,
|
||||
// Only admins may mark a message private.
|
||||
Private: incoming.Private && isAdmin,
|
||||
}
|
||||
|
||||
mu.Lock()
|
||||
wsDB.Create(&incoming)
|
||||
wsDB.Create(&msg)
|
||||
wsDB.Where("id NOT IN (?)",
|
||||
wsDB.Model(&models.Message{}).Select("id").Order("created_at DESC").Limit(maxMessages),
|
||||
).Delete(&models.Message{})
|
||||
|
||||
for client := range clients {
|
||||
if err := client.WriteJSON(incoming); err != nil {
|
||||
for client, clientAdmin := range clients {
|
||||
if msg.Private && !clientAdmin {
|
||||
continue
|
||||
}
|
||||
if err := client.WriteJSON(msg); err != nil {
|
||||
client.Close()
|
||||
delete(clients, client)
|
||||
}
|
||||
@@ -109,3 +148,24 @@ func HandleWebSocket(conn *websocket.Conn) {
|
||||
delete(clients, conn)
|
||||
mu.Unlock()
|
||||
}
|
||||
|
||||
// deleteMessage soft-deletes a message and tells every client to drop it.
|
||||
// Clients that never received the message (non-admins for a private one)
|
||||
// simply ignore the unknown id.
|
||||
func deleteMessage(id uint) {
|
||||
mu.Lock()
|
||||
defer mu.Unlock()
|
||||
|
||||
res := wsDB.Delete(&models.Message{}, id)
|
||||
if res.Error != nil || res.RowsAffected == 0 {
|
||||
return
|
||||
}
|
||||
|
||||
event := wsDeleteEvent{Action: "delete", ID: id}
|
||||
for client := range clients {
|
||||
if err := client.WriteJSON(event); err != nil {
|
||||
client.Close()
|
||||
delete(clients, client)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user