2017-08-16 15:58:30 +02: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.
|
|
|
|
*/
|
|
|
|
|
2018-07-04 03:55:56 +02:00
|
|
|
import React from 'react';
|
2017-08-16 15:58:30 +02:00
|
|
|
import Modal from './Modal';
|
2019-12-20 02:19:56 +01:00
|
|
|
import * as sdk from './';
|
2017-08-16 15:58:30 +02:00
|
|
|
import MultiInviter from './utils/MultiInviter';
|
|
|
|
import { _t } from './languageHandler';
|
2019-12-20 22:13:46 +01:00
|
|
|
import {MatrixClientPeg} from './MatrixClientPeg';
|
2018-05-01 12:18:45 +02:00
|
|
|
import GroupStore from './stores/GroupStore';
|
2020-05-28 23:33:00 +02:00
|
|
|
import StyledCheckbox from './components/views/elements/StyledCheckbox';
|
2017-08-16 15:58:30 +02:00
|
|
|
|
|
|
|
export function showGroupInviteDialog(groupId) {
|
2018-03-12 18:23:31 +01:00
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
const description = <div>
|
|
|
|
<div>{ _t("Who would you like to add to this community?") }</div>
|
|
|
|
<div className="warning">
|
|
|
|
{ _t(
|
|
|
|
"Warning: any person you add to a community will be publicly "+
|
|
|
|
"visible to anyone who knows the community ID",
|
|
|
|
) }
|
|
|
|
</div>
|
|
|
|
</div>;
|
2017-10-19 16:37:35 +02:00
|
|
|
|
2018-03-12 18:23:31 +01:00
|
|
|
const AddressPickerDialog = sdk.getComponent("dialogs.AddressPickerDialog");
|
|
|
|
Modal.createTrackedDialog('Group Invite', '', AddressPickerDialog, {
|
|
|
|
title: _t("Invite new community members"),
|
|
|
|
description: description,
|
2019-05-16 22:35:43 +02:00
|
|
|
placeholder: _t("Name or Matrix ID"),
|
2018-03-12 18:23:31 +01:00
|
|
|
button: _t("Invite to Community"),
|
|
|
|
validAddressTypes: ['mx-user-id'],
|
|
|
|
onFinished: (success, addrs) => {
|
|
|
|
if (!success) return;
|
2017-08-16 15:58:30 +02:00
|
|
|
|
2018-03-12 18:23:31 +01:00
|
|
|
_onGroupInviteFinished(groupId, addrs).then(resolve, reject);
|
|
|
|
},
|
2019-09-10 19:01:20 +02:00
|
|
|
}, /*className=*/null, /*isPriority=*/false, /*isStatic=*/true);
|
2017-08-16 15:58:30 +02:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2017-09-26 15:49:13 +02:00
|
|
|
export function showGroupAddRoomDialog(groupId) {
|
|
|
|
return new Promise((resolve, reject) => {
|
2017-11-07 19:51:41 +01:00
|
|
|
let addRoomsPublicly = false;
|
|
|
|
const onCheckboxClicked = (e) => {
|
|
|
|
addRoomsPublicly = e.target.checked;
|
|
|
|
};
|
2017-10-19 16:37:35 +02:00
|
|
|
const description = <div>
|
2017-10-19 16:55:57 +02:00
|
|
|
<div>{ _t("Which rooms would you like to add to this community?") }</div>
|
2017-10-19 16:37:35 +02:00
|
|
|
</div>;
|
|
|
|
|
2020-05-28 23:33:00 +02:00
|
|
|
const checkboxContainer = <StyledCheckbox
|
|
|
|
className="mx_GroupAddressPicker_checkboxContainer"
|
|
|
|
onChange={onCheckboxClicked}
|
|
|
|
>
|
|
|
|
{ _t("Show these rooms to non-members on the community page and room list?") }
|
|
|
|
</StyledCheckbox>;
|
2017-11-07 19:51:41 +01:00
|
|
|
|
2017-09-26 15:49:13 +02:00
|
|
|
const AddressPickerDialog = sdk.getComponent("dialogs.AddressPickerDialog");
|
|
|
|
Modal.createTrackedDialog('Add Rooms to Group', '', AddressPickerDialog, {
|
2017-10-19 16:01:16 +02:00
|
|
|
title: _t("Add rooms to the community"),
|
2017-10-19 16:37:35 +02:00
|
|
|
description: description,
|
2017-11-07 19:51:41 +01:00
|
|
|
extraNode: checkboxContainer,
|
2020-04-14 11:06:57 +02:00
|
|
|
placeholder: _t("Room name or address"),
|
2017-10-19 16:01:16 +02:00
|
|
|
button: _t("Add to community"),
|
2017-09-26 15:49:13 +02:00
|
|
|
pickerType: 'room',
|
2017-09-27 16:30:58 +02:00
|
|
|
validAddressTypes: ['mx-room-id'],
|
2017-09-26 15:49:13 +02:00
|
|
|
onFinished: (success, addrs) => {
|
|
|
|
if (!success) return;
|
|
|
|
|
2017-11-07 19:51:41 +01:00
|
|
|
_onGroupAddRoomFinished(groupId, addrs, addRoomsPublicly).then(resolve, reject);
|
2017-09-26 15:49:13 +02:00
|
|
|
},
|
2019-09-10 19:01:20 +02:00
|
|
|
}, /*className=*/null, /*isPriority=*/false, /*isStatic=*/true);
|
2017-09-26 15:49:13 +02:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2017-08-16 15:58:30 +02:00
|
|
|
function _onGroupInviteFinished(groupId, addrs) {
|
|
|
|
const multiInviter = new MultiInviter(groupId);
|
|
|
|
|
|
|
|
const addrTexts = addrs.map((addr) => addr.address);
|
|
|
|
|
2018-03-12 18:23:31 +01:00
|
|
|
return multiInviter.invite(addrTexts).then((completionStates) => {
|
2017-08-16 15:58:30 +02:00
|
|
|
// 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, {
|
2017-10-19 16:01:16 +02:00
|
|
|
title: _t("Failed to invite users to community"),
|
2017-08-16 15:58:30 +02:00
|
|
|
description: _t("Failed to invite users to %(groupId)s", {groupId: groupId}),
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2017-11-07 19:51:41 +01:00
|
|
|
function _onGroupAddRoomFinished(groupId, addrs, addRoomsPublicly) {
|
2017-10-18 16:00:54 +02:00
|
|
|
const matrixClient = MatrixClientPeg.get();
|
2017-09-26 15:49:13 +02:00
|
|
|
const errorList = [];
|
2021-05-25 17:12:34 +02:00
|
|
|
return Promise.allSettled(addrs.map((addr) => {
|
2018-05-01 12:18:45 +02:00
|
|
|
return GroupStore
|
|
|
|
.addRoomToGroup(groupId, addr.address, addRoomsPublicly)
|
2017-09-26 15:49:13 +02:00
|
|
|
.catch(() => { errorList.push(addr.address); })
|
2017-10-18 16:00:54 +02:00
|
|
|
.then(() => {
|
|
|
|
const roomId = addr.address;
|
|
|
|
const room = matrixClient.getRoom(roomId);
|
|
|
|
// Can the user change related groups?
|
|
|
|
if (!room || !room.currentState.mayClientSendStateEvent("m.room.related_groups", matrixClient)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
// Get the related groups
|
|
|
|
const relatedGroupsEvent = room.currentState.getStateEvents('m.room.related_groups', '');
|
|
|
|
const groups = relatedGroupsEvent ? relatedGroupsEvent.getContent().groups || [] : [];
|
|
|
|
|
|
|
|
// Add this group as related
|
|
|
|
if (!groups.includes(groupId)) {
|
|
|
|
groups.push(groupId);
|
|
|
|
return MatrixClientPeg.get().sendStateEvent(roomId, 'm.room.related_groups', {groups}, '');
|
|
|
|
}
|
2019-11-14 14:52:17 +01:00
|
|
|
});
|
2017-09-26 15:49:13 +02:00
|
|
|
})).then(() => {
|
|
|
|
if (errorList.length === 0) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
const ErrorDialog = sdk.getComponent("dialogs.ErrorDialog");
|
|
|
|
Modal.createTrackedDialog(
|
|
|
|
'Failed to add the following room to the group',
|
2021-04-29 19:57:02 +02:00
|
|
|
'',
|
|
|
|
ErrorDialog,
|
2021-04-27 17:23:27 +02:00
|
|
|
{
|
2021-04-29 19:57:02 +02:00
|
|
|
title: _t(
|
|
|
|
"Failed to add the following rooms to %(groupId)s:",
|
|
|
|
{groupId},
|
|
|
|
),
|
|
|
|
description: errorList.join(", "),
|
|
|
|
},
|
|
|
|
);
|
2017-09-26 15:49:13 +02:00
|
|
|
});
|
|
|
|
}
|