// List, add, or remove usernames from the plugin's KV store for the channel
// where the command was called from.
List, add, or remove usernames from the plugin's KV store for the channel
where the command was called from.
// List add or remove usernames from the plugin ' s KV store for the channel // where the command was called from .
func (p *RollbarPlugin) ExecuteCommand(c *plugin.Context, args *model.CommandArgs) (*model.CommandResponse, *model.AppError) {
	tokens := strings.Split(args.Command, " ")
	if len(tokens) < 2 || len(tokens) > 3 {
		return getCommandResponse(usageErrorMessage), nil
	}
	channelId := args.ChannelId
	existingUsers, err := p.API.KVGet(channelId)
	if err != nil {
		return p.returnGenericError(err), nil
	}
	usersMap := make(map[string]bool)
	if len(existingUsers) > 0 {
		if err := json.Unmarshal(existingUsers, &usersMap); err != nil {
			return p.returnGenericError(err), nil
		}
	}
	action := tokens[1]
	switch action {
	case "list":
		if len(tokens) != 2 {
			return getCommandResponse("Usage: `/rollbar list`"), nil
		}
		return getCommandResponse(fmt.Sprintf(usersListMessage, GetUsernameList(usersMap))), nil
	case "notify", "remove":
		if len(tokens) != 3 {
			return getCommandResponse(fmt.Sprintf("Usage: `/rollbar %s @username`", action)), nil
		}
		username := tokens[2]
		username = strings.TrimPrefix(username, "@")
		user, _ := p.API.GetUserByUsername(username)
		if user == nil {
			return getCommandResponse(fmt.Sprintf("User `%s` not found.", username)), nil
		}
		if action == "remove" {
			if usersMap[username] {
				delete(usersMap, username)
			} else {
				return getCommandResponse(fmt.Sprintf("User `%s` is already not being notified.", username)), nil
			}
		} else if action == "notify" {
			if !usersMap[username] {
				usersMap[username] = true
			} else {
				return getCommandResponse(fmt.Sprintf("User `%s` is already being notified.", username)), nil
			}
		}
		newValue, _ := json.Marshal(usersMap)
		if err := p.API.KVSet(channelId, newValue); err != nil {
			return p.returnGenericError(err), nil
		}
		return getCommandResponse(fmt.Sprintf(usersListMessage, GetUsernameList(usersMap))), nil
	default:
		return getCommandResponse(usageErrorMessage), nil
	}
}
func ( p * RollbarPlugin ) ExecuteCommand ( c * plugin . Context , args * model . CommandArgs ) ( * model . CommandResponse , * model . AppError ) { tokens := strings . Split ( args . Command , " " ) if len ( tokens ) < 2 || len ( tokens ) > 3 { return getCommandResponse ( usageErrorMessage ) , nil } channelId := args . ChannelId existingUsers , err := p . API . KVGet ( channelId ) if err != nil { return p . returnGenericError ( err ) , nil } usersMap := make ( map [ string ] bool ) if len ( existingUsers ) > 0 { if err := json . Unmarshal ( existingUsers , & usersMap ) ; err != nil { return p . returnGenericError ( err ) , nil } } action := tokens [ 1 ] switch action { case " " : if len ( tokens ) != 2 { return getCommandResponse ( " " ) , nil } return getCommandResponse ( fmt . Sprintf ( usersListMessage , GetUsernameList ( usersMap ) ) ) , nil case " " , " " : if len ( tokens ) != 3 { return getCommandResponse ( fmt . Sprintf ( " " , action ) ) , nil } username := tokens [ 2 ] username = strings . TrimPrefix ( username , " " ) user , _ := p . API . GetUserByUsername ( username ) if user == nil { return getCommandResponse ( fmt . Sprintf ( " " , username ) ) , nil } if action == " " { if usersMap [ username ] { delete ( usersMap , username ) } else { return getCommandResponse ( fmt . Sprintf ( " " , username ) ) , nil } } else if action == " " { if ! usersMap [ username ] { usersMap [ username ] = true } else { return getCommandResponse ( fmt . Sprintf ( " " , username ) ) , nil } } newValue , _ := json . Marshal ( usersMap ) if err := p . API . KVSet ( channelId , newValue ) ; err != nil { return p . returnGenericError ( err ) , nil } return getCommandResponse ( fmt . Sprintf ( usersListMessage , GetUsernameList ( usersMap ) ) ) , nil default : return getCommandResponse ( usageErrorMessage ) , nil } }
List, add, or remove usernames from the plugin's KV store for the channel
where the command was called from.
List add or remove usernames from the plugin ' s KV store for the channel where the command was called from .
// Users that are currently being notified