MM-34002: Improve AddUserToChannel (#17174)
* MM-34002: Improve AddUserToChannel When we would add a user to a channel, we would check whether the user is removed from that team or not. During LDAP sync, this check is not required because the team member would have just been created. Hence, we pass a boolean flag to bypass the check. And with that done, we can freely query the replica. https://mattermost.atlassian.net/browse/MM-34002 ```release-note NONE ``` * Refactor code * Rename a struct field * fix double negative
Этот коммит содержится в:
коммит произвёл
GitHub
родитель
4f0cfbe329
Коммит
db01f2a91b
@@ -380,14 +380,14 @@ func (th *TestHelper) InitBasic() *TestHelper {
|
||||
th.BasicPost = th.CreatePost()
|
||||
th.LinkUserToTeam(th.BasicUser, th.BasicTeam)
|
||||
th.LinkUserToTeam(th.BasicUser2, th.BasicTeam)
|
||||
th.App.AddUserToChannel(th.BasicUser, th.BasicChannel)
|
||||
th.App.AddUserToChannel(th.BasicUser2, th.BasicChannel)
|
||||
th.App.AddUserToChannel(th.BasicUser, th.BasicChannel2)
|
||||
th.App.AddUserToChannel(th.BasicUser2, th.BasicChannel2)
|
||||
th.App.AddUserToChannel(th.BasicUser, th.BasicPrivateChannel)
|
||||
th.App.AddUserToChannel(th.BasicUser2, th.BasicPrivateChannel)
|
||||
th.App.AddUserToChannel(th.BasicUser, th.BasicDeletedChannel)
|
||||
th.App.AddUserToChannel(th.BasicUser2, th.BasicDeletedChannel)
|
||||
th.App.AddUserToChannel(th.BasicUser, th.BasicChannel, false)
|
||||
th.App.AddUserToChannel(th.BasicUser2, th.BasicChannel, false)
|
||||
th.App.AddUserToChannel(th.BasicUser, th.BasicChannel2, false)
|
||||
th.App.AddUserToChannel(th.BasicUser2, th.BasicChannel2, false)
|
||||
th.App.AddUserToChannel(th.BasicUser, th.BasicPrivateChannel, false)
|
||||
th.App.AddUserToChannel(th.BasicUser2, th.BasicPrivateChannel, false)
|
||||
th.App.AddUserToChannel(th.BasicUser, th.BasicDeletedChannel, false)
|
||||
th.App.AddUserToChannel(th.BasicUser2, th.BasicDeletedChannel, false)
|
||||
th.App.UpdateUserRoles(th.BasicUser.Id, model.SYSTEM_USER_ROLE_ID, false)
|
||||
th.Client.DeleteChannel(th.BasicDeletedChannel.Id)
|
||||
th.LoginBasic()
|
||||
@@ -792,7 +792,7 @@ func (th *TestHelper) LinkUserToTeam(user *model.User, team *model.Team) {
|
||||
func (th *TestHelper) AddUserToChannel(user *model.User, channel *model.Channel) *model.ChannelMember {
|
||||
utils.DisableDebugLogForTest()
|
||||
|
||||
member, err := th.App.AddUserToChannel(user, channel)
|
||||
member, err := th.App.AddUserToChannel(user, channel, false)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
@@ -1540,7 +1540,10 @@ func addChannelMember(c *Context, w http.ResponseWriter, r *http.Request) {
|
||||
}
|
||||
}
|
||||
|
||||
cm, err := c.App.AddChannelMember(member.UserId, channel, c.App.Session().UserId, postRootId)
|
||||
cm, err := c.App.AddChannelMember(member.UserId, channel, app.ChannelMemberOpts{
|
||||
UserRequestorID: c.App.Session().UserId,
|
||||
PostRootID: postRootId,
|
||||
})
|
||||
if err != nil {
|
||||
c.Err = err
|
||||
return
|
||||
|
||||
@@ -107,7 +107,7 @@ func localAddChannelMember(c *Context, w http.ResponseWriter, r *http.Request) {
|
||||
}
|
||||
}
|
||||
|
||||
cm, err := c.App.AddUserToChannel(user, channel)
|
||||
cm, err := c.App.AddUserToChannel(user, channel, false)
|
||||
if err != nil {
|
||||
c.Err = err
|
||||
return
|
||||
|
||||
@@ -16,6 +16,7 @@ import (
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/require"
|
||||
|
||||
"github.com/mattermost/mattermost-server/v5/app"
|
||||
"github.com/mattermost/mattermost-server/v5/model"
|
||||
"github.com/mattermost/mattermost-server/v5/plugin/plugintest/mock"
|
||||
"github.com/mattermost/mattermost-server/v5/store/storetest/mocks"
|
||||
@@ -1497,8 +1498,8 @@ func TestDeleteChannel(t *testing.T) {
|
||||
|
||||
// successful delete of channel with multiple members
|
||||
publicChannel3 := th.CreatePublicChannel()
|
||||
th.App.AddUserToChannel(user, publicChannel3)
|
||||
th.App.AddUserToChannel(user2, publicChannel3)
|
||||
th.App.AddUserToChannel(user, publicChannel3, false)
|
||||
th.App.AddUserToChannel(user2, publicChannel3, false)
|
||||
_, resp = client.DeleteChannel(publicChannel3.Id)
|
||||
CheckNoError(t, resp)
|
||||
|
||||
@@ -1574,9 +1575,9 @@ func TestDeleteChannel2(t *testing.T) {
|
||||
// channels created by SystemAdmin
|
||||
publicChannel6 := th.CreateChannelWithClient(th.SystemAdminClient, model.CHANNEL_OPEN)
|
||||
privateChannel7 := th.CreateChannelWithClient(th.SystemAdminClient, model.CHANNEL_PRIVATE)
|
||||
th.App.AddUserToChannel(user, publicChannel6)
|
||||
th.App.AddUserToChannel(user, privateChannel7)
|
||||
th.App.AddUserToChannel(user, privateChannel7)
|
||||
th.App.AddUserToChannel(user, publicChannel6, false)
|
||||
th.App.AddUserToChannel(user, privateChannel7, false)
|
||||
th.App.AddUserToChannel(user, privateChannel7, false)
|
||||
|
||||
// successful delete by user
|
||||
_, resp := Client.DeleteChannel(publicChannel6.Id)
|
||||
@@ -1594,9 +1595,9 @@ func TestDeleteChannel2(t *testing.T) {
|
||||
// channels created by SystemAdmin
|
||||
publicChannel6 = th.CreateChannelWithClient(th.SystemAdminClient, model.CHANNEL_OPEN)
|
||||
privateChannel7 = th.CreateChannelWithClient(th.SystemAdminClient, model.CHANNEL_PRIVATE)
|
||||
th.App.AddUserToChannel(user, publicChannel6)
|
||||
th.App.AddUserToChannel(user, privateChannel7)
|
||||
th.App.AddUserToChannel(user, privateChannel7)
|
||||
th.App.AddUserToChannel(user, publicChannel6, false)
|
||||
th.App.AddUserToChannel(user, privateChannel7, false)
|
||||
th.App.AddUserToChannel(user, privateChannel7, false)
|
||||
|
||||
// cannot delete by user
|
||||
_, resp = Client.DeleteChannel(publicChannel6.Id)
|
||||
@@ -2295,7 +2296,7 @@ func TestUpdateChannelRoles(t *testing.T) {
|
||||
channel := th.CreatePublicChannel()
|
||||
|
||||
// Adds User 2 to the channel, making them a channel member by default.
|
||||
th.App.AddUserToChannel(th.BasicUser2, channel)
|
||||
th.App.AddUserToChannel(th.BasicUser2, channel, false)
|
||||
|
||||
// User 1 promotes User 2
|
||||
pass, resp := Client.UpdateChannelRoles(channel.Id, th.BasicUser2.Id, ChannelAdmin)
|
||||
@@ -2803,9 +2804,9 @@ func TestRemoveChannelMember(t *testing.T) {
|
||||
t.Run("success", func(t *testing.T) {
|
||||
// Setup the system administrator to listen for websocket events from the channels.
|
||||
th.LinkUserToTeam(th.SystemAdminUser, th.BasicTeam)
|
||||
_, err := th.App.AddUserToChannel(th.SystemAdminUser, th.BasicChannel)
|
||||
_, err := th.App.AddUserToChannel(th.SystemAdminUser, th.BasicChannel, false)
|
||||
require.Nil(t, err)
|
||||
_, err = th.App.AddUserToChannel(th.SystemAdminUser, th.BasicChannel2)
|
||||
_, err = th.App.AddUserToChannel(th.SystemAdminUser, th.BasicChannel2, false)
|
||||
require.Nil(t, err)
|
||||
props := map[string]string{}
|
||||
props[model.DESKTOP_NOTIFY_PROP] = model.CHANNEL_NOTIFY_ALL
|
||||
@@ -2847,7 +2848,7 @@ func TestRemoveChannelMember(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
th.App.AddUserToChannel(th.BasicUser2, th.BasicChannel)
|
||||
th.App.AddUserToChannel(th.BasicUser2, th.BasicChannel, false)
|
||||
_, resp = Client.RemoveUserFromChannel(th.BasicChannel.Id, th.BasicUser2.Id)
|
||||
CheckNoError(t, resp)
|
||||
|
||||
@@ -2878,8 +2879,8 @@ func TestRemoveChannelMember(t *testing.T) {
|
||||
// Leave deleted channel
|
||||
th.LoginBasic()
|
||||
deletedChannel := th.CreatePublicChannel()
|
||||
th.App.AddUserToChannel(th.BasicUser, deletedChannel)
|
||||
th.App.AddUserToChannel(th.BasicUser2, deletedChannel)
|
||||
th.App.AddUserToChannel(th.BasicUser, deletedChannel, false)
|
||||
th.App.AddUserToChannel(th.BasicUser2, deletedChannel, false)
|
||||
|
||||
deletedChannel.DeleteAt = 1
|
||||
th.App.UpdateChannel(deletedChannel)
|
||||
@@ -2889,7 +2890,7 @@ func TestRemoveChannelMember(t *testing.T) {
|
||||
|
||||
th.LoginBasic()
|
||||
private := th.CreatePrivateChannel()
|
||||
th.App.AddUserToChannel(th.BasicUser2, private)
|
||||
th.App.AddUserToChannel(th.BasicUser2, private, false)
|
||||
|
||||
_, resp = Client.RemoveUserFromChannel(private.Id, th.BasicUser2.Id)
|
||||
CheckNoError(t, resp)
|
||||
@@ -2899,7 +2900,7 @@ func TestRemoveChannelMember(t *testing.T) {
|
||||
CheckForbiddenStatus(t, resp)
|
||||
|
||||
th.TestForSystemAdminAndLocal(t, func(t *testing.T, client *model.Client4) {
|
||||
th.App.AddUserToChannel(th.BasicUser, private)
|
||||
th.App.AddUserToChannel(th.BasicUser, private, false)
|
||||
_, resp = client.RemoveUserFromChannel(private.Id, th.BasicUser.Id)
|
||||
CheckNoError(t, resp)
|
||||
})
|
||||
@@ -3436,9 +3437,9 @@ func TestChannelMembersMinusGroupMembers(t *testing.T) {
|
||||
|
||||
channel := th.CreatePrivateChannel()
|
||||
|
||||
_, err := th.App.AddChannelMember(user1.Id, channel, "", "")
|
||||
_, err := th.App.AddChannelMember(user1.Id, channel, app.ChannelMemberOpts{})
|
||||
require.Nil(t, err)
|
||||
_, err = th.App.AddChannelMember(user2.Id, channel, "", "")
|
||||
_, err = th.App.AddChannelMember(user2.Id, channel, app.ChannelMemberOpts{})
|
||||
require.Nil(t, err)
|
||||
|
||||
channel.GroupConstrained = model.NewBool(true)
|
||||
|
||||
@@ -540,7 +540,7 @@ func TestCreatePostSendOutOfChannelMentions(t *testing.T) {
|
||||
|
||||
inChannelUser := th.CreateUser()
|
||||
th.LinkUserToTeam(inChannelUser, th.BasicTeam)
|
||||
th.App.AddUserToChannel(inChannelUser, th.BasicChannel)
|
||||
th.App.AddUserToChannel(inChannelUser, th.BasicChannel, false)
|
||||
|
||||
post1 := &model.Post{ChannelId: th.BasicChannel.Id, Message: "@" + inChannelUser.Username}
|
||||
_, resp := Client.CreatePost(post1)
|
||||
@@ -1997,7 +1997,7 @@ func TestDeletePost(t *testing.T) {
|
||||
func TestDeletePostMessage(t *testing.T) {
|
||||
th := Setup(t).InitBasic()
|
||||
th.LinkUserToTeam(th.SystemAdminUser, th.BasicTeam)
|
||||
th.App.AddUserToChannel(th.SystemAdminUser, th.BasicChannel)
|
||||
th.App.AddUserToChannel(th.SystemAdminUser, th.BasicChannel, false)
|
||||
|
||||
defer th.TearDown()
|
||||
|
||||
@@ -2312,8 +2312,8 @@ func TestSearchPostsFromUser(t *testing.T) {
|
||||
th.LoginTeamAdmin()
|
||||
user := th.CreateUser()
|
||||
th.LinkUserToTeam(user, th.BasicTeam)
|
||||
th.App.AddUserToChannel(user, th.BasicChannel)
|
||||
th.App.AddUserToChannel(user, th.BasicChannel2)
|
||||
th.App.AddUserToChannel(user, th.BasicChannel, false)
|
||||
th.App.AddUserToChannel(user, th.BasicChannel2, false)
|
||||
|
||||
message := "sgtitlereview with space"
|
||||
_ = th.CreateMessagePost(message)
|
||||
|
||||
@@ -381,7 +381,7 @@ func TestCreateUserWebSocketEvent(t *testing.T) {
|
||||
_, _, err = th.App.AddUserToTeam(th.BasicTeam.Id, guest.Id, "")
|
||||
require.Nil(t, err)
|
||||
|
||||
_, err = th.App.AddUserToChannel(guest, th.BasicChannel)
|
||||
_, err = th.App.AddUserToChannel(guest, th.BasicChannel, false)
|
||||
require.Nil(t, err)
|
||||
|
||||
guestClient := th.CreateClient()
|
||||
@@ -5151,11 +5151,11 @@ func TestGetKnownUsers(t *testing.T) {
|
||||
th.LinkUserToTeam(u3, t2)
|
||||
th.LinkUserToTeam(u4, t3)
|
||||
|
||||
th.App.AddUserToChannel(u1, c1)
|
||||
th.App.AddUserToChannel(u1, c2)
|
||||
th.App.AddUserToChannel(u2, c1)
|
||||
th.App.AddUserToChannel(u3, c2)
|
||||
th.App.AddUserToChannel(u4, c3)
|
||||
th.App.AddUserToChannel(u1, c1, false)
|
||||
th.App.AddUserToChannel(u1, c2, false)
|
||||
th.App.AddUserToChannel(u2, c1, false)
|
||||
th.App.AddUserToChannel(u3, c2, false)
|
||||
th.App.AddUserToChannel(u4, c3, false)
|
||||
|
||||
t.Run("get know users sharing no channels", func(t *testing.T) {
|
||||
_, _ = th.Client.Login(u4.Email, u4.Password)
|
||||
|
||||
Ссылка в новой задаче
Block a user