forked from mattermost/mattermost-webapp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
post_utils.js
105 lines (89 loc) · 3.26 KB
/
post_utils.js
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
// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
// See LICENSE.txt for license information.
import {PostTypes} from 'mattermost-redux/action_types';
import {
markChannelAsRead,
markChannelAsUnread,
markChannelAsViewed,
} from 'mattermost-redux/actions/channels';
import * as PostActions from 'mattermost-redux/actions/posts';
import * as PostSelectors from 'mattermost-redux/selectors/entities/posts';
import {getCurrentChannelId} from 'mattermost-redux/selectors/entities/channels';
import {getCurrentUserId} from 'mattermost-redux/selectors/entities/users';
import {
isFromWebhook,
isSystemMessage,
shouldIgnorePost,
} from 'mattermost-redux/utils/post_utils';
import {sendDesktopNotification} from 'actions/notification_actions.jsx';
import {ActionTypes} from 'utils/constants';
export function completePostReceive(post, websocketMessageProps) {
return async (dispatch, getState) => {
const state = getState();
const rootPost = PostSelectors.getPost(state, post.root_id);
if (post.root_id && !rootPost) {
const {data: posts} = await dispatch(PostActions.getPostThread(post.root_id));
if (posts) {
dispatch(lastPostActions(post, websocketMessageProps));
}
return;
}
dispatch(lastPostActions(post, websocketMessageProps));
};
}
export function lastPostActions(post, websocketMessageProps) {
return (dispatch, getState) => {
const currentChannelId = getCurrentChannelId(getState());
if (post.channel_id === currentChannelId) {
dispatch({
type: ActionTypes.INCREASE_POST_VISIBILITY,
data: post.channel_id,
amount: 1,
});
}
// Need manual dispatch to remove pending post
dispatch({
type: PostTypes.RECEIVED_POSTS,
data: {
order: [],
posts: {
[post.id]: post,
},
},
channelId: post.channel_id,
});
// Still needed to update unreads
dispatch(setChannelReadAndView(post, websocketMessageProps));
dispatch(sendDesktopNotification(post, websocketMessageProps));
};
}
export function setChannelReadAndView(post, websocketMessageProps) {
return (dispatch, getState) => {
const state = getState();
if (shouldIgnorePost(post)) {
return;
}
let markAsRead = false;
let markAsReadOnServer = false;
if (
post.user_id === getCurrentUserId(state) &&
!isSystemMessage(post) &&
!isFromWebhook(post)
) {
markAsRead = true;
markAsReadOnServer = false;
} else if (
post.channel_id === getCurrentChannelId(state) &&
window.isActive
) {
markAsRead = true;
markAsReadOnServer = true;
}
if (markAsRead) {
dispatch(markChannelAsRead(post.channel_id, null, markAsReadOnServer));
dispatch(markChannelAsViewed(post.channel_id));
} else {
dispatch(markChannelAsUnread(websocketMessageProps.team_id, post.channel_id, websocketMessageProps.mentions));
}
};
}