2017-02-10 17:04:52 +00:00
|
|
|
//@flow
|
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
|
|
|
|
|
|
|
|
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.
|
|
|
|
*/
|
|
|
|
|
2016-07-03 16:45:13 +00:00
|
|
|
import React from 'react';
|
2017-05-25 10:39:08 +00:00
|
|
|
import { _t } from '../languageHandler';
|
2016-06-12 11:32:46 +00:00
|
|
|
import AutocompleteProvider from './AutocompleteProvider';
|
2016-08-17 11:57:19 +00:00
|
|
|
import {PillCompletion} from './Components';
|
|
|
|
import sdk from '../index';
|
2016-12-01 06:36:57 +00:00
|
|
|
import FuzzyMatcher from './FuzzyMatcher';
|
2017-02-10 17:04:52 +00:00
|
|
|
import _pull from 'lodash/pull';
|
|
|
|
import _sortBy from 'lodash/sortBy';
|
|
|
|
import MatrixClientPeg from '../MatrixClientPeg';
|
|
|
|
|
|
|
|
import type {Room, RoomMember} from 'matrix-js-sdk';
|
2016-06-12 11:32:46 +00:00
|
|
|
|
2016-09-16 10:08:29 +00:00
|
|
|
const USER_REGEX = /@\S*/g;
|
2016-06-12 11:32:46 +00:00
|
|
|
|
2016-06-20 08:22:55 +00:00
|
|
|
let instance = null;
|
|
|
|
|
2016-06-12 11:32:46 +00:00
|
|
|
export default class UserProvider extends AutocompleteProvider {
|
2017-02-10 17:04:52 +00:00
|
|
|
users: Array<RoomMember> = [];
|
|
|
|
|
2016-06-12 11:32:46 +00:00
|
|
|
constructor() {
|
2016-06-21 10:16:20 +00:00
|
|
|
super(USER_REGEX, {
|
2016-08-02 04:30:12 +00:00
|
|
|
keys: ['name', 'userId'],
|
2016-06-21 10:16:20 +00:00
|
|
|
});
|
2016-12-01 06:36:57 +00:00
|
|
|
this.matcher = new FuzzyMatcher([], {
|
2016-08-02 04:30:12 +00:00
|
|
|
keys: ['name', 'userId'],
|
2016-07-03 16:45:13 +00:00
|
|
|
});
|
2016-06-12 11:32:46 +00:00
|
|
|
}
|
|
|
|
|
2016-09-13 10:11:52 +00:00
|
|
|
async getCompletions(query: string, selection: {start: number, end: number}, force = false) {
|
2016-08-17 11:57:19 +00:00
|
|
|
const MemberAvatar = sdk.getComponent('views.avatars.MemberAvatar');
|
|
|
|
|
2016-06-12 11:32:46 +00:00
|
|
|
let completions = [];
|
2016-09-13 10:11:52 +00:00
|
|
|
let {command, range} = this.getCurrentCommand(query, selection, force);
|
2016-07-03 16:45:13 +00:00
|
|
|
if (command) {
|
2016-12-01 06:36:57 +00:00
|
|
|
completions = this.matcher.match(command[0]).map(user => {
|
2016-09-13 11:59:17 +00:00
|
|
|
let displayName = (user.name || user.userId || '').replace(' (IRC)', ''); // FIXME when groups are done
|
|
|
|
let completion = displayName;
|
|
|
|
if (range.start === 0) {
|
|
|
|
completion += ': ';
|
|
|
|
} else {
|
|
|
|
completion += ' ';
|
|
|
|
}
|
2016-06-12 11:32:46 +00:00
|
|
|
return {
|
2016-09-13 11:59:17 +00:00
|
|
|
completion,
|
2016-07-03 16:45:13 +00:00
|
|
|
component: (
|
2016-08-17 11:57:19 +00:00
|
|
|
<PillCompletion
|
|
|
|
initialComponent={<MemberAvatar member={user} width={24} height={24}/>}
|
2016-08-02 04:30:12 +00:00
|
|
|
title={displayName}
|
2016-07-03 16:45:13 +00:00
|
|
|
description={user.userId} />
|
|
|
|
),
|
2016-09-13 10:11:52 +00:00
|
|
|
range,
|
2016-06-12 11:32:46 +00:00
|
|
|
};
|
2016-06-20 23:35:23 +00:00
|
|
|
}).slice(0, 4);
|
2016-06-12 11:32:46 +00:00
|
|
|
}
|
2016-09-13 10:11:52 +00:00
|
|
|
return completions;
|
2016-06-12 11:32:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
getName() {
|
2017-05-23 14:16:31 +00:00
|
|
|
return '👥 ' + _t('Users');
|
2016-06-12 11:32:46 +00:00
|
|
|
}
|
2016-06-20 08:22:55 +00:00
|
|
|
|
2017-02-10 17:04:52 +00:00
|
|
|
setUserListFromRoom(room: Room) {
|
|
|
|
const events = room.getLiveTimeline().getEvents();
|
|
|
|
const lastSpoken = {};
|
|
|
|
|
|
|
|
for(const event of events) {
|
|
|
|
lastSpoken[event.getSender()] = event.getTs();
|
|
|
|
}
|
|
|
|
|
|
|
|
const currentUserId = MatrixClientPeg.get().credentials.userId;
|
|
|
|
this.users = room.getJoinedMembers().filter((member) => {
|
|
|
|
if (member.userId !== currentUserId) return true;
|
|
|
|
});
|
|
|
|
|
|
|
|
this.users = _sortBy(this.users, (user) => 1E20 - lastSpoken[user.userId] || 1E20);
|
|
|
|
|
|
|
|
this.matcher.setObjects(this.users);
|
|
|
|
}
|
|
|
|
|
|
|
|
onUserSpoke(user: RoomMember) {
|
|
|
|
if(user.userId === MatrixClientPeg.get().credentials.userId) return;
|
|
|
|
|
|
|
|
// Probably unsafe to compare by reference here?
|
|
|
|
_pull(this.users, user);
|
|
|
|
this.users.splice(0, 0, user);
|
2016-12-01 06:36:57 +00:00
|
|
|
this.matcher.setObjects(this.users);
|
2016-06-20 08:22:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static getInstance(): UserProvider {
|
2016-07-03 16:45:13 +00:00
|
|
|
if (instance == null) {
|
2016-06-20 08:22:55 +00:00
|
|
|
instance = new UserProvider();
|
2016-07-03 16:45:13 +00:00
|
|
|
}
|
2016-06-20 08:22:55 +00:00
|
|
|
return instance;
|
|
|
|
}
|
2016-08-17 11:57:19 +00:00
|
|
|
|
|
|
|
renderCompletions(completions: [React.Component]): ?React.Component {
|
2016-08-22 19:06:31 +00:00
|
|
|
return <div className="mx_Autocomplete_Completion_container_pill">
|
|
|
|
{completions}
|
|
|
|
</div>;
|
2016-08-17 11:57:19 +00:00
|
|
|
}
|
2016-09-13 10:11:52 +00:00
|
|
|
|
|
|
|
shouldForceComplete(): boolean {
|
|
|
|
return true;
|
|
|
|
}
|
2016-06-12 11:32:46 +00:00
|
|
|
}
|