Skip to content
This repository was archived by the owner on Sep 11, 2024. It is now read-only.

Commit 0ee1773

Browse files
committed
Support pasting a bunch of identifiers into the invite dialog
Part of element-hq/element-web#11199
1 parent c3a799b commit 0ee1773

File tree

2 files changed

+68
-0
lines changed

2 files changed

+68
-0
lines changed

src/components/views/dialogs/DMInviteDialog.js

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ import {getDefaultIdentityServerUrl, useDefaultIdentityServer} from "../../../ut
3030
import {abbreviateUrl} from "../../../utils/UrlUtils";
3131
import dis from "../../../dispatcher";
3232
import IdentityAuthClient from "../../../IdentityAuthClient";
33+
import Modal from "../../../Modal";
3334

3435
// TODO: [TravisR] Make this generic for all kinds of invites
3536

@@ -502,6 +503,70 @@ export default class DMInviteDialog extends React.PureComponent {
502503
}
503504
};
504505

506+
_onPaste = async (e) => {
507+
// Prevent the text being pasted into the textarea
508+
e.preventDefault();
509+
510+
// Process it as a list of addresses to add instead
511+
const text = e.clipboardData.getData("text");
512+
const possibleMembers = [
513+
// If we can avoid hitting the profile endpoint, we should.
514+
...this.state.recents,
515+
...this.state.suggestions,
516+
...this.state.serverResultsMixin,
517+
...this.state.threepidResultsMixin,
518+
];
519+
const toAdd = [];
520+
const failed = [];
521+
const potentialAddresses = text.split(/[\s,]+/);
522+
for (const address of potentialAddresses) {
523+
const member = possibleMembers.find(m => m.userId === address);
524+
if (member) {
525+
toAdd.push(member.user);
526+
continue;
527+
}
528+
529+
if (address.indexOf('@') > 0 && Email.looksValid(address)) {
530+
toAdd.push(new ThreepidMember(address));
531+
continue;
532+
}
533+
534+
if (address[0] !== '@') {
535+
failed.push(address); // not a user ID
536+
continue;
537+
}
538+
539+
try {
540+
const profile = await MatrixClientPeg.get().getProfileInfo(address);
541+
const displayName = profile ? profile.displayname : null;
542+
const avatarUrl = profile ? profile.avatar_url : null;
543+
toAdd.push(new DirectoryMember({
544+
user_id: address,
545+
display_name: displayName,
546+
avatar_url: avatarUrl,
547+
}));
548+
} catch (e) {
549+
console.error("Error looking up profile for " + address);
550+
console.error(e);
551+
failed.push(address);
552+
}
553+
}
554+
555+
if (failed.length > 0) {
556+
const QuestionDialog = sdk.getComponent('dialogs.QuestionDialog');
557+
Modal.createTrackedDialog('Invite Paste Fail', '', QuestionDialog, {
558+
title: _t('Failed to find the following users'),
559+
description: _t(
560+
"The following users might not exist or are invalid, and cannot be invited: %(csvNames)s",
561+
{csvNames: failed.join(", ")},
562+
),
563+
button: _t('OK'),
564+
});
565+
}
566+
567+
this.setState({targets: [...this.state.targets, ...toAdd]});
568+
};
569+
505570
_onClickInputArea = (e) => {
506571
// Stop the browser from highlighting text
507572
e.preventDefault();
@@ -624,6 +689,7 @@ export default class DMInviteDialog extends React.PureComponent {
624689
onChange={this._updateFilter}
625690
defaultValue={this.state.filterText}
626691
ref={this._editorRef}
692+
onPaste={this._onPaste}
627693
/>
628694
);
629695
return (

src/i18n/strings/en_EN.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1422,6 +1422,8 @@
14221422
"View Servers in Room": "View Servers in Room",
14231423
"Toolbox": "Toolbox",
14241424
"Developer Tools": "Developer Tools",
1425+
"Failed to find the following users": "Failed to find the following users",
1426+
"The following users might not exist or are invalid, and cannot be invited: %(csvNames)s": "The following users might not exist or are invalid, and cannot be invited: %(csvNames)s",
14251427
"Recent Conversations": "Recent Conversations",
14261428
"Suggestions": "Suggestions",
14271429
"Show more": "Show more",

0 commit comments

Comments
 (0)