Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[MM-42101] Implement option to enable calls by default #10

Merged
merged 2 commits into from
Feb 28, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 10 additions & 2 deletions plugin.json
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,16 @@
"key": "AllowEnableCalls",
"display_name": "Allow Enable Calls",
"type": "bool",
"help_text": "When set to true, it allows channel admins to enable calls in their channels. It also allows participants of DMs/GMs to enable calls.",
"help_text": "When set to true, it allows channel admins to enable or disable calls in their channels. It also allows participants of DMs/GMs to enable or disable calls.",
"default": false
}]
},
{
"key": "DefaultEnabled",
"display_name": "Default Enabled Calls",
"type": "bool",
"help_text": "When set to true, calls will be possible in all channels where they are not explicitly disabled.",
"default": true
}
]
}
}
8 changes: 8 additions & 0 deletions server/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,14 @@ func (p *Plugin) handleGetChannel(w http.ResponseWriter, r *http.Request, channe
info := ChannelState{
ChannelID: channelID,
}

cfg := p.getConfiguration()
if state == nil && cfg.DefaultEnabled != nil && *cfg.DefaultEnabled {
state = &channelState{
Enabled: true,
}
}

if state != nil {
info.Enabled = state.Enabled
if state.Call != nil {
Expand Down
15 changes: 13 additions & 2 deletions server/configuration.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,11 @@ type configuration struct {
type clientConfig struct {
// A comma separated list of ICE servers URLs (STUN/TURN) to use.
ICEServers ICEServers
// When set to true, it allows channel admins to enable calls in their channels.
// It also allows participants of DMs/GMs to enable calls.
// When set to true, it allows channel admins to enable or disable calls in their channels.
// It also allows participants of DMs/GMs to enable or disable calls.
AllowEnableCalls *bool
// When set to true, calls will be possible in all channels where they are not explicitly disabled.
DefaultEnabled *bool
}

type ICEServers []string
Expand Down Expand Up @@ -96,6 +98,7 @@ func (pr PortsRange) IsValid() error {
func (c *configuration) getClientConfig() clientConfig {
return clientConfig{
AllowEnableCalls: c.AllowEnableCalls,
DefaultEnabled: c.DefaultEnabled,
ICEServers: c.ICEServers,
}
}
Expand All @@ -107,6 +110,10 @@ func (c *configuration) SetDefaults() {
if c.AllowEnableCalls == nil {
c.AllowEnableCalls = new(bool)
}
if c.DefaultEnabled == nil {
c.DefaultEnabled = new(bool)
*c.DefaultEnabled = true
}
}

func (c *configuration) IsValid() error {
Expand Down Expand Up @@ -140,6 +147,10 @@ func (c *configuration) Clone() *configuration {
cfg.AllowEnableCalls = model.NewBool(*c.AllowEnableCalls)
}

if c.DefaultEnabled != nil {
cfg.DefaultEnabled = model.NewBool(*c.DefaultEnabled)
}

if c.ICEServers != nil {
cfg.ICEServers = make(ICEServers, len(c.ICEServers))
for i, u := range c.ICEServers {
Expand Down
3 changes: 3 additions & 0 deletions server/configuration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,10 @@ func TestGetClientConfig(t *testing.T) {
cfg.SetDefaults()
clientCfg := cfg.getClientConfig()
require.Equal(t, cfg.AllowEnableCalls, clientCfg.AllowEnableCalls)
require.Equal(t, cfg.DefaultEnabled, clientCfg.DefaultEnabled)
*cfg.AllowEnableCalls = true
*cfg.DefaultEnabled = true
clientCfg = cfg.getClientConfig()
require.Equal(t, cfg.AllowEnableCalls, clientCfg.AllowEnableCalls)
require.Equal(t, cfg.DefaultEnabled, clientCfg.DefaultEnabled)
}
10 changes: 9 additions & 1 deletion server/manifest.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 15 additions & 1 deletion server/session.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,10 +64,24 @@ func newUserSession(userID, channelID, connID string) *session {

func (p *Plugin) addUserSession(userID, channelID string, userSession *session) (channelState, error) {
var st channelState

cfg := p.getConfiguration()

err := p.kvSetAtomicChannelState(channelID, func(state *channelState) (*channelState, error) {
if state == nil {
return nil, fmt.Errorf("channel state is missing from store")
if cfg.DefaultEnabled != nil && *cfg.DefaultEnabled {
state = &channelState{
Enabled: true,
}
Comment on lines +72 to +75
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This essentially means we are implicitly enabling channels calls upon first call. An alternate approach was to leave it unset but that required data schema changes (converting bool to pointer to have the additional state) and it felt more convoluted overall. Happy to hear feedback though.

} else {
return nil, fmt.Errorf("channel state is missing from store")
}
}

if !state.Enabled {
return nil, fmt.Errorf("calls are not enabled")
}
Comment on lines +81 to +83
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Interestingly, from a server perspective it was possible to start a call on a channel with calls disabled.


if state.Call == nil {
state.Call = &callState{
ID: model.NewId(),
Expand Down
5 changes: 2 additions & 3 deletions webapp/src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -112,8 +112,7 @@ export default class CallsClient extends EventEmitter {
this.ws = ws;

ws.on('error', (err) => {
console.log(`ws error: ${err}`);
this.ws = null;
Copy link
Collaborator Author

@streamer45 streamer45 Feb 28, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This was preventing the ws connection from disconnecting in case of server side error (e.g. during join).

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice catch!

console.log('ws error', err);
this.disconnect();
});

Expand Down Expand Up @@ -168,7 +167,7 @@ export default class CallsClient extends EventEmitter {
}
});
peer.on('error', (err) => {
console.log(`peer error: ${err}`);
console.log('peer error', err);
this.disconnect();
});
peer.on('stream', (remoteStream) => {
Expand Down