Files
im_server/server/im_group_events.go
T

59 lines
1.3 KiB
Go

package main
import (
"encoding/json"
"strings"
"time"
"github.com/tinode/chat/server/logs"
"github.com/tinode/chat/server/store/types"
)
func publishIMGroupRealtimeEvent(tenantID types.TenantID, topic string, actor types.Uid, eventType string, payload any) {
topic = strings.TrimSpace(topic)
eventType = strings.TrimSpace(eventType)
if tenantID.IsZero() || topic == "" || eventType == "" {
return
}
encoded, err := json.Marshal(payload)
if err != nil {
logs.Warn.Printf("group realtime: encode event topic=%s event=%s: %v", topic, eventType, err)
encoded = []byte(`{}`)
}
from := ""
if !actor.IsZero() {
from = actor.UserId()
}
message := &ServerComMessage{
TenantID: tenantID,
RcptTo: topic,
AsUser: from,
Timestamp: time.Now().UTC().Round(time.Millisecond),
Info: &MsgServerInfo{
Topic: topic,
From: from,
What: "group",
Event: eventType,
Payload: json.RawMessage(encoded),
},
}
if globals.hub != nil {
select {
case globals.hub.routeSrv <- message:
default:
logs.Warn.Printf("group realtime: hub queue full topic=%s event=%s", topic, eventType)
}
}
publishRealtimeTopicMessage(topic, message)
}
func userIDs(uids []types.Uid) []string {
ids := make([]string, 0, len(uids))
for _, uid := range uids {
if !uid.IsZero() {
ids = append(ids, uid.UserId())
}
}
return ids
}