From a49e78b1d4e619d9f4f3558cdf29c296453defc2 Mon Sep 17 00:00:00 2001 From: Agniva De Sarker Date: Fri, 23 Jul 2021 12:35:59 +0530 Subject: [PATCH] MM-22051: Remove FromJson part 2 (#17981) ```release-note Removed the following functions: ChannelsWithCountFromJson ChannelFromJson ChannelPatchFromJson ChannelModerationsFromJson ChannelModerationsPatchFromJson ChannelMemberCountsByGroupFromJson ChannelCountsFromJson ChannelDataFromJson ChannelListFromJson ChannelSliceFromJson ChannelListWithTeamDataFromJson ChannelMembersFromJson ChannelUnreadFromJson ChannelUnreadAtFromJson ChannelMemberFromJson ChannelSearchFromJson SidebarCategoryFromJson SidebarCategoriesFromJson OrderedSidebarCategoriesFromJson ``` https://mattermost.atlassian.net/browse/MM-22051 --- api4/channel.go | 126 ++++---- api4/channel_category.go | 10 +- api4/channel_local.go | 35 ++- api4/data_retention.go | 5 +- model/channel.go | 36 --- model/channel_count.go | 7 - model/channel_data.go | 7 - model/channel_list.go | 19 -- model/channel_member.go | 25 -- model/channel_member_test.go | 18 -- model/channel_search.go | 8 - model/channel_search_test.go | 19 -- model/channel_sidebar.go | 19 -- model/channel_test.go | 15 - model/client4.go | 595 +++++++++++++++++++++++++---------- model/team_search_test.go | 8 +- 16 files changed, 528 insertions(+), 424 deletions(-) delete mode 100644 model/channel_search_test.go diff --git a/api4/channel.go b/api4/channel.go index b0448d0ea8..2340e4be39 100644 --- a/api4/channel.go +++ b/api4/channel.go @@ -79,8 +79,9 @@ func (api *API) InitChannel() { } func createChannel(c *Context, w http.ResponseWriter, r *http.Request) { - channel := model.ChannelFromJson(r.Body) - if channel == nil { + var channel *model.Channel + err := json.NewDecoder(r.Body).Decode(&channel) + if err != nil { c.SetInvalidParam("channel") return } @@ -99,9 +100,9 @@ func createChannel(c *Context, w http.ResponseWriter, r *http.Request) { return } - sc, err := c.App.CreateChannelWithUser(c.AppContext, channel, c.AppContext.Session().UserId) - if err != nil { - c.Err = err + sc, appErr := c.App.CreateChannelWithUser(c.AppContext, channel, c.AppContext.Session().UserId) + if appErr != nil { + c.Err = appErr return } @@ -119,9 +120,9 @@ func updateChannel(c *Context, w http.ResponseWriter, r *http.Request) { return } - channel := model.ChannelFromJson(r.Body) - - if channel == nil { + var channel *model.Channel + err := json.NewDecoder(r.Body).Decode(&channel) + if err != nil { c.SetInvalidParam("channel") return } @@ -135,9 +136,9 @@ func updateChannel(c *Context, w http.ResponseWriter, r *http.Request) { auditRec := c.MakeAuditRecord("updateChannel", audit.Fail) defer c.LogAuditRec(auditRec) - originalOldChannel, err := c.App.GetChannel(channel.Id) - if err != nil { - c.Err = err + originalOldChannel, appErr := c.App.GetChannel(channel.Id) + if appErr != nil { + c.Err = appErr return } oldChannel := originalOldChannel.DeepCopy() @@ -204,9 +205,9 @@ func updateChannel(c *Context, w http.ResponseWriter, r *http.Request) { oldChannel.GroupConstrained = channel.GroupConstrained } - updatedChannel, err := c.App.UpdateChannel(oldChannel) - if err != nil { - c.Err = err + updatedChannel, appErr := c.App.UpdateChannel(oldChannel) + if appErr != nil { + c.Err = appErr return } auditRec.AddMeta("update", updatedChannel) @@ -340,15 +341,16 @@ func patchChannel(c *Context, w http.ResponseWriter, r *http.Request) { if c.Err != nil { return } - patch := model.ChannelPatchFromJson(r.Body) - if patch == nil { + var patch *model.ChannelPatch + err := json.NewDecoder(r.Body).Decode(&patch) + if err != nil { c.SetInvalidParam("channel") return } - originalOldChannel, err := c.App.GetChannel(c.Params.ChannelId) - if err != nil { - c.Err = err + originalOldChannel, appErr := c.App.GetChannel(c.Params.ChannelId) + if appErr != nil { + c.Err = appErr return } oldChannel := originalOldChannel.DeepCopy() @@ -372,7 +374,7 @@ func patchChannel(c *Context, w http.ResponseWriter, r *http.Request) { case model.ChannelTypeGroup, model.ChannelTypeDirect: // Modifying the header is not linked to any specific permission for group/dm channels, so just check for membership. - if _, err = c.App.GetChannelMember(context.Background(), c.Params.ChannelId, c.AppContext.Session().UserId); err != nil { + if _, appErr = c.App.GetChannelMember(context.Background(), c.Params.ChannelId, c.AppContext.Session().UserId); appErr != nil { c.Err = model.NewAppError("patchChannel", "api.channel.patch_update_channel.forbidden.app_error", nil, "", http.StatusForbidden) return } @@ -382,15 +384,15 @@ func patchChannel(c *Context, w http.ResponseWriter, r *http.Request) { return } - rchannel, err := c.App.PatchChannel(c.AppContext, oldChannel, patch, c.AppContext.Session().UserId) - if err != nil { - c.Err = err + rchannel, appErr := c.App.PatchChannel(c.AppContext, oldChannel, patch, c.AppContext.Session().UserId) + if appErr != nil { + c.Err = appErr return } - err = c.App.FillInChannelProps(rchannel) - if err != nil { - c.Err = err + appErr = c.App.FillInChannelProps(rchannel) + if appErr != nil { + c.Err = appErr return } @@ -499,15 +501,16 @@ func createDirectChannel(c *Context, w http.ResponseWriter, r *http.Request) { } func searchGroupChannels(c *Context, w http.ResponseWriter, r *http.Request) { - props := model.ChannelSearchFromJson(r.Body) - if props == nil { + var props *model.ChannelSearch + err := json.NewDecoder(r.Body).Decode(&props) + if err != nil { c.SetInvalidParam("channel_search") return } - groupChannels, err := c.App.SearchGroupChannels(c.AppContext.Session().UserId, props.Term) - if err != nil { - c.Err = err + groupChannels, appErr := c.App.SearchGroupChannels(c.AppContext.Session().UserId, props.Term) + if appErr != nil { + c.Err = appErr return } @@ -950,28 +953,29 @@ func searchChannelsForTeam(c *Context, w http.ResponseWriter, r *http.Request) { return } - props := model.ChannelSearchFromJson(r.Body) - if props == nil { + var props *model.ChannelSearch + err := json.NewDecoder(r.Body).Decode(&props) + if err != nil { c.SetInvalidParam("channel_search") return } var channels *model.ChannelList - var err *model.AppError + var appErr *model.AppError if c.App.SessionHasPermissionToTeam(*c.AppContext.Session(), c.Params.TeamId, model.PermissionListTeamChannels) { - channels, err = c.App.SearchChannels(c.Params.TeamId, props.Term) + channels, appErr = c.App.SearchChannels(c.Params.TeamId, props.Term) } else { // If the user is not a team member, return a 404 - if _, err = c.App.GetTeamMember(c.Params.TeamId, c.AppContext.Session().UserId); err != nil { - c.Err = err + if _, appErr = c.App.GetTeamMember(c.Params.TeamId, c.AppContext.Session().UserId); appErr != nil { + c.Err = appErr return } - channels, err = c.App.SearchChannelsForUser(c.AppContext.Session().UserId, c.Params.TeamId, props.Term) + channels, appErr = c.App.SearchChannelsForUser(c.AppContext.Session().UserId, c.Params.TeamId, props.Term) } - if err != nil { - c.Err = err + if appErr != nil { + c.Err = appErr return } @@ -986,28 +990,29 @@ func searchArchivedChannelsForTeam(c *Context, w http.ResponseWriter, r *http.Re return } - props := model.ChannelSearchFromJson(r.Body) - if props == nil { + var props *model.ChannelSearch + err := json.NewDecoder(r.Body).Decode(&props) + if err != nil { c.SetInvalidParam("channel_search") return } var channels *model.ChannelList - var err *model.AppError + var appErr *model.AppError if c.App.SessionHasPermissionToTeam(*c.AppContext.Session(), c.Params.TeamId, model.PermissionListTeamChannels) { - channels, err = c.App.SearchArchivedChannels(c.Params.TeamId, props.Term, c.AppContext.Session().UserId) + channels, appErr = c.App.SearchArchivedChannels(c.Params.TeamId, props.Term, c.AppContext.Session().UserId) } else { // If the user is not a team member, return a 404 - if _, err = c.App.GetTeamMember(c.Params.TeamId, c.AppContext.Session().UserId); err != nil { - c.Err = err + if _, appErr = c.App.GetTeamMember(c.Params.TeamId, c.AppContext.Session().UserId); appErr != nil { + c.Err = appErr return } - channels, err = c.App.SearchArchivedChannels(c.Params.TeamId, props.Term, c.AppContext.Session().UserId) + channels, appErr = c.App.SearchArchivedChannels(c.Params.TeamId, props.Term, c.AppContext.Session().UserId) } - if err != nil { - c.Err = err + if appErr != nil { + c.Err = appErr return } @@ -1017,8 +1022,9 @@ func searchArchivedChannelsForTeam(c *Context, w http.ResponseWriter, r *http.Re } func searchAllChannels(c *Context, w http.ResponseWriter, r *http.Request) { - props := model.ChannelSearchFromJson(r.Body) - if props == nil { + var props *model.ChannelSearch + err := json.NewDecoder(r.Body).Decode(&props) + if err != nil { c.SetInvalidParam("channel_search") return } @@ -1829,17 +1835,23 @@ func patchChannelModerations(c *Context, w http.ResponseWriter, r *http.Request) return } - channel, err := c.App.GetChannel(c.Params.ChannelId) - if err != nil { - c.Err = err + channel, appErr := c.App.GetChannel(c.Params.ChannelId) + if appErr != nil { + c.Err = appErr return } auditRec.AddMeta("channel", channel) - channelModerationsPatch := model.ChannelModerationsPatchFromJson(r.Body) - channelModerations, err := c.App.PatchChannelModerationsForChannel(channel, channelModerationsPatch) + var channelModerationsPatch []*model.ChannelModerationPatch + err := json.NewDecoder(r.Body).Decode(&channelModerationsPatch) if err != nil { - c.Err = err + c.Err = model.NewAppError("Api4.patchChannelModerations", "api.marshal_error", nil, err.Error(), http.StatusInternalServerError) + return + } + + channelModerations, appErr := c.App.PatchChannelModerationsForChannel(channel, channelModerationsPatch) + if appErr != nil { + c.Err = appErr return } auditRec.AddMeta("patch", channelModerationsPatch) diff --git a/api4/channel_category.go b/api4/channel_category.go index 9e043ce827..9313fffc66 100644 --- a/api4/channel_category.go +++ b/api4/channel_category.go @@ -4,6 +4,7 @@ package api4 import ( + "encoding/json" "net/http" "github.com/mattermost/mattermost-server/v6/audit" @@ -45,7 +46,8 @@ func createCategoryForTeamForUser(c *Context, w http.ResponseWriter, r *http.Req auditRec := c.MakeAuditRecord("createCategoryForTeamForUser", audit.Fail) defer c.LogAuditRec(auditRec) - categoryCreateRequest, err := model.SidebarCategoryFromJson(r.Body) + var categoryCreateRequest *model.SidebarCategoryWithChannels + err := json.NewDecoder(r.Body).Decode(&categoryCreateRequest) if err != nil || c.Params.UserId != categoryCreateRequest.UserId || c.Params.TeamId != categoryCreateRequest.TeamId { c.SetInvalidParam("category") return @@ -153,7 +155,8 @@ func updateCategoriesForTeamForUser(c *Context, w http.ResponseWriter, r *http.R auditRec := c.MakeAuditRecord("updateCategoriesForTeamForUser", audit.Fail) defer c.LogAuditRec(auditRec) - categoriesUpdateRequest, err := model.SidebarCategoriesFromJson(r.Body) + var categoriesUpdateRequest []*model.SidebarCategoryWithChannels + err := json.NewDecoder(r.Body).Decode(&categoriesUpdateRequest) if err != nil { c.SetInvalidParam("category") return @@ -241,7 +244,8 @@ func updateCategoryForTeamForUser(c *Context, w http.ResponseWriter, r *http.Req auditRec := c.MakeAuditRecord("updateCategoryForTeamForUser", audit.Fail) defer c.LogAuditRec(auditRec) - categoryUpdateRequest, err := model.SidebarCategoryFromJson(r.Body) + var categoryUpdateRequest *model.SidebarCategoryWithChannels + err := json.NewDecoder(r.Body).Decode(&categoryUpdateRequest) if err != nil || categoryUpdateRequest.TeamId != c.Params.TeamId || categoryUpdateRequest.UserId != c.Params.UserId { c.SetInvalidParam("category") return diff --git a/api4/channel_local.go b/api4/channel_local.go index c85e7645bb..0eccd5c97c 100644 --- a/api4/channel_local.go +++ b/api4/channel_local.go @@ -4,6 +4,7 @@ package api4 import ( + "encoding/json" "net/http" "github.com/mattermost/mattermost-server/v6/app" @@ -36,8 +37,9 @@ func (api *API) InitChannelLocal() { } func localCreateChannel(c *Context, w http.ResponseWriter, r *http.Request) { - channel := model.ChannelFromJson(r.Body) - if channel == nil { + var channel *model.Channel + err := json.NewDecoder(r.Body).Decode(&channel) + if err != nil { c.SetInvalidParam("channel") return } @@ -46,9 +48,9 @@ func localCreateChannel(c *Context, w http.ResponseWriter, r *http.Request) { defer c.LogAuditRec(auditRec) auditRec.AddMeta("channel", channel) - sc, err := c.App.CreateChannel(c.AppContext, channel, false) - if err != nil { - c.Err = err + sc, appErr := c.App.CreateChannel(c.AppContext, channel, false) + if appErr != nil { + c.Err = appErr return } @@ -263,15 +265,16 @@ func localPatchChannel(c *Context, w http.ResponseWriter, r *http.Request) { return } - patch := model.ChannelPatchFromJson(r.Body) - if patch == nil { + var patch *model.ChannelPatch + err := json.NewDecoder(r.Body).Decode(&patch) + if err != nil { c.SetInvalidParam("channel") return } - originalOldChannel, err := c.App.GetChannel(c.Params.ChannelId) - if err != nil { - c.Err = err + originalOldChannel, appErr := c.App.GetChannel(c.Params.ChannelId) + if appErr != nil { + c.Err = appErr return } channel := originalOldChannel.DeepCopy() @@ -281,15 +284,15 @@ func localPatchChannel(c *Context, w http.ResponseWriter, r *http.Request) { auditRec.AddMeta("channel", channel) channel.Patch(patch) - rchannel, err := c.App.UpdateChannel(channel) - if err != nil { - c.Err = err + rchannel, appErr := c.App.UpdateChannel(channel) + if appErr != nil { + c.Err = appErr return } - err = c.App.FillInChannelProps(rchannel) - if err != nil { - c.Err = err + appErr = c.App.FillInChannelProps(rchannel) + if appErr != nil { + c.Err = appErr return } diff --git a/api4/data_retention.go b/api4/data_retention.go index a207cb5540..cae7786a92 100644 --- a/api4/data_retention.go +++ b/api4/data_retention.go @@ -302,8 +302,9 @@ func getChannelsForPolicy(c *Context, w http.ResponseWriter, r *http.Request) { func searchChannelsInPolicy(c *Context, w http.ResponseWriter, r *http.Request) { c.RequirePolicyId() - props := model.ChannelSearchFromJson(r.Body) - if props == nil { + var props *model.ChannelSearch + err := json.NewDecoder(r.Body).Decode(&props) + if err != nil { c.SetInvalidParam("channel_search") return } diff --git a/model/channel.go b/model/channel.go index e8b3aeaede..dfbd7e844f 100644 --- a/model/channel.go +++ b/model/channel.go @@ -180,42 +180,6 @@ func (o *ChannelsWithCount) ToJson() []byte { return b } -func ChannelsWithCountFromJson(data io.Reader) *ChannelsWithCount { - var o *ChannelsWithCount - json.NewDecoder(data).Decode(&o) - return o -} - -func ChannelFromJson(data io.Reader) *Channel { - var o *Channel - json.NewDecoder(data).Decode(&o) - return o -} - -func ChannelPatchFromJson(data io.Reader) *ChannelPatch { - var o *ChannelPatch - json.NewDecoder(data).Decode(&o) - return o -} - -func ChannelModerationsFromJson(data io.Reader) []*ChannelModeration { - var o []*ChannelModeration - json.NewDecoder(data).Decode(&o) - return o -} - -func ChannelModerationsPatchFromJson(data io.Reader) []*ChannelModerationPatch { - var o []*ChannelModerationPatch - json.NewDecoder(data).Decode(&o) - return o -} - -func ChannelMemberCountsByGroupFromJson(data io.Reader) []*ChannelMemberCountByGroup { - var o []*ChannelMemberCountByGroup - json.NewDecoder(data).Decode(&o) - return o -} - func (o *Channel) Etag() string { return Etag(o.Id, o.UpdateAt) } diff --git a/model/channel_count.go b/model/channel_count.go index 6230ab3d53..d56986dad5 100644 --- a/model/channel_count.go +++ b/model/channel_count.go @@ -7,7 +7,6 @@ import ( "crypto/md5" "encoding/json" "fmt" - "io" "sort" "strconv" ) @@ -47,9 +46,3 @@ func (o *ChannelCounts) ToJson() string { b, _ := json.Marshal(o) return string(b) } - -func ChannelCountsFromJson(data io.Reader) *ChannelCounts { - var o *ChannelCounts - json.NewDecoder(data).Decode(&o) - return o -} diff --git a/model/channel_data.go b/model/channel_data.go index 0a1e0d57af..69d4480280 100644 --- a/model/channel_data.go +++ b/model/channel_data.go @@ -5,7 +5,6 @@ package model import ( "encoding/json" - "io" ) type ChannelData struct { @@ -26,9 +25,3 @@ func (o *ChannelData) ToJson() string { b, _ := json.Marshal(o) return string(b) } - -func ChannelDataFromJson(data io.Reader) *ChannelData { - var o *ChannelData - json.NewDecoder(data).Decode(&o) - return o -} diff --git a/model/channel_list.go b/model/channel_list.go index 4ba9e5c33b..918ed51ef2 100644 --- a/model/channel_list.go +++ b/model/channel_list.go @@ -5,7 +5,6 @@ package model import ( "encoding/json" - "io" ) type ChannelList []*Channel @@ -40,18 +39,6 @@ func (o *ChannelList) Etag() string { return Etag(id, t, delta, len(*o)) } -func ChannelListFromJson(data io.Reader) *ChannelList { - var o *ChannelList - json.NewDecoder(data).Decode(&o) - return o -} - -func ChannelSliceFromJson(data io.Reader) []*Channel { - var o []*Channel - json.NewDecoder(data).Decode(&o) - return o -} - type ChannelListWithTeamData []*ChannelWithTeamData func (o *ChannelListWithTeamData) ToJson() string { @@ -87,9 +74,3 @@ func (o *ChannelListWithTeamData) Etag() string { return Etag(id, t, delta, len(*o)) } - -func ChannelListWithTeamDataFromJson(data io.Reader) *ChannelListWithTeamData { - var o *ChannelListWithTeamData - json.NewDecoder(data).Decode(&o) - return o -} diff --git a/model/channel_member.go b/model/channel_member.go index 068eafd903..d4527c0254 100644 --- a/model/channel_member.go +++ b/model/channel_member.go @@ -5,7 +5,6 @@ package model import ( "encoding/json" - "io" "net/http" "strings" ) @@ -88,35 +87,11 @@ func (o *ChannelUnreadAt) ToJson() string { return string(b) } -func ChannelMembersFromJson(data io.Reader) *ChannelMembers { - var o *ChannelMembers - json.NewDecoder(data).Decode(&o) - return o -} - -func ChannelUnreadFromJson(data io.Reader) *ChannelUnread { - var o *ChannelUnread - json.NewDecoder(data).Decode(&o) - return o -} - -func ChannelUnreadAtFromJson(data io.Reader) *ChannelUnreadAt { - var o *ChannelUnreadAt - json.NewDecoder(data).Decode(&o) - return o -} - func (o *ChannelMember) ToJson() string { b, _ := json.Marshal(o) return string(b) } -func ChannelMemberFromJson(data io.Reader) *ChannelMember { - var o *ChannelMember - json.NewDecoder(data).Decode(&o) - return o -} - func (o *ChannelMember) IsValid() *AppError { if !IsValidId(o.ChannelId) { diff --git a/model/channel_member_test.go b/model/channel_member_test.go index cc81761679..04594a7e21 100644 --- a/model/channel_member_test.go +++ b/model/channel_member_test.go @@ -4,20 +4,11 @@ package model import ( - "strings" "testing" "github.com/stretchr/testify/require" ) -func TestChannelMemberJson(t *testing.T) { - o := ChannelMember{ChannelId: NewId(), UserId: NewId()} - json := o.ToJson() - ro := ChannelMemberFromJson(strings.NewReader(json)) - - require.Equal(t, o.ChannelId, ro.ChannelId, "ids do not match") -} - func TestChannelMemberIsValid(t *testing.T) { o := ChannelMember{} @@ -47,12 +38,3 @@ func TestChannelMemberIsValid(t *testing.T) { o.Roles = "" require.Nil(t, o.IsValid(), "should be invalid") } - -func TestChannelUnreadJson(t *testing.T) { - o := ChannelUnread{ChannelId: NewId(), TeamId: NewId(), MsgCount: 5, MentionCount: 3} - json := o.ToJson() - ro := ChannelUnreadFromJson(strings.NewReader(json)) - - require.Equal(t, o.TeamId, ro.TeamId, "team Ids do not match") - require.Equal(t, o.MentionCount, ro.MentionCount, "mention count do not match") -} diff --git a/model/channel_search.go b/model/channel_search.go index ad76828066..6bde45abed 100644 --- a/model/channel_search.go +++ b/model/channel_search.go @@ -5,7 +5,6 @@ package model import ( "encoding/json" - "io" ) const ChannelSearchDefaultLimit = 50 @@ -31,10 +30,3 @@ func (c *ChannelSearch) ToJson() string { b, _ := json.Marshal(c) return string(b) } - -// ChannelSearchFromJson will decode the input and return a Channel -func ChannelSearchFromJson(data io.Reader) *ChannelSearch { - var cs *ChannelSearch - json.NewDecoder(data).Decode(&cs) - return cs -} diff --git a/model/channel_search_test.go b/model/channel_search_test.go deleted file mode 100644 index 7a5921f220..0000000000 --- a/model/channel_search_test.go +++ /dev/null @@ -1,19 +0,0 @@ -// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved. -// See LICENSE.txt for license information. - -package model - -import ( - "strings" - "testing" - - "github.com/stretchr/testify/assert" -) - -func TestChannelSearchJson(t *testing.T) { - channelSearch := ChannelSearch{Term: NewId()} - json := channelSearch.ToJson() - rchannelSearch := ChannelSearchFromJson(strings.NewReader(json)) - - assert.Equal(t, channelSearch.Term, rchannelSearch.Term) -} diff --git a/model/channel_sidebar.go b/model/channel_sidebar.go index 35301d1e3c..e2038b6b88 100644 --- a/model/channel_sidebar.go +++ b/model/channel_sidebar.go @@ -5,7 +5,6 @@ package model import ( "encoding/json" - "io" "regexp" ) @@ -74,24 +73,6 @@ type SidebarChannel struct { type SidebarChannels []*SidebarChannel type SidebarCategoriesWithChannels []*SidebarCategoryWithChannels -func SidebarCategoryFromJson(data io.Reader) (*SidebarCategoryWithChannels, error) { - var o *SidebarCategoryWithChannels - err := json.NewDecoder(data).Decode(&o) - return o, err -} - -func SidebarCategoriesFromJson(data io.Reader) ([]*SidebarCategoryWithChannels, error) { - var o []*SidebarCategoryWithChannels - err := json.NewDecoder(data).Decode(&o) - return o, err -} - -func OrderedSidebarCategoriesFromJson(data io.Reader) (*OrderedSidebarCategories, error) { - var o *OrderedSidebarCategories - err := json.NewDecoder(data).Decode(&o) - return o, err -} - func (o SidebarCategoryWithChannels) ToJson() []byte { b, _ := json.Marshal(o) return b diff --git a/model/channel_test.go b/model/channel_test.go index d2288e2c82..5e3d2847c2 100644 --- a/model/channel_test.go +++ b/model/channel_test.go @@ -10,21 +10,6 @@ import ( "github.com/stretchr/testify/require" ) -func TestChannelJson(t *testing.T) { - o := Channel{Id: NewId(), Name: NewId()} - json := o.ToJson() - ro := ChannelFromJson(strings.NewReader(json)) - - require.Equal(t, o.Id, ro.Id) - - p := ChannelPatch{Name: new(string)} - *p.Name = NewId() - json = p.ToJson() - rp := ChannelPatchFromJson(strings.NewReader(json)) - - require.Equal(t, *p.Name, *rp.Name) -} - func TestChannelCopy(t *testing.T) { o := Channel{Id: NewId(), Name: NewId()} ro := o.DeepCopy() diff --git a/model/client4.go b/model/client4.go index ce73cbe22a..e6dc55de44 100644 --- a/model/client4.go +++ b/model/client4.go @@ -2476,117 +2476,182 @@ func (c *Client4) GetAllChannelsExcludePolicyConstrained(page, perPage int, etag func (c *Client4) getAllChannels(page int, perPage int, etag string, opts ChannelSearchOpts) (*ChannelListWithTeamData, *Response) { query := fmt.Sprintf("?page=%v&per_page=%v&include_deleted=%v&exclude_policy_constrained=%v", page, perPage, opts.IncludeDeleted, opts.ExcludePolicyConstrained) - r, err := c.DoApiGet(c.GetChannelsRoute()+query, etag) - if err != nil { - return nil, BuildErrorResponse(r, err) + r, appErr := c.DoApiGet(c.GetChannelsRoute()+query, etag) + if appErr != nil { + return nil, BuildErrorResponse(r, appErr) } defer closeBody(r) - return ChannelListWithTeamDataFromJson(r.Body), BuildResponse(r) + + var ch *ChannelListWithTeamData + err := json.NewDecoder(r.Body).Decode(&ch) + if err != nil { + return nil, BuildErrorResponse(r, NewAppError("getAllChannels", "api.marshal_error", nil, err.Error(), http.StatusInternalServerError)) + } + return ch, BuildResponse(r) } // GetAllChannelsWithCount get all the channels including the total count. Must be a system administrator. func (c *Client4) GetAllChannelsWithCount(page int, perPage int, etag string) (*ChannelListWithTeamData, int64, *Response) { query := fmt.Sprintf("?page=%v&per_page=%v&include_total_count="+c.boolString(true), page, perPage) - r, err := c.DoApiGet(c.GetChannelsRoute()+query, etag) - if err != nil { - return nil, 0, BuildErrorResponse(r, err) + r, appErr := c.DoApiGet(c.GetChannelsRoute()+query, etag) + if appErr != nil { + return nil, 0, BuildErrorResponse(r, appErr) } defer closeBody(r) - cwc := ChannelsWithCountFromJson(r.Body) + + var cwc *ChannelsWithCount + err := json.NewDecoder(r.Body).Decode(&cwc) + if err != nil { + return nil, 0, BuildErrorResponse(r, NewAppError("GetAllChannelsWithCount", "api.marshal_error", nil, err.Error(), http.StatusInternalServerError)) + } return cwc.Channels, cwc.TotalCount, BuildResponse(r) } // CreateChannel creates a channel based on the provided channel struct. func (c *Client4) CreateChannel(channel *Channel) (*Channel, *Response) { - r, err := c.DoApiPost(c.GetChannelsRoute(), channel.ToJson()) - if err != nil { - return nil, BuildErrorResponse(r, err) + r, appErr := c.DoApiPost(c.GetChannelsRoute(), channel.ToJson()) + if appErr != nil { + return nil, BuildErrorResponse(r, appErr) } defer closeBody(r) - return ChannelFromJson(r.Body), BuildResponse(r) + + var ch *Channel + err := json.NewDecoder(r.Body).Decode(&ch) + if err != nil { + return nil, BuildErrorResponse(r, NewAppError("CreateChannel", "api.marshal_error", nil, err.Error(), http.StatusInternalServerError)) + } + return ch, BuildResponse(r) } // UpdateChannel updates a channel based on the provided channel struct. func (c *Client4) UpdateChannel(channel *Channel) (*Channel, *Response) { - r, err := c.DoApiPut(c.GetChannelRoute(channel.Id), channel.ToJson()) - if err != nil { - return nil, BuildErrorResponse(r, err) + r, appErr := c.DoApiPut(c.GetChannelRoute(channel.Id), channel.ToJson()) + if appErr != nil { + return nil, BuildErrorResponse(r, appErr) } defer closeBody(r) - return ChannelFromJson(r.Body), BuildResponse(r) + + var ch *Channel + err := json.NewDecoder(r.Body).Decode(&ch) + if err != nil { + return nil, BuildErrorResponse(r, NewAppError("UpdateChannel", "api.marshal_error", nil, err.Error(), http.StatusInternalServerError)) + } + return ch, BuildResponse(r) } // PatchChannel partially updates a channel. Any missing fields are not updated. func (c *Client4) PatchChannel(channelId string, patch *ChannelPatch) (*Channel, *Response) { - r, err := c.DoApiPut(c.GetChannelRoute(channelId)+"/patch", patch.ToJson()) - if err != nil { - return nil, BuildErrorResponse(r, err) + r, appErr := c.DoApiPut(c.GetChannelRoute(channelId)+"/patch", patch.ToJson()) + if appErr != nil { + return nil, BuildErrorResponse(r, appErr) } defer closeBody(r) - return ChannelFromJson(r.Body), BuildResponse(r) + + var ch *Channel + err := json.NewDecoder(r.Body).Decode(&ch) + if err != nil { + return nil, BuildErrorResponse(r, NewAppError("PatchChannel", "api.marshal_error", nil, err.Error(), http.StatusInternalServerError)) + } + return ch, BuildResponse(r) } // ConvertChannelToPrivate converts public to private channel. func (c *Client4) ConvertChannelToPrivate(channelId string) (*Channel, *Response) { - r, err := c.DoApiPost(c.GetChannelRoute(channelId)+"/convert", "") - if err != nil { - return nil, BuildErrorResponse(r, err) + r, appErr := c.DoApiPost(c.GetChannelRoute(channelId)+"/convert", "") + if appErr != nil { + return nil, BuildErrorResponse(r, appErr) } defer closeBody(r) - return ChannelFromJson(r.Body), BuildResponse(r) + + var ch *Channel + err := json.NewDecoder(r.Body).Decode(&ch) + if err != nil { + return nil, BuildErrorResponse(r, NewAppError("ConvertChannelToPrivate", "api.marshal_error", nil, err.Error(), http.StatusInternalServerError)) + } + return ch, BuildResponse(r) } // UpdateChannelPrivacy updates channel privacy func (c *Client4) UpdateChannelPrivacy(channelId string, privacy ChannelType) (*Channel, *Response) { requestBody := map[string]string{"privacy": string(privacy)} - r, err := c.DoApiPut(c.GetChannelRoute(channelId)+"/privacy", MapToJson(requestBody)) - if err != nil { - return nil, BuildErrorResponse(r, err) + r, appErr := c.DoApiPut(c.GetChannelRoute(channelId)+"/privacy", MapToJson(requestBody)) + if appErr != nil { + return nil, BuildErrorResponse(r, appErr) } defer closeBody(r) - return ChannelFromJson(r.Body), BuildResponse(r) + + var ch *Channel + err := json.NewDecoder(r.Body).Decode(&ch) + if err != nil { + return nil, BuildErrorResponse(r, NewAppError("UpdateChannelPrivacy", "api.marshal_error", nil, err.Error(), http.StatusInternalServerError)) + } + return ch, BuildResponse(r) } // RestoreChannel restores a previously deleted channel. Any missing fields are not updated. func (c *Client4) RestoreChannel(channelId string) (*Channel, *Response) { - r, err := c.DoApiPost(c.GetChannelRoute(channelId)+"/restore", "") - if err != nil { - return nil, BuildErrorResponse(r, err) + r, appErr := c.DoApiPost(c.GetChannelRoute(channelId)+"/restore", "") + if appErr != nil { + return nil, BuildErrorResponse(r, appErr) } defer closeBody(r) - return ChannelFromJson(r.Body), BuildResponse(r) + + var ch *Channel + err := json.NewDecoder(r.Body).Decode(&ch) + if err != nil { + return nil, BuildErrorResponse(r, NewAppError("RestoreChannel", "api.marshal_error", nil, err.Error(), http.StatusInternalServerError)) + } + return ch, BuildResponse(r) } // CreateDirectChannel creates a direct message channel based on the two user // ids provided. func (c *Client4) CreateDirectChannel(userId1, userId2 string) (*Channel, *Response) { requestBody := []string{userId1, userId2} - r, err := c.DoApiPost(c.GetChannelsRoute()+"/direct", ArrayToJson(requestBody)) - if err != nil { - return nil, BuildErrorResponse(r, err) + r, appErr := c.DoApiPost(c.GetChannelsRoute()+"/direct", ArrayToJson(requestBody)) + if appErr != nil { + return nil, BuildErrorResponse(r, appErr) } defer closeBody(r) - return ChannelFromJson(r.Body), BuildResponse(r) + + var ch *Channel + err := json.NewDecoder(r.Body).Decode(&ch) + if err != nil { + return nil, BuildErrorResponse(r, NewAppError("CreateDirectChannel", "api.marshal_error", nil, err.Error(), http.StatusInternalServerError)) + } + return ch, BuildResponse(r) } // CreateGroupChannel creates a group message channel based on userIds provided. func (c *Client4) CreateGroupChannel(userIds []string) (*Channel, *Response) { - r, err := c.DoApiPost(c.GetChannelsRoute()+"/group", ArrayToJson(userIds)) - if err != nil { - return nil, BuildErrorResponse(r, err) + r, appErr := c.DoApiPost(c.GetChannelsRoute()+"/group", ArrayToJson(userIds)) + if appErr != nil { + return nil, BuildErrorResponse(r, appErr) } defer closeBody(r) - return ChannelFromJson(r.Body), BuildResponse(r) + + var ch *Channel + err := json.NewDecoder(r.Body).Decode(&ch) + if err != nil { + return nil, BuildErrorResponse(r, NewAppError("CreateGroupChannel", "api.marshal_error", nil, err.Error(), http.StatusInternalServerError)) + } + return ch, BuildResponse(r) } // GetChannel returns a channel based on the provided channel id string. func (c *Client4) GetChannel(channelId, etag string) (*Channel, *Response) { - r, err := c.DoApiGet(c.GetChannelRoute(channelId), etag) - if err != nil { - return nil, BuildErrorResponse(r, err) + r, appErr := c.DoApiGet(c.GetChannelRoute(channelId), etag) + if appErr != nil { + return nil, BuildErrorResponse(r, appErr) } defer closeBody(r) - return ChannelFromJson(r.Body), BuildResponse(r) + + var ch *Channel + err := json.NewDecoder(r.Body).Decode(&ch) + if err != nil { + return nil, BuildErrorResponse(r, NewAppError("GetChannel", "api.marshal_error", nil, err.Error(), http.StatusInternalServerError)) + } + return ch, BuildResponse(r) } // GetChannelStats returns statistics for a channel. @@ -2622,116 +2687,182 @@ func (c *Client4) GetPinnedPosts(channelId string, etag string) (*PostList, *Res // GetPrivateChannelsForTeam returns a list of private channels based on the provided team id string. func (c *Client4) GetPrivateChannelsForTeam(teamId string, page int, perPage int, etag string) ([]*Channel, *Response) { query := fmt.Sprintf("/private?page=%v&per_page=%v", page, perPage) - r, err := c.DoApiGet(c.GetChannelsForTeamRoute(teamId)+query, etag) - if err != nil { - return nil, BuildErrorResponse(r, err) + r, appErr := c.DoApiGet(c.GetChannelsForTeamRoute(teamId)+query, etag) + if appErr != nil { + return nil, BuildErrorResponse(r, appErr) } defer closeBody(r) - return ChannelSliceFromJson(r.Body), BuildResponse(r) + + var ch []*Channel + err := json.NewDecoder(r.Body).Decode(&ch) + if err != nil { + return nil, BuildErrorResponse(r, NewAppError("GetPrivateChannelsForTeam", "api.marshal_error", nil, err.Error(), http.StatusInternalServerError)) + } + return ch, BuildResponse(r) } // GetPublicChannelsForTeam returns a list of public channels based on the provided team id string. func (c *Client4) GetPublicChannelsForTeam(teamId string, page int, perPage int, etag string) ([]*Channel, *Response) { query := fmt.Sprintf("?page=%v&per_page=%v", page, perPage) - r, err := c.DoApiGet(c.GetChannelsForTeamRoute(teamId)+query, etag) - if err != nil { - return nil, BuildErrorResponse(r, err) + r, appErr := c.DoApiGet(c.GetChannelsForTeamRoute(teamId)+query, etag) + if appErr != nil { + return nil, BuildErrorResponse(r, appErr) } defer closeBody(r) - return ChannelSliceFromJson(r.Body), BuildResponse(r) + + var ch []*Channel + err := json.NewDecoder(r.Body).Decode(&ch) + if err != nil { + return nil, BuildErrorResponse(r, NewAppError("GetPublicChannelsForTeam", "api.marshal_error", nil, err.Error(), http.StatusInternalServerError)) + } + return ch, BuildResponse(r) } // GetDeletedChannelsForTeam returns a list of public channels based on the provided team id string. func (c *Client4) GetDeletedChannelsForTeam(teamId string, page int, perPage int, etag string) ([]*Channel, *Response) { query := fmt.Sprintf("/deleted?page=%v&per_page=%v", page, perPage) - r, err := c.DoApiGet(c.GetChannelsForTeamRoute(teamId)+query, etag) - if err != nil { - return nil, BuildErrorResponse(r, err) + r, appErr := c.DoApiGet(c.GetChannelsForTeamRoute(teamId)+query, etag) + if appErr != nil { + return nil, BuildErrorResponse(r, appErr) } defer closeBody(r) - return ChannelSliceFromJson(r.Body), BuildResponse(r) + + var ch []*Channel + err := json.NewDecoder(r.Body).Decode(&ch) + if err != nil { + return nil, BuildErrorResponse(r, NewAppError("GetDeletedChannelsForTeam", "api.marshal_error", nil, err.Error(), http.StatusInternalServerError)) + } + return ch, BuildResponse(r) } // GetPublicChannelsByIdsForTeam returns a list of public channels based on provided team id string. func (c *Client4) GetPublicChannelsByIdsForTeam(teamId string, channelIds []string) ([]*Channel, *Response) { - r, err := c.DoApiPost(c.GetChannelsForTeamRoute(teamId)+"/ids", ArrayToJson(channelIds)) - if err != nil { - return nil, BuildErrorResponse(r, err) + r, appErr := c.DoApiPost(c.GetChannelsForTeamRoute(teamId)+"/ids", ArrayToJson(channelIds)) + if appErr != nil { + return nil, BuildErrorResponse(r, appErr) } defer closeBody(r) - return ChannelSliceFromJson(r.Body), BuildResponse(r) + + var ch []*Channel + err := json.NewDecoder(r.Body).Decode(&ch) + if err != nil { + return nil, BuildErrorResponse(r, NewAppError("GetPublicChannelsByIdsForTeam", "api.marshal_error", nil, err.Error(), http.StatusInternalServerError)) + } + return ch, BuildResponse(r) } // GetChannelsForTeamForUser returns a list channels of on a team for a user. func (c *Client4) GetChannelsForTeamForUser(teamId, userId string, includeDeleted bool, etag string) ([]*Channel, *Response) { - r, err := c.DoApiGet(c.GetChannelsForTeamForUserRoute(teamId, userId, includeDeleted), etag) - if err != nil { - return nil, BuildErrorResponse(r, err) + r, appErr := c.DoApiGet(c.GetChannelsForTeamForUserRoute(teamId, userId, includeDeleted), etag) + if appErr != nil { + return nil, BuildErrorResponse(r, appErr) } defer closeBody(r) - return ChannelSliceFromJson(r.Body), BuildResponse(r) + + var ch []*Channel + err := json.NewDecoder(r.Body).Decode(&ch) + if err != nil { + return nil, BuildErrorResponse(r, NewAppError("GetChannelsForTeamForUser", "api.marshal_error", nil, err.Error(), http.StatusInternalServerError)) + } + return ch, BuildResponse(r) } // GetChannelsForTeamAndUserWithLastDeleteAt returns a list channels of a team for a user, additionally filtered with lastDeleteAt. This does not have any effect if includeDeleted is set to false. func (c *Client4) GetChannelsForTeamAndUserWithLastDeleteAt(teamId, userId string, includeDeleted bool, lastDeleteAt int, etag string) ([]*Channel, *Response) { route := fmt.Sprintf(c.GetUserRoute(userId) + c.GetTeamRoute(teamId) + "/channels") route += fmt.Sprintf("?include_deleted=%v&last_delete_at=%d", includeDeleted, lastDeleteAt) - r, err := c.DoApiGet(route, etag) - if err != nil { - return nil, BuildErrorResponse(r, err) + r, appErr := c.DoApiGet(route, etag) + if appErr != nil { + return nil, BuildErrorResponse(r, appErr) } defer closeBody(r) - return ChannelSliceFromJson(r.Body), BuildResponse(r) + + var ch []*Channel + err := json.NewDecoder(r.Body).Decode(&ch) + if err != nil { + return nil, BuildErrorResponse(r, NewAppError("GetChannelsForTeamAndUserWithLastDeleteAt", "api.marshal_error", nil, err.Error(), http.StatusInternalServerError)) + } + return ch, BuildResponse(r) } // SearchChannels returns the channels on a team matching the provided search term. func (c *Client4) SearchChannels(teamId string, search *ChannelSearch) ([]*Channel, *Response) { - r, err := c.DoApiPost(c.GetChannelsForTeamRoute(teamId)+"/search", search.ToJson()) - if err != nil { - return nil, BuildErrorResponse(r, err) + r, appErr := c.DoApiPost(c.GetChannelsForTeamRoute(teamId)+"/search", search.ToJson()) + if appErr != nil { + return nil, BuildErrorResponse(r, appErr) } defer closeBody(r) - return ChannelSliceFromJson(r.Body), BuildResponse(r) + + var ch []*Channel + err := json.NewDecoder(r.Body).Decode(&ch) + if err != nil { + return nil, BuildErrorResponse(r, NewAppError("SearchChannels", "api.marshal_error", nil, err.Error(), http.StatusInternalServerError)) + } + return ch, BuildResponse(r) } // SearchArchivedChannels returns the archived channels on a team matching the provided search term. func (c *Client4) SearchArchivedChannels(teamId string, search *ChannelSearch) ([]*Channel, *Response) { - r, err := c.DoApiPost(c.GetChannelsForTeamRoute(teamId)+"/search_archived", search.ToJson()) - if err != nil { - return nil, BuildErrorResponse(r, err) + r, appErr := c.DoApiPost(c.GetChannelsForTeamRoute(teamId)+"/search_archived", search.ToJson()) + if appErr != nil { + return nil, BuildErrorResponse(r, appErr) } defer closeBody(r) - return ChannelSliceFromJson(r.Body), BuildResponse(r) + + var ch []*Channel + err := json.NewDecoder(r.Body).Decode(&ch) + if err != nil { + return nil, BuildErrorResponse(r, NewAppError("SearchArchivedChannels", "api.marshal_error", nil, err.Error(), http.StatusInternalServerError)) + } + return ch, BuildResponse(r) } // SearchAllChannels search in all the channels. Must be a system administrator. func (c *Client4) SearchAllChannels(search *ChannelSearch) (*ChannelListWithTeamData, *Response) { - r, err := c.DoApiPost(c.GetChannelsRoute()+"/search", search.ToJson()) - if err != nil { - return nil, BuildErrorResponse(r, err) + r, appErr := c.DoApiPost(c.GetChannelsRoute()+"/search", search.ToJson()) + if appErr != nil { + return nil, BuildErrorResponse(r, appErr) } defer closeBody(r) - return ChannelListWithTeamDataFromJson(r.Body), BuildResponse(r) + + var ch *ChannelListWithTeamData + err := json.NewDecoder(r.Body).Decode(&ch) + if err != nil { + return nil, BuildErrorResponse(r, NewAppError("SearchAllChannels", "api.marshal_error", nil, err.Error(), http.StatusInternalServerError)) + } + return ch, BuildResponse(r) } // SearchAllChannelsPaged searches all the channels and returns the results paged with the total count. func (c *Client4) SearchAllChannelsPaged(search *ChannelSearch) (*ChannelsWithCount, *Response) { - r, err := c.DoApiPost(c.GetChannelsRoute()+"/search", search.ToJson()) - if err != nil { - return nil, BuildErrorResponse(r, err) + r, appErr := c.DoApiPost(c.GetChannelsRoute()+"/search", search.ToJson()) + if appErr != nil { + return nil, BuildErrorResponse(r, appErr) } defer closeBody(r) - return ChannelsWithCountFromJson(r.Body), BuildResponse(r) + + var cwc *ChannelsWithCount + err := json.NewDecoder(r.Body).Decode(&cwc) + if err != nil { + return nil, BuildErrorResponse(r, NewAppError("GetAllChannelsWithCount", "api.marshal_error", nil, err.Error(), http.StatusInternalServerError)) + } + return cwc, BuildResponse(r) } // SearchGroupChannels returns the group channels of the user whose members' usernames match the search term. func (c *Client4) SearchGroupChannels(search *ChannelSearch) ([]*Channel, *Response) { - r, err := c.DoApiPost(c.GetChannelsRoute()+"/group/search", search.ToJson()) - if err != nil { - return nil, BuildErrorResponse(r, err) + r, appErr := c.DoApiPost(c.GetChannelsRoute()+"/group/search", search.ToJson()) + if appErr != nil { + return nil, BuildErrorResponse(r, appErr) } defer closeBody(r) - return ChannelSliceFromJson(r.Body), BuildResponse(r) + + var ch []*Channel + err := json.NewDecoder(r.Body).Decode(&ch) + if err != nil { + return nil, BuildErrorResponse(r, NewAppError("SearchGroupChannels", "api.marshal_error", nil, err.Error(), http.StatusInternalServerError)) + } + return ch, BuildResponse(r) } // DeleteChannel deletes channel based on the provided channel id string. @@ -2760,115 +2891,181 @@ func (c *Client4) MoveChannel(channelId, teamId string, force bool) (*Channel, * "team_id": teamId, "force": force, } - r, err := c.DoApiPost(c.GetChannelRoute(channelId)+"/move", StringInterfaceToJson(requestBody)) - if err != nil { - return nil, BuildErrorResponse(r, err) + r, appErr := c.DoApiPost(c.GetChannelRoute(channelId)+"/move", StringInterfaceToJson(requestBody)) + if appErr != nil { + return nil, BuildErrorResponse(r, appErr) } defer closeBody(r) - return ChannelFromJson(r.Body), BuildResponse(r) + + var ch *Channel + err := json.NewDecoder(r.Body).Decode(&ch) + if err != nil { + return nil, BuildErrorResponse(r, NewAppError("MoveChannel", "api.marshal_error", nil, err.Error(), http.StatusInternalServerError)) + } + return ch, BuildResponse(r) } // GetChannelByName returns a channel based on the provided channel name and team id strings. func (c *Client4) GetChannelByName(channelName, teamId string, etag string) (*Channel, *Response) { - r, err := c.DoApiGet(c.GetChannelByNameRoute(channelName, teamId), etag) - if err != nil { - return nil, BuildErrorResponse(r, err) + r, appErr := c.DoApiGet(c.GetChannelByNameRoute(channelName, teamId), etag) + if appErr != nil { + return nil, BuildErrorResponse(r, appErr) } defer closeBody(r) - return ChannelFromJson(r.Body), BuildResponse(r) + + var ch *Channel + err := json.NewDecoder(r.Body).Decode(&ch) + if err != nil { + return nil, BuildErrorResponse(r, NewAppError("GetChannelByName", "api.marshal_error", nil, err.Error(), http.StatusInternalServerError)) + } + return ch, BuildResponse(r) } // GetChannelByNameIncludeDeleted returns a channel based on the provided channel name and team id strings. Other then GetChannelByName it will also return deleted channels. func (c *Client4) GetChannelByNameIncludeDeleted(channelName, teamId string, etag string) (*Channel, *Response) { - r, err := c.DoApiGet(c.GetChannelByNameRoute(channelName, teamId)+"?include_deleted="+c.boolString(true), etag) - if err != nil { - return nil, BuildErrorResponse(r, err) + r, appErr := c.DoApiGet(c.GetChannelByNameRoute(channelName, teamId)+"?include_deleted="+c.boolString(true), etag) + if appErr != nil { + return nil, BuildErrorResponse(r, appErr) } defer closeBody(r) - return ChannelFromJson(r.Body), BuildResponse(r) + + var ch *Channel + err := json.NewDecoder(r.Body).Decode(&ch) + if err != nil { + return nil, BuildErrorResponse(r, NewAppError("GetChannelByNameIncludeDeleted", "api.marshal_error", nil, err.Error(), http.StatusInternalServerError)) + } + return ch, BuildResponse(r) } // GetChannelByNameForTeamName returns a channel based on the provided channel name and team name strings. func (c *Client4) GetChannelByNameForTeamName(channelName, teamName string, etag string) (*Channel, *Response) { - r, err := c.DoApiGet(c.GetChannelByNameForTeamNameRoute(channelName, teamName), etag) - if err != nil { - return nil, BuildErrorResponse(r, err) + r, appErr := c.DoApiGet(c.GetChannelByNameForTeamNameRoute(channelName, teamName), etag) + if appErr != nil { + return nil, BuildErrorResponse(r, appErr) } defer closeBody(r) - return ChannelFromJson(r.Body), BuildResponse(r) + + var ch *Channel + err := json.NewDecoder(r.Body).Decode(&ch) + if err != nil { + return nil, BuildErrorResponse(r, NewAppError("GetChannelByNameForTeamName", "api.marshal_error", nil, err.Error(), http.StatusInternalServerError)) + } + return ch, BuildResponse(r) } // GetChannelByNameForTeamNameIncludeDeleted returns a channel based on the provided channel name and team name strings. Other then GetChannelByNameForTeamName it will also return deleted channels. func (c *Client4) GetChannelByNameForTeamNameIncludeDeleted(channelName, teamName string, etag string) (*Channel, *Response) { - r, err := c.DoApiGet(c.GetChannelByNameForTeamNameRoute(channelName, teamName)+"?include_deleted="+c.boolString(true), etag) - if err != nil { - return nil, BuildErrorResponse(r, err) + r, appErr := c.DoApiGet(c.GetChannelByNameForTeamNameRoute(channelName, teamName)+"?include_deleted="+c.boolString(true), etag) + if appErr != nil { + return nil, BuildErrorResponse(r, appErr) } defer closeBody(r) - return ChannelFromJson(r.Body), BuildResponse(r) + + var ch *Channel + err := json.NewDecoder(r.Body).Decode(&ch) + if err != nil { + return nil, BuildErrorResponse(r, NewAppError("GetChannelByNameForTeamNameIncludeDeleted", "api.marshal_error", nil, err.Error(), http.StatusInternalServerError)) + } + return ch, BuildResponse(r) } // GetChannelMembers gets a page of channel members. func (c *Client4) GetChannelMembers(channelId string, page, perPage int, etag string) (*ChannelMembers, *Response) { query := fmt.Sprintf("?page=%v&per_page=%v", page, perPage) - r, err := c.DoApiGet(c.GetChannelMembersRoute(channelId)+query, etag) - if err != nil { - return nil, BuildErrorResponse(r, err) + r, appErr := c.DoApiGet(c.GetChannelMembersRoute(channelId)+query, etag) + if appErr != nil { + return nil, BuildErrorResponse(r, appErr) } defer closeBody(r) - return ChannelMembersFromJson(r.Body), BuildResponse(r) + + var ch *ChannelMembers + err := json.NewDecoder(r.Body).Decode(&ch) + if err != nil { + return nil, BuildErrorResponse(r, NewAppError("GetChannelMembers", "api.marshal_error", nil, err.Error(), http.StatusInternalServerError)) + } + return ch, BuildResponse(r) } // GetChannelMembersByIds gets the channel members in a channel for a list of user ids. func (c *Client4) GetChannelMembersByIds(channelId string, userIds []string) (*ChannelMembers, *Response) { - r, err := c.DoApiPost(c.GetChannelMembersRoute(channelId)+"/ids", ArrayToJson(userIds)) - if err != nil { - return nil, BuildErrorResponse(r, err) + r, appErr := c.DoApiPost(c.GetChannelMembersRoute(channelId)+"/ids", ArrayToJson(userIds)) + if appErr != nil { + return nil, BuildErrorResponse(r, appErr) } defer closeBody(r) - return ChannelMembersFromJson(r.Body), BuildResponse(r) + + var ch *ChannelMembers + err := json.NewDecoder(r.Body).Decode(&ch) + if err != nil { + return nil, BuildErrorResponse(r, NewAppError("GetChannelMembersByIds", "api.marshal_error", nil, err.Error(), http.StatusInternalServerError)) + } + return ch, BuildResponse(r) } // GetChannelMember gets a channel member. func (c *Client4) GetChannelMember(channelId, userId, etag string) (*ChannelMember, *Response) { - r, err := c.DoApiGet(c.GetChannelMemberRoute(channelId, userId), etag) - if err != nil { - return nil, BuildErrorResponse(r, err) + r, appErr := c.DoApiGet(c.GetChannelMemberRoute(channelId, userId), etag) + if appErr != nil { + return nil, BuildErrorResponse(r, appErr) } defer closeBody(r) - return ChannelMemberFromJson(r.Body), BuildResponse(r) + + var ch *ChannelMember + err := json.NewDecoder(r.Body).Decode(&ch) + if err != nil { + return nil, BuildErrorResponse(r, NewAppError("GetChannelMember", "api.marshal_error", nil, err.Error(), http.StatusInternalServerError)) + } + return ch, BuildResponse(r) } // GetChannelMembersForUser gets all the channel members for a user on a team. func (c *Client4) GetChannelMembersForUser(userId, teamId, etag string) (*ChannelMembers, *Response) { - r, err := c.DoApiGet(fmt.Sprintf(c.GetUserRoute(userId)+"/teams/%v/channels/members", teamId), etag) - if err != nil { - return nil, BuildErrorResponse(r, err) + r, appErr := c.DoApiGet(fmt.Sprintf(c.GetUserRoute(userId)+"/teams/%v/channels/members", teamId), etag) + if appErr != nil { + return nil, BuildErrorResponse(r, appErr) } defer closeBody(r) - return ChannelMembersFromJson(r.Body), BuildResponse(r) + + var ch *ChannelMembers + err := json.NewDecoder(r.Body).Decode(&ch) + if err != nil { + return nil, BuildErrorResponse(r, NewAppError("GetChannelMembersForUser", "api.marshal_error", nil, err.Error(), http.StatusInternalServerError)) + } + return ch, BuildResponse(r) } // 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) (*ChannelViewResponse, *Response) { url := fmt.Sprintf(c.GetChannelsRoute()+"/members/%v/view", userId) - r, err := c.DoApiPost(url, view.ToJson()) - if err != nil { - return nil, BuildErrorResponse(r, err) + r, appErr := c.DoApiPost(url, view.ToJson()) + if appErr != nil { + return nil, BuildErrorResponse(r, appErr) } defer closeBody(r) - return ChannelViewResponseFromJson(r.Body), BuildResponse(r) + + var ch *ChannelViewResponse + err := json.NewDecoder(r.Body).Decode(&ch) + if err != nil { + return nil, BuildErrorResponse(r, NewAppError("ViewChannel", "api.marshal_error", nil, err.Error(), http.StatusInternalServerError)) + } + return ch, BuildResponse(r) } // GetChannelUnread will return a ChannelUnread object that contains the number of // unread messages and mentions for a user. func (c *Client4) GetChannelUnread(channelId, userId string) (*ChannelUnread, *Response) { - r, err := c.DoApiGet(c.GetUserRoute(userId)+c.GetChannelRoute(channelId)+"/unread", "") - if err != nil { - return nil, BuildErrorResponse(r, err) + r, appErr := c.DoApiGet(c.GetUserRoute(userId)+c.GetChannelRoute(channelId)+"/unread", "") + if appErr != nil { + return nil, BuildErrorResponse(r, appErr) } defer closeBody(r) - return ChannelUnreadFromJson(r.Body), BuildResponse(r) + + var ch *ChannelUnread + err := json.NewDecoder(r.Body).Decode(&ch) + if err != nil { + return nil, BuildErrorResponse(r, NewAppError("GetChannelUnread", "api.marshal_error", nil, err.Error(), http.StatusInternalServerError)) + } + return ch, BuildResponse(r) } // UpdateChannelRoles will update the roles on a channel for a user. @@ -2905,23 +3102,35 @@ func (c *Client4) UpdateChannelNotifyProps(channelId, userId string, props map[s // AddChannelMember adds user to channel and return a channel member. func (c *Client4) AddChannelMember(channelId, userId string) (*ChannelMember, *Response) { requestBody := map[string]string{"user_id": userId} - r, err := c.DoApiPost(c.GetChannelMembersRoute(channelId)+"", MapToJson(requestBody)) - if err != nil { - return nil, BuildErrorResponse(r, err) + r, appErr := c.DoApiPost(c.GetChannelMembersRoute(channelId)+"", MapToJson(requestBody)) + if appErr != nil { + return nil, BuildErrorResponse(r, appErr) } defer closeBody(r) - return ChannelMemberFromJson(r.Body), BuildResponse(r) + + var ch *ChannelMember + err := json.NewDecoder(r.Body).Decode(&ch) + if err != nil { + return nil, BuildErrorResponse(r, NewAppError("AddChannelMember", "api.marshal_error", nil, err.Error(), http.StatusInternalServerError)) + } + return ch, BuildResponse(r) } // AddChannelMemberWithRootId adds user to channel and return a channel member. Post add to channel message has the postRootId. func (c *Client4) AddChannelMemberWithRootId(channelId, userId, postRootId string) (*ChannelMember, *Response) { requestBody := map[string]string{"user_id": userId, "post_root_id": postRootId} - r, err := c.DoApiPost(c.GetChannelMembersRoute(channelId)+"", MapToJson(requestBody)) - if err != nil { - return nil, BuildErrorResponse(r, err) + r, appErr := c.DoApiPost(c.GetChannelMembersRoute(channelId)+"", MapToJson(requestBody)) + if appErr != nil { + return nil, BuildErrorResponse(r, appErr) } defer closeBody(r) - return ChannelMemberFromJson(r.Body), BuildResponse(r) + + var ch *ChannelMember + err := json.NewDecoder(r.Body).Decode(&ch) + if err != nil { + return nil, BuildErrorResponse(r, NewAppError("AddChannelMemberWithRootId", "api.marshal_error", nil, err.Error(), http.StatusInternalServerError)) + } + return ch, BuildResponse(r) } // RemoveUserFromChannel will delete the channel member object for a user, effectively removing the user from a channel. @@ -2937,23 +3146,35 @@ func (c *Client4) RemoveUserFromChannel(channelId, userId string) (bool, *Respon // AutocompleteChannelsForTeam will return an ordered list of channels autocomplete suggestions. func (c *Client4) AutocompleteChannelsForTeam(teamId, name string) (*ChannelList, *Response) { query := fmt.Sprintf("?name=%v", name) - r, err := c.DoApiGet(c.GetChannelsForTeamRoute(teamId)+"/autocomplete"+query, "") - if err != nil { - return nil, BuildErrorResponse(r, err) + r, app := c.DoApiGet(c.GetChannelsForTeamRoute(teamId)+"/autocomplete"+query, "") + if app != nil { + return nil, BuildErrorResponse(r, app) } defer closeBody(r) - return ChannelListFromJson(r.Body), BuildResponse(r) + + var ch *ChannelList + err := json.NewDecoder(r.Body).Decode(&ch) + if err != nil { + return nil, BuildErrorResponse(r, NewAppError("AutocompleteChannelsForTeam", "api.marshal_error", nil, err.Error(), http.StatusInternalServerError)) + } + return ch, BuildResponse(r) } // AutocompleteChannelsForTeamForSearch will return an ordered list of your channels autocomplete suggestions. func (c *Client4) AutocompleteChannelsForTeamForSearch(teamId, name string) (*ChannelList, *Response) { query := fmt.Sprintf("?name=%v", name) - r, err := c.DoApiGet(c.GetChannelsForTeamRoute(teamId)+"/search_autocomplete"+query, "") - if err != nil { - return nil, BuildErrorResponse(r, err) + r, appErr := c.DoApiGet(c.GetChannelsForTeamRoute(teamId)+"/search_autocomplete"+query, "") + if appErr != nil { + return nil, BuildErrorResponse(r, appErr) } defer closeBody(r) - return ChannelListFromJson(r.Body), BuildResponse(r) + + var ch *ChannelList + err := json.NewDecoder(r.Body).Decode(&ch) + if err != nil { + return nil, BuildErrorResponse(r, NewAppError("AutocompleteChannelsForTeamForSearch", "api.marshal_error", nil, err.Error(), http.StatusInternalServerError)) + } + return ch, BuildResponse(r) } // Post Section @@ -5441,12 +5662,18 @@ func (c *Client4) GetTeamsForScheme(schemeId string, page int, perPage int) ([]* // GetChannelsForScheme gets the channels using this scheme, sorted alphabetically by display name. func (c *Client4) GetChannelsForScheme(schemeId string, page int, perPage int) (ChannelList, *Response) { - r, err := c.DoApiGet(c.GetSchemeRoute(schemeId)+fmt.Sprintf("/channels?page=%v&per_page=%v", page, perPage), "") - if err != nil { - return nil, BuildErrorResponse(r, err) + r, appErr := c.DoApiGet(c.GetSchemeRoute(schemeId)+fmt.Sprintf("/channels?page=%v&per_page=%v", page, perPage), "") + if appErr != nil { + return nil, BuildErrorResponse(r, appErr) } defer closeBody(r) - return *ChannelListFromJson(r.Body), BuildResponse(r) + + var ch ChannelList + err := json.NewDecoder(r.Body).Decode(&ch) + if err != nil { + return nil, BuildErrorResponse(r, NewAppError("GetChannelsForScheme", "api.marshal_error", nil, err.Error(), http.StatusInternalServerError)) + } + return ch, BuildResponse(r) } // Plugin Section @@ -5858,22 +6085,38 @@ func (c *Client4) PatchConfig(config *Config) (*Config, *Response) { } func (c *Client4) GetChannelModerations(channelID string, etag string) ([]*ChannelModeration, *Response) { - r, err := c.DoApiGet(c.GetChannelRoute(channelID)+"/moderations", etag) - if err != nil { - return nil, BuildErrorResponse(r, err) + r, appErr := c.DoApiGet(c.GetChannelRoute(channelID)+"/moderations", etag) + if appErr != nil { + return nil, BuildErrorResponse(r, appErr) } defer closeBody(r) - return ChannelModerationsFromJson(r.Body), BuildResponse(r) + + var ch []*ChannelModeration + err := json.NewDecoder(r.Body).Decode(&ch) + if err != nil { + return nil, BuildErrorResponse(r, NewAppError("GetChannelModerations", "api.marshal_error", nil, err.Error(), http.StatusInternalServerError)) + } + return ch, BuildResponse(r) } func (c *Client4) PatchChannelModerations(channelID string, patch []*ChannelModerationPatch) ([]*ChannelModeration, *Response) { - payload, _ := json.Marshal(patch) - r, err := c.DoApiPut(c.GetChannelRoute(channelID)+"/moderations/patch", string(payload)) + payload, err := json.Marshal(patch) if err != nil { - return nil, BuildErrorResponse(r, err) + return nil, BuildErrorResponse(nil, NewAppError("PatchChannelModerations", "api.marshal_error", nil, err.Error(), http.StatusInternalServerError)) + } + + r, appErr := c.DoApiPut(c.GetChannelRoute(channelID)+"/moderations/patch", string(payload)) + if appErr != nil { + return nil, BuildErrorResponse(r, appErr) } defer closeBody(r) - return ChannelModerationsFromJson(r.Body), BuildResponse(r) + + var ch []*ChannelModeration + err = json.NewDecoder(r.Body).Decode(&ch) + if err != nil { + return nil, BuildErrorResponse(r, NewAppError("PatchChannelModerations", "api.marshal_error", nil, err.Error(), http.StatusInternalServerError)) + } + return ch, BuildResponse(r) } func (c *Client4) GetKnownUsers() ([]string, *Response) { @@ -5898,12 +6141,18 @@ func (c *Client4) PublishUserTyping(userID string, typingRequest TypingRequest) } func (c *Client4) GetChannelMemberCountsByGroup(channelID string, includeTimezones bool, etag string) ([]*ChannelMemberCountByGroup, *Response) { - r, err := c.DoApiGet(c.GetChannelRoute(channelID)+"/member_counts_by_group?include_timezones="+strconv.FormatBool(includeTimezones), etag) - if err != nil { - return nil, BuildErrorResponse(r, err) + r, appErr := c.DoApiGet(c.GetChannelRoute(channelID)+"/member_counts_by_group?include_timezones="+strconv.FormatBool(includeTimezones), etag) + if appErr != nil { + return nil, BuildErrorResponse(r, appErr) } defer closeBody(r) - return ChannelMemberCountsByGroupFromJson(r.Body), BuildResponse(r) + + var ch []*ChannelMemberCountByGroup + err := json.NewDecoder(r.Body).Decode(&ch) + if err != nil { + return nil, BuildErrorResponse(r, NewAppError("GetChannelMemberCountsByGroup", "api.marshal_error", nil, err.Error(), http.StatusInternalServerError)) + } + return ch, BuildResponse(r) } // RequestTrialLicense will request a trial license and install it in the server @@ -5933,7 +6182,9 @@ func (c *Client4) GetSidebarCategoriesForTeamForUser(userID, teamID, etag string if appErr != nil { return nil, BuildErrorResponse(r, appErr) } - cat, err := OrderedSidebarCategoriesFromJson(r.Body) + + var cat *OrderedSidebarCategories + err := json.NewDecoder(r.Body).Decode(&cat) if err != nil { return nil, BuildErrorResponse(r, NewAppError("Client4.GetSidebarCategoriesForTeamForUser", "model.utils.decode_json.app_error", nil, err.Error(), r.StatusCode)) } @@ -5948,7 +6199,8 @@ func (c *Client4) CreateSidebarCategoryForTeamForUser(userID, teamID string, cat return nil, BuildErrorResponse(r, appErr) } defer closeBody(r) - cat, err := SidebarCategoryFromJson(r.Body) + var cat *SidebarCategoryWithChannels + err := json.NewDecoder(r.Body).Decode(&cat) if err != nil { return nil, BuildErrorResponse(r, NewAppError("Client4.CreateSidebarCategoryForTeamForUser", "model.utils.decode_json.app_error", nil, err.Error(), r.StatusCode)) } @@ -5965,12 +6217,13 @@ func (c *Client4) UpdateSidebarCategoriesForTeamForUser(userID, teamID string, c } defer closeBody(r) - categories, err := SidebarCategoriesFromJson(r.Body) + var cat []*SidebarCategoryWithChannels + err := json.NewDecoder(r.Body).Decode(&cat) if err != nil { return nil, BuildErrorResponse(r, NewAppError("Client4.UpdateSidebarCategoriesForTeamForUser", "model.utils.decode_json.app_error", nil, err.Error(), r.StatusCode)) } - return categories, BuildResponse(r) + return cat, BuildResponse(r) } func (c *Client4) GetSidebarCategoryOrderForTeamForUser(userID, teamID, etag string) ([]string, *Response) { @@ -6001,7 +6254,8 @@ func (c *Client4) GetSidebarCategoryForTeamForUser(userID, teamID, categoryID, e return nil, BuildErrorResponse(r, appErr) } defer closeBody(r) - cat, err := SidebarCategoryFromJson(r.Body) + var cat *SidebarCategoryWithChannels + err := json.NewDecoder(r.Body).Decode(&cat) if err != nil { return nil, BuildErrorResponse(r, NewAppError("Client4.UpdateSidebarCategoriesForTeamForUser", "model.utils.decode_json.app_error", nil, err.Error(), r.StatusCode)) } @@ -6017,7 +6271,8 @@ func (c *Client4) UpdateSidebarCategoryForTeamForUser(userID, teamID, categoryID return nil, BuildErrorResponse(r, appErr) } defer closeBody(r) - cat, err := SidebarCategoryFromJson(r.Body) + var cat *SidebarCategoryWithChannels + err := json.NewDecoder(r.Body).Decode(&cat) if err != nil { return nil, BuildErrorResponse(r, NewAppError("Client4.UpdateSidebarCategoriesForTeamForUser", "model.utils.decode_json.app_error", nil, err.Error(), r.StatusCode)) } diff --git a/model/team_search_test.go b/model/team_search_test.go index e07e878c44..d66d1dc292 100644 --- a/model/team_search_test.go +++ b/model/team_search_test.go @@ -4,16 +4,18 @@ package model import ( - "strings" + "encoding/json" "testing" "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" ) func TestTeamSearchJson(t *testing.T) { teamSearch := TeamSearch{Term: NewId()} - json := teamSearch.ToJson() - rteamSearch := ChannelSearchFromJson(strings.NewReader(json)) + var rteamSearch *ChannelSearch + err := json.Unmarshal([]byte(teamSearch.ToJson()), &rteamSearch) + require.NoError(t, err) assert.Equal(t, teamSearch.Term, rteamSearch.Term, "Terms do not match") }