MM-10425 Include active_channel in cluster update user status messages (#8967)
* Include active_channel in cluster update user status messages * Update to use new ToJson method * Update tests
Этот коммит содержится в:
коммит произвёл
Christopher Speller
родитель
d36ad6cb54
Коммит
46f969e5dd
@@ -29,7 +29,7 @@ func (a *App) AddStatusCache(status *model.Status) {
|
||||
msg := &model.ClusterMessage{
|
||||
Event: model.CLUSTER_EVENT_UPDATE_STATUS,
|
||||
SendType: model.CLUSTER_SEND_BEST_EFFORT,
|
||||
Data: status.ToJson(),
|
||||
Data: status.ToClusterJson(),
|
||||
}
|
||||
a.Cluster.SendClusterMessage(msg)
|
||||
}
|
||||
|
||||
@@ -24,10 +24,18 @@ type Status struct {
|
||||
Status string `json:"status"`
|
||||
Manual bool `json:"manual"`
|
||||
LastActivityAt int64 `json:"last_activity_at"`
|
||||
ActiveChannel string `json:"-" db:"-"`
|
||||
ActiveChannel string `json:"active_channel,omitempty" db:"-"`
|
||||
}
|
||||
|
||||
func (o *Status) ToJson() string {
|
||||
tempChannelId := o.ActiveChannel
|
||||
o.ActiveChannel = ""
|
||||
b, _ := json.Marshal(o)
|
||||
o.ActiveChannel = tempChannelId
|
||||
return string(b)
|
||||
}
|
||||
|
||||
func (o *Status) ToClusterJson() string {
|
||||
b, _ := json.Marshal(o)
|
||||
return string(b)
|
||||
}
|
||||
@@ -39,7 +47,18 @@ func StatusFromJson(data io.Reader) *Status {
|
||||
}
|
||||
|
||||
func StatusListToJson(u []*Status) string {
|
||||
activeChannels := make([]string, len(u))
|
||||
for index, s := range u {
|
||||
activeChannels[index] = s.ActiveChannel
|
||||
s.ActiveChannel = ""
|
||||
}
|
||||
|
||||
b, _ := json.Marshal(u)
|
||||
|
||||
for index, s := range u {
|
||||
s.ActiveChannel = activeChannels[index]
|
||||
}
|
||||
|
||||
return string(b)
|
||||
}
|
||||
|
||||
|
||||
@@ -7,10 +7,12 @@ import (
|
||||
"encoding/json"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
func TestStatus(t *testing.T) {
|
||||
status := Status{NewId(), STATUS_ONLINE, true, 0, ""}
|
||||
status := Status{NewId(), STATUS_ONLINE, true, 0, "123"}
|
||||
json := status.ToJson()
|
||||
status2 := StatusFromJson(strings.NewReader(json))
|
||||
|
||||
@@ -29,10 +31,17 @@ func TestStatus(t *testing.T) {
|
||||
if status.Manual != status2.Manual {
|
||||
t.Fatal("Manual should have matched")
|
||||
}
|
||||
|
||||
assert.Equal(t, "", status2.ActiveChannel)
|
||||
|
||||
json = status.ToClusterJson()
|
||||
status2 = StatusFromJson(strings.NewReader(json))
|
||||
|
||||
assert.Equal(t, status.ActiveChannel, status2.ActiveChannel)
|
||||
}
|
||||
|
||||
func TestStatusListToJson(t *testing.T) {
|
||||
statuses := []*Status{{NewId(), STATUS_ONLINE, true, 0, ""}, {NewId(), STATUS_OFFLINE, true, 0, ""}}
|
||||
statuses := []*Status{{NewId(), STATUS_ONLINE, true, 0, "123"}, {NewId(), STATUS_OFFLINE, true, 0, ""}}
|
||||
jsonStatuses := StatusListToJson(statuses)
|
||||
|
||||
var dat []map[string]interface{}
|
||||
@@ -40,15 +49,13 @@ func TestStatusListToJson(t *testing.T) {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
if len(dat) != 2 {
|
||||
t.Fatal("Status array should contain 2 elements")
|
||||
}
|
||||
if statuses[0].UserId != dat[0]["user_id"] {
|
||||
t.Fatal("UserId should be equal")
|
||||
}
|
||||
if statuses[1].UserId != dat[1]["user_id"] {
|
||||
t.Fatal("UserId should be equal")
|
||||
}
|
||||
assert.Equal(t, len(dat), 2)
|
||||
|
||||
_, ok := dat[0]["active_channel"]
|
||||
assert.False(t, ok)
|
||||
assert.Equal(t, statuses[0].ActiveChannel, "123")
|
||||
assert.Equal(t, statuses[0].UserId, dat[0]["user_id"])
|
||||
assert.Equal(t, statuses[1].UserId, dat[1]["user_id"])
|
||||
}
|
||||
|
||||
func TestStatusListFromJson(t *testing.T) {
|
||||
|
||||
Ссылка в новой задаче
Block a user