summaryrefslogtreecommitdiffstats
path: root/webapp/stores/user_typing_store.jsx
blob: 39805fdb4910cfd4596b1158586ea0885756b63f (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
// Copyright (c) 2016-present Mattermost, Inc. All Rights Reserved.
// See License.txt for license information.

import AppDispatcher from '../dispatcher/app_dispatcher.jsx';
import UserStore from 'stores/user_store.jsx';
import EventEmitter from 'events';
import * as Utils from 'utils/utils.jsx';

import Constants from 'utils/constants.jsx';
const ActionTypes = Constants.ActionTypes;

const CHANGE_EVENT = 'change';

class UserTypingStoreClass extends EventEmitter {
    constructor() {
        super();

        // All typeing users by channel
        // this.typingUsers.[channelId+postParentId].user if present then user us typing
        // Value is timeout to remove user
        this.typingUsers = {};
    }

    emitChange() {
        this.emit(CHANGE_EVENT);
    }

    addChangeListener(callback) {
        this.on(CHANGE_EVENT, callback);
    }

    removeChangeListener(callback) {
        this.removeListener(CHANGE_EVENT, callback);
    }

    nameFromId(userId) {
        let name = Utils.localizeMessage('msg_typing.someone', 'Someone');
        if (UserStore.hasProfile(userId)) {
            name = Utils.displayUsername(userId);
        }
        return name;
    }

    userTyping(channelId, userId, postParentId) {
        const name = this.nameFromId(userId);

        // Key representing a location where users can type
        const loc = channelId + postParentId;

        // Create entry
        if (!this.typingUsers[loc]) {
            this.typingUsers[loc] = {};
        }

        // If we already have this user, clear it's timeout to be deleted
        if (this.typingUsers[loc][name]) {
            clearTimeout(this.typingUsers[loc][name].timeout);
        }

        // Set the user and a timeout to remove it
        this.typingUsers[loc][name] = setTimeout(() => {
            Reflect.deleteProperty(this.typingUsers[loc], name);
            if (this.typingUsers[loc] === {}) {
                Reflect.deleteProperty(this.typingUsers, loc);
            }
            this.emitChange();
        }, parseInt(window.mm_config.TimeBetweenUserTypingUpdatesMilliseconds, 10));
        this.emitChange();
    }

    getUsersTyping(channelId, postParentId) {
        // Key representing a location where users can type
        const loc = channelId + postParentId;

        return this.typingUsers[loc];
    }

    userPosted(userId, channelId, postParentId) {
        const name = this.nameFromId(userId);
        const loc = channelId + postParentId;

        if (this.typingUsers[loc]) {
            clearTimeout(this.typingUsers[loc][name]);
            Reflect.deleteProperty(this.typingUsers[loc], name);
            if (this.typingUsers[loc] === {}) {
                Reflect.deleteProperty(this.typingUsers, loc);
            }
            this.emitChange();
        }
    }
}

var UserTypingStore = new UserTypingStoreClass();

UserTypingStore.dispatchToken = AppDispatcher.register((payload) => {
    var action = payload.action;

    switch (action.type) {
    case ActionTypes.RECEIVED_POST:
        UserTypingStore.userPosted(action.post.user_id, action.post.channel_id, action.post.parent_id);
        break;
    case ActionTypes.USER_TYPING:
        UserTypingStore.userTyping(action.channelId, action.userId, action.postParentId);
        break;
    }
});

export default UserTypingStore;