forked from mattermost/mattermost-webapp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
invite_actions.jsx
142 lines (129 loc) · 6.28 KB
/
invite_actions.jsx
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
// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
// See LICENSE.txt for license information.
import * as TeamActions from 'mattermost-redux/actions/teams';
import {getTeamMember} from 'mattermost-redux/selectors/entities/teams';
import {getChannelMembersInChannels} from 'mattermost-redux/selectors/entities/channels';
import {joinChannel} from 'mattermost-redux/actions/channels';
import {addUsersToTeam} from 'actions/team_actions';
import {t} from 'utils/i18n';
import {isGuest, localizeMessage} from 'utils/utils';
export function sendMembersInvites(teamId, users, emails) {
return async (dispatch, getState) => {
if (users.length > 0) {
await dispatch(TeamActions.getTeamMembersByIds(teamId, users.map((u) => u.id)));
}
const state = getState();
const sent = [];
const notSent = [];
const usersToAdd = [];
for (const user of users) {
const member = getTeamMember(state, teamId, user.id);
if (isGuest(user)) {
notSent.push({user, reason: localizeMessage('invite.members.user-is-guest', 'Contact your admin to make this guest a full member.')});
} else if (member) {
notSent.push({user, reason: localizeMessage('invite.members.already-member', 'This person is already a team member.')});
} else {
usersToAdd.push(user);
}
}
if (usersToAdd.length > 0) {
const response = await dispatch(addUsersToTeam(teamId, usersToAdd.map((u) => u.id)));
const members = response.data || [];
for (const userToAdd of usersToAdd) {
const memberWithError = members.find((m) => m.user_id === userToAdd.id && m.error);
if (memberWithError) {
notSent.push({user: userToAdd, reason: memberWithError.error.message});
} else {
sent.push({user: userToAdd, reason: localizeMessage('invite.members.added-to-team', 'This member has been added to the team.')});
}
}
}
if (emails.length > 0) {
let response;
try {
response = await dispatch(TeamActions.sendEmailInvitesToTeamGracefully(teamId, emails));
} catch (e) {
response = {data: emails.map((email) => ({email, error: {error: localizeMessage('invite.members.unable-to-add-the-user-to-the-team', 'Unable to add the user to the team.')}}))};
}
const invitesWithErrors = response.data || [];
for (const email of emails) {
const inviteWithError = invitesWithErrors.find((i) => email === i.email && i.error);
if (inviteWithError) {
notSent.push({email, reason: inviteWithError.error.message});
} else {
sent.push({email, reason: localizeMessage('invite.members.invite-sent', 'An invitation email has been sent.')});
}
}
}
return {sent, notSent};
};
}
export async function sendGuestInviteForUser(dispatch, user, teamId, channels, members) {
if (!isGuest(user)) {
return {notSent: {user, reason: localizeMessage('invite.members.user-is-not-guest', 'This person is already a member.')}};
}
let memberOfAll = true;
let memberOfAny = false;
for (const channel of channels) {
const member = members && members[channel] && members[channel][user.id];
if (member) {
memberOfAny = true;
} else {
memberOfAll = false;
}
}
if (memberOfAll) {
return {notSent: {user, reason: localizeMessage('invite.guests.already-all-channels-member', 'This person is already a member of all the channels.')}};
}
try {
await dispatch(addUsersToTeam(teamId, [user.id]));
for (const channel of channels) {
const member = members && members[channel] && members[channel][user.id];
if (!member) {
await dispatch(joinChannel(user.id, teamId, channel)); // eslint-disable-line no-await-in-loop
}
}
} catch (e) {
return {notSent: {user, reason: localizeMessage('invite.guests.unable-to-add-the-user-to-the-channels', 'Unable to add the guest to the channels.')}};
}
if (memberOfAny) {
return {notSent: {user, reason: localizeMessage('invite.guests.already-some-channels-member', 'This person is already a member of some of the channels.')}};
}
return {sent: {user, reason: {id: t('invite.guests.new-member'), message: 'This guest has been added to the team and {count, plural, one {channel} other {channels}}.', values: {count: channels.length}}}};
}
export function sendGuestsInvites(teamId, channels, users, emails, message) {
return async (dispatch, getState) => {
if (users.length > 0) {
await dispatch(TeamActions.getTeamMembersByIds(teamId, users.map((u) => u.id)));
}
const state = getState();
const sent = [];
const notSent = [];
const members = getChannelMembersInChannels(state);
const results = await Promise.all(users.map((user) => sendGuestInviteForUser(dispatch, user, teamId, channels, members)));
for (const result of results) {
if (result.sent) {
sent.push(result.sent);
}
if (result.notSent) {
notSent.push(result.notSent);
}
}
if (emails.length > 0) {
let response;
try {
response = await dispatch(TeamActions.sendEmailGuestInvitesToChannelsGracefully(teamId, channels, emails, message));
} catch (e) {
response = {data: emails.map((email) => ({email, error: {error: localizeMessage('invite.guests.unable-to-add-the-user-to-the-channels', 'Unable to add the guest to the channels.')}}))};
}
for (const res of (response.data || [])) {
if (res.error) {
notSent.push({email: res.email, reason: res.error.message});
} else {
sent.push({email: res.email, reason: localizeMessage('invite.guests.added-to-channel', 'An invitation email has been sent.')});
}
}
}
return {sent, notSent};
};
}