mirror of
https://github.com/mattermost/mattermost.git
synced 2025-02-25 18:55:24 -06:00
PLT-7404 Return viewed at times in view channel API response (#7428)
* Return viewed at times in view channel API response * Updated transaction to read and write once * Remove transaction and only update if new value greater than older
This commit is contained in:
committed by
Christopher Speller
parent
5e50d3f461
commit
8b9dbb8613
@@ -32,3 +32,28 @@ func ChannelViewFromJson(data io.Reader) *ChannelView {
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
||||
type ChannelViewResponse struct {
|
||||
Status string `json:"status"`
|
||||
LastViewedAtTimes map[string]int64 `json:"last_viewed_at_times"`
|
||||
}
|
||||
|
||||
func (o *ChannelViewResponse) ToJson() string {
|
||||
b, err := json.Marshal(o)
|
||||
if err != nil {
|
||||
return ""
|
||||
} else {
|
||||
return string(b)
|
||||
}
|
||||
}
|
||||
|
||||
func ChannelViewResponseFromJson(data io.Reader) *ChannelViewResponse {
|
||||
decoder := json.NewDecoder(data)
|
||||
var o ChannelViewResponse
|
||||
err := decoder.Decode(&o)
|
||||
if err == nil {
|
||||
return &o
|
||||
} else {
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
||||
38
model/channel_view_test.go
Normal file
38
model/channel_view_test.go
Normal file
@@ -0,0 +1,38 @@
|
||||
// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
|
||||
// See License.txt for license information.
|
||||
|
||||
package model
|
||||
|
||||
import (
|
||||
"strings"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestChannelViewJson(t *testing.T) {
|
||||
o := ChannelView{ChannelId: NewId(), PrevChannelId: NewId()}
|
||||
json := o.ToJson()
|
||||
ro := ChannelViewFromJson(strings.NewReader(json))
|
||||
|
||||
if o.ChannelId != ro.ChannelId {
|
||||
t.Fatal("ChannelIdIds do not match")
|
||||
}
|
||||
|
||||
if o.PrevChannelId != ro.PrevChannelId {
|
||||
t.Fatal("PrevChannelIds do not match")
|
||||
}
|
||||
}
|
||||
|
||||
func TestChannelViewResponseJson(t *testing.T) {
|
||||
id := NewId()
|
||||
o := ChannelViewResponse{Status: "OK", LastViewedAtTimes: map[string]int64{id: 12345}}
|
||||
json := o.ToJson()
|
||||
ro := ChannelViewResponseFromJson(strings.NewReader(json))
|
||||
|
||||
if o.Status != ro.Status {
|
||||
t.Fatal("ChannelIdIds do not match")
|
||||
}
|
||||
|
||||
if o.LastViewedAtTimes[id] != ro.LastViewedAtTimes[id] {
|
||||
t.Fatal("LastViewedAtTimes do not match")
|
||||
}
|
||||
}
|
||||
@@ -1570,13 +1570,13 @@ func (c *Client4) GetChannelMembersForUser(userId, teamId, etag string) (*Channe
|
||||
}
|
||||
|
||||
// ViewChannel performs a view action for a user. Synonymous with switching channels or marking channels as read by a user.
|
||||
func (c *Client4) ViewChannel(userId string, view *ChannelView) (bool, *Response) {
|
||||
func (c *Client4) ViewChannel(userId string, view *ChannelView) (*ChannelViewResponse, *Response) {
|
||||
url := fmt.Sprintf(c.GetChannelsRoute()+"/members/%v/view", userId)
|
||||
if r, err := c.DoApiPost(url, view.ToJson()); err != nil {
|
||||
return false, BuildErrorResponse(r, err)
|
||||
return nil, BuildErrorResponse(r, err)
|
||||
} else {
|
||||
defer closeBody(r)
|
||||
return CheckStatusOK(r), BuildResponse(r)
|
||||
return ChannelViewResponseFromJson(r.Body), BuildResponse(r)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user