Skip to content

Commit

Permalink
Review fixes #1
Browse files Browse the repository at this point in the history
  • Loading branch information
vespian committed Apr 12, 2020
1 parent a33b3c0 commit c6e6f60
Showing 1 changed file with 28 additions and 27 deletions.
55 changes: 28 additions & 27 deletions server/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,10 @@ func getCommand() *model.Command {
}
}

func responsef(format string, args ...interface{}) *model.CommandResponse {
func (p *Plugin) responsef(format string, args ...interface{}) *model.CommandResponse {
return &model.CommandResponse{
ResponseType: model.COMMAND_RESPONSE_TYPE_EPHEMERAL,
Username: p.botUserID,
Text: fmt.Sprintf(format, args...),
Type: model.POST_DEFAULT,
}
Expand All @@ -46,27 +47,27 @@ func (p *Plugin) hasSysadminRole(userId string) (bool, error) {
return true, nil
}

func validateCommand(action string, parameters []string) *model.CommandResponse {
func (p *Plugin) validateCommand(action string, parameters []string) *model.CommandResponse {
switch action {
case "preview":
if len(parameters) != 1 {
return responsef("Please specify a team, for which preview should be made.")
return p.responsef("Please specify a team, for which preview should be made.")
}
case "list":
if len(parameters) > 0 {
return responsef("List command does not accept any extra parameters")
return p.responsef("List command does not accept any extra parameters")
}
case "set_channel_welcome":
if len(parameters) == 0 {
return responsef("`set_channel_welcome` command requires the message to be provided")
return p.responsef("`set_channel_welcome` command requires the message to be provided")
}
case "get_channel_welcome":
if len(parameters) > 0 {
return responsef("`get_channel_welcome` command does not accept any extra parameters")
return p.responsef("`get_channel_welcome` command does not accept any extra parameters")
}
case "delete_channel_welcome":
if len(parameters) > 0 {
return responsef("`delete_channel_welcome` command does not accept any extra parameters")
return p.responsef("`delete_channel_welcome` command does not accept any extra parameters")
}
}

Expand All @@ -78,15 +79,15 @@ func (p *Plugin) executeCommandPreview(teamName string, args *model.CommandArgs)
for _, message := range p.getWelcomeMessages() {
if message.TeamName == teamName {
if err := p.previewWelcomeMessage(teamName, args, *message); err != nil {
return responsef("error occured while processing greeting for team `%s`: `%s`", teamName, err)
return p.responsef("error occured while processing greeting for team `%s`: `%s`", teamName, err)
}

found = true
}
}

if !found {
return responsef("team `%s` has not been found", teamName)
return p.responsef("team `%s` has not been found", teamName)
}

return &model.CommandResponse{}
Expand All @@ -96,7 +97,7 @@ func (p *Plugin) executeCommandList(args *model.CommandArgs) *model.CommandRespo
wecomeMessages := p.getWelcomeMessages()

if len(wecomeMessages) == 0 {
return responsef("There are no welcome messages defined")
return p.responsef("There are no welcome messages defined")
}

// Deduplicate entries
Expand All @@ -110,17 +111,17 @@ func (p *Plugin) executeCommandList(args *model.CommandArgs) *model.CommandRespo
for team := range teams {
str.WriteString(fmt.Sprintf("\n * %s", team))
}
return responsef(str.String())
return p.responsef(str.String())
}

func (p *Plugin) executeCommandSetWelcome(args *model.CommandArgs) *model.CommandResponse {
channelInfo, appErr := p.API.GetChannel(args.ChannelId)
if appErr != nil {
return responsef("error occured while checking the type of the chanelId `%s`: `%s`", args.ChannelId, appErr)
return p.responsef("error occured while checking the type of the chanelId `%s`: `%s`", args.ChannelId, appErr)
}

if channelInfo.Type == model.CHANNEL_PRIVATE {
return responsef("welcome messages are not supported for direct channels")
return p.responsef("welcome messages are not supported for direct channels")
}

// strings.Fields will consume ALL whitespace, so plain re-joining of the
Expand All @@ -130,43 +131,43 @@ func (p *Plugin) executeCommandSetWelcome(args *model.CommandArgs) *model.Comman

key := fmt.Sprintf("%s%s", args.ChannelId, WELCOMEBOT_CHANNEL_WELCOME_KEY)
if appErr := p.API.KVSet(key, []byte(message)); appErr != nil {
return responsef("error occured while storing the welcome message for the chanel: `%s`", appErr)
return p.responsef("error occured while storing the welcome message for the chanel: `%s`", appErr)
}

return responsef("stored the welcome message:\n%s", message)
return p.responsef("stored the welcome message:\n%s", message)
}

func (p *Plugin) executeCommandGetWelcome(args *model.CommandArgs) *model.CommandResponse {
key := fmt.Sprintf("%s%s", args.ChannelId, WELCOMEBOT_CHANNEL_WELCOME_KEY)
data, appErr := p.API.KVGet(key)
if appErr != nil {
return responsef("error occured while retrieving the welcome message for the chanel: `%s`", appErr)
return p.responsef("error occured while retrieving the welcome message for the chanel: `%s`", appErr)
}

if data == nil {
return responsef("welcome message has not been set yet")
return p.responsef("welcome message has not been set yet")
}

return responsef("Welcome message is:\n%s", string(data))
return p.responsef("Welcome message is:\n%s", string(data))
}

func (p *Plugin) executeCommandDeleteWelcome(args *model.CommandArgs) *model.CommandResponse {
key := fmt.Sprintf("%s%s", args.ChannelId, WELCOMEBOT_CHANNEL_WELCOME_KEY)
data, appErr := p.API.KVGet(key)

if appErr != nil {
return responsef("error occured while retrieving the welcome message for the chanel: `%s`", appErr)
return p.responsef("error occured while retrieving the welcome message for the chanel: `%s`", appErr)
}

if data == nil {
return responsef("welcome message has not been set yet")
return p.responsef("welcome message has not been set yet")
}

if appErr := p.API.KVDelete(key); appErr != nil {
return responsef("error occured while deleting the welcome message for the chanel: `%s`", appErr)
return p.responsef("error occured while deleting the welcome message for the chanel: `%s`", appErr)
}

return responsef("welcome message has been deleted")
return p.responsef("welcome message has been deleted")
}

func (p *Plugin) ExecuteCommand(_ *plugin.Context, args *model.CommandArgs) (*model.CommandResponse, *model.AppError) {
Expand All @@ -185,16 +186,16 @@ func (p *Plugin) ExecuteCommand(_ *plugin.Context, args *model.CommandArgs) (*mo
return &model.CommandResponse{}, nil
}

if response := validateCommand(action, parameters); response != nil {
if response := p.validateCommand(action, parameters); response != nil {
return response, nil
}

isSysadmin, err := p.hasSysadminRole(args.UserId)
if err != nil {
return responsef("authorization failed: %s", err), nil
return p.responsef("authorization failed: %s", err), nil
}
if !isSysadmin {
return responsef("/welcomebot commands can only be executed by the user with system admin role"), nil
return p.responsef("/welcomebot commands can only be executed by the user with system admin role"), nil
}

switch action {
Expand All @@ -213,8 +214,8 @@ func (p *Plugin) ExecuteCommand(_ *plugin.Context, args *model.CommandArgs) (*mo
fallthrough
case "":
text := "###### Mattermost welcomebot Plugin - Slash Command Help\n" + strings.Replace(COMMAND_HELP, "|", "`", -1)
return responsef(text), nil
return p.responsef(text), nil
}

return responsef("Unknown action %v", action), nil
return p.responsef("Unknown action %v", action), nil
}

0 comments on commit c6e6f60

Please sign in to comment.