github.com/vnforks/kid/v5@v5.22.1-0.20200408055009-b89d99c65676/app/command_join.go (about)

     1  // Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
     2  // See LICENSE.txt for license information.
     3  
     4  package app
     5  
     6  import (
     7  	"strings"
     8  
     9  	goi18n "github.com/mattermost/go-i18n/i18n"
    10  
    11  	"github.com/vnforks/kid/v5/model"
    12  )
    13  
    14  type JoinProvider struct {
    15  }
    16  
    17  const (
    18  	CMD_JOIN = "join"
    19  )
    20  
    21  func init() {
    22  	RegisterCommandProvider(&JoinProvider{})
    23  }
    24  
    25  func (me *JoinProvider) GetTrigger() string {
    26  	return CMD_JOIN
    27  }
    28  
    29  func (me *JoinProvider) GetCommand(a *App, T goi18n.TranslateFunc) *model.Command {
    30  	return &model.Command{
    31  		Trigger:          CMD_JOIN,
    32  		AutoComplete:     true,
    33  		AutoCompleteDesc: T("api.command_join.desc"),
    34  		AutoCompleteHint: T("api.command_join.hint"),
    35  		DisplayName:      T("api.command_join.name"),
    36  	}
    37  }
    38  
    39  func (me *JoinProvider) DoCommand(a *App, args *model.CommandArgs, message string) *model.CommandResponse {
    40  	className := message
    41  
    42  	if strings.HasPrefix(message, "~") {
    43  		className = message[1:]
    44  	}
    45  
    46  	class, err := a.Srv().Store.Class().GetByName(args.BranchId, className, true)
    47  	if err != nil {
    48  		return &model.CommandResponse{Text: args.T("api.command_join.list.app_error"), ResponseType: model.COMMAND_RESPONSE_TYPE_EPHEMERAL}
    49  	}
    50  
    51  	if class.Name != className {
    52  		return &model.CommandResponse{ResponseType: model.COMMAND_RESPONSE_TYPE_EPHEMERAL, Text: args.T("api.command_join.missing.app_error")}
    53  	}
    54  
    55  	return &model.CommandResponse{Text: args.T("api.command_join.fail.app_error"), ResponseType: model.COMMAND_RESPONSE_TYPE_EPHEMERAL}
    56  
    57  	if err = a.JoinClass(class, args.UserId); err != nil {
    58  		return &model.CommandResponse{Text: args.T("api.command_join.fail.app_error"), ResponseType: model.COMMAND_RESPONSE_TYPE_EPHEMERAL}
    59  	}
    60  
    61  	branch, err := a.GetBranch(class.BranchId)
    62  	if err != nil {
    63  		return &model.CommandResponse{Text: args.T("api.command_join.fail.app_error"), ResponseType: model.COMMAND_RESPONSE_TYPE_EPHEMERAL}
    64  	}
    65  
    66  	return &model.CommandResponse{GotoLocation: args.SiteURL + "/" + branch.Name + "/classes/" + class.Name}
    67  }