Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/develop' into refactor/subscript…
Browse files Browse the repository at this point in the history
…ions-model-6
  • Loading branch information
ggazzo committed Mar 30, 2023
2 parents 67c0b60 + 35499f5 commit e92476a
Show file tree
Hide file tree
Showing 87 changed files with 590 additions and 1,060 deletions.
10 changes: 7 additions & 3 deletions apps/meteor/app/api/server/v1/misc.ts
Expand Up @@ -558,7 +558,9 @@ API.v1.addRoute(
const result = await Meteor.callAsync(method, ...params);
return API.v1.success(mountResult({ id, result }));
} catch (err) {
SystemLogger.error({ msg: `Exception while invoking method ${method}`, err });
if (!(err as any).isClientSafe && !(err as any).meteorError) {
SystemLogger.error({ msg: `Exception while invoking method ${method}`, err });
}

if (settings.get('Log_Level') === '2') {
Meteor._debug(`Exception while invoking method ${method}`, err);
Expand Down Expand Up @@ -606,6 +608,7 @@ API.v1.addRoute(

try {
DDPRateLimiter._increment(rateLimiterInput);

const rateLimitResult = DDPRateLimiter._check(rateLimiterInput);
if (!rateLimitResult.allowed) {
throw new Meteor.Error('too-many-requests', DDPRateLimiter.getErrorMessage(rateLimitResult), {
Expand All @@ -616,8 +619,9 @@ API.v1.addRoute(
const result = await Meteor.callAsync(method, ...params);
return API.v1.success(mountResult({ id, result }));
} catch (err) {
SystemLogger.error({ msg: `Exception while invoking method ${method}`, err });

if (!(err as any).isClientSafe && !(err as any).meteorError) {
SystemLogger.error({ msg: `Exception while invoking method ${method}`, err });
}
if (settings.get('Log_Level') === '2') {
Meteor._debug(`Exception while invoking method ${method}`, err);
}
Expand Down
28 changes: 14 additions & 14 deletions apps/meteor/app/apps/server/bridges/persistence.ts
Expand Up @@ -12,7 +12,7 @@ export class AppPersistenceBridge extends PersistenceBridge {
protected async purge(appId: string): Promise<void> {
this.orch.debugLog(`The App's persistent storage is being purged: ${appId}`);

this.orch.getPersistenceModel().remove({ appId });
await this.orch.getPersistenceModel().remove({ appId });
}

protected async create(data: object, appId: string): Promise<string> {
Expand All @@ -22,7 +22,7 @@ export class AppPersistenceBridge extends PersistenceBridge {
throw new Error('Attempted to store an invalid data type, it must be an object.');
}

return this.orch.getPersistenceModel().insert({ appId, data });
return this.orch.getPersistenceModel().insertOne({ appId, data });
}

protected async createWithAssociations(data: object, associations: Array<RocketChatAssociationRecord>, appId: string): Promise<string> {
Expand All @@ -36,41 +36,41 @@ export class AppPersistenceBridge extends PersistenceBridge {
throw new Error('Attempted to store an invalid data type, it must be an object.');
}

return this.orch.getPersistenceModel().insert({ appId, associations, data });
return this.orch.getPersistenceModel().insertOne({ appId, associations, data });
}

protected async readById(id: string, appId: string): Promise<object> {
this.orch.debugLog(`The App ${appId} is reading their data in their persistence with the id: "${id}"`);

const record = this.orch.getPersistenceModel().findOneById(id);
const record = await this.orch.getPersistenceModel().findOneById(id);

return record.data;
return record?.data;
}

protected async readByAssociations(associations: Array<RocketChatAssociationRecord>, appId: string): Promise<Array<object>> {
this.orch.debugLog(`The App ${appId} is searching for records that are associated with the following:`, associations);

const records = this.orch
const records = await this.orch
.getPersistenceModel()
.find({
appId,
associations: { $all: associations },
})
.fetch();
.toArray();

return Array.isArray(records) ? records.map((r) => r.data) : [];
}

protected async remove(id: string, appId: string): Promise<object | undefined> {
this.orch.debugLog(`The App ${appId} is removing one of their records by the id: "${id}"`);

const record = this.orch.getPersistenceModel().findOne({ _id: id, appId });
const record = await this.orch.getPersistenceModel().findOne({ _id: id, appId });

if (!record) {
return undefined;
}

this.orch.getPersistenceModel().remove({ _id: id, appId });
await this.orch.getPersistenceModel().remove({ _id: id, appId });

return record.data;
}
Expand All @@ -88,13 +88,13 @@ export class AppPersistenceBridge extends PersistenceBridge {
},
};

const records = this.orch.getPersistenceModel().find(query).fetch();
const records = await this.orch.getPersistenceModel().find(query).toArray();

if (!records) {
if (!records || !records.length) {
return undefined;
}

this.orch.getPersistenceModel().remove(query);
await this.orch.getPersistenceModel().remove(query);

return Array.isArray(records) ? records.map((r) => r.data) : [];
}
Expand All @@ -112,7 +112,7 @@ export class AppPersistenceBridge extends PersistenceBridge {
protected async updateByAssociations(
associations: Array<RocketChatAssociationRecord>,
data: object,
upsert: boolean,
upsert = true,
appId: string,
): Promise<string> {
this.orch.debugLog(`The App ${appId} is updating the record with association to data as follows:`, associations, data);
Expand All @@ -126,6 +126,6 @@ export class AppPersistenceBridge extends PersistenceBridge {
associations,
};

return this.orch.getPersistenceModel().upsert(query, { $set: { data } }, { upsert });
return this.orch.getPersistenceModel().update(query, { $set: { data } }, { upsert });
}
}
16 changes: 8 additions & 8 deletions apps/meteor/app/apps/server/bridges/rooms.ts
Expand Up @@ -5,10 +5,10 @@ import type { IUser } from '@rocket.chat/apps-engine/definition/users';
import type { IMessage } from '@rocket.chat/apps-engine/definition/messages';
import { Meteor } from 'meteor/meteor';
import type { ISubscription, IUser as ICoreUser } from '@rocket.chat/core-typings';
import { Subscriptions } from '@rocket.chat/models';
import { Subscriptions, Rooms } from '@rocket.chat/models';

import type { AppServerOrchestrator } from '../../../../ee/server/apps/orchestrator';
import { Rooms, Users } from '../../../models/server';
import { Users } from '../../../models/server';
import { addUserToRoom } from '../../../lib/server/functions/addUserToRoom';
import { deleteRoom } from '../../../lib/server/functions/deleteRoom';

Expand Down Expand Up @@ -72,7 +72,7 @@ export class AppRoomBridge extends RoomBridge {
protected async getCreatorById(roomId: string, appId: string): Promise<IUser | undefined> {
this.orch.debugLog(`The App ${appId} is getting the room's creator by id: "${roomId}"`);

const room = Rooms.findOneById(roomId);
const room = await Rooms.findOneById(roomId);

if (!room || !room.u || !room.u._id) {
return undefined;
Expand All @@ -84,7 +84,7 @@ export class AppRoomBridge extends RoomBridge {
protected async getCreatorByName(roomName: string, appId: string): Promise<IUser | undefined> {
this.orch.debugLog(`The App ${appId} is getting the room's creator by name: "${roomName}"`);

const room = Rooms.findOneByName(roomName, {});
const room = await Rooms.findOneByName(roomName, {});

if (!room || !room.u || !room.u._id) {
return undefined;
Expand All @@ -111,13 +111,13 @@ export class AppRoomBridge extends RoomBridge {
protected async update(room: IRoom, members: Array<string> = [], appId: string): Promise<void> {
this.orch.debugLog(`The App ${appId} is updating a room.`);

if (!room.id || !Rooms.findOneById(room.id)) {
if (!room.id || !(await Rooms.findOneById(room.id))) {
throw new Error('A room must exist to update.');
}

const rm = await this.orch.getConverters()?.get('rooms').convertAppRoom(room);

Rooms.update(rm._id, rm);
await Rooms.updateOne({ _id: rm._id }, { $set: rm });

for await (const username of members) {
const member = Users.findOneByUsername(username, {});
Expand All @@ -132,7 +132,7 @@ export class AppRoomBridge extends RoomBridge {

protected async delete(roomId: string, appId: string): Promise<void> {
this.orch.debugLog(`The App ${appId} is deleting a room.`);
deleteRoom(roomId);
await deleteRoom(roomId);
}

protected async createDiscussion(
Expand All @@ -151,7 +151,7 @@ export class AppRoomBridge extends RoomBridge {
rcMessage = this.orch.getConverters()?.get('messages').convertAppMessage(parentMessage);
}

if (!rcRoom.prid || !Rooms.findOneById(rcRoom.prid)) {
if (!rcRoom.prid || !(await Rooms.findOneById(rcRoom.prid))) {
throw new Error('There must be a parent room to create a discussion.');
}

Expand Down
Expand Up @@ -13,7 +13,7 @@ const updateFName = async (rid, displayName) => {
};

const updateRoomName = async (rid, displayName) => {
const slugifiedRoomName = getValidRoomName(displayName, rid);
const slugifiedRoomName = await getValidRoomName(displayName, rid);

// Check if the username is available
if (!(await checkUsernameAvailability(slugifiedRoomName))) {
Expand Down
Expand Up @@ -51,7 +51,7 @@ callbacks.add(
}
}
if (message.drid) {
deleteRoom(message.drid);
await deleteRoom(message.drid);
}
return message;
},
Expand All @@ -61,7 +61,7 @@ callbacks.add(

callbacks.add(
'afterDeleteRoom',
(rid) => {
async (rid) => {
RoomsSync.find({ prid: rid }, { fields: { _id: 1 } }).forEach(({ _id }: Pick<IRoom, '_id'>) => deleteRoom(_id));
return rid;
},
Expand Down
2 changes: 1 addition & 1 deletion apps/meteor/app/importer-slack/server/importer.js
Expand Up @@ -268,7 +268,7 @@ export class SlackImporter extends Base {
await this.updateRecord({ messagesstatus: `${channel}/${date}` });
await this.addCountToTotal(tempMessages.length);

const slackChannelId = Promise.await(ImportData.findChannelImportIdByNameOrImportId(channel));
const slackChannelId = await ImportData.findChannelImportIdByNameOrImportId(channel);

if (slackChannelId) {
for await (const message of tempMessages) {
Expand Down

0 comments on commit e92476a

Please sign in to comment.