github.com/masterhung0112/hk_server/v5@v5.0.0-20220302090640-ec71aef15e1c/app/slashcommands/command_invite_people.go (about)

     1  // Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
     2  // See LICENSE.txt for license information.
     3  
     4  package slashcommands
     5  
     6  import (
     7  	"strings"
     8  
     9  	"github.com/masterhung0112/hk_server/v5/app"
    10  	"github.com/masterhung0112/hk_server/v5/app/request"
    11  	"github.com/masterhung0112/hk_server/v5/model"
    12  	"github.com/masterhung0112/hk_server/v5/shared/i18n"
    13  	"github.com/masterhung0112/hk_server/v5/shared/mlog"
    14  )
    15  
    16  type InvitePeopleProvider struct {
    17  }
    18  
    19  const (
    20  	CmdInvite_PEOPLE = "invite_people"
    21  )
    22  
    23  func init() {
    24  	app.RegisterCommandProvider(&InvitePeopleProvider{})
    25  }
    26  
    27  func (*InvitePeopleProvider) GetTrigger() string {
    28  	return CmdInvite_PEOPLE
    29  }
    30  
    31  func (*InvitePeopleProvider) GetCommand(a *app.App, T i18n.TranslateFunc) *model.Command {
    32  	autoComplete := true
    33  	if !*a.Config().EmailSettings.SendEmailNotifications || !*a.Config().TeamSettings.EnableUserCreation || !*a.Config().ServiceSettings.EnableEmailInvitations {
    34  		autoComplete = false
    35  	}
    36  	return &model.Command{
    37  		Trigger:          CmdInvite_PEOPLE,
    38  		AutoComplete:     autoComplete,
    39  		AutoCompleteDesc: T("api.command.invite_people.desc"),
    40  		AutoCompleteHint: T("api.command.invite_people.hint"),
    41  		DisplayName:      T("api.command.invite_people.name"),
    42  	}
    43  }
    44  
    45  func (*InvitePeopleProvider) DoCommand(a *app.App, c *request.Context, args *model.CommandArgs, message string) *model.CommandResponse {
    46  	if !a.HasPermissionToTeam(args.UserId, args.TeamId, model.PERMISSION_INVITE_USER) {
    47  		return &model.CommandResponse{Text: args.T("api.command_invite_people.permission.app_error"), ResponseType: model.COMMAND_RESPONSE_TYPE_EPHEMERAL}
    48  	}
    49  
    50  	if !a.HasPermissionToTeam(args.UserId, args.TeamId, model.PERMISSION_ADD_USER_TO_TEAM) {
    51  		return &model.CommandResponse{Text: args.T("api.command_invite_people.permission.app_error"), ResponseType: model.COMMAND_RESPONSE_TYPE_EPHEMERAL}
    52  	}
    53  
    54  	if !*a.Config().EmailSettings.SendEmailNotifications {
    55  		return &model.CommandResponse{ResponseType: model.COMMAND_RESPONSE_TYPE_EPHEMERAL, Text: args.T("api.command.invite_people.email_off")}
    56  	}
    57  
    58  	if !*a.Config().TeamSettings.EnableUserCreation {
    59  		return &model.CommandResponse{ResponseType: model.COMMAND_RESPONSE_TYPE_EPHEMERAL, Text: args.T("api.command.invite_people.invite_off")}
    60  	}
    61  
    62  	if !*a.Config().ServiceSettings.EnableEmailInvitations {
    63  		return &model.CommandResponse{ResponseType: model.COMMAND_RESPONSE_TYPE_EPHEMERAL, Text: args.T("api.command.invite_people.email_invitations_off")}
    64  	}
    65  
    66  	emailList := strings.Fields(message)
    67  
    68  	for i := len(emailList) - 1; i >= 0; i-- {
    69  		emailList[i] = strings.Trim(emailList[i], ",")
    70  		if !strings.Contains(emailList[i], "@") {
    71  			emailList = append(emailList[:i], emailList[i+1:]...)
    72  		}
    73  	}
    74  
    75  	if len(emailList) == 0 {
    76  		return &model.CommandResponse{ResponseType: model.COMMAND_RESPONSE_TYPE_EPHEMERAL, Text: args.T("api.command.invite_people.no_email")}
    77  	}
    78  
    79  	if err := a.InviteNewUsersToTeam(emailList, args.TeamId, args.UserId); err != nil {
    80  		mlog.Error(err.Error())
    81  		return &model.CommandResponse{ResponseType: model.COMMAND_RESPONSE_TYPE_EPHEMERAL, Text: args.T("api.command.invite_people.fail")}
    82  	}
    83  
    84  	return &model.CommandResponse{ResponseType: model.COMMAND_RESPONSE_TYPE_EPHEMERAL, Text: args.T("api.command.invite_people.sent")}
    85  }