github.com/gigforks/mattermost-server@v4.9.1-0.20180619094218-800d97fa55d0+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 {
    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.Config().EmailSettings.SendEmailNotifications {
    45  		return &model.CommandResponse{ResponseType: model.COMMAND_RESPONSE_TYPE_EPHEMERAL, Text: args.T("api.command.invite_people.email_off")}
    46  	}
    47  
    48  	if !a.Config().TeamSettings.EnableUserCreation {
    49  		return &model.CommandResponse{ResponseType: model.COMMAND_RESPONSE_TYPE_EPHEMERAL, Text: args.T("api.command.invite_people.invite_off")}
    50  	}
    51  
    52  	emailList := strings.Fields(message)
    53  
    54  	for i := len(emailList) - 1; i >= 0; i-- {
    55  		emailList[i] = strings.Trim(emailList[i], ",")
    56  		if !strings.Contains(emailList[i], "@") {
    57  			emailList = append(emailList[:i], emailList[i+1:]...)
    58  		}
    59  	}
    60  
    61  	if len(emailList) == 0 {
    62  		return &model.CommandResponse{ResponseType: model.COMMAND_RESPONSE_TYPE_EPHEMERAL, Text: args.T("api.command.invite_people.no_email")}
    63  	}
    64  
    65  	if err := a.InviteNewUsersToTeam(emailList, args.TeamId, args.UserId); err != nil {
    66  		mlog.Error(err.Error())
    67  		return &model.CommandResponse{ResponseType: model.COMMAND_RESPONSE_TYPE_EPHEMERAL, Text: args.T("api.command.invite_people.fail")}
    68  	}
    69  
    70  	return &model.CommandResponse{ResponseType: model.COMMAND_RESPONSE_TYPE_EPHEMERAL, Text: args.T("api.command.invite_people.sent")}
    71  }