1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
|
// Copyright (c) 2015 Mattermost, Inc. All Rights Reserved.
// See License.txt for license information.
import React from 'react';
import SuggestionStore from 'stores/suggestion_store.jsx';
import ChannelStore from 'stores/channel_store.jsx';
import UserStore from 'stores/user_store.jsx';
import * as Utils from 'utils/utils.jsx';
import Client from 'client/web_client.jsx';
import Constants from 'utils/constants.jsx';
import {FormattedMessage} from 'react-intl';
import Suggestion from './suggestion.jsx';
const MaxUserSuggestions = 40;
class AtMentionSuggestion extends Suggestion {
render() {
const isSelection = this.props.isSelection;
const user = this.props.item;
let username;
let description;
let icon;
if (user.username === 'all') {
username = 'all';
description = (
<FormattedMessage
id='suggestion.mention.all'
defaultMessage='Notifies everyone in the channel, use in {townsquare} to notify the whole team'
values={{
townsquare: ChannelStore.getByName('town-square').display_name
}}
/>
);
icon = <i className='mention__image fa fa-users fa-2x'/>;
} else if (user.username === 'channel') {
username = 'channel';
description = (
<FormattedMessage
id='suggestion.mention.channel'
defaultMessage='Notifies everyone in the channel'
/>
);
icon = <i className='mention__image fa fa-users fa-2x'/>;
} else if (user.username === 'here') {
username = 'here';
description = (
<FormattedMessage
id='suggestion.mention.here'
defaultMessage='Notifies everyone in the channel and online'
/>
);
icon = <i className='mention__image fa fa-users fa-2x'/>;
} else {
username = user.username;
if ((user.first_name || user.last_name) && user.nickname) {
description = `- ${Utils.getFullName(user)} (${user.nickname})`;
} else if (user.nickname) {
description = `- (${user.nickname})`;
} else if (user.first_name || user.last_name) {
description = `- ${Utils.getFullName(user)}`;
}
icon = (
<img
className='mention__image'
src={Client.getUsersRoute() + '/' + user.id + '/image?time=' + user.update_at}
/>
);
}
let className = 'mentions__name';
if (isSelection) {
className += ' suggestion--selected';
}
return (
<div
className={className}
onClick={this.handleClick}
>
<div className='pull-left'>
{icon}
</div>
<div className='pull-left mention--align'>
<span>
{'@' + username}
</span>
<span className='mention__fullname'>
{' '}
{description}
</span>
</div>
</div>
);
}
}
function filterUsersByPrefix(users, prefix, limit) {
const filtered = [];
for (const id of Object.keys(users)) {
if (filtered.length >= limit) {
break;
}
const user = users[id];
if (user.delete_at > 0) {
continue;
}
if (user.username.startsWith(prefix) ||
(user.first_name && user.first_name.toLowerCase().startsWith(prefix)) ||
(user.last_name && user.last_name.toLowerCase().startsWith(prefix)) ||
(user.nickname && user.nickname.toLowerCase().startsWith(prefix))) {
filtered.push(user);
}
}
return filtered;
}
export default class AtMentionProvider {
handlePretextChanged(suggestionId, pretext) {
const captured = (/@([a-z0-9\-\._]*)$/i).exec(pretext.toLowerCase());
if (captured) {
const prefix = captured[1];
// Group users into members and nonmembers of the channel.
const users = UserStore.getActiveOnlyProfiles(true);
const channelMembers = {};
const extra = ChannelStore.getCurrentExtraInfo();
for (let i = 0; i < extra.members.length; i++) {
const id = extra.members[i].id;
if (users[id]) {
channelMembers[id] = users[id];
Reflect.deleteProperty(users, id);
}
}
const channelNonmembers = users;
// Filter users by prefix.
const filteredMembers = filterUsersByPrefix(
channelMembers, prefix, MaxUserSuggestions);
const filteredNonmembers = filterUsersByPrefix(
channelNonmembers, prefix, MaxUserSuggestions - filteredMembers.length);
let filteredSpecialMentions = [];
if (!pretext.startsWith('/msg')) {
filteredSpecialMentions = ['here', 'channel', 'all'].filter((item) => {
return item.startsWith(prefix);
}).map((name) => {
return {username: name};
});
}
// Sort users by username.
[filteredMembers, filteredNonmembers].forEach((items) => {
items.sort((a, b) => {
const aPrefix = a.username.startsWith(prefix);
const bPrefix = b.username.startsWith(prefix);
if (aPrefix === bPrefix) {
return a.username.localeCompare(b.username);
} else if (aPrefix) {
return -1;
}
return 1;
});
});
filteredMembers.forEach((item) => {
item.type = Constants.MENTION_MEMBERS;
});
filteredNonmembers.forEach((item) => {
item.type = Constants.MENTION_NONMEMBERS;
});
filteredSpecialMentions.forEach((item) => {
item.type = Constants.MENTION_SPECIAL;
});
const filtered = filteredMembers.concat(filteredSpecialMentions).concat(filteredNonmembers);
const mentions = filtered.map((user) => '@' + user.username);
SuggestionStore.addSuggestions(suggestionId, mentions, filtered, AtMentionSuggestion, captured[0]);
}
}
}
|