From 2df3951f6fbe59ec46054e161e7d543ea1595d25 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jes=C3=BAs=20Espino?= Date: Tue, 6 Aug 2019 11:33:32 +0200 Subject: [PATCH] Create direct message respect the user role (Guest/Normal user) (#11697) * Create direct message respect the user role (Guest/Normal user) * Fixing build * Updating Timer store layer --- app/channel.go | 10 +++++++--- store/sqlstore/channel_store.go | 14 ++++++++------ store/store.go | 2 +- store/storetest/channel_store.go | 23 +++++++++++++++-------- store/storetest/compliance_store.go | 4 ++-- store/storetest/mocks/ChannelStore.go | 6 +++--- store/timer_layer.go | 2 +- 7 files changed, 37 insertions(+), 24 deletions(-) diff --git a/app/channel.go b/app/channel.go index 7e4956d741..f6f6b70f32 100644 --- a/app/channel.go +++ b/app/channel.go @@ -340,15 +340,19 @@ func (a *App) createDirectChannel(userId string, otherUserId string) (*model.Cha close(uc2) }() - if result := <-uc1; result.Err != nil { + result := <-uc1 + if result.Err != nil { return nil, model.NewAppError("CreateDirectChannel", "api.channel.create_direct_channel.invalid_user.app_error", nil, userId, http.StatusBadRequest) } + user := result.Data.(*model.User) - if result := <-uc2; result.Err != nil { + result = <-uc2 + if result.Err != nil { return nil, model.NewAppError("CreateDirectChannel", "api.channel.create_direct_channel.invalid_user.app_error", nil, otherUserId, http.StatusBadRequest) } + otherUser := result.Data.(*model.User) - channel, err := a.Srv.Store.Channel().CreateDirectChannel(userId, otherUserId) + channel, err := a.Srv.Store.Channel().CreateDirectChannel(user, otherUser) if err != nil { if err.Id == store.CHANNEL_EXISTS_ERROR { return channel, err diff --git a/store/sqlstore/channel_store.go b/store/sqlstore/channel_store.go index ca2ff3297e..d4f3d3c2d5 100644 --- a/store/sqlstore/channel_store.go +++ b/store/sqlstore/channel_store.go @@ -489,24 +489,26 @@ func (s SqlChannelStore) Save(channel *model.Channel, maxChannelsPerTeam int64) return newChannel, nil } -func (s SqlChannelStore) CreateDirectChannel(userId string, otherUserId string) (*model.Channel, *model.AppError) { +func (s SqlChannelStore) CreateDirectChannel(user *model.User, otherUser *model.User) (*model.Channel, *model.AppError) { channel := new(model.Channel) channel.DisplayName = "" - channel.Name = model.GetDMNameFromIds(otherUserId, userId) + channel.Name = model.GetDMNameFromIds(otherUser.Id, user.Id) channel.Header = "" channel.Type = model.CHANNEL_DIRECT cm1 := &model.ChannelMember{ - UserId: userId, + UserId: user.Id, NotifyProps: model.GetDefaultChannelNotifyProps(), - SchemeUser: true, + SchemeGuest: user.IsGuest(), + SchemeUser: !user.IsGuest(), } cm2 := &model.ChannelMember{ - UserId: otherUserId, + UserId: otherUser.Id, NotifyProps: model.GetDefaultChannelNotifyProps(), - SchemeUser: true, + SchemeGuest: otherUser.IsGuest(), + SchemeUser: !otherUser.IsGuest(), } return s.SaveDirectChannel(channel, cm1, cm2) diff --git a/store/store.go b/store/store.go index 5cb239ec37..c0d1cabdf5 100644 --- a/store/store.go +++ b/store/store.go @@ -105,7 +105,7 @@ type TeamStore interface { type ChannelStore interface { Save(channel *model.Channel, maxChannelsPerTeam int64) (*model.Channel, *model.AppError) - CreateDirectChannel(userId string, otherUserId string) (*model.Channel, *model.AppError) + CreateDirectChannel(userId *model.User, otherUserId *model.User) (*model.Channel, *model.AppError) SaveDirectChannel(channel *model.Channel, member1 *model.ChannelMember, member2 *model.ChannelMember) (*model.Channel, *model.AppError) Update(channel *model.Channel) (*model.Channel, *model.AppError) Get(id string, allowFromCache bool) (*model.Channel, *model.AppError) diff --git a/store/storetest/channel_store.go b/store/storetest/channel_store.go index d243c85ea2..edf4531a75 100644 --- a/store/storetest/channel_store.go +++ b/store/storetest/channel_store.go @@ -230,7 +230,7 @@ func testChannelStoreCreateDirectChannel(t *testing.T, ss store.Store) { _, err = ss.Team().SaveMember(&model.TeamMember{TeamId: model.NewId(), UserId: u2.Id}, -1) require.Nil(t, err) - c1, err := ss.Channel().CreateDirectChannel(u1.Id, u2.Id) + c1, err := ss.Channel().CreateDirectChannel(u1, u2) if err != nil { t.Fatal("couldn't create direct channel", err) } @@ -1173,7 +1173,9 @@ func testChannelStoreGetAllChannels(t *testing.T, ss store.Store, s SqlSupplier) _, err = ss.Channel().Save(&c3, -1) require.Nil(t, err) - _, err = ss.Channel().CreateDirectChannel(model.NewId(), model.NewId()) + u1 := model.User{Id: model.NewId()} + u2 := model.User{Id: model.NewId()} + _, err = ss.Channel().CreateDirectChannel(&u1, &u2) require.Nil(t, err) userIds := []string{model.NewId(), model.NewId(), model.NewId()} @@ -1649,12 +1651,17 @@ func testChannelStoreGetMembersForUser(t *testing.T, ss store.Store) { }) t.Run("with channels and direct messages", func(t *testing.T) { - _, err = ss.Channel().CreateDirectChannel(model.NewId(), m1.UserId) + user := model.User{Id: m1.UserId} + u1 := model.User{Id: model.NewId()} + u2 := model.User{Id: model.NewId()} + u3 := model.User{Id: model.NewId()} + u4 := model.User{Id: model.NewId()} + _, err = ss.Channel().CreateDirectChannel(&u1, &user) require.Nil(t, err) - _, err = ss.Channel().CreateDirectChannel(model.NewId(), m1.UserId) + _, err = ss.Channel().CreateDirectChannel(&u2, &user) require.Nil(t, err) // other user direct message - _, err = ss.Channel().CreateDirectChannel(model.NewId(), model.NewId()) + _, err = ss.Channel().CreateDirectChannel(&u3, &u4) require.Nil(t, err) var members *model.ChannelMembers @@ -3004,9 +3011,9 @@ func testChannelStoreAutocompleteInTeamForSearch(t *testing.T, ss store.Store, s _, err = ss.Channel().Save(&o5, -1) require.Nil(t, err) - _, err = ss.Channel().CreateDirectChannel(u1.Id, u2.Id) + _, err = ss.Channel().CreateDirectChannel(u1, u2) require.Nil(t, err) - _, err = ss.Channel().CreateDirectChannel(u2.Id, u3.Id) + _, err = ss.Channel().CreateDirectChannel(u2, u3) require.Nil(t, err) tt := []struct { @@ -3273,7 +3280,7 @@ func testChannelStoreAnalyticsDeletedTypeCount(t *testing.T, ss store.Store) { _, err = ss.User().Save(u2) require.Nil(t, err) - d4, err := ss.Channel().CreateDirectChannel(u1.Id, u2.Id) + d4, err := ss.Channel().CreateDirectChannel(u1, u2) if err != nil { t.Fatalf(err.Error()) } diff --git a/store/storetest/compliance_store.go b/store/storetest/compliance_store.go index f79b776ef2..48ac4c8af8 100644 --- a/store/storetest/compliance_store.go +++ b/store/storetest/compliance_store.go @@ -206,7 +206,7 @@ func testComplianceExportDirectMessages(t *testing.T, ss store.Store) { c1, err = ss.Channel().Save(c1, -1) require.Nil(t, err) - cDM, err := ss.Channel().CreateDirectChannel(u1.Id, u2.Id) + cDM, err := ss.Channel().CreateDirectChannel(u1, u2) require.Nil(t, err) o1 := &model.Post{} o1.ChannelId = c1.Id @@ -507,7 +507,7 @@ func testMessageExportDirectMessageChannel(t *testing.T, ss store.Store) { require.Nil(t, err) // as well as a DM channel between those users - directMessageChannel, err := ss.Channel().CreateDirectChannel(user1.Id, user2.Id) + directMessageChannel, err := ss.Channel().CreateDirectChannel(user1, user2) require.Nil(t, err) // user1 also sends a DM to user2 diff --git a/store/storetest/mocks/ChannelStore.go b/store/storetest/mocks/ChannelStore.go index e8dc3fd5cb..9c78f72efd 100644 --- a/store/storetest/mocks/ChannelStore.go +++ b/store/storetest/mocks/ChannelStore.go @@ -131,11 +131,11 @@ func (_m *ChannelStore) ClearCaches() { } // CreateDirectChannel provides a mock function with given fields: userId, otherUserId -func (_m *ChannelStore) CreateDirectChannel(userId string, otherUserId string) (*model.Channel, *model.AppError) { +func (_m *ChannelStore) CreateDirectChannel(userId *model.User, otherUserId *model.User) (*model.Channel, *model.AppError) { ret := _m.Called(userId, otherUserId) var r0 *model.Channel - if rf, ok := ret.Get(0).(func(string, string) *model.Channel); ok { + if rf, ok := ret.Get(0).(func(*model.User, *model.User) *model.Channel); ok { r0 = rf(userId, otherUserId) } else { if ret.Get(0) != nil { @@ -144,7 +144,7 @@ func (_m *ChannelStore) CreateDirectChannel(userId string, otherUserId string) ( } var r1 *model.AppError - if rf, ok := ret.Get(1).(func(string, string) *model.AppError); ok { + if rf, ok := ret.Get(1).(func(*model.User, *model.User) *model.AppError); ok { r1 = rf(userId, otherUserId) } else { if ret.Get(1) != nil { diff --git a/store/timer_layer.go b/store/timer_layer.go index c7ae481932..17848f300f 100644 --- a/store/timer_layer.go +++ b/store/timer_layer.go @@ -583,7 +583,7 @@ func (s *TimerLayerChannelStore) ClearCaches() { return } -func (s *TimerLayerChannelStore) CreateDirectChannel(userId string, otherUserId string) (*model.Channel, *model.AppError) { +func (s *TimerLayerChannelStore) CreateDirectChannel(userId *model.User, otherUserId *model.User) (*model.Channel, *model.AppError) { start := timemodule.Now() resultVar0, resultVar1 := s.ChannelStore.CreateDirectChannel(userId, otherUserId)