github.com/vnforks/kid/v5@v5.22.1-0.20200408055009-b89d99c65676/app/command_leave.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  	goi18n "github.com/mattermost/go-i18n/i18n"
     8  	"github.com/vnforks/kid/v5/model"
     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 class *model.Class
    37  	var noClassErr *model.AppError
    38  	if class, noClassErr = a.GetClass(args.ClassId); noClassErr != nil {
    39  		return &model.CommandResponse{Text: args.T("api.command_leave.fail.app_error"), ResponseType: model.COMMAND_RESPONSE_TYPE_EPHEMERAL}
    40  	}
    41  
    42  	branch, err := a.GetBranch(args.BranchId)
    43  	if err != nil {
    44  		return &model.CommandResponse{Text: args.T("api.command_leave.fail.app_error"), ResponseType: model.COMMAND_RESPONSE_TYPE_EPHEMERAL}
    45  	}
    46  
    47  	err = a.LeaveClass(args.ClassId, args.UserId)
    48  	if err != nil {
    49  		if class.Name == model.DEFAULT_CLASS {
    50  			return &model.CommandResponse{Text: args.T("api.class.leave.default.app_error", map[string]interface{}{"Class": model.DEFAULT_CLASS}), ResponseType: model.COMMAND_RESPONSE_TYPE_EPHEMERAL}
    51  		}
    52  		return &model.CommandResponse{Text: args.T("api.command_leave.fail.app_error"), ResponseType: model.COMMAND_RESPONSE_TYPE_EPHEMERAL}
    53  	}
    54  
    55  	member, err := a.GetBranchMember(branch.Id, args.UserId)
    56  	if err != nil || member.DeleteAt != 0 {
    57  		return &model.CommandResponse{GotoLocation: args.SiteURL + "/"}
    58  	}
    59  
    60  	user, err := a.GetUser(args.UserId)
    61  	if err != nil {
    62  		return &model.CommandResponse{Text: args.T("api.command_leave.fail.app_error"), ResponseType: model.COMMAND_RESPONSE_TYPE_EPHEMERAL}
    63  	}
    64  
    65  	if user.IsGuest() {
    66  		members, err := a.GetClassMembersForUser(branch.Id, args.UserId)
    67  		if err != nil || len(*members) == 0 {
    68  			return &model.CommandResponse{Text: args.T("api.command_leave.fail.app_error"), ResponseType: model.COMMAND_RESPONSE_TYPE_EPHEMERAL}
    69  		}
    70  		class, err := a.GetClass((*members)[0].ClassId)
    71  		if err != nil {
    72  			return &model.CommandResponse{Text: args.T("api.command_leave.fail.app_error"), ResponseType: model.COMMAND_RESPONSE_TYPE_EPHEMERAL}
    73  		}
    74  		return &model.CommandResponse{GotoLocation: args.SiteURL + "/" + branch.Name + "/classes/" + class.Name}
    75  	}
    76  
    77  	return &model.CommandResponse{GotoLocation: args.SiteURL + "/" + branch.Name + "/classes/" + model.DEFAULT_CLASS}
    78  }