Changes the event to send when a channel is shared or unshared (#28183)

* Changes the event to send when a channel is shared or unshared

Before we were sending the CHANNEL_CONVERTED websockets event, which
is intended to notify of a public channel being converted into a
private one, so the interface was showing the channel as private until
the client was refreshed.

Now a full channel update event is sent when the channel is shared or
unshared, so the interface gets the new information correctly.

* Fix message type on command tests
This commit is contained in:
Miguel de la Cruz 2024-09-13 23:40:02 +02:00 committed by GitHub
parent cae456de2d
commit d2c69fbf95
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 26 additions and 10 deletions

View file

@ -26,7 +26,7 @@ func setupForSharedChannels(tb testing.TB) *TestHelper {
}
func TestShareProviderDoCommand(t *testing.T) {
t.Run("share command sends a websocket channel converted event", func(t *testing.T) {
t.Run("share command sends a websocket channel updated event", func(t *testing.T) {
th := setupForSharedChannels(t).initBasic()
defer th.tearDown()
@ -52,14 +52,14 @@ func TestShareProviderDoCommand(t *testing.T) {
response := commandProvider.DoCommand(th.App, th.Context, args, "")
require.Equal(t, "##### "+args.T("api.command_share.channel_shared"), response.Text)
channelConvertedMessages := testCluster.SelectMessages(func(msg *model.ClusterMessage) bool {
channelUpdatedMessages := testCluster.SelectMessages(func(msg *model.ClusterMessage) bool {
event, err := model.WebSocketEventFromJSON(bytes.NewReader(msg.Data))
return err == nil && event.EventType() == model.WebsocketEventChannelConverted
return err == nil && event.EventType() == model.WebsocketEventChannelUpdated
})
assert.Len(t, channelConvertedMessages, 1) // one msg for share creation
assert.Len(t, channelUpdatedMessages, 1) // one msg for share creation
})
t.Run("unshare command sends a websocket channel converted event", func(t *testing.T) {
t.Run("unshare command sends a websocket channel updated event", func(t *testing.T) {
th := setupForSharedChannels(t).initBasic()
defer th.tearDown()
@ -84,11 +84,11 @@ func TestShareProviderDoCommand(t *testing.T) {
response := commandProvider.DoCommand(th.App, th.Context, args, "")
require.Equal(t, "##### "+args.T("api.command_share.shared_channel_unavailable"), response.Text)
channelConvertedMessages := testCluster.SelectMessages(func(msg *model.ClusterMessage) bool {
channelUpdatedMessages := testCluster.SelectMessages(func(msg *model.ClusterMessage) bool {
event, err := model.WebSocketEventFromJSON(bytes.NewReader(msg.Data))
return err == nil && event.EventType() == model.WebsocketEventChannelConverted
return err == nil && event.EventType() == model.WebsocketEventChannelUpdated
})
require.Len(t, channelConvertedMessages, 2) // one msg for share creation, one for unshare.
require.Len(t, channelUpdatedMessages, 2) // one msg for share creation, one for unshare.
})
t.Run("invite remote to channel shared with us", func(t *testing.T) {

View file

@ -4,6 +4,7 @@
package sharedchannel
import (
"encoding/json"
"errors"
"fmt"
"net/url"
@ -257,8 +258,17 @@ func (scs *Service) onConnectionStateChange(rc *model.RemoteCluster, online bool
func (scs *Service) notifyClientsForSharedChannelConverted(channel *model.Channel) {
scs.platform.InvalidateCacheForChannel(channel)
messageWs := model.NewWebSocketEvent(model.WebsocketEventChannelConverted, channel.TeamId, "", "", nil, "")
messageWs.Add("channel_id", channel.Id)
messageWs := model.NewWebSocketEvent(model.WebsocketEventChannelUpdated, "", channel.Id, "", nil, "")
channelJSON, err := json.Marshal(channel)
if err != nil {
scs.server.Log().Log(mlog.LvlSharedChannelServiceWarn, "Cannot marshal channel to notify clients",
mlog.String("channel_id", channel.Id),
mlog.Err(err),
)
return
}
messageWs.Add("channel", string(channelJSON))
scs.app.Publish(messageWs)
}

View file

@ -59,6 +59,9 @@ func (scs *Service) ShareChannel(sc *model.SharedChannel) (*model.SharedChannel,
if err != nil {
return nil, err
}
// to avoid fetching the channel again, we manually set the shared
// flag before notifying the clients
channel.Shared = model.NewPointer(true)
scs.notifyClientsForSharedChannelConverted(channel)
return scNew, nil
@ -93,6 +96,9 @@ func (scs *Service) UnshareChannel(channelID string) (bool, error) {
if err != nil {
return false, err
}
// to avoid fetching the channel again, we manually set the shared
// flag before notifying the clients
channel.Shared = model.NewPointer(false)
scs.notifyClientsForSharedChannelConverted(channel)
return deleted, nil