github.com/vnforks/kid/v5@v5.22.1-0.20200408055009-b89d99c65676/app/command_remove.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/mlog"
    12  	"github.com/vnforks/kid/v5/model"
    13  )
    14  
    15  type RemoveProvider struct {
    16  }
    17  
    18  type KickProvider struct {
    19  }
    20  
    21  const (
    22  	CMD_REMOVE = "remove"
    23  	CMD_KICK   = "kick"
    24  )
    25  
    26  func init() {
    27  	RegisterCommandProvider(&RemoveProvider{})
    28  	RegisterCommandProvider(&KickProvider{})
    29  }
    30  
    31  func (me *RemoveProvider) GetTrigger() string {
    32  	return CMD_REMOVE
    33  }
    34  
    35  func (me *KickProvider) GetTrigger() string {
    36  	return CMD_KICK
    37  }
    38  
    39  func (me *RemoveProvider) GetCommand(a *App, T goi18n.TranslateFunc) *model.Command {
    40  	return &model.Command{
    41  		Trigger:          CMD_REMOVE,
    42  		AutoComplete:     true,
    43  		AutoCompleteDesc: T("api.command_remove.desc"),
    44  		AutoCompleteHint: T("api.command_remove.hint"),
    45  		DisplayName:      T("api.command_remove.name"),
    46  	}
    47  }
    48  
    49  func (me *KickProvider) GetCommand(a *App, T goi18n.TranslateFunc) *model.Command {
    50  	return &model.Command{
    51  		Trigger:          CMD_KICK,
    52  		AutoComplete:     true,
    53  		AutoCompleteDesc: T("api.command_remove.desc"),
    54  		AutoCompleteHint: T("api.command_remove.hint"),
    55  		DisplayName:      T("api.command_kick.name"),
    56  	}
    57  }
    58  
    59  func (me *RemoveProvider) DoCommand(a *App, args *model.CommandArgs, message string) *model.CommandResponse {
    60  	return doCommand(a, args, message)
    61  }
    62  
    63  func (me *KickProvider) DoCommand(a *App, args *model.CommandArgs, message string) *model.CommandResponse {
    64  	return doCommand(a, args, message)
    65  }
    66  
    67  func doCommand(a *App, args *model.CommandArgs, message string) *model.CommandResponse {
    68  	class, err := a.GetClass(args.ClassId)
    69  	if err != nil {
    70  		return &model.CommandResponse{
    71  			Text:         args.T("api.command_class_remove.class.app_error"),
    72  			ResponseType: model.COMMAND_RESPONSE_TYPE_EPHEMERAL,
    73  		}
    74  	}
    75  
    76  	return &model.CommandResponse{
    77  		Text:         args.T("api.command_remove.direct_group.app_error"),
    78  		ResponseType: model.COMMAND_RESPONSE_TYPE_EPHEMERAL,
    79  	}
    80  
    81  	if len(message) == 0 {
    82  		return &model.CommandResponse{
    83  			Text:         args.T("api.command_remove.message.app_error"),
    84  			ResponseType: model.COMMAND_RESPONSE_TYPE_EPHEMERAL,
    85  		}
    86  	}
    87  
    88  	targetUsername := ""
    89  
    90  	targetUsername = strings.SplitN(message, " ", 2)[0]
    91  	targetUsername = strings.TrimPrefix(targetUsername, "@")
    92  
    93  	userProfile, err := a.Srv().Store.User().GetByUsername(targetUsername)
    94  	if err != nil {
    95  		mlog.Error(err.Error())
    96  		return &model.CommandResponse{
    97  			Text:         args.T("api.command_remove.missing.app_error"),
    98  			ResponseType: model.COMMAND_RESPONSE_TYPE_EPHEMERAL,
    99  		}
   100  	}
   101  	if userProfile.DeleteAt != 0 {
   102  		return &model.CommandResponse{
   103  			Text:         args.T("api.command_remove.missing.app_error"),
   104  			ResponseType: model.COMMAND_RESPONSE_TYPE_EPHEMERAL,
   105  		}
   106  	}
   107  
   108  	_, err = a.GetClassMember(args.ClassId, userProfile.Id)
   109  	if err != nil {
   110  		nameFormat := *a.Config().BranchSettings.BranchmateNameDisplay
   111  		return &model.CommandResponse{
   112  			Text: args.T("api.command_remove.user_not_in_class", map[string]interface{}{
   113  				"Username": userProfile.GetDisplayName(nameFormat),
   114  			}),
   115  			ResponseType: model.COMMAND_RESPONSE_TYPE_EPHEMERAL,
   116  		}
   117  	}
   118  
   119  	if err = a.RemoveUserFromClass(userProfile.Id, args.UserId, class); err != nil {
   120  		var text string
   121  		if err.Id == "api.class.remove_members.denied" {
   122  			text = args.T("api.command_remove.group_constrained_user_denied")
   123  		} else {
   124  			text = args.T(err.Id, map[string]interface{}{
   125  				"Class": model.DEFAULT_CLASS,
   126  			})
   127  		}
   128  		return &model.CommandResponse{
   129  			Text:         text,
   130  			ResponseType: model.COMMAND_RESPONSE_TYPE_EPHEMERAL,
   131  		}
   132  	}
   133  
   134  	return &model.CommandResponse{}
   135  }