summaryrefslogtreecommitdiffstats
path: root/webapp/components/suggestion/at_mention_provider.jsx
blob: e650ad3e7d67aee33664e583ec83b17d27c3697c (plain)
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
// 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 'utils/web_client.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 {
            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>
        );
    }
}

export default class AtMentionProvider {
    handlePretextChanged(suggestionId, pretext) {
        const captured = (/@([a-z0-9\-\._]*)$/i).exec(pretext.toLowerCase());
        if (captured) {
            const prefix = captured[1];

            const users = UserStore.getActiveOnlyProfiles(true);

            const filtered = [];

            for (const id of Object.keys(users)) {
                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);
                }

                if (filtered.length >= MaxUserSuggestions) {
                    break;
                }
            }

            if (!pretext.startsWith('/msg')) {
                // add dummy users to represent the @channel and @all special mentions when not using the /msg command
                if ('channel'.startsWith(prefix)) {
                    filtered.push({username: 'channel'});
                }
                if ('all'.startsWith(prefix)) {
                    filtered.push({username: 'all'});
                }
            }

            filtered.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;
            });

            const mentions = filtered.map((user) => '@' + user.username);

            SuggestionStore.addSuggestions(suggestionId, mentions, filtered, AtMentionSuggestion, captured[0]);
        }
    }
}