Skip to content

Commit 5800622

Browse files
committed
chore: adjust Rooms store usage in UserProvider and create convertSort
1 parent 42553fc commit 5800622

File tree

3 files changed

+59
-7
lines changed

3 files changed

+59
-7
lines changed

apps/meteor/client/lib/cachedCollections/pipe.ts

Lines changed: 37 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -68,11 +68,44 @@ export function pipe<D>(
6868
};
6969
}
7070

71-
export const applyQueryOptions = <T extends { _id: string }>(records: T[], options: FindOptions<T>): T[] => {
71+
type OriginalStructure = FindOptions['sort'];
72+
73+
type SortField = 'lm' | 'lowerCaseFName' | 'lowerCaseName';
74+
type SortDirection = -1 | 1;
75+
76+
type SortObject = {
77+
field: SortField;
78+
direction: SortDirection;
79+
}[];
80+
81+
/**
82+
* Converts a MongoDB-style sort structure to a sort object.
83+
*/
84+
export const convertSort = (original: OriginalStructure): SortObject => {
85+
const convertedSort: SortObject = [];
86+
87+
if (!original) {
88+
return convertedSort;
89+
}
90+
Object.keys(original as Record<string, any>).forEach((key) => {
91+
const direction = original[key as keyof OriginalStructure];
92+
93+
if (direction === -1 || direction === 1) {
94+
convertedSort.push({
95+
field: key as SortField,
96+
direction,
97+
});
98+
}
99+
});
100+
return convertedSort;
101+
};
102+
103+
export const applyQueryOptions = <T extends Record<string, any>>(records: T[], options: FindOptions): T[] => {
72104
let currentPipeline = pipe(records);
73-
if (options.sort && options.sort.length > 0) {
74-
for (let i = options.sort.length - 1; i >= 0; i--) {
75-
const { field, direction } = options.sort[i];
105+
if (options.sort) {
106+
const sortObj = convertSort(options.sort);
107+
for (let i = sortObj.sort.length - 1; i >= 0; i--) {
108+
const { field, direction } = sortObj[i];
76109
currentPipeline = currentPipeline.sortByField(field, direction);
77110
}
78111
}

apps/meteor/client/providers/UserProvider/UserProvider.tsx

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import type { IRoom, ISubscription, IUser } from '@rocket.chat/core-typings';
22
import { Emitter } from '@rocket.chat/emitter';
33
import { useLocalStorage } from '@rocket.chat/fuselage-hooks';
4+
import { createPredicateFromFilter } from '@rocket.chat/mongo-adapter';
45
import type { SubscriptionWithRoom } from '@rocket.chat/ui-contexts';
56
import { UserContext, useEndpoint, useRouteParameter, useSearchParameter } from '@rocket.chat/ui-contexts';
67
import { useQueryClient } from '@tanstack/react-query';
@@ -20,6 +21,7 @@ import { sdk } from '../../../app/utils/client/lib/SDKClient';
2021
import { afterLogoutCleanUpCallback } from '../../../lib/callbacks/afterLogoutCleanUpCallback';
2122
import { useIdleConnection } from '../../hooks/useIdleConnection';
2223
import { useReactiveValue } from '../../hooks/useReactiveValue';
24+
import { applyQueryOptions } from '../../lib/cachedCollections/pipe';
2325
import { createReactiveSubscriptionFactory } from '../../lib/createReactiveSubscriptionFactory';
2426
import { useSamlInviteToken } from '../../views/invite/hooks/useSamlInviteToken';
2527

@@ -74,13 +76,28 @@ const UserProvider = ({ children }: UserProviderProps): ReactElement => {
7476
querySubscription: createReactiveSubscriptionFactory<ISubscription | undefined>((query, fields, sort) =>
7577
Subscriptions.findOne(query, { fields, sort }),
7678
),
77-
queryRoom: createReactiveSubscriptionFactory<IRoom | undefined>((query, fields) => Rooms.findOne(query, { fields })),
79+
queryRoom: createReactiveSubscriptionFactory<IRoom | undefined>((query) => {
80+
const predicate = createPredicateFromFilter(query);
81+
return Rooms.state.find(predicate);
82+
}),
7883
querySubscriptions: createReactiveSubscriptionFactory<SubscriptionWithRoom[]>((query, options) => {
7984
if (userId) {
80-
return Subscriptions.find(query, options).fetch();
85+
const predicate = createPredicateFromFilter(query);
86+
const subs = Subscriptions.state.filter(predicate);
87+
88+
if (options?.sort || options?.limit || options?.skip) {
89+
return applyQueryOptions(subs, options);
90+
}
91+
return subs;
8192
}
93+
// Anonnymous users don't have subscriptions. Fetch Rooms instead.
94+
const predicate = createPredicateFromFilter(query);
95+
const rooms = Rooms.state.filter(predicate) as unknown as SubscriptionWithRoom[]; // FIXME: this is a hack to make the typings work. How was it working before?
8296

83-
return Rooms.find(query, options).fetch();
97+
if (options?.sort || options?.limit) {
98+
return applyQueryOptions(rooms, options);
99+
}
100+
return rooms;
84101
}),
85102
logout: async () => Meteor.logout(),
86103
onLogout: (cb) => {

packages/ui-contexts/src/UserContext.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@ export type Sort<TSchema extends Document = Document> = Exclude<MongoFindOptions
2424
export type FindOptions<TSchema extends Document = Document> = {
2525
fields?: Fields<TSchema>;
2626
sort?: Sort<TSchema>;
27+
skip?: number;
28+
limit?: number;
2729
};
2830

2931
export type UserContextValue = {

0 commit comments

Comments
 (0)