PLT-3131 Add slash command for sending email invites (#3218)
* Add slash command for sending email invites * Add response for empty message and trim commas * Check for @ in email addresses before sending * Update invite people error message
Этот коммит содержится в:
коммит произвёл
enahum
родитель
3386c58d7c
Коммит
8165f5d91f
78
api/command_invite_people.go
Обычный файл
78
api/command_invite_people.go
Обычный файл
@@ -0,0 +1,78 @@
|
|||||||
|
// Copyright (c) 2016 Mattermost, Inc. All Rights Reserved.
|
||||||
|
// See License.txt for license information.
|
||||||
|
|
||||||
|
package api
|
||||||
|
|
||||||
|
import (
|
||||||
|
"strings"
|
||||||
|
|
||||||
|
"github.com/mattermost/platform/model"
|
||||||
|
"github.com/mattermost/platform/utils"
|
||||||
|
)
|
||||||
|
|
||||||
|
type InvitePeopleProvider struct {
|
||||||
|
}
|
||||||
|
|
||||||
|
const (
|
||||||
|
CMD_INVITE_PEOPLE = "invite_people"
|
||||||
|
)
|
||||||
|
|
||||||
|
func init() {
|
||||||
|
RegisterCommandProvider(&InvitePeopleProvider{})
|
||||||
|
}
|
||||||
|
|
||||||
|
func (me *InvitePeopleProvider) GetTrigger() string {
|
||||||
|
return CMD_INVITE_PEOPLE
|
||||||
|
}
|
||||||
|
|
||||||
|
func (me *InvitePeopleProvider) GetCommand(c *Context) *model.Command {
|
||||||
|
return &model.Command{
|
||||||
|
Trigger: CMD_INVITE_PEOPLE,
|
||||||
|
AutoComplete: true,
|
||||||
|
AutoCompleteDesc: c.T("api.command.invite_people.desc"),
|
||||||
|
AutoCompleteHint: c.T("api.command.invite_people.hint"),
|
||||||
|
DisplayName: c.T("api.command.invite_people.name"),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (me *InvitePeopleProvider) DoCommand(c *Context, channelId string, message string) *model.CommandResponse {
|
||||||
|
if !utils.Cfg.EmailSettings.SendEmailNotifications {
|
||||||
|
return &model.CommandResponse{ResponseType: model.COMMAND_RESPONSE_TYPE_EPHEMERAL, Text: c.T("api.command.invite_people.email_off")}
|
||||||
|
}
|
||||||
|
|
||||||
|
tchan := Srv.Store.Team().Get(c.TeamId)
|
||||||
|
uchan := Srv.Store.User().Get(c.Session.UserId)
|
||||||
|
|
||||||
|
emailList := strings.Fields(message)
|
||||||
|
|
||||||
|
for i := len(emailList) - 1; i >= 0; i-- {
|
||||||
|
emailList[i] = strings.Trim(emailList[i], ",")
|
||||||
|
if !strings.Contains(emailList[i], "@") {
|
||||||
|
emailList = append(emailList[:i], emailList[i+1:]...)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if len(emailList) == 0 {
|
||||||
|
return &model.CommandResponse{ResponseType: model.COMMAND_RESPONSE_TYPE_EPHEMERAL, Text: c.T("api.command.invite_people.no_email")}
|
||||||
|
}
|
||||||
|
|
||||||
|
var team *model.Team
|
||||||
|
if result := <-tchan; result.Err != nil {
|
||||||
|
c.Err = result.Err
|
||||||
|
return &model.CommandResponse{ResponseType: model.COMMAND_RESPONSE_TYPE_EPHEMERAL, Text: c.T("api.command.invite_people.fail")}
|
||||||
|
} else {
|
||||||
|
team = result.Data.(*model.Team)
|
||||||
|
}
|
||||||
|
|
||||||
|
var user *model.User
|
||||||
|
if result := <-uchan; result.Err != nil {
|
||||||
|
c.Err = result.Err
|
||||||
|
return &model.CommandResponse{ResponseType: model.COMMAND_RESPONSE_TYPE_EPHEMERAL, Text: c.T("api.command.invite_people.fail")}
|
||||||
|
} else {
|
||||||
|
user = result.Data.(*model.User)
|
||||||
|
}
|
||||||
|
|
||||||
|
go InviteMembers(c, team, user, emailList)
|
||||||
|
|
||||||
|
return &model.CommandResponse{ResponseType: model.COMMAND_RESPONSE_TYPE_EPHEMERAL, Text: c.T("api.command.invite_people.sent")}
|
||||||
|
}
|
||||||
31
api/command_invite_people_test.go
Обычный файл
31
api/command_invite_people_test.go
Обычный файл
@@ -0,0 +1,31 @@
|
|||||||
|
// Copyright (c) 2016 Mattermost, Inc. All Rights Reserved.
|
||||||
|
// See License.txt for license information.
|
||||||
|
|
||||||
|
package api
|
||||||
|
|
||||||
|
import (
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"github.com/mattermost/platform/model"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestInvitePeopleCommand(t *testing.T) {
|
||||||
|
th := Setup().InitBasic()
|
||||||
|
Client := th.BasicClient
|
||||||
|
channel := th.BasicChannel
|
||||||
|
|
||||||
|
r1 := Client.Must(Client.Command(channel.Id, "/invite_people test@example.com", false)).Data.(*model.CommandResponse)
|
||||||
|
if r1 == nil {
|
||||||
|
t.Fatal("Command failed to execute")
|
||||||
|
}
|
||||||
|
|
||||||
|
r2 := Client.Must(Client.Command(channel.Id, "/invite_people test1@example.com test2@example.com", false)).Data.(*model.CommandResponse)
|
||||||
|
if r2 == nil {
|
||||||
|
t.Fatal("Command failed to execute")
|
||||||
|
}
|
||||||
|
|
||||||
|
r3 := Client.Must(Client.Command(channel.Id, "/invite_people", false)).Data.(*model.CommandResponse)
|
||||||
|
if r3 == nil {
|
||||||
|
t.Fatal("Command failed to execute")
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -39,7 +39,7 @@ func InitTeam() {
|
|||||||
|
|
||||||
BaseRoutes.NeedTeam.Handle("/add_user_to_team", ApiUserRequired(addUserToTeam)).Methods("POST")
|
BaseRoutes.NeedTeam.Handle("/add_user_to_team", ApiUserRequired(addUserToTeam)).Methods("POST")
|
||||||
|
|
||||||
// These should be moved to the global admain console
|
// These should be moved to the global admin console
|
||||||
BaseRoutes.NeedTeam.Handle("/import_team", ApiUserRequired(importTeam)).Methods("POST")
|
BaseRoutes.NeedTeam.Handle("/import_team", ApiUserRequired(importTeam)).Methods("POST")
|
||||||
BaseRoutes.Teams.Handle("/add_user_to_team_from_invite", ApiUserRequired(addUserToTeamFromInvite)).Methods("POST")
|
BaseRoutes.Teams.Handle("/add_user_to_team_from_invite", ApiUserRequired(addUserToTeamFromInvite)).Methods("POST")
|
||||||
}
|
}
|
||||||
@@ -399,12 +399,12 @@ func inviteMembers(c *Context, w http.ResponseWriter, r *http.Request) {
|
|||||||
user = result.Data.(*model.User)
|
user = result.Data.(*model.User)
|
||||||
}
|
}
|
||||||
|
|
||||||
ia := make([]string, len(invites.Invites))
|
emailList := make([]string, len(invites.Invites))
|
||||||
for _, invite := range invites.Invites {
|
for _, invite := range invites.Invites {
|
||||||
ia = append(ia, invite["email"])
|
emailList = append(emailList, invite["email"])
|
||||||
}
|
}
|
||||||
|
|
||||||
InviteMembers(c, team, user, ia)
|
InviteMembers(c, team, user, emailList)
|
||||||
|
|
||||||
w.Write([]byte(invites.ToJson()))
|
w.Write([]byte(invites.ToJson()))
|
||||||
}
|
}
|
||||||
|
|||||||
28
i18n/en.json
28
i18n/en.json
@@ -339,6 +339,34 @@
|
|||||||
"id": "api.command.execute_command.start.app_error",
|
"id": "api.command.execute_command.start.app_error",
|
||||||
"translation": "No command trigger found"
|
"translation": "No command trigger found"
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
"id": "api.command.invite_people.desc",
|
||||||
|
"translation": "Send an email invite to your Mattermost team"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"id": "api.command.invite_people.email_off",
|
||||||
|
"translation": "Email has not been configured, no invite(s) sent"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"id": "api.command.invite_people.fail",
|
||||||
|
"translation": "Encountered an error sending email invite(s)"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"id": "api.command.invite_people.hint",
|
||||||
|
"translation": "[name@domain.com ...]"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"id": "api.command.invite_people.name",
|
||||||
|
"translation": "invite_people"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"id": "api.command.invite_people.no_email",
|
||||||
|
"translation": "Please specify one or more valid email addresses"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"id": "api.command.invite_people.sent",
|
||||||
|
"translation": "Email invite(s) sent"
|
||||||
|
},
|
||||||
{
|
{
|
||||||
"id": "api.command.init.debug",
|
"id": "api.command.init.debug",
|
||||||
"translation": "Initializing command api routes"
|
"translation": "Initializing command api routes"
|
||||||
|
|||||||
Ссылка в новой задаче
Block a user