Skip to content
Snippets Groups Projects
Unverified Commit fd4793f6 authored by gabriellsh's avatar gabriellsh Committed by GitHub
Browse files

Regression: Client crashing when updating CachedChatSubscription (#28062)

parent dabbae56
No related branches found
No related tags found
No related merge requests found
import type { IOmnichannelRoom, IRoomWithRetentionPolicy } from '@rocket.chat/core-typings';
import type { IOmnichannelRoom, IRoomWithRetentionPolicy, ISubscription } from '@rocket.chat/core-typings';
import { DEFAULT_SLA_CONFIG, LivechatPriorityWeight } from '@rocket.chat/core-typings';
import { CachedCollection } from '../../../ui-cached-collection/client';
......@@ -6,16 +6,16 @@ import type { SubscriptionWithRoom } from '../../../../client/definitions/Subscr
import { ChatRoom } from './ChatRoom';
import { CachedChatRoom } from './CachedChatRoom';
class CachedChatSubscription extends CachedCollection<SubscriptionWithRoom> {
class CachedChatSubscription extends CachedCollection<SubscriptionWithRoom, ISubscription> {
constructor() {
super({ name: 'subscriptions' });
}
protected handleLoadFromServer(record: SubscriptionWithRoom) {
protected handleLoadFromServer(record: ISubscription) {
return this.mergeWithRoom(record);
}
protected handleReceived(record: SubscriptionWithRoom, action: 'changed' | 'removed') {
protected handleReceived(record: ISubscription, action: 'changed' | 'removed') {
const newRecord = this.mergeWithRoom(record);
if (action === 'removed') {
......@@ -26,11 +26,11 @@ class CachedChatSubscription extends CachedCollection<SubscriptionWithRoom> {
return newRecord;
}
protected handleSync(record: SubscriptionWithRoom) {
protected handleSync(record: ISubscription) {
return this.mergeWithRoom(record);
}
private mergeWithRoom(subscription: SubscriptionWithRoom): SubscriptionWithRoom {
private mergeWithRoom(subscription: ISubscription): SubscriptionWithRoom {
const options = {
fields: {
lm: 1,
......@@ -75,7 +75,7 @@ class CachedChatSubscription extends CachedCollection<SubscriptionWithRoom> {
const room = ChatRoom.findOne({ _id: subscription.rid }, options);
const lastRoomUpdate = room?.lm || subscription.ts;
const lastRoomUpdate = room?.lm || subscription.ts || room?.ts;
return {
...subscription,
......@@ -124,7 +124,7 @@ class CachedChatSubscription extends CachedCollection<SubscriptionWithRoom> {
source: (room as IOmnichannelRoom | undefined)?.source,
queuedAt: (room as IOmnichannelRoom | undefined)?.queuedAt,
federated: room?.federated,
lm: subscription.lr ? new Date(Math.max(subscription.lr.getTime(), lastRoomUpdate.getTime())) : lastRoomUpdate,
lm: subscription.lr ? new Date(Math.max(subscription.lr.getTime(), lastRoomUpdate?.getTime() || 0)) : lastRoomUpdate,
};
}
......
......@@ -33,7 +33,7 @@ const hasUnserializedUpdatedAt = <T>(record: T): record is T & { _updatedAt: Con
'_updatedAt' in record &&
!((record as unknown as { _updatedAt: unknown })._updatedAt instanceof Date);
export class CachedCollection<T extends object> extends Emitter<{ changed: T; removed: T }> {
export class CachedCollection<T extends object, U = T> extends Emitter<{ changed: T; removed: T }> {
private static MAX_CACHE_TIME = 60 * 60 * 24 * 30;
public collection: MinimongoCollection<T>;
......@@ -147,13 +147,13 @@ export class CachedCollection<T extends object> extends Emitter<{ changed: T; re
private async callLoad() {
// TODO: workaround for bad function overload
const data = await call(`${this.name}/get`);
return data as unknown as T[];
return data as unknown as U[];
}
private async callSync(updatedSince: Date) {
// TODO: workaround for bad function overload
const data = await call(`${this.name}/get`, updatedSince);
return data as unknown as { update: T[]; remove: T[] };
return data as unknown as { update: U[]; remove: U[] };
}
private async loadFromServer() {
......@@ -179,16 +179,16 @@ export class CachedCollection<T extends object> extends Emitter<{ changed: T; re
this.updatedAt = this.updatedAt === lastTime ? startTime : this.updatedAt;
}
protected handleLoadFromServer(record: T) {
return record;
protected handleLoadFromServer(record: U): T {
return record as unknown as T;
}
protected handleReceived(record: T, _action: 'removed' | 'changed') {
return record;
protected handleReceived(record: U, _action: 'removed' | 'changed'): T {
return record as unknown as T;
}
protected handleSync(record: T, _action: 'removed' | 'changed') {
return record;
protected handleSync(record: U, _action: 'removed' | 'changed'): T {
return record as unknown as T;
}
private async loadFromServerAndPopulate() {
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment