Skip to content

Commit

Permalink
Changes the event to send when a channel is shared or unshared (matte…
Browse files Browse the repository at this point in the history
…rmost#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
  • Loading branch information
mgdelacroix authored Sep 13, 2024
1 parent cae456d commit d2c69fb
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 10 deletions.
16 changes: 8 additions & 8 deletions server/channels/app/slashcommands/command_share_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Expand All @@ -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()

Expand All @@ -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) {
Expand Down
14 changes: 12 additions & 2 deletions server/platform/services/sharedchannel/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
package sharedchannel

import (
"encoding/json"
"errors"
"fmt"
"net/url"
Expand Down Expand Up @@ -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)
}

Expand Down
6 changes: 6 additions & 0 deletions server/platform/services/sharedchannel/service_api.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit d2c69fb

Please sign in to comment.