MM-22706: pass along set_online flag in websocket response (#14591)

* MM-22706: pass along set_online flag in websocket response

To let the client know whether a user has created a post without
being online or not, we get the set_online query param and
pass it down to the websocket event being passed down to the client.

With this PR, the "data" field of the `posted` event will contain
a `set_online` boolean field set to true/false depending on the
query_param set_online value set in the createPost call.

* Setting to false for auto responder

Co-authored-by: mattermod <mattermod@users.noreply.github.com>
Этот коммит содержится в:
Agniva De Sarker
2020-05-27 17:35:02 +05:30
коммит произвёл GitHub
родитель b9309b2af1
Коммит 14f7118dde
22 изменённых файлов: 211 добавлений и 176 удалений

Просмотреть файл

@@ -67,14 +67,6 @@ func createPost(c *Context, w http.ResponseWriter, r *http.Request) {
post.CreateAt = 0
}
rp, err := c.App.CreatePostAsUser(c.App.PostWithProxyRemovedFromImageURLs(post), c.App.Session().Id)
if err != nil {
c.Err = err
return
}
auditRec.Success()
auditRec.AddMeta("post", rp) // overwrite meta
setOnline := r.URL.Query().Get("set_online")
setOnlineBool := true // By default, always set online.
var err2 error
@@ -85,6 +77,15 @@ func createPost(c *Context, w http.ResponseWriter, r *http.Request) {
setOnlineBool = true // Set online nevertheless.
}
}
rp, err := c.App.CreatePostAsUser(c.App.PostWithProxyRemovedFromImageURLs(post), c.App.Session().Id, setOnlineBool)
if err != nil {
c.Err = err
return
}
auditRec.Success()
auditRec.AddMeta("post", rp) // overwrite meta
if setOnlineBool {
c.App.SetStatusOnline(c.App.Session().UserId, false)
}

Просмотреть файл

@@ -12,6 +12,7 @@ import (
"reflect"
"sort"
"strings"
"sync"
"testing"
"time"
@@ -593,6 +594,38 @@ func TestCreatePostCheckOnlineStatus(t *testing.T) {
api := Init(th.Server, th.Server.AppOptions, th.Server.Router)
session, _ := th.App.GetSession(th.Client.AuthToken)
cli := th.CreateClient()
_, loginResp := cli.Login(th.BasicUser2.Username, th.BasicUser2.Password)
require.Nil(t, loginResp.Error)
var wg sync.WaitGroup
wsClient, err := th.CreateWebSocketClientWithClient(cli)
require.Nil(t, err)
defer func() {
wsClient.Close()
wg.Wait()
}()
wsClient.Listen()
wg.Add(1)
go func() {
defer wg.Done()
i := 0
for ev := range wsClient.EventChannel {
if ev.EventType() == model.WEBSOCKET_EVENT_POSTED {
if i == 0 {
assert.False(t, ev.GetData()["set_online"].(bool))
} else {
assert.True(t, ev.GetData()["set_online"].(bool))
}
i++
}
}
assert.Equal(t, 2, i, "unexpected number of posted events")
}()
handler := api.ApiHandler(createPost)
resp := httptest.NewRecorder()
post := &model.Post{
@@ -606,7 +639,7 @@ func TestCreatePostCheckOnlineStatus(t *testing.T) {
handler.ServeHTTP(resp, req)
assert.Equal(t, http.StatusCreated, resp.Code)
_, err := th.App.GetStatus(th.BasicUser.Id)
_, err = th.App.GetStatus(th.BasicUser.Id)
require.NotNil(t, err)
assert.Equal(t, "store.sql_status.get.missing.app_error", err.Id)
@@ -643,7 +676,7 @@ func TestUpdatePost(t *testing.T) {
ChannelId: channel.Id,
Message: "zz" + model.NewId() + "a",
FileIds: fileIds,
}, channel, false)
}, channel, false, true)
require.Nil(t, err)
assert.Equal(t, rpost.Message, rpost.Message, "full name didn't match")
@@ -699,7 +732,7 @@ func TestUpdatePost(t *testing.T) {
Message: "zz" + model.NewId() + "a",
Type: model.POST_JOIN_LEAVE,
UserId: th.BasicUser.Id,
}, channel, false)
}, channel, false, true)
require.Nil(t, err)
up2 := &model.Post{
@@ -715,7 +748,7 @@ func TestUpdatePost(t *testing.T) {
ChannelId: channel.Id,
Message: "zz" + model.NewId() + "a",
UserId: th.BasicUser.Id,
}, channel, false)
}, channel, false, true)
require.Nil(t, err)
t.Run("new message, add files", func(t *testing.T) {