2017-08-16 13:58:30 +00:00
|
|
|
/*
|
|
|
|
Copyright 2017 New Vector Ltd
|
|
|
|
|
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
you may not use this file except in compliance with the License.
|
|
|
|
You may obtain a copy of the License at
|
|
|
|
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
See the License for the specific language governing permissions and
|
|
|
|
limitations under the License.
|
|
|
|
*/
|
|
|
|
|
|
|
|
import Modal from './Modal';
|
|
|
|
import sdk from './';
|
|
|
|
import MultiInviter from './utils/MultiInviter';
|
|
|
|
import { _t } from './languageHandler';
|
2017-09-26 13:49:13 +00:00
|
|
|
import MatrixClientPeg from './MatrixClientPeg';
|
2017-10-04 15:56:35 +00:00
|
|
|
import GroupStoreCache from './stores/GroupStoreCache';
|
2017-08-16 13:58:30 +00:00
|
|
|
|
|
|
|
export function showGroupInviteDialog(groupId) {
|
2017-09-21 15:53:10 +00:00
|
|
|
const AddressPickerDialog = sdk.getComponent("dialogs.AddressPickerDialog");
|
|
|
|
Modal.createTrackedDialog('Group Invite', '', AddressPickerDialog, {
|
2017-09-26 13:49:13 +00:00
|
|
|
title: _t("Invite new group members"),
|
2017-08-16 13:58:30 +00:00
|
|
|
description: _t("Who would you like to add to this group?"),
|
|
|
|
placeholder: _t("Name or matrix ID"),
|
|
|
|
button: _t("Invite to Group"),
|
2017-09-27 14:30:58 +00:00
|
|
|
validAddressTypes: ['mx-user-id'],
|
2017-08-25 11:10:13 +00:00
|
|
|
onFinished: (success, addrs) => {
|
|
|
|
if (!success) return;
|
2017-08-16 13:58:30 +00:00
|
|
|
|
|
|
|
_onGroupInviteFinished(groupId, addrs);
|
|
|
|
},
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2017-09-26 13:49:13 +00:00
|
|
|
export function showGroupAddRoomDialog(groupId) {
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
const AddressPickerDialog = sdk.getComponent("dialogs.AddressPickerDialog");
|
|
|
|
Modal.createTrackedDialog('Add Rooms to Group', '', AddressPickerDialog, {
|
|
|
|
title: _t("Add rooms to the group"),
|
|
|
|
description: _t("Which rooms would you like to add to this group?"),
|
|
|
|
placeholder: _t("Room name or alias"),
|
|
|
|
button: _t("Add to group"),
|
|
|
|
pickerType: 'room',
|
2017-09-27 14:30:58 +00:00
|
|
|
validAddressTypes: ['mx-room-id'],
|
2017-09-26 13:49:13 +00:00
|
|
|
onFinished: (success, addrs) => {
|
|
|
|
if (!success) return;
|
|
|
|
|
|
|
|
_onGroupAddRoomFinished(groupId, addrs).then(resolve, reject);
|
|
|
|
},
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2017-08-16 13:58:30 +00:00
|
|
|
function _onGroupInviteFinished(groupId, addrs) {
|
|
|
|
const multiInviter = new MultiInviter(groupId);
|
|
|
|
|
|
|
|
const addrTexts = addrs.map((addr) => addr.address);
|
|
|
|
|
|
|
|
multiInviter.invite(addrTexts).then((completionStates) => {
|
|
|
|
// Show user any errors
|
|
|
|
const errorList = [];
|
|
|
|
for (const addr of Object.keys(completionStates)) {
|
|
|
|
if (addrs[addr] === "error") {
|
|
|
|
errorList.push(addr);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (errorList.length > 0) {
|
|
|
|
const ErrorDialog = sdk.getComponent("dialogs.ErrorDialog");
|
|
|
|
Modal.createTrackedDialog('Failed to invite the following users to the group', '', ErrorDialog, {
|
|
|
|
title: _t("Failed to invite the following users to %(groupId)s:", {groupId: groupId}),
|
|
|
|
description: errorList.join(", "),
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}).catch((err) => {
|
|
|
|
const ErrorDialog = sdk.getComponent("dialogs.ErrorDialog");
|
|
|
|
Modal.createTrackedDialog('Failed to invite users to group', '', ErrorDialog, {
|
|
|
|
title: _t("Failed to invite users group"),
|
|
|
|
description: _t("Failed to invite users to %(groupId)s", {groupId: groupId}),
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2017-09-26 13:49:13 +00:00
|
|
|
function _onGroupAddRoomFinished(groupId, addrs) {
|
2017-10-04 15:56:35 +00:00
|
|
|
const groupStore = GroupStoreCache.getGroupStore(MatrixClientPeg.get(), groupId);
|
2017-09-26 13:49:13 +00:00
|
|
|
const errorList = [];
|
|
|
|
return Promise.all(addrs.map((addr) => {
|
2017-10-04 15:56:35 +00:00
|
|
|
return groupStore
|
|
|
|
.addRoomToGroup(addr.address)
|
2017-09-26 13:49:13 +00:00
|
|
|
.catch(() => { errorList.push(addr.address); })
|
|
|
|
.reflect();
|
|
|
|
})).then(() => {
|
|
|
|
if (errorList.length === 0) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
const ErrorDialog = sdk.getComponent("dialogs.ErrorDialog");
|
|
|
|
Modal.createTrackedDialog(
|
|
|
|
'Failed to add the following room to the group',
|
|
|
|
'', ErrorDialog,
|
|
|
|
{
|
|
|
|
title: _t(
|
|
|
|
"Failed to add the following rooms to %(groupId)s:",
|
|
|
|
{groupId},
|
|
|
|
),
|
|
|
|
description: errorList.join(", "),
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|