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-42549] Implement slash command to turn on/off experimental features #30

Merged
merged 1 commit into from
Mar 17, 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
44 changes: 38 additions & 6 deletions server/slash_command.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,20 +9,22 @@ import (
)

const (
rootCommandTrigger = "call"
joinCommandTrigger = "join"
leaveCommandTrigger = "leave"
linkCommandTrigger = "link"
rootCommandTrigger = "call"
joinCommandTrigger = "join"
leaveCommandTrigger = "leave"
linkCommandTrigger = "link"
experimentalCommandTrigger = "experimental"
)

var subCommands = []string{joinCommandTrigger, leaveCommandTrigger, linkCommandTrigger}
var subCommands = []string{joinCommandTrigger, leaveCommandTrigger, linkCommandTrigger, experimentalCommandTrigger}

func getAutocompleteData() *model.AutocompleteData {
data := model.NewAutocompleteData(rootCommandTrigger, "[command]",
"Available commands: "+strings.Join(subCommands, ","))
data.AddCommand(model.NewAutocompleteData(joinCommandTrigger, "", "Joins or starts a call in the current channel"))
data.AddCommand(model.NewAutocompleteData(leaveCommandTrigger, "", "Leaves a call in the current channel"))
data.AddCommand(model.NewAutocompleteData(linkCommandTrigger, "", "Generates a link to join a call in the current channel"))
data.AddCommand(model.NewAutocompleteData(experimentalCommandTrigger, "", "Turns on/off experimental features"))
return data
}

Expand Down Expand Up @@ -67,6 +69,25 @@ func (p *Plugin) handleLinkCommand(args *model.CommandArgs) (*model.CommandRespo
}, nil
}

func handleExperimentalCommand(fields []string) (*model.CommandResponse, error) {
var msg string
if len(fields) != 3 {
return nil, fmt.Errorf("Invalid number of arguments provided")
}
if fields[2] == "on" {
msg = "Experimental features were turned on"
} else if fields[2] == "off" {
msg = "Experimental features were turned off"
}
if msg == "" {
return nil, fmt.Errorf("Invalid arguments provided")
}
return &model.CommandResponse{
ResponseType: model.CommandResponseTypeEphemeral,
Text: msg,
}, nil
}

func (p *Plugin) ExecuteCommand(c *plugin.Context, args *model.CommandArgs) (*model.CommandResponse, *model.AppError) {
fields := strings.Fields(args.Command)

Expand All @@ -78,7 +99,7 @@ func (p *Plugin) ExecuteCommand(c *plugin.Context, args *model.CommandArgs) (*mo
}, nil
}

if len(fields) != 2 {
if len(fields) < 2 {
return &model.CommandResponse{
ResponseType: model.CommandResponseTypeEphemeral,
Text: "Invalid number of arguments provided",
Expand All @@ -98,6 +119,17 @@ func (p *Plugin) ExecuteCommand(c *plugin.Context, args *model.CommandArgs) (*mo
return resp, nil
}

if subCmd == experimentalCommandTrigger {
resp, err := handleExperimentalCommand(fields)
if err != nil {
return &model.CommandResponse{
ResponseType: model.CommandResponseTypeEphemeral,
Text: fmt.Sprintf("Error: %s", err.Error()),
}, nil
}
return resp, nil
}

for _, cmd := range subCommands {
if cmd == subCmd {
return &model.CommandResponse{}, nil
Expand Down
22 changes: 17 additions & 5 deletions webapp/src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -253,7 +253,7 @@ export default class Plugin {
registry.registerSlashCommandWillBePostedHook((message, args) => {
const fullCmd = message.trim();
const fields = fullCmd.split(/\s+/);
if (fields.length !== 2) {
if (fields.length < 2) {
return {message, args};
}

Expand All @@ -266,20 +266,32 @@ export default class Plugin {

const connectedID = connectedChannelID(store.getState());

if (subCmd === 'join') {
switch (subCmd) {
case 'join':
if (!connectedID) {
connectCall(args.channel_id);
return {};
}
return {error: {message: 'You are already connected to a call in the current channel.'}};
} else if (subCmd === 'leave') {
case 'leave':
if (connectedID && args.channel_id === connectedID && window.callsClient) {
window.callsClient.disconnect();
return {};
}
return {error: {message: 'You are not connected to a call in the current channel.'}};
} else if (subCmd === 'link') {
return {message, args};
case 'link':
break;
case 'experimental':
if (fields.length < 3) {
break;
}
if (fields[2] === 'on') {
window.localStorage.setItem('calls_experimental_features', 'on');
console.log('experimental features enabled');
} else if (fields[2] === 'off') {
console.log('experimental features disabled');
window.localStorage.removeItem('calls_experimental_features');
}
}

return {message, args};
Expand Down
4 changes: 4 additions & 0 deletions webapp/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -284,3 +284,7 @@ export function setSDPMaxVideoBW(sdp: string, bandwidth: number) {
}
return sdp;
}

export function hasExperimentalFlag() {
return window.localStorage.getItem('calls_experimental_features') === 'on';
}