2017-06-01 14:18:06 +00:00
|
|
|
/*
|
2017-06-01 16:29:40 +00:00
|
|
|
Copyright 2016 Aviral Dasgupta
|
2017-06-01 14:18:06 +00:00
|
|
|
Copyright 2017 Vector Creations Ltd
|
2018-06-19 11:06:13 +00:00
|
|
|
Copyright 2017, 2018 New Vector Ltd
|
2018-06-25 08:32:51 +00:00
|
|
|
Copyright 2018 Michael Telatynski <7t3chguy@gmail.com>
|
2017-06-01 14:18:06 +00:00
|
|
|
|
|
|
|
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-04-21 09:01:05 +00:00
|
|
|
import React from "react";
|
2021-06-17 13:24:53 +00:00
|
|
|
import { sortBy } from "lodash";
|
2023-08-08 07:16:04 +00:00
|
|
|
import {
|
|
|
|
MatrixEvent,
|
|
|
|
Room,
|
|
|
|
RoomEvent,
|
|
|
|
RoomMember,
|
|
|
|
RoomState,
|
|
|
|
RoomStateEvent,
|
|
|
|
IRoomTimelineData,
|
|
|
|
} from "matrix-js-sdk/src/matrix";
|
2024-03-18 14:40:52 +00:00
|
|
|
import { KnownMembership } from "matrix-js-sdk/src/types";
|
2021-10-22 22:23:32 +00:00
|
|
|
|
|
|
|
import { MatrixClientPeg } from "../MatrixClientPeg";
|
|
|
|
import QueryMatcher from "./QueryMatcher";
|
|
|
|
import { PillCompletion } from "./Components";
|
|
|
|
import AutocompleteProvider from "./AutocompleteProvider";
|
|
|
|
import { _t } from "../languageHandler";
|
2021-06-17 13:24:53 +00:00
|
|
|
import { makeUserPermalink } from "../utils/permalinks/Permalinks";
|
|
|
|
import { ICompletion, ISelectionRange } from "./Autocompleter";
|
2021-07-02 15:08:27 +00:00
|
|
|
import MemberAvatar from "../components/views/avatars/MemberAvatar";
|
2021-11-18 12:47:11 +00:00
|
|
|
import { TimelineRenderingType } from "../contexts/RoomContext";
|
2022-01-25 09:40:02 +00:00
|
|
|
import UserIdentifierCustomisations from "../customisations/UserIdentifier";
|
2016-06-12 11:32:46 +00:00
|
|
|
|
2018-06-25 08:32:51 +00:00
|
|
|
const USER_REGEX = /\B@\S*/g;
|
2016-06-12 11:32:46 +00:00
|
|
|
|
2018-05-13 02:04:40 +00:00
|
|
|
// used when you hit 'tab' - we allow some separator chars at the beginning
|
|
|
|
// to allow you to tab-complete /mat into /(matthew)
|
|
|
|
const FORCED_USER_REGEX = /[^/,:; \t\n]\S*/g;
|
2016-06-12 11:32:46 +00:00
|
|
|
|
|
|
|
export default class UserProvider extends AutocompleteProvider {
|
2022-12-16 12:29:59 +00:00
|
|
|
public matcher: QueryMatcher<RoomMember>;
|
2023-05-05 08:11:14 +00:00
|
|
|
public users?: RoomMember[];
|
2022-12-16 12:29:59 +00:00
|
|
|
public room: Room;
|
2017-02-10 17:04:52 +00:00
|
|
|
|
2022-12-16 12:29:59 +00:00
|
|
|
public constructor(room: Room, renderingType?: TimelineRenderingType) {
|
2021-11-18 12:47:11 +00:00
|
|
|
super({
|
|
|
|
commandRegex: USER_REGEX,
|
|
|
|
forcedCommandRegex: FORCED_USER_REGEX,
|
|
|
|
renderingType,
|
|
|
|
});
|
2017-11-02 17:51:08 +00:00
|
|
|
this.room = room;
|
2023-02-13 17:01:43 +00:00
|
|
|
this.matcher = new QueryMatcher<RoomMember>([], {
|
2018-10-11 17:34:01 +00:00
|
|
|
keys: ["name"],
|
|
|
|
funcs: [(obj) => obj.userId.slice(1)], // index by user id minus the leading '@'
|
2018-06-23 01:00:36 +00:00
|
|
|
shouldMatchWordsOnly: false,
|
2016-07-03 16:45:13 +00:00
|
|
|
});
|
2017-11-02 17:51:08 +00:00
|
|
|
|
2023-06-21 16:29:44 +00:00
|
|
|
MatrixClientPeg.safeGet().on(RoomEvent.Timeline, this.onRoomTimeline);
|
|
|
|
MatrixClientPeg.safeGet().on(RoomStateEvent.Update, this.onRoomStateUpdate);
|
2017-11-02 17:51:08 +00:00
|
|
|
}
|
|
|
|
|
2023-01-12 13:25:14 +00:00
|
|
|
public destroy(): void {
|
2023-06-21 16:29:44 +00:00
|
|
|
MatrixClientPeg.get()?.removeListener(RoomEvent.Timeline, this.onRoomTimeline);
|
|
|
|
MatrixClientPeg.get()?.removeListener(RoomStateEvent.Update, this.onRoomStateUpdate);
|
2017-11-02 17:51:08 +00:00
|
|
|
}
|
|
|
|
|
2020-08-29 00:11:08 +00:00
|
|
|
private onRoomTimeline = (
|
|
|
|
ev: MatrixEvent,
|
2023-02-13 17:01:43 +00:00
|
|
|
room: Room | undefined,
|
2023-05-12 08:49:37 +00:00
|
|
|
toStartOfTimeline: boolean | undefined,
|
2020-08-29 00:11:08 +00:00
|
|
|
removed: boolean,
|
|
|
|
data: IRoomTimelineData,
|
2023-01-12 13:25:14 +00:00
|
|
|
): void => {
|
2022-01-26 13:24:14 +00:00
|
|
|
if (!room) return; // notification timeline, we'll get this event again with a room specific timeline
|
2017-11-02 18:14:21 +00:00
|
|
|
if (removed) return;
|
2017-11-02 18:14:46 +00:00
|
|
|
if (room.roomId !== this.room.roomId) return;
|
2017-11-02 17:51:08 +00:00
|
|
|
|
|
|
|
// ignore events from filtered timelines
|
|
|
|
if (data.timeline.getTimelineSet() !== room.getUnfilteredTimelineSet()) return;
|
|
|
|
|
|
|
|
// ignore anything but real-time updates at the end of the room:
|
|
|
|
// updates from pagination will happen when the paginate completes.
|
|
|
|
if (toStartOfTimeline || !data || !data.liveEvent) return;
|
|
|
|
|
2018-03-16 01:21:46 +00:00
|
|
|
// TODO: lazyload if we have no ev.sender room member?
|
2017-11-02 17:51:08 +00:00
|
|
|
this.onUserSpoke(ev.sender);
|
2020-04-20 18:00:54 +00:00
|
|
|
};
|
2017-11-02 17:51:08 +00:00
|
|
|
|
2023-01-12 13:25:14 +00:00
|
|
|
private onRoomStateUpdate = (state: RoomState): void => {
|
2022-02-24 14:39:25 +00:00
|
|
|
// ignore updates in other rooms
|
|
|
|
if (state.roomId !== this.room.roomId) return;
|
2017-11-02 17:51:08 +00:00
|
|
|
|
|
|
|
// blow away the users cache
|
2023-05-05 08:11:14 +00:00
|
|
|
this.users = undefined;
|
2020-04-20 18:00:54 +00:00
|
|
|
};
|
2016-06-12 11:32:46 +00:00
|
|
|
|
2022-12-16 12:29:59 +00:00
|
|
|
public async getCompletions(
|
2021-05-12 11:18:56 +00:00
|
|
|
rawQuery: string,
|
|
|
|
selection: ISelectionRange,
|
|
|
|
force = false,
|
|
|
|
limit = -1,
|
|
|
|
): Promise<ICompletion[]> {
|
2017-09-22 13:31:29 +00:00
|
|
|
// lazy-load user list into matcher
|
2021-07-19 21:43:11 +00:00
|
|
|
if (!this.users) this.makeUsers();
|
2017-09-22 13:31:29 +00:00
|
|
|
|
2021-06-29 12:11:58 +00:00
|
|
|
const { command, range } = this.getCurrentCommand(rawQuery, selection, force);
|
2018-04-24 12:59:18 +00:00
|
|
|
|
2023-02-13 11:39:16 +00:00
|
|
|
const fullMatch = command?.[0];
|
2018-04-24 12:59:18 +00:00
|
|
|
// Don't search if the query is a single "@"
|
|
|
|
if (fullMatch && fullMatch !== "@") {
|
2018-08-13 18:15:42 +00:00
|
|
|
// Don't include the '@' in our search query - it's only used as a way to trigger completion
|
|
|
|
const query = fullMatch.startsWith("@") ? fullMatch.substring(1) : fullMatch;
|
2023-02-13 11:39:16 +00:00
|
|
|
return this.matcher.match(query, limit).map((user) => {
|
2023-02-13 17:01:43 +00:00
|
|
|
const description = UserIdentifierCustomisations.getDisplayUserIdentifier?.(user.userId, {
|
2022-01-25 09:40:02 +00:00
|
|
|
roomId: this.room.roomId,
|
|
|
|
withDisplayName: true,
|
|
|
|
});
|
2018-10-03 18:39:47 +00:00
|
|
|
const displayName = user.name || user.userId || "";
|
2016-06-12 11:32:46 +00:00
|
|
|
return {
|
2017-08-08 09:28:11 +00:00
|
|
|
// Length of completion should equal length of text in decorator. draft-js
|
|
|
|
// relies on the length of the entity === length of the text in the decoration.
|
2018-10-03 18:34:06 +00:00
|
|
|
completion: user.rawDisplayName,
|
2018-05-12 19:04:58 +00:00
|
|
|
completionId: user.userId,
|
2019-09-23 12:39:19 +00:00
|
|
|
type: "user",
|
2023-02-13 17:01:43 +00:00
|
|
|
suffix: selection.beginning && range!.start === 0 ? ": " : " ",
|
2017-12-12 23:33:40 +00:00
|
|
|
href: makeUserPermalink(user.userId),
|
2016-07-03 16:45:13 +00:00
|
|
|
component: (
|
2022-01-25 09:40:02 +00:00
|
|
|
<PillCompletion title={displayName} description={description}>
|
2023-08-24 03:48:35 +00:00
|
|
|
<MemberAvatar member={user} size="24px" />
|
2020-05-30 12:30:59 +00:00
|
|
|
</PillCompletion>
|
2016-07-03 16:45:13 +00:00
|
|
|
),
|
2023-02-13 17:01:43 +00:00
|
|
|
range: range!,
|
2016-06-12 11:32:46 +00:00
|
|
|
};
|
2017-07-04 12:53:06 +00:00
|
|
|
});
|
2016-06-12 11:32:46 +00:00
|
|
|
}
|
2023-02-13 11:39:16 +00:00
|
|
|
return [];
|
2016-06-12 11:32:46 +00:00
|
|
|
}
|
|
|
|
|
2022-12-16 12:29:59 +00:00
|
|
|
public getName(): string {
|
2023-09-21 08:11:26 +00:00
|
|
|
return _t("composer|autocomplete|user_description");
|
2016-06-12 11:32:46 +00:00
|
|
|
}
|
2016-06-20 08:22:55 +00:00
|
|
|
|
2023-01-12 13:25:14 +00:00
|
|
|
private makeUsers(): void {
|
2017-09-22 13:31:29 +00:00
|
|
|
const events = this.room.getLiveTimeline().getEvents();
|
2023-02-13 11:39:16 +00:00
|
|
|
const lastSpoken: Record<string, number> = {};
|
2017-02-10 17:04:52 +00:00
|
|
|
|
2017-11-16 13:19:36 +00:00
|
|
|
for (const event of events) {
|
2023-02-13 17:01:43 +00:00
|
|
|
lastSpoken[event.getSender()!] = event.getTs();
|
2017-02-10 17:04:52 +00:00
|
|
|
}
|
|
|
|
|
2023-06-21 16:29:44 +00:00
|
|
|
const currentUserId = MatrixClientPeg.safeGet().credentials.userId;
|
2021-06-29 12:11:58 +00:00
|
|
|
this.users = this.room.getJoinedMembers().filter(({ userId }) => userId !== currentUserId);
|
2024-03-12 14:52:54 +00:00
|
|
|
this.users = this.users.concat(this.room.getMembersWithMembership(KnownMembership.Invite));
|
2017-02-10 17:04:52 +00:00
|
|
|
|
2020-08-28 17:53:43 +00:00
|
|
|
this.users = sortBy(this.users, (member) => 1e20 - lastSpoken[member.userId] || 1e20);
|
2017-02-10 17:04:52 +00:00
|
|
|
|
|
|
|
this.matcher.setObjects(this.users);
|
|
|
|
}
|
|
|
|
|
2023-02-13 17:01:43 +00:00
|
|
|
public onUserSpoke(user: RoomMember | null): void {
|
2020-04-22 12:18:54 +00:00
|
|
|
if (!this.users) return;
|
2018-03-16 01:21:46 +00:00
|
|
|
if (!user) return;
|
2023-06-21 16:29:44 +00:00
|
|
|
if (user.userId === MatrixClientPeg.safeGet().getSafeUserId()) return;
|
2017-02-10 17:04:52 +00:00
|
|
|
|
2017-07-11 09:42:02 +00:00
|
|
|
// Move the user that spoke to the front of the array
|
|
|
|
this.users.splice(
|
2017-07-04 12:53:06 +00:00
|
|
|
this.users.findIndex((user2) => user2.userId === user.userId),
|
|
|
|
1,
|
|
|
|
);
|
|
|
|
this.users = [user, ...this.users];
|
|
|
|
|
2016-12-01 06:36:57 +00:00
|
|
|
this.matcher.setObjects(this.users);
|
2016-06-20 08:22:55 +00:00
|
|
|
}
|
|
|
|
|
2022-12-16 12:29:59 +00:00
|
|
|
public renderCompletions(completions: React.ReactNode[]): React.ReactNode {
|
2019-09-30 13:32:42 +00:00
|
|
|
return (
|
2020-08-29 00:11:08 +00:00
|
|
|
<div
|
|
|
|
className="mx_Autocomplete_Completion_container_pill"
|
2021-02-18 10:55:24 +00:00
|
|
|
role="presentation"
|
2023-09-21 08:11:26 +00:00
|
|
|
aria-label={_t("composer|autocomplete|user_a11y")}
|
2020-08-29 00:11:08 +00:00
|
|
|
>
|
2019-09-30 13:32:42 +00:00
|
|
|
{completions}
|
|
|
|
</div>
|
|
|
|
);
|
2016-08-17 11:57:19 +00:00
|
|
|
}
|
2016-09-13 10:11:52 +00:00
|
|
|
|
2022-12-16 12:29:59 +00:00
|
|
|
public shouldForceComplete(): boolean {
|
2016-09-13 10:11:52 +00:00
|
|
|
return true;
|
|
|
|
}
|
2016-06-12 11:32:46 +00:00
|
|
|
}
|