mattermost-redux
Version:
Common code (API client, Redux stores, logic, utility functions) for building a Mattermost client
72 lines (71 loc) • 2.73 kB
JavaScript
;
// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
// See LICENSE.txt for license information.
Object.defineProperty(exports, "__esModule", { value: true });
exports.default = messageCounts;
exports.updateMessageCount = updateMessageCount;
const action_types_1 = require("mattermost-redux/action_types");
const constants_1 = require("mattermost-redux/constants");
function messageCounts(state = {}, action) {
switch (action.type) {
case action_types_1.ChannelTypes.RECEIVED_CHANNEL: {
const channel = action.data;
return updateMessageCount(state, channel);
}
case action_types_1.AdminTypes.RECEIVED_DATA_RETENTION_CUSTOM_POLICY_CHANNELS: {
const channels = action.data.channels;
return channels.reduce(updateMessageCount, state);
}
case action_types_1.AdminTypes.RECEIVED_DATA_RETENTION_CUSTOM_POLICY_CHANNELS_SEARCH:
case action_types_1.ChannelTypes.RECEIVED_CHANNELS:
case action_types_1.ChannelTypes.RECEIVED_ALL_CHANNELS:
case action_types_1.SchemeTypes.RECEIVED_SCHEME_CHANNELS: {
const channels = action.data;
return channels.reduce(updateMessageCount, state);
}
case action_types_1.ChannelTypes.LEAVE_CHANNEL: {
const channel = action.data;
if (!channel || channel.type !== constants_1.General.OPEN_CHANNEL) {
return state;
}
const nextState = { ...state };
Reflect.deleteProperty(nextState, channel.id);
return nextState;
}
case action_types_1.ChannelTypes.INCREMENT_TOTAL_MSG_COUNT: {
const channelId = action.data.channelId;
const amount = action.data.amount;
const amountRoot = action.data.amountRoot;
const existing = state[channelId];
if (!existing) {
return state;
}
return {
...state,
[channelId]: {
root: existing.root + amountRoot,
total: existing.total + amount,
},
};
}
case action_types_1.UserTypes.LOGOUT_SUCCESS:
return {};
default:
return state;
}
}
function updateMessageCount(state, channel) {
const existing = state[channel.id];
if (existing &&
existing.root === channel.total_msg_count_root &&
existing.total === channel.total_msg_count) {
return state;
}
return {
...state,
[channel.id]: {
root: channel.total_msg_count_root,
total: channel.total_msg_count,
},
};
}