-
Notifications
You must be signed in to change notification settings - Fork 7.3k
/
command_invite.go
171 lines (151 loc) · 5.31 KB
/
command_invite.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
// See LICENSE.txt for license information.
package app
import (
"strings"
goi18n "github.com/mattermost/go-i18n/i18n"
"github.com/mattermost/mattermost-server/v5/mlog"
"github.com/mattermost/mattermost-server/v5/model"
)
type InviteProvider struct {
}
const (
CMD_INVITE = "invite"
)
func init() {
RegisterCommandProvider(&InviteProvider{})
}
func (me *InviteProvider) GetTrigger() string {
return CMD_INVITE
}
func (me *InviteProvider) GetCommand(a *App, T goi18n.TranslateFunc) *model.Command {
return &model.Command{
Trigger: CMD_INVITE,
AutoComplete: true,
AutoCompleteDesc: T("api.command_invite.desc"),
AutoCompleteHint: T("api.command_invite.hint"),
DisplayName: T("api.command_invite.name"),
}
}
func (me *InviteProvider) DoCommand(a *App, args *model.CommandArgs, message string) *model.CommandResponse {
if message == "" {
return &model.CommandResponse{
Text: args.T("api.command_invite.missing_message.app_error"),
ResponseType: model.COMMAND_RESPONSE_TYPE_EPHEMERAL,
}
}
splitMessage := strings.SplitN(message, " ", 2)
targetUsername := splitMessage[0]
targetUsername = strings.TrimPrefix(targetUsername, "@")
userProfile, err := a.Srv.Store.User().GetByUsername(targetUsername)
if err != nil {
mlog.Error(err.Error())
return &model.CommandResponse{
Text: args.T("api.command_invite.missing_user.app_error"),
ResponseType: model.COMMAND_RESPONSE_TYPE_EPHEMERAL,
}
}
if userProfile.DeleteAt != 0 {
return &model.CommandResponse{
Text: args.T("api.command_invite.missing_user.app_error"),
ResponseType: model.COMMAND_RESPONSE_TYPE_EPHEMERAL,
}
}
var channelToJoin *model.Channel
// User set a channel to add the invited user
if len(splitMessage) > 1 && splitMessage[1] != "" {
targetChannelName := strings.TrimPrefix(strings.TrimSpace(splitMessage[1]), "~")
if channelToJoin, err = a.GetChannelByName(targetChannelName, args.TeamId, false); err != nil {
return &model.CommandResponse{
Text: args.T("api.command_invite.channel.error", map[string]interface{}{
"Channel": targetChannelName,
}),
ResponseType: model.COMMAND_RESPONSE_TYPE_EPHEMERAL,
}
}
} else {
channelToJoin, err = a.GetChannel(args.ChannelId)
if err != nil {
return &model.CommandResponse{
Text: args.T("api.command_invite.channel.app_error"),
ResponseType: model.COMMAND_RESPONSE_TYPE_EPHEMERAL,
}
}
}
// Permissions Check
switch channelToJoin.Type {
case model.CHANNEL_OPEN:
if !a.SessionHasPermissionToChannel(args.Session, channelToJoin.Id, model.PERMISSION_MANAGE_PUBLIC_CHANNEL_MEMBERS) {
return &model.CommandResponse{
Text: args.T("api.command_invite.permission.app_error", map[string]interface{}{
"User": userProfile.Username,
"Channel": channelToJoin.Name,
}),
ResponseType: model.COMMAND_RESPONSE_TYPE_EPHEMERAL,
}
}
case model.CHANNEL_PRIVATE:
if !a.SessionHasPermissionToChannel(args.Session, channelToJoin.Id, model.PERMISSION_MANAGE_PRIVATE_CHANNEL_MEMBERS) {
if _, err = a.GetChannelMember(channelToJoin.Id, args.UserId); err == nil {
// User doing the inviting is a member of the channel.
return &model.CommandResponse{
Text: args.T("api.command_invite.permission.app_error", map[string]interface{}{
"User": userProfile.Username,
"Channel": channelToJoin.Name,
}),
ResponseType: model.COMMAND_RESPONSE_TYPE_EPHEMERAL,
}
} else {
// User doing the inviting is *not* a member of the channel.
return &model.CommandResponse{
Text: args.T("api.command_invite.private_channel.app_error", map[string]interface{}{
"Channel": channelToJoin.Name,
}),
ResponseType: model.COMMAND_RESPONSE_TYPE_EPHEMERAL,
}
}
}
default:
return &model.CommandResponse{
Text: args.T("api.command_invite.directchannel.app_error"),
ResponseType: model.COMMAND_RESPONSE_TYPE_EPHEMERAL,
}
}
// Check if user is already in the channel
_, err = a.GetChannelMember(channelToJoin.Id, userProfile.Id)
if err == nil {
return &model.CommandResponse{
Text: args.T("api.command_invite.user_already_in_channel.app_error", map[string]interface{}{
"User": userProfile.Username,
}),
ResponseType: model.COMMAND_RESPONSE_TYPE_EPHEMERAL,
}
}
if _, err := a.AddChannelMember(userProfile.Id, channelToJoin, args.Session.UserId, ""); err != nil {
var text string
if err.Id == "api.channel.add_members.user_denied" {
text = args.T("api.command_invite.group_constrained_user_denied")
} else if err.Id == "store.sql_team.get_member.missing.app_error" ||
err.Id == "api.channel.add_user.to.channel.failed.deleted.app_error" {
text = args.T("api.command_invite.user_not_in_team.app_error", map[string]interface{}{
"Username": userProfile.Username,
})
} else {
text = args.T("api.command_invite.fail.app_error")
}
return &model.CommandResponse{
Text: text,
ResponseType: model.COMMAND_RESPONSE_TYPE_EPHEMERAL,
}
}
if args.ChannelId != channelToJoin.Id {
return &model.CommandResponse{
Text: args.T("api.command_invite.success", map[string]interface{}{
"User": userProfile.Username,
"Channel": channelToJoin.Name,
}),
ResponseType: model.COMMAND_RESPONSE_TYPE_EPHEMERAL,
}
}
return &model.CommandResponse{}
}