mattermost-redux
Version:
Common code (API client, Redux stores, logic, utility functions) for building a Mattermost client
99 lines (98 loc) • 4.92 kB
JavaScript
;
// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
// See LICENSE.txt for license information.
Object.defineProperty(exports, "__esModule", { value: true });
exports.getThreadsInChannel = exports.getUnreadThreadOrderInCurrentTeam = exports.getNewestThreadInTeam = exports.getThreadOrderInCurrentTeam = exports.getThreadCountsInCurrentTeam = exports.getUnreadThreadsInCurrentTeam = exports.getThreadsInCurrentTeam = void 0;
exports.getThreadsInTeam = getThreadsInTeam;
exports.getUnreadThreadsInTeam = getUnreadThreadsInTeam;
exports.getThreadCounts = getThreadCounts;
exports.getThreadCountsIncludingDirect = getThreadCountsIncludingDirect;
exports.getThreads = getThreads;
exports.getThread = getThread;
exports.makeGetThreadOrSynthetic = makeGetThreadOrSynthetic;
const threads_1 = require("@mattermost/types/threads");
const create_selector_1 = require("mattermost-redux/selectors/create_selector");
const teams_1 = require("mattermost-redux/selectors/entities/teams");
function getThreadsInTeam(state) {
return state.entities.threads.threadsInTeam;
}
function getUnreadThreadsInTeam(state) {
return state.entities.threads.unreadThreadsInTeam;
}
exports.getThreadsInCurrentTeam = (0, create_selector_1.createSelector)('getThreadsInCurrentTeam', teams_1.getCurrentTeamId, getThreadsInTeam, (currentTeamId, threadsInTeam) => {
return threadsInTeam?.[currentTeamId] ?? [];
});
exports.getUnreadThreadsInCurrentTeam = (0, create_selector_1.createSelector)('getUnreadThreadsInCurrentTeam', teams_1.getCurrentTeamId, getUnreadThreadsInTeam, (currentTeamId, threadsInTeam) => {
return threadsInTeam?.[currentTeamId] ?? [];
});
function getThreadCounts(state) {
return state.entities.threads.counts;
}
function getThreadCountsIncludingDirect(state) {
return state.entities.threads.countsIncludingDirect;
}
exports.getThreadCountsInCurrentTeam = (0, create_selector_1.createSelector)('getThreadCountsInCurrentTeam', teams_1.getCurrentTeamId, getThreadCountsIncludingDirect, (currentTeamId, counts) => {
return counts?.[currentTeamId];
});
function getThreads(state) {
return state.entities.threads.threads;
}
function getThread(state, threadId) {
if (!threadId) {
return null;
}
const threads = getThreads(state);
return threads[threadId];
}
function makeGetThreadOrSynthetic() {
return (0, create_selector_1.createSelector)('getThreadOrSynthetic', (_, rootPost) => rootPost, getThreads, (rootPost, threads) => {
const thread = threads[rootPost.id];
if (thread?.id) {
return thread;
}
return {
id: rootPost.id,
type: threads_1.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 ?? null,
post: {
user_id: rootPost.user_id,
channel_id: rootPost.channel_id,
},
};
});
}
exports.getThreadOrderInCurrentTeam = (0, create_selector_1.createSelector)('getThreadOrderInCurrentTeam', exports.getThreadsInCurrentTeam, getThreads, (threadsInTeam, threads) => {
const ids = [...threadsInTeam.filter((id) => threads[id].is_following)];
return sortByLastReply(ids, threads);
});
exports.getNewestThreadInTeam = (0, create_selector_1.createSelector)('getNewestThreadInTeam', getThreadsInTeam, getThreads, (state, teamID) => teamID, (threadsInTeam, threads, teamID) => {
const threadsInGivenTeam = threadsInTeam?.[teamID] ?? [];
if (!threadsInGivenTeam) {
return null;
}
const ids = [...threadsInGivenTeam.filter((id) => threads[id].is_following)];
return threads[sortByLastReply(ids, threads)[0]];
});
exports.getUnreadThreadOrderInCurrentTeam = (0, create_selector_1.createSelector)('getUnreadThreadOrderInCurrentTeam', exports.getUnreadThreadsInCurrentTeam, getThreads, (threadsInTeam, threads) => {
const ids = threadsInTeam.filter((id) => {
const thread = threads[id];
return thread.is_following && (thread.unread_replies || thread.unread_mentions);
});
return sortByLastReply(ids, threads);
});
function sortByLastReply(ids, threads) {
return ids.filter((id) => threads[id].last_reply_at !== 0).sort((a, b) => threads[b].last_reply_at - threads[a].last_reply_at);
}
exports.getThreadsInChannel = (0, create_selector_1.createSelector)('getThreadsInChannel', getThreads, (_, channelID) => channelID, (threads, channelID) => {
const allThreads = Object.values(threads);
const threadsInChannel = [];
for (const thread of allThreads) {
if (thread && thread.post && thread.post.channel_id && thread.post.channel_id === channelID) {
threadsInChannel.push(thread);
}
}
return threadsInChannel;
});