github.com/jlevesy/mattermost-server@v5.3.2-0.20181003190404-7468f35cb0c8+incompatible/app/command_leave.go (about)

     1  // Copyright (c) 2017-present Mattermost, Inc. All Rights Reserved.
     2  // See License.txt for license information.
     3  
     4  package app
     5  
     6  import (
     7  	"github.com/mattermost/mattermost-server/model"
     8  	goi18n "github.com/nicksnyder/go-i18n/i18n"
     9  )
    10  
    11  type LeaveProvider struct {
    12  }
    13  
    14  const (
    15  	CMD_LEAVE = "leave"
    16  )
    17  
    18  func init() {
    19  	RegisterCommandProvider(&LeaveProvider{})
    20  }
    21  
    22  func (me *LeaveProvider) GetTrigger() string {
    23  	return CMD_LEAVE
    24  }
    25  
    26  func (me *LeaveProvider) GetCommand(a *App, T goi18n.TranslateFunc) *model.Command {
    27  	return &model.Command{
    28  		Trigger:          CMD_LEAVE,
    29  		AutoComplete:     true,
    30  		AutoCompleteDesc: T("api.command_leave.desc"),
    31  		DisplayName:      T("api.command_leave.name"),
    32  	}
    33  }
    34  
    35  func (me *LeaveProvider) DoCommand(a *App, args *model.CommandArgs, message string) *model.CommandResponse {
    36  	var channel *model.Channel
    37  	var noChannelErr *model.AppError
    38  	if channel, noChannelErr = a.GetChannel(args.ChannelId); noChannelErr != nil {
    39  		return &model.CommandResponse{Text: args.T("api.command_leave.fail.app_error"), ResponseType: model.COMMAND_RESPONSE_TYPE_EPHEMERAL}
    40  	}
    41  	if channel.Name == model.DEFAULT_CHANNEL {
    42  		return &model.CommandResponse{Text: args.T("api.channel.leave.default.app_error", map[string]interface{}{"Channel": model.DEFAULT_CHANNEL}), ResponseType: model.COMMAND_RESPONSE_TYPE_EPHEMERAL}
    43  	}
    44  	err := a.LeaveChannel(args.ChannelId, args.UserId)
    45  	if err != nil {
    46  		return &model.CommandResponse{Text: args.T("api.command_leave.fail.app_error"), ResponseType: model.COMMAND_RESPONSE_TYPE_EPHEMERAL}
    47  	}
    48  
    49  	team, err := a.GetTeam(args.TeamId)
    50  	if err != nil {
    51  		return &model.CommandResponse{Text: args.T("api.command_leave.fail.app_error"), ResponseType: model.COMMAND_RESPONSE_TYPE_EPHEMERAL}
    52  	}
    53  
    54  	return &model.CommandResponse{GotoLocation: args.SiteURL + "/" + team.Name + "/channels/" + model.DEFAULT_CHANNEL, Text: args.T("api.command_leave.success"), ResponseType: model.COMMAND_RESPONSE_TYPE_EPHEMERAL}
    55  }