From 908ed5555f8a3d37cd057035b2792d66c8b7838a Mon Sep 17 00:00:00 2001 From: Carlos Tadeu Panato Junior Date: Sat, 13 Oct 2018 12:35:57 +0200 Subject: [APIv4] add getChannelMembersTimezone (#9286) * add getChannelMembersTimezone * update per feedback review * add delimeter to error --- app/channel.go | 18 ++++++++++++++++++ app/channel_test.go | 38 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 56 insertions(+) (limited to 'app') diff --git a/app/channel.go b/app/channel.go index 93037cf05..dee856b94 100644 --- a/app/channel.go +++ b/app/channel.go @@ -1153,6 +1153,24 @@ func (a *App) GetChannelMembersPage(channelId string, page, perPage int) (*model return result.Data.(*model.ChannelMembers), nil } +func (a *App) GetChannelMembersTimezones(channelId string) ([]string, *model.AppError) { + result := <-a.Srv.Store.Channel().GetChannelMembersTimezones(channelId) + if result.Err != nil { + return nil, result.Err + } + membersTimezones := result.Data.([]map[string]string) + + var timezones []string + for _, membersTimezone := range membersTimezones { + if membersTimezone["automaticTimezone"] == "" && membersTimezone["manualTimezone"] == "" { + continue + } + timezones = append(timezones, model.GetPreferredTimezone(membersTimezone)) + } + + return model.RemoveDuplicateStrings(timezones), nil +} + func (a *App) GetChannelMembersByIds(channelId string, userIds []string) (*model.ChannelMembers, *model.AppError) { result := <-a.Srv.Store.Channel().GetMembersByIds(channelId, userIds) if result.Err != nil { diff --git a/app/channel_test.go b/app/channel_test.go index 4b09bbb78..b6f460741 100644 --- a/app/channel_test.go +++ b/app/channel_test.go @@ -4,6 +4,7 @@ package app import ( + "strings" "testing" "github.com/mattermost/mattermost-server/model" @@ -709,3 +710,40 @@ func TestRenameChannel(t *testing.T) { }) } } + +func TestGetChannelMembersTimezones(t *testing.T) { + th := Setup().InitBasic().InitSystemAdmin() + defer th.TearDown() + + userRequestorId := "" + postRootId := "" + if _, err := th.App.AddChannelMember(th.BasicUser2.Id, th.BasicChannel, userRequestorId, postRootId, false); err != nil { + t.Fatal("Failed to add user to channel. Error: " + err.Message) + } + + user := th.BasicUser + user.Timezone["useAutomaticTimezone"] = "false" + user.Timezone["manualTimezone"] = "XOXO/BLABLA" + th.App.UpdateUser(user, false) + + user2 := th.BasicUser2 + user2.Timezone["automaticTimezone"] = "NoWhere/Island" + th.App.UpdateUser(user2, false) + + user3 := model.User{Email: strings.ToLower(model.NewId()) + "success+test@example.com", Nickname: "Darth Vader", Username: "vader" + model.NewId(), Password: "passwd1", AuthService: ""} + ruser, _ := th.App.CreateUser(&user3) + th.App.AddUserToChannel(ruser, th.BasicChannel) + + ruser.Timezone["automaticTimezone"] = "NoWhere/Island" + th.App.UpdateUser(ruser, false) + + user4 := model.User{Email: strings.ToLower(model.NewId()) + "success+test@example.com", Nickname: "Darth Vader", Username: "vader" + model.NewId(), Password: "passwd1", AuthService: ""} + ruser, _ = th.App.CreateUser(&user4) + th.App.AddUserToChannel(ruser, th.BasicChannel) + + timezones, err := th.App.GetChannelMembersTimezones(th.BasicChannel.Id) + if err != nil { + t.Fatal("Failed to get the timezones for a channel. Error: " + err.Error()) + } + assert.Equal(t, 2, len(timezones)) +} -- cgit v1.2.3-1-g7c22