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

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