2020-01-03 03:40:18 +03:00
|
|
|
/*
|
|
|
|
Copyright 2019, 2020 The Matrix.org Foundation C.I.C.
|
|
|
|
|
|
|
|
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.
|
|
|
|
*/
|
|
|
|
|
2020-01-07 06:51:23 +03:00
|
|
|
import React, {createRef} from 'react';
|
2020-01-03 03:40:18 +03:00
|
|
|
import PropTypes from 'prop-types';
|
|
|
|
import {_t} from "../../../languageHandler";
|
2020-01-10 00:16:32 +03:00
|
|
|
import * as sdk from "../../../index";
|
|
|
|
import {MatrixClientPeg} from "../../../MatrixClientPeg";
|
2020-01-17 00:40:12 +03:00
|
|
|
import {makeRoomPermalink, makeUserPermalink} from "../../../utils/permalinks/Permalinks";
|
2020-01-03 03:40:18 +03:00
|
|
|
import DMRoomMap from "../../../utils/DMRoomMap";
|
2020-01-10 00:16:32 +03:00
|
|
|
import {RoomMember} from "matrix-js-sdk/src/matrix";
|
2020-01-04 05:41:06 +03:00
|
|
|
import SdkConfig from "../../../SdkConfig";
|
2020-01-10 00:16:32 +03:00
|
|
|
import {getHttpUriForMxc} from "matrix-js-sdk/src/content-repo";
|
2020-01-09 06:49:29 +03:00
|
|
|
import * as Email from "../../../email";
|
|
|
|
import {getDefaultIdentityServerUrl, useDefaultIdentityServer} from "../../../utils/IdentityServerUtils";
|
|
|
|
import {abbreviateUrl} from "../../../utils/UrlUtils";
|
|
|
|
import dis from "../../../dispatcher";
|
|
|
|
import IdentityAuthClient from "../../../IdentityAuthClient";
|
2020-01-09 07:19:00 +03:00
|
|
|
import Modal from "../../../Modal";
|
2020-01-14 07:20:01 +03:00
|
|
|
import {humanizeTime} from "../../../utils/humanize";
|
2020-02-18 14:30:10 +03:00
|
|
|
import createRoom, {canEncryptToAllUsers} from "../../../createRoom";
|
2020-01-15 09:32:00 +03:00
|
|
|
import {inviteMultipleToRoom} from "../../../RoomInvite";
|
2020-01-23 19:00:55 +03:00
|
|
|
import SettingsStore from '../../../settings/SettingsStore';
|
2020-02-25 05:43:11 +03:00
|
|
|
import RoomListStore, {TAG_DM} from "../../../stores/RoomListStore";
|
2020-01-03 03:40:18 +03:00
|
|
|
|
2020-01-17 00:40:12 +03:00
|
|
|
export const KIND_DM = "dm";
|
|
|
|
export const KIND_INVITE = "invite";
|
2020-01-03 03:40:18 +03:00
|
|
|
|
|
|
|
const INITIAL_ROOMS_SHOWN = 3; // Number of rooms to show at first
|
|
|
|
const INCREMENT_ROOMS_SHOWN = 5; // Number of rooms to add when 'show more' is clicked
|
|
|
|
|
2020-01-07 06:51:23 +03:00
|
|
|
// This is the interface that is expected by various components in this file. It is a bit
|
|
|
|
// awkward because it also matches the RoomMember class from the js-sdk with some extra support
|
|
|
|
// for 3PIDs/email addresses.
|
|
|
|
//
|
2020-01-09 23:39:50 +03:00
|
|
|
// XXX: We should use TypeScript interfaces instead of this weird "abstract" class.
|
2020-01-07 06:51:23 +03:00
|
|
|
class Member {
|
|
|
|
/**
|
|
|
|
* The display name of this Member. For users this should be their profile's display
|
|
|
|
* name or user ID if none set. For 3PIDs this should be the 3PID address (email).
|
|
|
|
*/
|
|
|
|
get name(): string { throw new Error("Member class not implemented"); }
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The ID of this Member. For users this should be their user ID. For 3PIDs this should
|
|
|
|
* be the 3PID address (email).
|
|
|
|
*/
|
|
|
|
get userId(): string { throw new Error("Member class not implemented"); }
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets the MXC URL of this Member's avatar. For users this should be their profile's
|
|
|
|
* avatar MXC URL or null if none set. For 3PIDs this should always be null.
|
|
|
|
*/
|
|
|
|
getMxcAvatarUrl(): string { throw new Error("Member class not implemented"); }
|
|
|
|
}
|
|
|
|
|
|
|
|
class DirectoryMember extends Member {
|
2020-01-04 07:17:48 +03:00
|
|
|
_userId: string;
|
|
|
|
_displayName: string;
|
|
|
|
_avatarUrl: string;
|
|
|
|
|
|
|
|
constructor(userDirResult: {user_id: string, display_name: string, avatar_url: string}) {
|
2020-01-07 06:51:23 +03:00
|
|
|
super();
|
2020-01-04 07:17:48 +03:00
|
|
|
this._userId = userDirResult.user_id;
|
|
|
|
this._displayName = userDirResult.display_name;
|
|
|
|
this._avatarUrl = userDirResult.avatar_url;
|
|
|
|
}
|
|
|
|
|
2020-01-07 06:51:23 +03:00
|
|
|
// These next class members are for the Member interface
|
2020-01-04 07:17:48 +03:00
|
|
|
get name(): string {
|
|
|
|
return this._displayName || this._userId;
|
|
|
|
}
|
|
|
|
|
|
|
|
get userId(): string {
|
|
|
|
return this._userId;
|
|
|
|
}
|
|
|
|
|
|
|
|
getMxcAvatarUrl(): string {
|
|
|
|
return this._avatarUrl;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-01-09 06:49:29 +03:00
|
|
|
class ThreepidMember extends Member {
|
|
|
|
_id: string;
|
|
|
|
|
|
|
|
constructor(id: string) {
|
|
|
|
super();
|
|
|
|
this._id = id;
|
|
|
|
}
|
|
|
|
|
|
|
|
// This is a getter that would be falsey on all other implementations. Until we have
|
|
|
|
// better type support in the react-sdk we can use this trick to determine the kind
|
|
|
|
// of 3PID we're dealing with, if any.
|
|
|
|
get isEmail(): boolean {
|
|
|
|
return this._id.includes('@');
|
|
|
|
}
|
|
|
|
|
|
|
|
// These next class members are for the Member interface
|
|
|
|
get name(): string {
|
|
|
|
return this._id;
|
|
|
|
}
|
|
|
|
|
|
|
|
get userId(): string {
|
|
|
|
return this._id;
|
|
|
|
}
|
|
|
|
|
|
|
|
getMxcAvatarUrl(): string {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-01-07 06:51:23 +03:00
|
|
|
class DMUserTile extends React.PureComponent {
|
|
|
|
static propTypes = {
|
|
|
|
member: PropTypes.object.isRequired, // Should be a Member (see interface above)
|
|
|
|
onRemove: PropTypes.func.isRequired, // takes 1 argument, the member being removed
|
|
|
|
};
|
|
|
|
|
|
|
|
_onRemove = (e) => {
|
|
|
|
// Stop the browser from highlighting text
|
|
|
|
e.preventDefault();
|
|
|
|
e.stopPropagation();
|
|
|
|
|
|
|
|
this.props.onRemove(this.props.member);
|
|
|
|
};
|
|
|
|
|
|
|
|
render() {
|
|
|
|
const BaseAvatar = sdk.getComponent("views.avatars.BaseAvatar");
|
|
|
|
const AccessibleButton = sdk.getComponent("elements.AccessibleButton");
|
|
|
|
|
|
|
|
const avatarSize = 20;
|
|
|
|
const avatar = this.props.member.isEmail
|
|
|
|
? <img
|
2020-01-16 23:42:34 +03:00
|
|
|
className='mx_InviteDialog_userTile_avatar mx_InviteDialog_userTile_threepidAvatar'
|
2020-01-07 06:51:23 +03:00
|
|
|
src={require("../../../../res/img/icon-email-pill-avatar.svg")}
|
|
|
|
width={avatarSize} height={avatarSize} />
|
|
|
|
: <BaseAvatar
|
2020-01-16 23:42:34 +03:00
|
|
|
className='mx_InviteDialog_userTile_avatar'
|
2020-01-07 06:51:23 +03:00
|
|
|
url={getHttpUriForMxc(
|
|
|
|
MatrixClientPeg.get().getHomeserverUrl(), this.props.member.getMxcAvatarUrl(),
|
|
|
|
avatarSize, avatarSize, "crop")}
|
|
|
|
name={this.props.member.name}
|
|
|
|
idName={this.props.member.userId}
|
|
|
|
width={avatarSize}
|
|
|
|
height={avatarSize} />;
|
|
|
|
|
|
|
|
return (
|
2020-01-16 23:42:34 +03:00
|
|
|
<span className='mx_InviteDialog_userTile'>
|
|
|
|
<span className='mx_InviteDialog_userTile_pill'>
|
2020-01-07 06:51:23 +03:00
|
|
|
{avatar}
|
2020-01-16 23:42:34 +03:00
|
|
|
<span className='mx_InviteDialog_userTile_name'>{this.props.member.name}</span>
|
2020-01-07 06:51:23 +03:00
|
|
|
</span>
|
|
|
|
<AccessibleButton
|
2020-01-16 23:42:34 +03:00
|
|
|
className='mx_InviteDialog_userTile_remove'
|
2020-01-07 06:51:23 +03:00
|
|
|
onClick={this._onRemove}
|
|
|
|
>
|
|
|
|
<img src={require("../../../../res/img/icon-pill-remove.svg")} alt={_t('Remove')} width={8} height={8} />
|
|
|
|
</AccessibleButton>
|
|
|
|
</span>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-01-03 20:24:07 +03:00
|
|
|
class DMRoomTile extends React.PureComponent {
|
2020-01-03 03:40:18 +03:00
|
|
|
static propTypes = {
|
2020-01-07 06:51:23 +03:00
|
|
|
member: PropTypes.object.isRequired, // Should be a Member (see interface above)
|
2020-01-03 03:40:18 +03:00
|
|
|
lastActiveTs: PropTypes.number,
|
2020-01-07 06:51:23 +03:00
|
|
|
onToggle: PropTypes.func.isRequired, // takes 1 argument, the member being toggled
|
2020-01-04 06:35:30 +03:00
|
|
|
highlightWord: PropTypes.string,
|
2020-01-07 22:12:31 +03:00
|
|
|
isSelected: PropTypes.bool,
|
2020-01-03 03:40:18 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
_onClick = (e) => {
|
|
|
|
// Stop the browser from highlighting text
|
|
|
|
e.preventDefault();
|
|
|
|
e.stopPropagation();
|
|
|
|
|
2020-01-07 06:51:23 +03:00
|
|
|
this.props.onToggle(this.props.member);
|
2020-01-03 03:40:18 +03:00
|
|
|
};
|
|
|
|
|
2020-01-04 06:35:30 +03:00
|
|
|
_highlightName(str: string) {
|
|
|
|
if (!this.props.highlightWord) return str;
|
|
|
|
|
|
|
|
// We convert things to lowercase for index searching, but pull substrings from
|
2020-01-06 22:21:59 +03:00
|
|
|
// the submitted text to preserve case. Note: we don't need to htmlEntities the
|
|
|
|
// string because React will safely encode the text for us.
|
2020-01-04 06:35:30 +03:00
|
|
|
const lowerStr = str.toLowerCase();
|
|
|
|
const filterStr = this.props.highlightWord.toLowerCase();
|
|
|
|
|
|
|
|
const result = [];
|
|
|
|
|
|
|
|
let i = 0;
|
|
|
|
let ii;
|
|
|
|
while ((ii = lowerStr.indexOf(filterStr, i)) >= 0) {
|
|
|
|
// Push any text we missed (first bit/middle of text)
|
|
|
|
if (ii > i) {
|
2020-01-06 22:21:59 +03:00
|
|
|
// Push any text we aren't highlighting (middle of text match, or beginning of text)
|
|
|
|
result.push(<span key={i + 'begin'}>{str.substring(i, ii)}</span>);
|
2020-01-04 06:35:30 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
i = ii; // copy over ii only if we have a match (to preserve i for end-of-text matching)
|
|
|
|
|
|
|
|
// Highlight the word the user entered
|
|
|
|
const substr = str.substring(i, filterStr.length + i);
|
2020-01-16 23:42:34 +03:00
|
|
|
result.push(<span className='mx_InviteDialog_roomTile_highlight' key={i + 'bold'}>{substr}</span>);
|
2020-01-04 06:35:30 +03:00
|
|
|
i += substr.length;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Push any text we missed (end of text)
|
|
|
|
if (i < (str.length - 1)) {
|
|
|
|
result.push(<span key={i + 'end'}>{str.substring(i)}</span>);
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2020-01-03 03:40:18 +03:00
|
|
|
render() {
|
2020-01-04 07:17:48 +03:00
|
|
|
const BaseAvatar = sdk.getComponent("views.avatars.BaseAvatar");
|
2020-01-03 03:40:18 +03:00
|
|
|
|
|
|
|
let timestamp = null;
|
|
|
|
if (this.props.lastActiveTs) {
|
2020-01-14 07:20:01 +03:00
|
|
|
const humanTs = humanizeTime(this.props.lastActiveTs);
|
2020-01-16 23:42:34 +03:00
|
|
|
timestamp = <span className='mx_InviteDialog_roomTile_time'>{humanTs}</span>;
|
2020-01-03 03:40:18 +03:00
|
|
|
}
|
|
|
|
|
2020-01-04 07:17:48 +03:00
|
|
|
const avatarSize = 36;
|
2020-01-07 22:20:02 +03:00
|
|
|
const avatar = this.props.member.isEmail
|
2020-01-07 06:51:23 +03:00
|
|
|
? <img
|
|
|
|
src={require("../../../../res/img/icon-email-pill-avatar.svg")}
|
|
|
|
width={avatarSize} height={avatarSize} />
|
|
|
|
: <BaseAvatar
|
|
|
|
url={getHttpUriForMxc(
|
|
|
|
MatrixClientPeg.get().getHomeserverUrl(), this.props.member.getMxcAvatarUrl(),
|
|
|
|
avatarSize, avatarSize, "crop")}
|
|
|
|
name={this.props.member.name}
|
|
|
|
idName={this.props.member.userId}
|
|
|
|
width={avatarSize}
|
|
|
|
height={avatarSize} />;
|
2020-01-04 07:17:48 +03:00
|
|
|
|
2020-01-07 22:12:31 +03:00
|
|
|
let checkmark = null;
|
|
|
|
if (this.props.isSelected) {
|
|
|
|
// To reduce flickering we put the 'selected' room tile above the real avatar
|
2020-01-16 23:42:34 +03:00
|
|
|
checkmark = <div className='mx_InviteDialog_roomTile_selected' />;
|
2020-01-07 22:12:31 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// To reduce flickering we put the checkmark on top of the actual avatar (prevents
|
|
|
|
// the browser from reloading the image source when the avatar remounts).
|
2020-01-07 22:20:02 +03:00
|
|
|
const stackedAvatar = (
|
2020-01-16 23:42:34 +03:00
|
|
|
<span className='mx_InviteDialog_roomTile_avatarStack'>
|
2020-01-07 22:12:31 +03:00
|
|
|
{avatar}
|
|
|
|
{checkmark}
|
|
|
|
</span>
|
|
|
|
);
|
|
|
|
|
2020-01-03 03:40:18 +03:00
|
|
|
return (
|
2020-01-16 23:42:34 +03:00
|
|
|
<div className='mx_InviteDialog_roomTile' onClick={this._onClick}>
|
2020-01-07 22:12:31 +03:00
|
|
|
{stackedAvatar}
|
2020-01-16 23:42:34 +03:00
|
|
|
<span className='mx_InviteDialog_roomTile_name'>{this._highlightName(this.props.member.name)}</span>
|
|
|
|
<span className='mx_InviteDialog_roomTile_userId'>{this._highlightName(this.props.member.userId)}</span>
|
2020-01-03 03:40:18 +03:00
|
|
|
{timestamp}
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-01-16 23:42:34 +03:00
|
|
|
export default class InviteDialog extends React.PureComponent {
|
2020-01-03 03:40:18 +03:00
|
|
|
static propTypes = {
|
|
|
|
// Takes an array of user IDs/emails to invite.
|
|
|
|
onFinished: PropTypes.func.isRequired,
|
2020-01-17 00:40:12 +03:00
|
|
|
|
|
|
|
// The kind of invite being performed. Assumed to be KIND_DM if
|
|
|
|
// not provided.
|
|
|
|
kind: PropTypes.string,
|
|
|
|
|
|
|
|
// The room ID this dialog is for. Only required for KIND_INVITE.
|
|
|
|
roomId: PropTypes.string,
|
|
|
|
};
|
|
|
|
|
|
|
|
static defaultProps = {
|
|
|
|
kind: KIND_DM,
|
2020-01-03 03:40:18 +03:00
|
|
|
};
|
|
|
|
|
2020-01-04 07:17:48 +03:00
|
|
|
_debounceTimer: number = null;
|
2020-01-07 06:51:23 +03:00
|
|
|
_editorRef: any = null;
|
2020-01-04 07:17:48 +03:00
|
|
|
|
2020-01-17 00:40:12 +03:00
|
|
|
constructor(props) {
|
|
|
|
super(props);
|
|
|
|
|
|
|
|
if (props.kind === KIND_INVITE && !props.roomId) {
|
|
|
|
throw new Error("When using KIND_INVITE a roomId is required for an InviteDialog");
|
|
|
|
}
|
2020-01-03 03:40:18 +03:00
|
|
|
|
2020-01-30 22:27:59 +03:00
|
|
|
const alreadyInvited = new Set([MatrixClientPeg.get().getUserId(), SdkConfig.get()['welcomeUserId']]);
|
2020-01-17 00:40:25 +03:00
|
|
|
if (props.roomId) {
|
|
|
|
const room = MatrixClientPeg.get().getRoom(props.roomId);
|
|
|
|
if (!room) throw new Error("Room ID given to InviteDialog does not look like a room");
|
2020-01-30 22:27:59 +03:00
|
|
|
room.getMembersWithMembership('invite').forEach(m => alreadyInvited.add(m.userId));
|
|
|
|
room.getMembersWithMembership('join').forEach(m => alreadyInvited.add(m.userId));
|
|
|
|
// add banned users, so we don't try to invite them
|
|
|
|
room.getMembersWithMembership('ban').forEach(m => alreadyInvited.add(m.userId));
|
2020-01-17 00:40:25 +03:00
|
|
|
}
|
|
|
|
|
2020-01-03 03:40:18 +03:00
|
|
|
this.state = {
|
2020-01-07 06:51:23 +03:00
|
|
|
targets: [], // array of Member objects (see interface above)
|
2020-01-03 03:40:18 +03:00
|
|
|
filterText: "",
|
2020-01-17 00:40:25 +03:00
|
|
|
recents: this._buildRecents(alreadyInvited),
|
2020-01-03 03:40:18 +03:00
|
|
|
numRecentsShown: INITIAL_ROOMS_SHOWN,
|
2020-01-17 00:40:25 +03:00
|
|
|
suggestions: this._buildSuggestions(alreadyInvited),
|
2020-01-04 05:41:06 +03:00
|
|
|
numSuggestionsShown: INITIAL_ROOMS_SHOWN,
|
2020-01-04 07:17:48 +03:00
|
|
|
serverResultsMixin: [], // { user: DirectoryMember, userId: string }[], like recents and suggestions
|
2020-01-09 06:49:29 +03:00
|
|
|
threepidResultsMixin: [], // { user: ThreepidMember, userId: string}[], like recents and suggestions
|
|
|
|
canUseIdentityServer: !!MatrixClientPeg.get().getIdentityServerUrl(),
|
|
|
|
tryingIdentityServer: false,
|
2020-01-15 09:32:00 +03:00
|
|
|
|
|
|
|
// These two flags are used for the 'Go' button to communicate what is going on.
|
2020-01-15 09:35:45 +03:00
|
|
|
busy: false,
|
|
|
|
errorText: null,
|
2020-01-03 03:40:18 +03:00
|
|
|
};
|
2020-01-07 06:51:23 +03:00
|
|
|
|
|
|
|
this._editorRef = createRef();
|
2020-01-03 03:40:18 +03:00
|
|
|
}
|
|
|
|
|
2020-01-30 22:27:59 +03:00
|
|
|
_buildRecents(excludedTargetIds: Set<string>): {userId: string, user: RoomMember, lastActive: number} {
|
2020-02-25 05:43:11 +03:00
|
|
|
const rooms = DMRoomMap.shared().getUniqueRoomsWithIndividuals(); // map of userId => js-sdk Room
|
|
|
|
|
2020-02-25 19:19:37 +03:00
|
|
|
// Also pull in all the rooms tagged as TAG_DM so we don't miss anything. Sometimes the
|
2020-02-25 05:43:11 +03:00
|
|
|
// room list doesn't tag the room for the DMRoomMap, but does for the room list.
|
|
|
|
const taggedRooms = RoomListStore.getRoomLists();
|
|
|
|
const dmTaggedRooms = taggedRooms[TAG_DM];
|
|
|
|
const myUserId = MatrixClientPeg.get().getUserId();
|
|
|
|
for (const dmRoom of dmTaggedRooms) {
|
|
|
|
const otherMembers = dmRoom.getJoinedMembers().filter(u => u.userId !== myUserId);
|
|
|
|
for (const member of otherMembers) {
|
|
|
|
if (rooms[member.userId]) continue; // already have a room
|
|
|
|
|
|
|
|
console.warn(`Adding DM room for ${member.userId} as ${dmRoom.roomId} from tag, not DM map`);
|
|
|
|
rooms[member.userId] = dmRoom;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-01-03 03:40:18 +03:00
|
|
|
const recents = [];
|
|
|
|
for (const userId in rooms) {
|
2020-01-17 00:40:25 +03:00
|
|
|
// Filter out user IDs that are already in the room / should be excluded
|
2020-01-30 22:27:59 +03:00
|
|
|
if (excludedTargetIds.has(userId)) {
|
2020-01-24 03:35:36 +03:00
|
|
|
console.warn(`[Invite:Recents] Excluding ${userId} from recents`);
|
|
|
|
continue;
|
|
|
|
}
|
2020-01-17 00:40:25 +03:00
|
|
|
|
2020-01-03 03:40:18 +03:00
|
|
|
const room = rooms[userId];
|
|
|
|
const member = room.getMember(userId);
|
2020-01-24 03:35:36 +03:00
|
|
|
if (!member) {
|
|
|
|
// just skip people who don't have memberships for some reason
|
|
|
|
console.warn(`[Invite:Recents] ${userId} is missing a member object in their own DM (${room.roomId})`);
|
|
|
|
continue;
|
|
|
|
}
|
2020-01-03 03:40:18 +03:00
|
|
|
|
2020-01-29 14:14:33 +03:00
|
|
|
// Find the last timestamp for a message event
|
|
|
|
const searchTypes = ["m.room.message", "m.room.encrypted", "m.sticker"];
|
|
|
|
const maxSearchEvents = 20; // to prevent traversing history
|
|
|
|
let lastEventTs = 0;
|
|
|
|
if (room.timeline && room.timeline.length) {
|
|
|
|
for (let i = room.timeline.length - 1; i >= 0; i--) {
|
|
|
|
const ev = room.timeline[i];
|
|
|
|
if (searchTypes.includes(ev.getType())) {
|
|
|
|
lastEventTs = ev.getTs();
|
2020-01-29 14:23:48 +03:00
|
|
|
break;
|
2020-01-29 14:14:33 +03:00
|
|
|
}
|
|
|
|
if (room.timeline.length - i > maxSearchEvents) break;
|
|
|
|
}
|
|
|
|
}
|
2020-01-24 03:35:36 +03:00
|
|
|
if (!lastEventTs) {
|
|
|
|
// something weird is going on with this room
|
|
|
|
console.warn(`[Invite:Recents] ${userId} (${room.roomId}) has a weird last timestamp: ${lastEventTs}`);
|
|
|
|
continue;
|
|
|
|
}
|
2020-01-03 03:40:18 +03:00
|
|
|
|
|
|
|
recents.push({userId, user: member, lastActive: lastEventTs});
|
|
|
|
}
|
2020-01-24 03:35:36 +03:00
|
|
|
if (!recents) console.warn("[Invite:Recents] No recents to suggest!");
|
2020-01-03 03:40:18 +03:00
|
|
|
|
|
|
|
// Sort the recents by last active to save us time later
|
|
|
|
recents.sort((a, b) => b.lastActive - a.lastActive);
|
|
|
|
|
|
|
|
return recents;
|
|
|
|
}
|
|
|
|
|
2020-01-30 22:27:59 +03:00
|
|
|
_buildSuggestions(excludedTargetIds: Set<string>): {userId: string, user: RoomMember} {
|
2020-01-04 05:41:06 +03:00
|
|
|
const maxConsideredMembers = 200;
|
2020-01-31 00:01:55 +03:00
|
|
|
const joinedRooms = MatrixClientPeg.get().getRooms()
|
|
|
|
.filter(r => r.getMyMembership() === 'join' && r.getJoinedMemberCount() <= maxConsideredMembers);
|
2020-01-06 22:23:35 +03:00
|
|
|
|
|
|
|
// Generates { userId: {member, rooms[]} }
|
2020-01-04 05:41:06 +03:00
|
|
|
const memberRooms = joinedRooms.reduce((members, room) => {
|
2020-01-14 07:04:13 +03:00
|
|
|
// Filter out DMs (we'll handle these in the recents section)
|
2020-01-14 07:21:18 +03:00
|
|
|
if (DMRoomMap.shared().getUserIdForRoomId(room.roomId)) {
|
2020-01-14 07:04:13 +03:00
|
|
|
return members; // Do nothing
|
|
|
|
}
|
|
|
|
|
2020-01-30 22:27:59 +03:00
|
|
|
const joinedMembers = room.getJoinedMembers().filter(u => !excludedTargetIds.has(u.userId));
|
2020-01-04 05:41:06 +03:00
|
|
|
for (const member of joinedMembers) {
|
2020-01-17 00:40:25 +03:00
|
|
|
// Filter out user IDs that are already in the room / should be excluded
|
2020-01-30 22:27:59 +03:00
|
|
|
if (excludedTargetIds.has(member.userId)) {
|
2020-01-17 00:40:25 +03:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2020-01-04 05:41:06 +03:00
|
|
|
if (!members[member.userId]) {
|
|
|
|
members[member.userId] = {
|
|
|
|
member: member,
|
|
|
|
// Track the room size of the 'picked' member so we can use the profile of
|
|
|
|
// the smallest room (likely a DM).
|
|
|
|
pickedMemberRoomSize: room.getJoinedMemberCount(),
|
|
|
|
rooms: [],
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
members[member.userId].rooms.push(room);
|
|
|
|
|
|
|
|
if (room.getJoinedMemberCount() < members[member.userId].pickedMemberRoomSize) {
|
|
|
|
members[member.userId].member = member;
|
|
|
|
members[member.userId].pickedMemberRoomSize = room.getJoinedMemberCount();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return members;
|
2020-01-06 22:23:35 +03:00
|
|
|
}, {});
|
|
|
|
|
|
|
|
// Generates { userId: {member, numRooms, score} }
|
2020-01-04 05:41:06 +03:00
|
|
|
const memberScores = Object.values(memberRooms).reduce((scores, entry) => {
|
|
|
|
const numMembersTotal = entry.rooms.reduce((c, r) => c + r.getJoinedMemberCount(), 0);
|
|
|
|
const maxRange = maxConsideredMembers * entry.rooms.length;
|
|
|
|
scores[entry.member.userId] = {
|
|
|
|
member: entry.member,
|
|
|
|
numRooms: entry.rooms.length,
|
|
|
|
score: Math.max(0, Math.pow(1 - (numMembersTotal / maxRange), 5)),
|
|
|
|
};
|
|
|
|
return scores;
|
2020-01-06 22:23:35 +03:00
|
|
|
}, {});
|
|
|
|
|
2020-01-18 00:36:23 +03:00
|
|
|
// Now that we have scores for being in rooms, boost those people who have sent messages
|
|
|
|
// recently, as a way to improve the quality of suggestions. We do this by checking every
|
|
|
|
// room to see who has sent a message in the last few hours, and giving them a score
|
|
|
|
// which correlates to the freshness of their message. In theory, this results in suggestions
|
|
|
|
// which are closer to "continue this conversation" rather than "this person exists".
|
2020-01-31 00:07:32 +03:00
|
|
|
const trueJoinedRooms = MatrixClientPeg.get().getRooms().filter(r => r.getMyMembership() === 'join');
|
2020-01-18 00:36:23 +03:00
|
|
|
const now = (new Date()).getTime();
|
2020-01-20 19:29:33 +03:00
|
|
|
const earliestAgeConsidered = now - (60 * 60 * 1000); // 1 hour ago
|
2020-01-18 00:36:23 +03:00
|
|
|
const maxMessagesConsidered = 50; // so we don't iterate over a huge amount of traffic
|
|
|
|
const lastSpoke = {}; // userId: timestamp
|
|
|
|
const lastSpokeMembers = {}; // userId: room member
|
|
|
|
for (const room of trueJoinedRooms) {
|
|
|
|
// Skip low priority rooms and DMs
|
2020-01-18 00:40:33 +03:00
|
|
|
const isDm = DMRoomMap.shared().getUserIdForRoomId(room.roomId);
|
|
|
|
if (Object.keys(room.tags).includes("m.lowpriority") || isDm) {
|
2020-01-18 00:36:23 +03:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
const events = room.getLiveTimeline().getEvents(); // timelines are most recent last
|
|
|
|
for (let i = events.length - 1; i >= Math.max(0, events.length - maxMessagesConsidered); i--) {
|
|
|
|
const ev = events[i];
|
2020-01-30 22:27:59 +03:00
|
|
|
if (excludedTargetIds.has(ev.getSender())) {
|
2020-01-18 00:36:23 +03:00
|
|
|
continue;
|
|
|
|
}
|
2020-01-20 19:29:33 +03:00
|
|
|
if (ev.getTs() <= earliestAgeConsidered) {
|
2020-01-18 00:36:23 +03:00
|
|
|
break; // give up: all events from here on out are too old
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!lastSpoke[ev.getSender()] || lastSpoke[ev.getSender()] < ev.getTs()) {
|
|
|
|
lastSpoke[ev.getSender()] = ev.getTs();
|
|
|
|
lastSpokeMembers[ev.getSender()] = room.getMember(ev.getSender());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for (const userId in lastSpoke) {
|
|
|
|
const ts = lastSpoke[userId];
|
|
|
|
const member = lastSpokeMembers[userId];
|
|
|
|
if (!member) continue; // skip people we somehow don't have profiles for
|
|
|
|
|
|
|
|
// Scores from being in a room give a 'good' score of about 1.0-1.5, so for our
|
|
|
|
// boost we'll try and award at least +1.0 for making the list, with +4.0 being
|
|
|
|
// an approximate maximum for being selected.
|
|
|
|
const distanceFromNow = Math.abs(now - ts); // abs to account for slight future messages
|
2020-01-20 19:29:33 +03:00
|
|
|
const inverseTime = (now - earliestAgeConsidered) - distanceFromNow;
|
2020-01-18 00:36:23 +03:00
|
|
|
const scoreBoost = Math.max(1, inverseTime / (15 * 60 * 1000)); // 15min segments to keep scores sane
|
|
|
|
|
|
|
|
let record = memberScores[userId];
|
|
|
|
if (!record) record = memberScores[userId] = {score: 0};
|
|
|
|
record.member = member;
|
|
|
|
record.score += scoreBoost;
|
|
|
|
}
|
|
|
|
|
2020-01-04 05:41:06 +03:00
|
|
|
const members = Object.values(memberScores);
|
|
|
|
members.sort((a, b) => {
|
|
|
|
if (a.score === b.score) {
|
|
|
|
if (a.numRooms === b.numRooms) {
|
|
|
|
return a.member.userId.localeCompare(b.member.userId);
|
|
|
|
}
|
|
|
|
|
|
|
|
return b.numRooms - a.numRooms;
|
|
|
|
}
|
|
|
|
return b.score - a.score;
|
|
|
|
});
|
2020-01-14 07:04:13 +03:00
|
|
|
|
2020-01-04 06:35:30 +03:00
|
|
|
return members.map(m => ({userId: m.member.userId, user: m.member}));
|
2020-01-04 05:41:06 +03:00
|
|
|
}
|
|
|
|
|
2020-01-17 00:40:12 +03:00
|
|
|
_shouldAbortAfterInviteError(result): boolean {
|
|
|
|
const failedUsers = Object.keys(result.states).filter(a => result.states[a] === 'error');
|
|
|
|
if (failedUsers.length > 0) {
|
|
|
|
console.log("Failed to invite users: ", result);
|
|
|
|
this.setState({
|
|
|
|
busy: false,
|
|
|
|
errorText: _t("Failed to invite the following users to chat: %(csvUsers)s", {
|
|
|
|
csvUsers: failedUsers.join(", "),
|
|
|
|
}),
|
|
|
|
});
|
|
|
|
return true; // abort
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2020-02-21 09:10:43 +03:00
|
|
|
_convertFilter(): Member[] {
|
2020-02-21 09:16:21 +03:00
|
|
|
// Check to see if there's anything to convert first
|
|
|
|
if (!this.state.filterText || !this.state.filterText.includes('@')) return this.state.targets || [];
|
2020-02-21 09:10:43 +03:00
|
|
|
|
|
|
|
let newMember: Member;
|
|
|
|
if (this.state.filterText.startsWith('@')) {
|
|
|
|
// Assume mxid
|
|
|
|
newMember = new DirectoryMember({user_id: this.state.filterText, display_name: null, avatar_url: null});
|
|
|
|
} else {
|
|
|
|
// Assume email
|
|
|
|
newMember = new ThreepidMember(this.state.filterText);
|
|
|
|
}
|
2020-02-21 09:16:21 +03:00
|
|
|
const newTargets = [...(this.state.targets || []), newMember];
|
2020-02-21 09:10:43 +03:00
|
|
|
this.setState({targets: newTargets, filterText: ''});
|
|
|
|
return newTargets;
|
|
|
|
}
|
|
|
|
|
2020-01-23 19:00:55 +03:00
|
|
|
_startDm = async () => {
|
2020-01-15 09:32:00 +03:00
|
|
|
this.setState({busy: true});
|
2020-02-21 09:10:43 +03:00
|
|
|
const targets = this._convertFilter();
|
|
|
|
const targetIds = targets.map(t => t.userId);
|
2020-01-15 09:32:00 +03:00
|
|
|
|
|
|
|
// Check if there is already a DM with these people and reuse it if possible.
|
|
|
|
const existingRoom = DMRoomMap.shared().getDMRoomForIdentifiers(targetIds);
|
|
|
|
if (existingRoom) {
|
|
|
|
dis.dispatch({
|
|
|
|
action: 'view_room',
|
|
|
|
room_id: existingRoom.roomId,
|
|
|
|
should_peek: false,
|
|
|
|
joining: false,
|
|
|
|
});
|
|
|
|
this.props.onFinished();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-02-28 03:10:31 +03:00
|
|
|
const createRoomOptions = {inlineErrors: true};
|
2020-01-23 19:00:55 +03:00
|
|
|
|
|
|
|
if (SettingsStore.isFeatureEnabled("feature_cross_signing")) {
|
|
|
|
// Check whether all users have uploaded device keys before.
|
|
|
|
// If so, enable encryption in the new room.
|
|
|
|
const client = MatrixClientPeg.get();
|
2020-02-18 14:30:10 +03:00
|
|
|
const allHaveDeviceKeys = await canEncryptToAllUsers(client, targetIds);
|
2020-01-23 19:00:55 +03:00
|
|
|
if (allHaveDeviceKeys) {
|
|
|
|
createRoomOptions.encryption = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-01-15 09:32:00 +03:00
|
|
|
// Check if it's a traditional DM and create the room if required.
|
|
|
|
// TODO: [Canonical DMs] Remove this check and instead just create the multi-person DM
|
|
|
|
let createRoomPromise = Promise.resolve();
|
2020-02-21 09:16:21 +03:00
|
|
|
const isSelf = targetIds.length === 1 && targetIds[0] === MatrixClientPeg.get().getUserId();
|
|
|
|
if (targetIds.length === 1 && !isSelf) {
|
2020-01-23 19:00:55 +03:00
|
|
|
createRoomOptions.dmUserId = targetIds[0];
|
|
|
|
createRoomPromise = createRoom(createRoomOptions);
|
2020-02-21 09:16:21 +03:00
|
|
|
} else if (isSelf) {
|
|
|
|
createRoomPromise = createRoom(createRoomOptions);
|
2020-01-15 09:32:00 +03:00
|
|
|
} else {
|
|
|
|
// Create a boring room and try to invite the targets manually.
|
2020-01-23 19:00:55 +03:00
|
|
|
createRoomPromise = createRoom(createRoomOptions).then(roomId => {
|
2020-01-15 09:32:00 +03:00
|
|
|
return inviteMultipleToRoom(roomId, targetIds);
|
|
|
|
}).then(result => {
|
2020-01-17 00:40:12 +03:00
|
|
|
if (this._shouldAbortAfterInviteError(result)) {
|
2020-01-15 09:32:00 +03:00
|
|
|
return true; // abort
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
// the createRoom call will show the room for us, so we don't need to worry about that.
|
|
|
|
createRoomPromise.then(abort => {
|
|
|
|
if (abort === true) return; // only abort on true booleans, not roomIds or something
|
|
|
|
this.props.onFinished();
|
|
|
|
}).catch(err => {
|
|
|
|
console.error(err);
|
|
|
|
this.setState({
|
|
|
|
busy: false,
|
|
|
|
errorText: _t("We couldn't create your DM. Please check the users you want to invite and try again."),
|
|
|
|
});
|
|
|
|
});
|
2020-01-03 03:40:18 +03:00
|
|
|
};
|
|
|
|
|
2020-01-17 00:40:12 +03:00
|
|
|
_inviteUsers = () => {
|
|
|
|
this.setState({busy: true});
|
2020-02-21 09:10:43 +03:00
|
|
|
this._convertFilter();
|
|
|
|
const targets = this._convertFilter();
|
|
|
|
const targetIds = targets.map(t => t.userId);
|
2020-01-17 00:40:12 +03:00
|
|
|
|
|
|
|
const room = MatrixClientPeg.get().getRoom(this.props.roomId);
|
|
|
|
if (!room) {
|
|
|
|
console.error("Failed to find the room to invite users to");
|
|
|
|
this.setState({
|
|
|
|
busy: false,
|
|
|
|
errorText: _t("Something went wrong trying to invite the users."),
|
|
|
|
});
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
inviteMultipleToRoom(this.props.roomId, targetIds).then(result => {
|
|
|
|
if (!this._shouldAbortAfterInviteError(result)) { // handles setting error message too
|
|
|
|
this.props.onFinished();
|
|
|
|
}
|
|
|
|
}).catch(err => {
|
|
|
|
console.error(err);
|
|
|
|
this.setState({
|
|
|
|
busy: false,
|
2020-01-17 00:45:17 +03:00
|
|
|
errorText: _t(
|
|
|
|
"We couldn't invite those users. Please check the users you want to invite and try again.",
|
|
|
|
),
|
2020-01-17 00:40:12 +03:00
|
|
|
});
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2020-01-03 03:40:18 +03:00
|
|
|
_cancel = () => {
|
2020-01-15 09:32:00 +03:00
|
|
|
// We do not want the user to close the dialog while an action is in progress
|
|
|
|
if (this.state.busy) return;
|
|
|
|
|
|
|
|
this.props.onFinished();
|
2020-01-03 03:40:18 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
_updateFilter = (e) => {
|
2020-01-04 07:17:48 +03:00
|
|
|
const term = e.target.value;
|
|
|
|
this.setState({filterText: term});
|
|
|
|
|
|
|
|
// Debounce server lookups to reduce spam. We don't clear the existing server
|
|
|
|
// results because they might still be vaguely accurate, likewise for races which
|
|
|
|
// could happen here.
|
|
|
|
if (this._debounceTimer) {
|
|
|
|
clearTimeout(this._debounceTimer);
|
|
|
|
}
|
2020-01-09 06:49:29 +03:00
|
|
|
this._debounceTimer = setTimeout(async () => {
|
2020-01-23 08:33:50 +03:00
|
|
|
MatrixClientPeg.get().searchUserDirectory({term}).then(async r => {
|
2020-01-04 07:17:48 +03:00
|
|
|
if (term !== this.state.filterText) {
|
|
|
|
// Discard the results - we were probably too slow on the server-side to make
|
|
|
|
// these results useful. This is a race we want to avoid because we could overwrite
|
|
|
|
// more accurate results.
|
|
|
|
return;
|
|
|
|
}
|
2020-01-23 08:33:50 +03:00
|
|
|
|
|
|
|
if (!r.results) r.results = [];
|
|
|
|
|
|
|
|
// While we're here, try and autocomplete a search result for the mxid itself
|
|
|
|
// if there's no matches (and the input looks like a mxid).
|
2020-02-21 09:01:50 +03:00
|
|
|
if (term[0] === '@' && term.indexOf(':') > 1) {
|
2020-01-23 08:33:50 +03:00
|
|
|
try {
|
|
|
|
const profile = await MatrixClientPeg.get().getProfileInfo(term);
|
|
|
|
if (profile) {
|
|
|
|
// If we have a profile, we have enough information to assume that
|
2020-02-21 09:01:50 +03:00
|
|
|
// the mxid can be invited - add it to the list. We stick it at the
|
|
|
|
// top so it is most obviously presented to the user.
|
|
|
|
r.results.splice(0, 0, {
|
2020-01-23 08:33:50 +03:00
|
|
|
user_id: term,
|
|
|
|
display_name: profile['displayname'],
|
|
|
|
avatar_url: profile['avatar_url'],
|
|
|
|
});
|
|
|
|
}
|
|
|
|
} catch (e) {
|
|
|
|
console.warn("Non-fatal error trying to make an invite for a user ID");
|
|
|
|
console.warn(e);
|
2020-02-20 20:50:35 +03:00
|
|
|
|
2020-02-21 09:01:50 +03:00
|
|
|
// Add a result anyways, just without a profile. We stick it at the
|
|
|
|
// top so it is most obviously presented to the user.
|
2020-02-21 09:21:35 +03:00
|
|
|
r.results.splice(0, 0, {
|
2020-02-20 20:50:35 +03:00
|
|
|
user_id: term,
|
|
|
|
display_name: term,
|
|
|
|
avatar_url: null,
|
|
|
|
});
|
2020-01-23 08:33:50 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-01-04 07:17:48 +03:00
|
|
|
this.setState({
|
|
|
|
serverResultsMixin: r.results.map(u => ({
|
|
|
|
userId: u.user_id,
|
|
|
|
user: new DirectoryMember(u),
|
|
|
|
})),
|
|
|
|
});
|
|
|
|
}).catch(e => {
|
|
|
|
console.error("Error searching user directory:");
|
|
|
|
console.error(e);
|
|
|
|
this.setState({serverResultsMixin: []}); // clear results because it's moderately fatal
|
|
|
|
});
|
2020-01-09 06:49:29 +03:00
|
|
|
|
|
|
|
// Whenever we search the directory, also try to search the identity server. It's
|
|
|
|
// all debounced the same anyways.
|
|
|
|
if (!this.state.canUseIdentityServer) {
|
|
|
|
// The user doesn't have an identity server set - warn them of that.
|
|
|
|
this.setState({tryingIdentityServer: true});
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (term.indexOf('@') > 0 && Email.looksValid(term)) {
|
|
|
|
// Start off by suggesting the plain email while we try and resolve it
|
|
|
|
// to a real account.
|
|
|
|
this.setState({
|
|
|
|
// per above: the userId is a lie here - it's just a regular identifier
|
|
|
|
threepidResultsMixin: [{user: new ThreepidMember(term), userId: term}],
|
|
|
|
});
|
|
|
|
try {
|
|
|
|
const authClient = new IdentityAuthClient();
|
|
|
|
const token = await authClient.getAccessToken();
|
|
|
|
if (term !== this.state.filterText) return; // abandon hope
|
|
|
|
|
|
|
|
const lookup = await MatrixClientPeg.get().lookupThreePid(
|
|
|
|
'email',
|
|
|
|
term,
|
|
|
|
undefined, // callback
|
|
|
|
token,
|
|
|
|
);
|
|
|
|
if (term !== this.state.filterText) return; // abandon hope
|
|
|
|
|
|
|
|
if (!lookup || !lookup.mxid) {
|
|
|
|
// We weren't able to find anyone - we're already suggesting the plain email
|
|
|
|
// as an alternative, so do nothing.
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// We append the user suggestion to give the user an option to click
|
|
|
|
// the email anyways, and so we don't cause things to jump around. In
|
|
|
|
// theory, the user would see the user pop up and think "ah yes, that
|
|
|
|
// person!"
|
|
|
|
const profile = await MatrixClientPeg.get().getProfileInfo(lookup.mxid);
|
|
|
|
if (term !== this.state.filterText || !profile) return; // abandon hope
|
|
|
|
this.setState({
|
|
|
|
threepidResultsMixin: [...this.state.threepidResultsMixin, {
|
|
|
|
user: new DirectoryMember({
|
|
|
|
user_id: lookup.mxid,
|
|
|
|
display_name: profile.displayname,
|
|
|
|
avatar_url: profile.avatar_url,
|
|
|
|
}),
|
|
|
|
userId: lookup.mxid,
|
|
|
|
}],
|
|
|
|
});
|
|
|
|
} catch (e) {
|
|
|
|
console.error("Error searching identity server:");
|
|
|
|
console.error(e);
|
|
|
|
this.setState({threepidResultsMixin: []}); // clear results because it's moderately fatal
|
|
|
|
}
|
|
|
|
}
|
2020-01-04 07:17:48 +03:00
|
|
|
}, 150); // 150ms debounce (human reaction time + some)
|
2020-01-03 03:40:18 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
_showMoreRecents = () => {
|
|
|
|
this.setState({numRecentsShown: this.state.numRecentsShown + INCREMENT_ROOMS_SHOWN});
|
|
|
|
};
|
|
|
|
|
2020-01-04 05:41:06 +03:00
|
|
|
_showMoreSuggestions = () => {
|
|
|
|
this.setState({numSuggestionsShown: this.state.numSuggestionsShown + INCREMENT_ROOMS_SHOWN});
|
|
|
|
};
|
|
|
|
|
2020-01-07 06:51:23 +03:00
|
|
|
_toggleMember = (member: Member) => {
|
2020-01-24 03:27:37 +03:00
|
|
|
let filterText = this.state.filterText;
|
2020-01-03 03:40:18 +03:00
|
|
|
const targets = this.state.targets.map(t => t); // cheap clone for mutation
|
2020-01-07 06:51:23 +03:00
|
|
|
const idx = targets.indexOf(member);
|
2020-01-24 03:27:37 +03:00
|
|
|
if (idx >= 0) {
|
|
|
|
targets.splice(idx, 1);
|
|
|
|
} else {
|
|
|
|
targets.push(member);
|
|
|
|
filterText = ""; // clear the filter when the user accepts a suggestion
|
|
|
|
}
|
|
|
|
this.setState({targets, filterText});
|
2020-01-03 03:40:18 +03:00
|
|
|
};
|
|
|
|
|
2020-01-07 06:51:23 +03:00
|
|
|
_removeMember = (member: Member) => {
|
|
|
|
const targets = this.state.targets.map(t => t); // cheap clone for mutation
|
|
|
|
const idx = targets.indexOf(member);
|
|
|
|
if (idx >= 0) {
|
|
|
|
targets.splice(idx, 1);
|
|
|
|
this.setState({targets});
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2020-01-09 07:19:00 +03:00
|
|
|
_onPaste = async (e) => {
|
2020-01-29 14:22:03 +03:00
|
|
|
if (this.state.filterText) {
|
|
|
|
// if the user has already typed something, just let them
|
|
|
|
// paste normally.
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-01-09 07:19:00 +03:00
|
|
|
// Prevent the text being pasted into the textarea
|
|
|
|
e.preventDefault();
|
|
|
|
|
|
|
|
// Process it as a list of addresses to add instead
|
|
|
|
const text = e.clipboardData.getData("text");
|
|
|
|
const possibleMembers = [
|
|
|
|
// If we can avoid hitting the profile endpoint, we should.
|
|
|
|
...this.state.recents,
|
|
|
|
...this.state.suggestions,
|
|
|
|
...this.state.serverResultsMixin,
|
|
|
|
...this.state.threepidResultsMixin,
|
|
|
|
];
|
|
|
|
const toAdd = [];
|
|
|
|
const failed = [];
|
2020-02-21 23:26:37 +03:00
|
|
|
const potentialAddresses = text.split(/[\s,]+/).map(p => p.trim()).filter(p => !!p); // filter empty strings
|
2020-01-09 07:19:00 +03:00
|
|
|
for (const address of potentialAddresses) {
|
|
|
|
const member = possibleMembers.find(m => m.userId === address);
|
|
|
|
if (member) {
|
|
|
|
toAdd.push(member.user);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (address.indexOf('@') > 0 && Email.looksValid(address)) {
|
|
|
|
toAdd.push(new ThreepidMember(address));
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (address[0] !== '@') {
|
|
|
|
failed.push(address); // not a user ID
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
|
|
|
const profile = await MatrixClientPeg.get().getProfileInfo(address);
|
|
|
|
const displayName = profile ? profile.displayname : null;
|
|
|
|
const avatarUrl = profile ? profile.avatar_url : null;
|
|
|
|
toAdd.push(new DirectoryMember({
|
|
|
|
user_id: address,
|
|
|
|
display_name: displayName,
|
|
|
|
avatar_url: avatarUrl,
|
|
|
|
}));
|
|
|
|
} catch (e) {
|
|
|
|
console.error("Error looking up profile for " + address);
|
|
|
|
console.error(e);
|
|
|
|
failed.push(address);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (failed.length > 0) {
|
|
|
|
const QuestionDialog = sdk.getComponent('dialogs.QuestionDialog');
|
|
|
|
Modal.createTrackedDialog('Invite Paste Fail', '', QuestionDialog, {
|
|
|
|
title: _t('Failed to find the following users'),
|
|
|
|
description: _t(
|
|
|
|
"The following users might not exist or are invalid, and cannot be invited: %(csvNames)s",
|
|
|
|
{csvNames: failed.join(", ")},
|
|
|
|
),
|
|
|
|
button: _t('OK'),
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
this.setState({targets: [...this.state.targets, ...toAdd]});
|
|
|
|
};
|
|
|
|
|
2020-01-07 06:51:23 +03:00
|
|
|
_onClickInputArea = (e) => {
|
|
|
|
// Stop the browser from highlighting text
|
|
|
|
e.preventDefault();
|
|
|
|
e.stopPropagation();
|
|
|
|
|
|
|
|
if (this._editorRef && this._editorRef.current) {
|
|
|
|
this._editorRef.current.focus();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2020-01-09 07:01:53 +03:00
|
|
|
_onUseDefaultIdentityServerClick = (e) => {
|
2020-01-09 06:49:29 +03:00
|
|
|
e.preventDefault();
|
|
|
|
|
|
|
|
// Update the IS in account data. Actually using it may trigger terms.
|
|
|
|
// eslint-disable-next-line react-hooks/rules-of-hooks
|
|
|
|
useDefaultIdentityServer();
|
|
|
|
this.setState({canUseIdentityServer: true, tryingIdentityServer: false});
|
2020-01-09 07:01:53 +03:00
|
|
|
};
|
2020-01-09 06:49:29 +03:00
|
|
|
|
2020-01-09 07:01:53 +03:00
|
|
|
_onManageSettingsClick = (e) => {
|
2020-01-09 06:49:29 +03:00
|
|
|
e.preventDefault();
|
|
|
|
dis.dispatch({ action: 'view_user_settings' });
|
|
|
|
this._cancel();
|
2020-01-09 07:01:53 +03:00
|
|
|
};
|
2020-01-09 06:49:29 +03:00
|
|
|
|
2020-01-04 05:41:06 +03:00
|
|
|
_renderSection(kind: "recents"|"suggestions") {
|
2020-01-04 06:35:30 +03:00
|
|
|
let sourceMembers = kind === 'recents' ? this.state.recents : this.state.suggestions;
|
2020-01-04 05:41:06 +03:00
|
|
|
let showNum = kind === 'recents' ? this.state.numRecentsShown : this.state.numSuggestionsShown;
|
|
|
|
const showMoreFn = kind === 'recents' ? this._showMoreRecents.bind(this) : this._showMoreSuggestions.bind(this);
|
|
|
|
const lastActive = (m) => kind === 'recents' ? m.lastActive : null;
|
2020-01-17 00:40:12 +03:00
|
|
|
let sectionName = kind === 'recents' ? _t("Recent Conversations") : _t("Suggestions");
|
|
|
|
|
|
|
|
if (this.props.kind === KIND_INVITE) {
|
|
|
|
sectionName = kind === 'recents' ? _t("Recently Direct Messaged") : _t("Suggestions");
|
|
|
|
}
|
2020-01-04 05:41:06 +03:00
|
|
|
|
2020-01-09 06:49:29 +03:00
|
|
|
// Mix in the server results if we have any, but only if we're searching. We track the additional
|
|
|
|
// members separately because we want to filter sourceMembers but trust the mixin arrays to have
|
|
|
|
// the right members in them.
|
|
|
|
let additionalMembers = [];
|
|
|
|
const hasMixins = this.state.serverResultsMixin || this.state.threepidResultsMixin;
|
|
|
|
if (this.state.filterText && hasMixins && kind === 'suggestions') {
|
|
|
|
// We don't want to duplicate members though, so just exclude anyone we've already seen.
|
2020-01-09 06:53:20 +03:00
|
|
|
const notAlreadyExists = (u: Member): boolean => {
|
2020-01-09 06:49:29 +03:00
|
|
|
return !sourceMembers.some(m => m.userId === u.userId)
|
|
|
|
&& !additionalMembers.some(m => m.userId === u.userId);
|
2020-01-09 06:53:20 +03:00
|
|
|
};
|
2020-01-09 06:49:29 +03:00
|
|
|
|
|
|
|
const uniqueServerResults = this.state.serverResultsMixin.filter(notAlreadyExists);
|
|
|
|
additionalMembers = additionalMembers.concat(...uniqueServerResults);
|
2020-01-04 07:17:48 +03:00
|
|
|
|
2020-01-09 06:49:29 +03:00
|
|
|
const uniqueThreepidResults = this.state.threepidResultsMixin.filter(notAlreadyExists);
|
|
|
|
additionalMembers = additionalMembers.concat(...uniqueThreepidResults);
|
2020-01-04 07:17:48 +03:00
|
|
|
}
|
|
|
|
|
2020-01-04 06:35:30 +03:00
|
|
|
// Hide the section if there's nothing to filter by
|
2020-01-09 06:49:29 +03:00
|
|
|
if (sourceMembers.length === 0 && additionalMembers.length === 0) return null;
|
2020-01-04 05:41:06 +03:00
|
|
|
|
2020-01-04 06:35:30 +03:00
|
|
|
// Do some simple filtering on the input before going much further. If we get no results, say so.
|
|
|
|
if (this.state.filterText) {
|
|
|
|
const filterBy = this.state.filterText.toLowerCase();
|
|
|
|
sourceMembers = sourceMembers
|
|
|
|
.filter(m => m.user.name.toLowerCase().includes(filterBy) || m.userId.toLowerCase().includes(filterBy));
|
|
|
|
|
2020-01-09 06:49:29 +03:00
|
|
|
if (sourceMembers.length === 0 && additionalMembers.length === 0) {
|
2020-01-04 06:35:30 +03:00
|
|
|
return (
|
2020-01-16 23:42:34 +03:00
|
|
|
<div className='mx_InviteDialog_section'>
|
2020-01-04 06:35:30 +03:00
|
|
|
<h3>{sectionName}</h3>
|
|
|
|
<p>{_t("No results")}</p>
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-01-09 06:49:29 +03:00
|
|
|
// Now we mix in the additional members. Again, we presume these have already been filtered. We
|
|
|
|
// also assume they are more relevant than our suggestions and prepend them to the list.
|
|
|
|
sourceMembers = [...additionalMembers, ...sourceMembers];
|
|
|
|
|
2020-01-04 05:41:06 +03:00
|
|
|
// If we're going to hide one member behind 'show more', just use up the space of the button
|
|
|
|
// with the member's tile instead.
|
|
|
|
if (showNum === sourceMembers.length - 1) showNum++;
|
2020-01-03 03:40:18 +03:00
|
|
|
|
|
|
|
// .slice() will return an incomplete array but won't error on us if we go too far
|
2020-01-04 05:41:06 +03:00
|
|
|
const toRender = sourceMembers.slice(0, showNum);
|
|
|
|
const hasMore = toRender.length < sourceMembers.length;
|
2020-01-03 03:40:18 +03:00
|
|
|
|
|
|
|
const AccessibleButton = sdk.getComponent("elements.AccessibleButton");
|
|
|
|
let showMore = null;
|
|
|
|
if (hasMore) {
|
|
|
|
showMore = (
|
2020-01-04 05:41:06 +03:00
|
|
|
<AccessibleButton onClick={showMoreFn} kind="link">
|
2020-01-03 03:40:18 +03:00
|
|
|
{_t("Show more")}
|
|
|
|
</AccessibleButton>
|
2020-01-03 03:44:19 +03:00
|
|
|
);
|
2020-01-03 03:40:18 +03:00
|
|
|
}
|
|
|
|
|
2020-01-03 03:44:19 +03:00
|
|
|
const tiles = toRender.map(r => (
|
2020-01-04 06:35:30 +03:00
|
|
|
<DMRoomTile
|
|
|
|
member={r.user}
|
|
|
|
lastActiveTs={lastActive(r)}
|
|
|
|
key={r.userId}
|
|
|
|
onToggle={this._toggleMember}
|
|
|
|
highlightWord={this.state.filterText}
|
2020-01-07 22:12:31 +03:00
|
|
|
isSelected={this.state.targets.some(t => t.userId === r.userId)}
|
2020-01-04 06:35:30 +03:00
|
|
|
/>
|
2020-01-03 03:44:19 +03:00
|
|
|
));
|
2020-01-03 03:40:18 +03:00
|
|
|
return (
|
2020-01-16 23:42:34 +03:00
|
|
|
<div className='mx_InviteDialog_section'>
|
2020-01-04 05:41:06 +03:00
|
|
|
<h3>{sectionName}</h3>
|
2020-01-03 03:44:19 +03:00
|
|
|
{tiles}
|
2020-01-03 03:40:18 +03:00
|
|
|
{showMore}
|
|
|
|
</div>
|
2020-01-03 03:44:19 +03:00
|
|
|
);
|
2020-01-03 03:40:18 +03:00
|
|
|
}
|
|
|
|
|
2020-01-07 06:51:23 +03:00
|
|
|
_renderEditor() {
|
|
|
|
const targets = this.state.targets.map(t => (
|
|
|
|
<DMUserTile member={t} onRemove={this._removeMember} key={t.userId} />
|
|
|
|
));
|
|
|
|
const input = (
|
|
|
|
<textarea
|
|
|
|
key={"input"}
|
|
|
|
rows={1}
|
|
|
|
onChange={this._updateFilter}
|
2020-01-24 03:27:37 +03:00
|
|
|
value={this.state.filterText}
|
2020-01-07 06:51:23 +03:00
|
|
|
ref={this._editorRef}
|
2020-01-09 07:19:00 +03:00
|
|
|
onPaste={this._onPaste}
|
2020-01-29 14:01:00 +03:00
|
|
|
autoFocus={true}
|
2020-01-07 06:51:23 +03:00
|
|
|
/>
|
|
|
|
);
|
|
|
|
return (
|
2020-01-16 23:42:34 +03:00
|
|
|
<div className='mx_InviteDialog_editor' onClick={this._onClickInputArea}>
|
2020-01-07 06:51:23 +03:00
|
|
|
{targets}
|
|
|
|
{input}
|
|
|
|
</div>
|
2020-01-07 22:20:02 +03:00
|
|
|
);
|
2020-01-07 06:51:23 +03:00
|
|
|
}
|
|
|
|
|
2020-01-09 06:49:29 +03:00
|
|
|
_renderIdentityServerWarning() {
|
|
|
|
if (!this.state.tryingIdentityServer || this.state.canUseIdentityServer) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
const defaultIdentityServerUrl = getDefaultIdentityServerUrl();
|
|
|
|
if (defaultIdentityServerUrl) {
|
|
|
|
return (
|
|
|
|
<div className="mx_AddressPickerDialog_identityServer">{_t(
|
|
|
|
"Use an identity server to invite by email. " +
|
|
|
|
"<default>Use the default (%(defaultIdentityServerName)s)</default> " +
|
|
|
|
"or manage in <settings>Settings</settings>.",
|
|
|
|
{
|
|
|
|
defaultIdentityServerName: abbreviateUrl(defaultIdentityServerUrl),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
default: sub => <a href="#" onClick={this._onUseDefaultIdentityServerClick}>{sub}</a>,
|
|
|
|
settings: sub => <a href="#" onClick={this._onManageSettingsClick}>{sub}</a>,
|
|
|
|
},
|
|
|
|
)}</div>
|
|
|
|
);
|
|
|
|
} else {
|
|
|
|
return (
|
|
|
|
<div className="mx_AddressPickerDialog_identityServer">{_t(
|
|
|
|
"Use an identity server to invite by email. " +
|
|
|
|
"Manage in <settings>Settings</settings>.",
|
|
|
|
{}, {
|
|
|
|
settings: sub => <a href="#" onClick={this._onManageSettingsClick}>{sub}</a>,
|
|
|
|
},
|
|
|
|
)}</div>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-01-03 03:40:18 +03:00
|
|
|
render() {
|
|
|
|
const BaseDialog = sdk.getComponent('views.dialogs.BaseDialog');
|
|
|
|
const AccessibleButton = sdk.getComponent("elements.AccessibleButton");
|
2020-01-15 09:32:00 +03:00
|
|
|
const Spinner = sdk.getComponent("elements.Spinner");
|
|
|
|
|
|
|
|
let spinner = null;
|
|
|
|
if (this.state.busy) {
|
|
|
|
spinner = <Spinner w={20} h={20} />;
|
|
|
|
}
|
2020-01-03 03:40:18 +03:00
|
|
|
|
2020-01-17 00:40:12 +03:00
|
|
|
|
|
|
|
let title;
|
|
|
|
let helpText;
|
|
|
|
let buttonText;
|
|
|
|
let goButtonFn;
|
|
|
|
|
|
|
|
if (this.props.kind === KIND_DM) {
|
|
|
|
const userId = MatrixClientPeg.get().getUserId();
|
|
|
|
|
|
|
|
title = _t("Direct Messages");
|
|
|
|
helpText = _t(
|
2020-01-24 03:22:39 +03:00
|
|
|
"If you can't find someone, ask them for their username, share your " +
|
2020-01-17 00:40:12 +03:00
|
|
|
"username (%(userId)s) or <a>profile link</a>.",
|
|
|
|
{userId},
|
2020-02-24 01:14:29 +03:00
|
|
|
{a: (sub) => <a href={makeUserPermalink(userId)} rel="noreferrer noopener" target="_blank">{sub}</a>},
|
2020-01-17 00:40:12 +03:00
|
|
|
);
|
|
|
|
buttonText = _t("Go");
|
|
|
|
goButtonFn = this._startDm;
|
|
|
|
} else { // KIND_INVITE
|
|
|
|
title = _t("Invite to this room");
|
|
|
|
helpText = _t(
|
|
|
|
"If you can't find someone, ask them for their username (e.g. @user:server.com) or " +
|
|
|
|
"<a>share this room</a>.", {},
|
2020-02-24 01:19:18 +03:00
|
|
|
{
|
|
|
|
a: (sub) =>
|
|
|
|
<a href={makeRoomPermalink(this.props.roomId)} rel="noreferrer noopener" target="_blank">{sub}</a>,
|
|
|
|
},
|
2020-01-17 00:40:12 +03:00
|
|
|
);
|
|
|
|
buttonText = _t("Invite");
|
|
|
|
goButtonFn = this._inviteUsers;
|
|
|
|
}
|
|
|
|
|
2020-02-21 09:21:35 +03:00
|
|
|
const hasSelection = this.state.targets.length > 0
|
|
|
|
|| (this.state.filterText && this.state.filterText.includes('@'));
|
2020-01-03 03:40:18 +03:00
|
|
|
return (
|
|
|
|
<BaseDialog
|
2020-01-16 23:42:34 +03:00
|
|
|
className='mx_InviteDialog'
|
2020-01-03 03:40:18 +03:00
|
|
|
hasCancel={true}
|
|
|
|
onFinished={this._cancel}
|
2020-01-17 00:40:12 +03:00
|
|
|
title={title}
|
2020-01-03 03:40:18 +03:00
|
|
|
>
|
2020-01-16 23:42:34 +03:00
|
|
|
<div className='mx_InviteDialog_content'>
|
2020-01-23 07:07:29 +03:00
|
|
|
<p className='mx_InviteDialog_helpText'>{helpText}</p>
|
2020-01-16 23:42:34 +03:00
|
|
|
<div className='mx_InviteDialog_addressBar'>
|
2020-01-07 06:51:23 +03:00
|
|
|
{this._renderEditor()}
|
2020-01-16 23:42:34 +03:00
|
|
|
<div className='mx_InviteDialog_buttonAndSpinner'>
|
2020-01-15 09:32:00 +03:00
|
|
|
<AccessibleButton
|
|
|
|
kind="primary"
|
2020-01-17 00:40:12 +03:00
|
|
|
onClick={goButtonFn}
|
2020-01-16 23:42:34 +03:00
|
|
|
className='mx_InviteDialog_goButton'
|
2020-02-21 09:10:43 +03:00
|
|
|
disabled={this.state.busy || !hasSelection}
|
2020-01-15 09:32:00 +03:00
|
|
|
>
|
2020-01-17 00:40:12 +03:00
|
|
|
{buttonText}
|
2020-01-15 09:32:00 +03:00
|
|
|
</AccessibleButton>
|
|
|
|
{spinner}
|
|
|
|
</div>
|
2020-01-03 03:40:18 +03:00
|
|
|
</div>
|
2020-01-15 09:32:00 +03:00
|
|
|
{this._renderIdentityServerWarning()}
|
|
|
|
<div className='error'>{this.state.errorText}</div>
|
2020-01-23 07:07:29 +03:00
|
|
|
<div className='mx_InviteDialog_userSections'>
|
|
|
|
{this._renderSection('recents')}
|
|
|
|
{this._renderSection('suggestions')}
|
|
|
|
</div>
|
2020-01-03 03:40:18 +03:00
|
|
|
</div>
|
|
|
|
</BaseDialog>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|