summaryrefslogtreecommitdiffstats
path: root/webapp/stores/post_store.jsx
blob: a1bdfa4e92e16dfe486b2d66b486c57827b35e0c (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
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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
// See License.txt for license information.

import AppDispatcher from '../dispatcher/app_dispatcher.jsx';
import EventEmitter from 'events';

import ChannelStore from 'stores/channel_store.jsx';
import BrowserStore from 'stores/browser_store.jsx';
import UserStore from 'stores/user_store.jsx';

import * as PostUtils from 'utils/post_utils.jsx';
import {Constants} from 'utils/constants.jsx';
const ActionTypes = Constants.ActionTypes;

const FOCUSED_POST_CHANGE = 'focused_post_change';
const EDIT_POST_EVENT = 'edit_post';
const POST_PINNED_CHANGE_EVENT = 'post_pinned_change';

import store from 'stores/redux_store.jsx';
const dispatch = store.dispatch;
const getState = store.getState;

import * as Selectors from 'mattermost-redux/selectors/entities/posts';

class PostStoreClass extends EventEmitter {
    constructor() {
        super();
        this.selectedPostId = null;
        this.currentFocusedPostId = null;
    }

    emitPostFocused() {
        this.emit(FOCUSED_POST_CHANGE);
    }

    addPostFocusedListener(callback) {
        this.on(FOCUSED_POST_CHANGE, callback);
    }

    removePostFocusedListener(callback) {
        this.removeListener(FOCUSED_POST_CHANGE, callback);
    }

    emitEditPost(post) {
        this.emit(EDIT_POST_EVENT, post);
    }

    addEditPostListener(callback) {
        this.on(EDIT_POST_EVENT, callback);
    }

    removeEditPostListner(callback) {
        this.removeListener(EDIT_POST_EVENT, callback);
    }

    emitPostPinnedChange() {
        this.emit(POST_PINNED_CHANGE_EVENT);
    }

    addPostPinnedChangeListener(callback) {
        this.on(POST_PINNED_CHANGE_EVENT, callback);
    }

    removePostPinnedChangeListener(callback) {
        this.removeListener(POST_PINNED_CHANGE_EVENT, callback);
    }

    getLatestPostId(channelId) {
        const postsInChannel = getState().entities.posts.postsInChannel[channelId] || [];
        return postsInChannel[0];
    }

    getLatestReplyablePost(channelId) {
        const postIds = getState().entities.posts.postsInChannel[channelId] || [];
        const posts = getState().entities.posts.posts;

        for (const postId of postIds) {
            const post = posts[postId] || {};
            if (post.state !== Constants.POST_DELETED && !PostUtils.isSystemMessage(post)) {
                return post;
            }
        }

        return null;
    }

    getVisiblePosts() {
        const posts = Selectors.getPostsInCurrentChannel(getState());
        const currentChannelId = getState().entities.channels.currentChannelId;
        return posts.slice(0, getState().views.channel.postVisibility[currentChannelId]);
    }

    getFocusedPostId() {
        return this.currentFocusedPostId;
    }

    storeFocusedPostId(postId) {
        this.currentFocusedPostId = postId;
    }

    clearFocusedPost() {
        this.currentFocusedPostId = null;
    }

    getCurrentUsersLatestPost(channelId, rootId) {
        const userId = UserStore.getCurrentId();

        const postIds = getState().entities.posts.postsInChannel[channelId] || [];

        let lastPost = null;

        for (const id of postIds) {
            const post = Selectors.getPost(getState(), id) || {};

            // don't edit webhook posts, deleted posts, or system messages
            if (post.user_id !== userId ||
                (post.props && post.props.from_webhook) ||
                post.state === Constants.POST_DELETED ||
                (post.type && post.type.startsWith(Constants.SYSTEM_MESSAGE_PREFIX))) {
                continue;
            }

            if (rootId) {
                if (post.root_id === rootId || post.id === rootId) {
                    lastPost = post;
                    break;
                }
            } else {
                lastPost = post;
                break;
            }
        }

        return lastPost;
    }

    normalizeDraft(originalDraft) {
        let draft = {
            message: '',
            uploadsInProgress: [],
            fileInfos: []
        };

        // Make sure that the post draft is non-null and has all the required fields
        if (originalDraft) {
            draft = {
                message: originalDraft.message || draft.message,
                uploadsInProgress: originalDraft.uploadsInProgress || draft.uploadsInProgress,
                fileInfos: originalDraft.fileInfos || draft.fileInfos
            };
        }

        return draft;
    }

    storeCurrentDraft(draft) {
        var channelId = ChannelStore.getCurrentId();
        BrowserStore.setGlobalItem('draft_' + channelId, draft);
    }

    getCurrentDraft() {
        var channelId = ChannelStore.getCurrentId();
        return this.getDraft(channelId);
    }

    storeDraft(channelId, draft) {
        BrowserStore.setGlobalItem('draft_' + channelId, draft);
    }

    getDraft(channelId) {
        return this.normalizeDraft(BrowserStore.getGlobalItem('draft_' + channelId));
    }

    storeCommentDraft(parentPostId, draft) {
        BrowserStore.setGlobalItem('comment_draft_' + parentPostId, draft);
    }

    getCommentDraft(parentPostId) {
        return this.normalizeDraft(BrowserStore.getGlobalItem('comment_draft_' + parentPostId));
    }

    clearDraftUploads() {
        BrowserStore.actionOnGlobalItemsWithPrefix('draft_', (key, value) => {
            if (value) {
                value.uploadsInProgress = [];
                BrowserStore.setGlobalItem(key, value);
            }
        });
    }

    clearCommentDraftUploads() {
        BrowserStore.actionOnGlobalItemsWithPrefix('comment_draft_', (key, value) => {
            if (value) {
                value.uploadsInProgress = [];
                BrowserStore.setGlobalItem(key, value);
            }
        });
    }

    getCommentCount(rootPost) {
        const postIds = getState().entities.posts.postsInChannel[rootPost.channel_id] || [];

        let commentCount = 0;
        for (const postId of postIds) {
            const post = Selectors.getPost(getState(), postId) || {};
            if (post.root_id === rootPost.id) {
                commentCount += 1;
            }
        }

        return commentCount;
    }
}

var PostStore = new PostStoreClass();

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

    switch (action.type) {
    case ActionTypes.RECEIVED_FOCUSED_POST:
        PostStore.storeFocusedPostId(action.postId);
        PostStore.emitPostFocused();
        break;
    case ActionTypes.CLICK_CHANNEL:
        PostStore.clearFocusedPost();
        break;
    case ActionTypes.RECEIVED_EDIT_POST:
        PostStore.emitEditPost(action);
        break;
    case ActionTypes.RECEIVED_POST_SELECTED:
        dispatch({...action, type: ActionTypes.SELECT_POST});
        break;
    case ActionTypes.RECEIVED_POST_PINNED:
    case ActionTypes.RECEIVED_POST_UNPINNED:
        PostStore.emitPostPinnedChange();
        break;
    default:
    }
});

export default PostStore;