Fix topic handling for websocket with namespace

This commit is contained in:
Dane Everitt 2020-04-06 21:22:43 -07:00
parent 222091b68c
commit 33875105b6
No known key found for this signature in database
GPG Key ID: EEA66103B3D71F53
3 changed files with 20 additions and 5 deletions

View File

@ -93,7 +93,7 @@ func (h *Handler) SendJson(v *Message) error {
// If the user does not have permission to see backup events, do not emit
// them over the socket.
if v.Event == server.BackupCompletedEvent {
if strings.HasPrefix(v.Event, server.BackupCompletedEvent) {
if h.JWT != nil && !h.JWT.HasPermission(PermissionReceiveBackups) {
return nil
}

View File

@ -172,10 +172,10 @@ func (b *Backup) BackupAndNotify() error {
// Emit an event over the socket so we can update the backup in realtime on
// the frontend for the server.
b.server.Events().PublishJson(BackupCompletedEvent, map[string]interface{}{
"uuid": b.Uuid,
b.server.Events().PublishJson(BackupCompletedEvent+":"+b.Uuid, map[string]interface{}{
"uuid": b.Uuid,
"sha256_hash": resp.Sha256Hash,
"file_size": resp.FileSize,
"file_size": resp.FileSize,
})
return nil

View File

@ -2,6 +2,7 @@ package server
import (
"encoding/json"
"strings"
"sync"
)
@ -42,7 +43,21 @@ func (e *EventBus) Publish(topic string, data string) {
e.mu.Lock()
defer e.mu.Unlock()
if ch, ok := e.subscribers[topic]; ok {
t := topic
// Some of our topics for the socket support passing a more specific namespace,
// such as "backup completed:1234" to indicate which specific backup was completed.
//
// In these cases, we still need to the send the event using the standard listener
// name of "backup completed".
if strings.Contains(topic, ":") {
parts := strings.SplitN(topic, ":", 2)
if len(parts) == 2 {
t = parts[0]
}
}
if ch, ok := e.subscribers[t]; ok {
go func(data Event, cs []chan Event) {
for _, channel := range cs {
channel <- data