CRT fixes for MM-44210 & MM-44156 (#6295)

* CRT fixes for MM-44210 & MM-44156

* Covered other conditions and renamed variables to be consistent

Co-authored-by: Anurag Shivarathri <anurag6713@gmail.com>
This commit is contained in:
Elias Nahum
2022-05-19 20:58:12 -04:00
committed by GitHub
parent 4573732fd2
commit dfe2b7cfbd
12 changed files with 48 additions and 81 deletions

View File

@@ -36,7 +36,7 @@ export interface ChannelHandlerMix {
handleChannelMembership: ({channelMemberships, prepareRecordsOnly}: HandleChannelMembershipArgs) => Promise<ChannelMembershipModel[]>;
handleMyChannelSettings: ({settings, prepareRecordsOnly}: HandleMyChannelSettingsArgs) => Promise<MyChannelSettingsModel[]>;
handleChannelInfo: ({channelInfos, prepareRecordsOnly}: HandleChannelInfoArgs) => Promise<ChannelInfoModel[]>;
handleMyChannel: ({channels, myChannels, prepareRecordsOnly}: HandleMyChannelArgs) => Promise<MyChannelModel[]>;
handleMyChannel: ({channels, myChannels, isCRTEnabled, prepareRecordsOnly}: HandleMyChannelArgs) => Promise<MyChannelModel[]>;
}
const ChannelHandler = (superclass: any) => class extends superclass {
@@ -138,7 +138,7 @@ const ChannelHandler = (superclass: any) => class extends superclass {
* @throws DataOperatorException
* @returns {Promise<MyChannelModel[]>}
*/
handleMyChannel = async ({channels, myChannels, prepareRecordsOnly = true}: HandleMyChannelArgs): Promise<MyChannelModel[]> => {
handleMyChannel = async ({channels, myChannels, isCRTEnabled, prepareRecordsOnly = true}: HandleMyChannelArgs): Promise<MyChannelModel[]> => {
if (!myChannels?.length) {
// eslint-disable-next-line no-console
console.warn(
@@ -157,7 +157,7 @@ const ChannelHandler = (superclass: any) => class extends superclass {
return [];
}
const isCRTEnabled = await getIsCRTEnabled(this.database);
const isCRT = isCRTEnabled ?? await getIsCRTEnabled(this.database);
const channelMap = channels.reduce((result: Record<string, Channel>, channel) => {
result[channel.id] = channel;
@@ -167,11 +167,13 @@ const ChannelHandler = (superclass: any) => class extends superclass {
for (const my of myChannels) {
const channel = channelMap[my.channel_id];
if (channel) {
const total = isCRTEnabled ? channel.total_msg_count_root! : channel.total_msg_count;
const myMsgCount = isCRTEnabled ? my.msg_count_root! : my.msg_count;
const msgCount = Math.max(0, total - myMsgCount);
const totalMsg = isCRT ? channel.total_msg_count_root! : channel.total_msg_count;
const myMsgCount = isCRT ? my.msg_count_root! : my.msg_count;
const msgCount = Math.max(0, totalMsg - myMsgCount);
my.msg_count = msgCount;
my.mention_count = isCRT ? my.mention_count_root! : my.mention_count;
my.is_unread = msgCount > 0;
my.last_post_at = (isCRT ? (my.last_root_post_at || my.last_post_at) : my.last_post_at) || 0;
}
}

View File

@@ -4,7 +4,6 @@
import {MM_TABLES} from '@constants/database';
import {prepareBaseRecord} from '@database/operator/server_data_operator/transformers/index';
import {extractChannelDisplayName} from '@helpers/database';
import {getIsCRTEnabled} from '@queries/servers/thread';
import {OperationType} from '@typings/database/enums';
import type {TransformerArgs} from '@typings/database/database';
@@ -126,18 +125,16 @@ export const transformMyChannelRecord = async ({action, database, value}: Transf
const record = value.record as MyChannelModel;
const isCreateAction = action === OperationType.CREATE;
const isCRTEnabled = await getIsCRTEnabled(database);
const fieldsMapper = (myChannel: MyChannelModel) => {
myChannel._raw.id = isCreateAction ? (raw.channel_id || myChannel.id) : record.id;
myChannel.roles = raw.roles;
// ignoring msg_count_root because msg_count is already calculated in "handleMyChannel" based on CRT is enabled or not
// ignoring msg_count_root because msg_count, mention_count, last_post_at is already calculated in "handleMyChannel" based on CRT is enabled or not
myChannel.messageCount = raw.msg_count;
myChannel.mentionsCount = raw.mention_count;
myChannel.lastPostAt = raw.last_post_at || 0;
myChannel.isUnread = Boolean(raw.is_unread);
myChannel.mentionsCount = isCRTEnabled ? raw.mention_count_root! : raw.mention_count;
myChannel.lastPostAt = (isCRTEnabled ? (raw.last_root_post_at || raw.last_post_at) : raw.last_post_at) || 0;
myChannel.lastViewedAt = raw.last_viewed_at;
myChannel.viewedAt = record?.viewedAt || 0;
};