Skip to content

Commit

Permalink
[MM-42101] Implement option to enable calls by default (#10)
Browse files Browse the repository at this point in the history
* Fix logging

* Implement opt out option for channel calls
  • Loading branch information
streamer45 authored Feb 28, 2022
1 parent 9635108 commit dd88a49
Show file tree
Hide file tree
Showing 7 changed files with 60 additions and 9 deletions.
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,
}
} else {
return nil, fmt.Errorf("channel state is missing from store")
}
}

if !state.Enabled {
return nil, fmt.Errorf("calls are not enabled")
}

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;
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

0 comments on commit dd88a49

Please sign in to comment.