forked from mattermost/mattermost-webapp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
threads.ts
168 lines (142 loc) · 5.33 KB
/
threads.ts
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
// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
// See LICENSE.txt for license information.
import {createSelector} from 'reselect';
import {getMyChannels} from 'mattermost-redux/selectors/entities/channels';
import {getCurrentTeamId} from 'mattermost-redux/selectors/entities/teams';
import {GlobalState} from 'mattermost-redux/types/store';
import {Team} from 'mattermost-redux/types/teams';
import {UserThread, ThreadsState, UserThreadType, UserThreadSynthetic} from 'mattermost-redux/types/threads';
import {Post} from 'mattermost-redux/types/posts';
import {$ID, IDMappedObjects, RelationOneToMany} from 'mattermost-redux/types/utilities';
export function getThreadsInTeam(state: GlobalState): RelationOneToMany<Team, UserThread> {
return state.entities.threads.threadsInTeam;
}
export function getUnreadThreadsInTeam(state: GlobalState): RelationOneToMany<Team, UserThread> {
return state.entities.threads.unreadThreadsInTeam;
}
export const getThreadsInCurrentTeam: (state: GlobalState) => Array<$ID<UserThread>> = createSelector(
'getThreadsInCurrentTeam',
getCurrentTeamId,
getThreadsInTeam,
(
currentTeamId,
threadsInTeam,
) => {
return threadsInTeam?.[currentTeamId] ?? [];
},
);
export const getUnreadThreadsInCurrentTeam: (state: GlobalState) => Array<$ID<UserThread>> = createSelector(
'getUnreadThreadsInCurrentTeam',
getCurrentTeamId,
getUnreadThreadsInTeam,
(
currentTeamId,
threadsInTeam,
) => {
return threadsInTeam?.[currentTeamId] ?? [];
},
);
export function getThreadCounts(state: GlobalState): ThreadsState['counts'] {
return state.entities.threads.counts;
}
export function getTeamThreadCounts(state: GlobalState, teamId: $ID<Team>): ThreadsState['counts'][$ID<Team>] {
return getThreadCounts(state)[teamId];
}
export const getThreadCountsInCurrentTeam: (state: GlobalState) => ThreadsState['counts'][$ID<Team>] = createSelector(
'getThreadCountsInCurrentTeam',
getCurrentTeamId,
getThreadCounts,
(
currentTeamId,
counts,
) => {
return counts?.[currentTeamId];
},
);
export function getThreads(state: GlobalState): IDMappedObjects<UserThread> {
return state.entities.threads.threads;
}
export function getThread(state: GlobalState, threadId?: $ID<UserThread>) {
const threads = getThreads(state);
const myChannels = getMyChannels(state).map((c) => c.id);
if (!threadId) {
return null;
}
const thread = threads[threadId];
if (!thread || (thread.post && myChannels.indexOf(thread.post.channel_id) === -1)) {
return null;
}
return thread;
}
export function getThreadOrSynthetic(state: GlobalState, rootPost: Post): UserThread | UserThreadSynthetic {
const thread = getThreads(state)[rootPost.id];
if (thread?.id) {
return thread;
}
return {
id: rootPost.id,
type: UserThreadType.Synthetic,
reply_count: rootPost.reply_count,
participants: rootPost.participants,
last_reply_at: rootPost.last_reply_at ?? 0,
is_following: thread?.is_following ?? rootPost.is_following ?? false,
post: {
user_id: rootPost.user_id,
channel_id: rootPost.channel_id,
},
};
}
export const getThreadOrderInCurrentTeam: (state: GlobalState, selectedThreadIdInTeam?: $ID<UserThread>) => Array<$ID<UserThread>> = createSelector(
'getThreadOrderInCurrentTeam',
getThreadsInCurrentTeam,
getThreads,
(state: GlobalState, selectedThreadIdInTeam?: $ID<UserThread>) => selectedThreadIdInTeam,
(
threadsInTeam,
threads,
selectedThreadIdInTeam,
) => {
const ids = [...threadsInTeam.filter((id) => threads[id].is_following)];
if (selectedThreadIdInTeam && !ids.includes(selectedThreadIdInTeam)) {
ids.push(selectedThreadIdInTeam);
}
return sortByLastReply(ids, threads);
},
);
export const getUnreadThreadOrderInCurrentTeam: (
state: GlobalState,
selectedThreadIdInTeam?: $ID<UserThread>,
) => Array<$ID<UserThread>> = createSelector(
'getUnreadThreadOrderInCurrentTeam',
getUnreadThreadsInCurrentTeam,
getThreads,
(state: GlobalState, selectedThreadIdInTeam?: $ID<UserThread>) => selectedThreadIdInTeam,
(
threadsInTeam,
threads,
selectedThreadIdInTeam,
) => {
const ids = threadsInTeam.filter((id) => {
const thread = threads[id];
return thread.is_following && (thread.unread_replies || thread.unread_mentions);
});
if (selectedThreadIdInTeam && !ids.includes(selectedThreadIdInTeam)) {
ids.push(selectedThreadIdInTeam);
}
return sortByLastReply(ids, threads);
},
);
function sortByLastReply(ids: Array<$ID<UserThread>>, threads: ReturnType<typeof getThreads>) {
return ids.sort((a, b) => threads[b].last_reply_at - threads[a].last_reply_at);
}
export const getThreadsInChannel: (
state: GlobalState,
channelID: string,
) => Array<$ID<UserThread>> = createSelector(
'getThreadsInChannel',
getThreads,
(state: GlobalState, channelID: string) => channelID,
(allThreads: IDMappedObjects<UserThread>, channelID: string) => {
return Object.keys(allThreads).filter((id) => allThreads[id].post.channel_id === channelID);
},
);