57 lines
1.5 KiB
Go
57 lines
1.5 KiB
Go
package main
|
|
|
|
import (
|
|
"encoding/json"
|
|
"strings"
|
|
"time"
|
|
|
|
"github.com/tinode/chat/server/logs"
|
|
"github.com/tinode/chat/server/store/types"
|
|
)
|
|
|
|
const p2pPermissionsRefreshedEvent = "p2p.permissions.refreshed"
|
|
|
|
type p2pPermissionsPayload struct {
|
|
UserIDs []string `json:"user_ids"`
|
|
}
|
|
|
|
// publishIMP2PPermissionsRefresh asks the topic master to reload permissions
|
|
// after the HTTP API has repaired or created the two P2P subscriptions.
|
|
func publishIMP2PPermissionsRefresh(tenantID types.TenantID, topic string, uids ...types.Uid) {
|
|
topic = strings.TrimSpace(topic)
|
|
if !tenantID.IsValid() || topic == "" {
|
|
return
|
|
}
|
|
payload := p2pPermissionsPayload{UserIDs: make([]string, 0, len(uids))}
|
|
for _, uid := range uids {
|
|
if !uid.IsZero() {
|
|
payload.UserIDs = append(payload.UserIDs, uid.UserId())
|
|
}
|
|
}
|
|
if len(payload.UserIDs) == 0 {
|
|
return
|
|
}
|
|
encoded, err := json.Marshal(payload)
|
|
if err != nil {
|
|
logs.Warn.Printf("p2p permissions refresh: encode topic=%s: %v", topic, err)
|
|
return
|
|
}
|
|
message := &ServerComMessage{
|
|
TenantID: tenantID,
|
|
RcptTo: topic,
|
|
Timestamp: time.Now().UTC().Round(time.Millisecond),
|
|
Info: &MsgServerInfo{
|
|
Topic: topic, What: "p2p", Event: p2pPermissionsRefreshedEvent,
|
|
Payload: json.RawMessage(encoded),
|
|
},
|
|
}
|
|
if globals.hub != nil {
|
|
select {
|
|
case globals.hub.routeSrv <- message:
|
|
default:
|
|
logs.Warn.Printf("p2p permissions refresh: hub queue full topic=%s", topic)
|
|
}
|
|
}
|
|
publishRealtimeTopicMessage(topic, message)
|
|
}
|