This repository has been archived by the owner on Mar 13, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2.7k
/
post_store.jsx
217 lines (174 loc) · 6.41 KB
/
post_store.jsx
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
// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
// See License.txt for license information.
import EventEmitter from 'events';
import * as Selectors from 'mattermost-redux/selectors/entities/posts';
import {PostTypes} from 'mattermost-redux/action_types';
import BrowserStore from 'stores/browser_store.jsx';
import ChannelStore from 'stores/channel_store.jsx';
import store from 'stores/redux_store.jsx';
import UserStore from 'stores/user_store.jsx';
import {Constants} from 'utils/constants.jsx';
import AppDispatcher from '../dispatcher/app_dispatcher.jsx';
const ActionTypes = Constants.ActionTypes;
const FOCUSED_POST_CHANGE = 'focused_post_change';
const POST_PINNED_CHANGE_EVENT = 'post_pinned_change';
const dispatch = store.dispatch;
const getState = store.getState;
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);
}
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);
}
getMostRecentPostIdInChannel(channelId) {
return Selectors.getMostRecentPostIdInChannel(getState(), channelId);
}
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) {
// deep clone because many components need to modify the draft
return JSON.parse(JSON.stringify(this.normalizeDraft(BrowserStore.getGlobalItem('draft_' + channelId))));
}
storeCommentDraft(parentPostId, draft) {
BrowserStore.setGlobalItem('comment_draft_' + parentPostId, draft);
}
getCommentDraft(parentPostId) {
// deep clone because many components need to modify the draft
return JSON.parse(JSON.stringify(this.normalizeDraft(BrowserStore.getGlobalItem('comment_draft_' + parentPostId))));
}
clearDraftUploads() {
BrowserStore.actionOnGlobalItemsWithPrefix('draft_', (key, value) => {
if (value) {
return {...value, uploadsInProgress: []};
}
return value;
});
}
clearCommentDraftUploads() {
BrowserStore.actionOnGlobalItemsWithPrefix('comment_draft_', (key, value) => {
if (value) {
return {...value, uploadsInProgress: []};
}
return 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:
dispatch({...action, type: ActionTypes.EDIT_POST});
break;
case ActionTypes.RECEIVED_POST_SELECTED:
dispatch({data: action.postId, type: PostTypes.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;