Skip to content

Commit

Permalink
refactor: Easy replacements of Meteor.call to Meteor.callAsync (#28595)
Browse files Browse the repository at this point in the history
Co-authored-by: Guilherme Gazzo <guilhermegazzo@gmail.com>
  • Loading branch information
rodrigok and ggazzo committed Mar 24, 2023
1 parent 26f7220 commit a6ae079
Show file tree
Hide file tree
Showing 28 changed files with 107 additions and 108 deletions.
4 changes: 2 additions & 2 deletions apps/meteor/app/api/server/api.js
Expand Up @@ -585,7 +585,7 @@ export class APIClass extends Restivus {
'login',
{ authRequired: false },
{
post() {
async post() {
const args = loginCompatibility(this.bodyParams, this.request);
const getUserInfo = self.getHelperMethod('getUserInfo');

Expand All @@ -599,7 +599,7 @@ export class APIClass extends Restivus {

let auth;
try {
auth = DDP._CurrentInvocation.withValue(invocation, () => Meteor.call('login', args));
auth = await DDP._CurrentInvocation.withValue(invocation, () => Meteor.callAsync('login', args));
} catch (error) {
let e = error;
if (error.reason === 'User not found') {
Expand Down
16 changes: 8 additions & 8 deletions apps/meteor/app/api/server/v1/misc.ts
Expand Up @@ -334,10 +334,10 @@ API.v1.addRoute(
validateParams: isSpotlightProps,
},
{
get() {
async get() {
const { query } = this.queryParams;

const result = Meteor.call('spotlight', query);
const result = await Meteor.callAsync('spotlight', query);

return API.v1.success(result);
},
Expand All @@ -351,7 +351,7 @@ API.v1.addRoute(
validateParams: isDirectoryProps,
},
{
get() {
async get() {
const { offset, count } = this.getPaginationItems();
const { sort, query } = this.parseJsonQuery();

Expand All @@ -363,7 +363,7 @@ API.v1.addRoute(
const sortBy = sort ? Object.keys(sort)[0] : undefined;
const sortDirection = sort && Object.values(sort)[0] === 1 ? 'asc' : 'desc';

const result = Meteor.call('browseChannels', {
const result = await Meteor.callAsync('browseChannels', {
text,
type,
workspace,
Expand Down Expand Up @@ -514,7 +514,7 @@ API.v1.addRoute(
validateParams: isMeteorCall,
},
{
post() {
async post() {
check(this.bodyParams, {
message: String,
});
Expand Down Expand Up @@ -551,7 +551,7 @@ API.v1.addRoute(
});
}

const result = Meteor.call(method, ...params);
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 });
Expand All @@ -572,7 +572,7 @@ API.v1.addRoute(
validateParams: isMeteorCall,
},
{
post() {
async post() {
check(this.bodyParams, {
message: String,
});
Expand Down Expand Up @@ -609,7 +609,7 @@ API.v1.addRoute(
});
}

const result = Meteor.call(method, ...params);
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 });
Expand Down
4 changes: 2 additions & 2 deletions apps/meteor/app/api/server/v1/permissions.ts
Expand Up @@ -21,7 +21,7 @@ API.v1.addRoute(
updatedSinceDate = new Date(updatedSince);
}

const result = (await Meteor.call('permissions/get', updatedSinceDate)) as {
const result = (await Meteor.callAsync('permissions/get', updatedSinceDate)) as {
update: IPermission[];
remove: IPermission[];
};
Expand Down Expand Up @@ -72,7 +72,7 @@ API.v1.addRoute(
await Permissions.setRoles(permission._id, permission.roles);
}

const result = (await Meteor.call('permissions/get')) as IPermission[];
const result = (await Meteor.callAsync('permissions/get')) as IPermission[];

return API.v1.success({
permissions: result,
Expand Down
18 changes: 8 additions & 10 deletions apps/meteor/app/api/server/v1/push.ts
Expand Up @@ -12,7 +12,7 @@ API.v1.addRoute(
'push.token',
{ authRequired: true },
{
post() {
async post() {
const { id, type, value, appName } = this.bodyParams;

if (id && typeof id !== 'string') {
Expand All @@ -33,15 +33,13 @@ API.v1.addRoute(
throw new Meteor.Error('error-appName-param-not-valid', 'The required "appName" body param is missing or invalid.');
}

const result = Meteor.runAsUser(this.userId, () =>
Meteor.call('raix:push-update', {
id: deviceId,
token: { [type]: value },
authToken: this.request.headers['x-auth-token'],
appName,
userId: this.userId,
}),
);
const result = await Meteor.callAsync('raix:push-update', {
id: deviceId,
token: { [type]: value },
authToken: this.request.headers['x-auth-token'],
appName,
userId: this.userId,
});

return API.v1.success({ result });
},
Expand Down
2 changes: 1 addition & 1 deletion apps/meteor/app/api/server/v1/roles.ts
Expand Up @@ -79,7 +79,7 @@ API.v1.addRoute(
throw new Meteor.Error('error-user-already-in-role', 'User already in role');
}

await Meteor.call('authorization:addUserToRole', role._id, user.username, roomId);
await Meteor.callAsync('authorization:addUserToRole', role._id, user.username, roomId);

return API.v1.success({
role,
Expand Down
24 changes: 12 additions & 12 deletions apps/meteor/app/api/server/v1/rooms.ts
Expand Up @@ -70,10 +70,10 @@ API.v1.addRoute(
validateParams: isGETRoomsNameExists,
},
{
get() {
async get() {
const { roomName } = this.queryParams;

return API.v1.success({ exists: Meteor.call('roomNameExists', roomName) });
return API.v1.success({ exists: await Meteor.callAsync('roomNameExists', roomName) });
},
},
);
Expand Down Expand Up @@ -114,7 +114,7 @@ API.v1.addRoute(
}
}

let result: { update: IRoom[]; remove: IRoom[] } = await Meteor.call('rooms/get', updatedSinceDate);
let result: { update: IRoom[]; remove: IRoom[] } = await Meteor.callAsync('rooms/get', updatedSinceDate);

if (Array.isArray(result)) {
result = {
Expand Down Expand Up @@ -175,7 +175,7 @@ API.v1.addRoute(

delete fields.description;

await Meteor.call('sendFileMessage', this.urlParams.rid, null, uploadedFile, fields);
await Meteor.callAsync('sendFileMessage', this.urlParams.rid, null, uploadedFile, fields);

const message = await Messages.getMessageByFileIdAndUsername(uploadedFile._id, this.userId);

Expand Down Expand Up @@ -203,7 +203,7 @@ API.v1.addRoute(

await Promise.all(
Object.keys(notifications as Notifications).map(async (notificationKey) =>
Meteor.call('saveNotificationSettings', roomId, notificationKey, notifications[notificationKey as keyof Notifications]),
Meteor.callAsync('saveNotificationSettings', roomId, notificationKey, notifications[notificationKey as keyof Notifications]),
),
);

Expand All @@ -225,7 +225,7 @@ API.v1.addRoute(

const room = await findRoomByIdOrName({ params: this.bodyParams });

await Meteor.call('toggleFavorite', room._id, favorite);
await Meteor.callAsync('toggleFavorite', room._id, favorite);

return API.v1.success();
},
Expand Down Expand Up @@ -259,7 +259,7 @@ API.v1.addRoute(
return API.v1.failure('Body parameter "oldest" is required.');
}

const count = await Meteor.call('cleanRoomHistory', {
const count = await Meteor.callAsync('cleanRoomHistory', {
roomId: _id,
latest: new Date(latest),
oldest: new Date(oldest),
Expand Down Expand Up @@ -300,7 +300,7 @@ API.v1.addRoute(
{
async post() {
const room = await findRoomByIdOrName({ params: this.bodyParams });
await Meteor.call('leaveRoom', room._id);
await Meteor.callAsync('leaveRoom', room._id);

return API.v1.success();
},
Expand Down Expand Up @@ -328,7 +328,7 @@ API.v1.addRoute(
return API.v1.failure('Body parameter "encrypted" must be a boolean when included.');
}

const discussion = await Meteor.call('createDiscussion', {
const discussion = await Meteor.callAsync('createDiscussion', {
prid,
pmid,
t_name,
Expand Down Expand Up @@ -516,7 +516,7 @@ API.v1.addRoute(
async post() {
const { rid, ...params } = this.bodyParams;

const result = await Meteor.call('saveRoomSettings', rid, params);
const result = await Meteor.callAsync('saveRoomSettings', rid, params);

return API.v1.success({ rid: result.rid });
},
Expand All @@ -532,9 +532,9 @@ API.v1.addRoute(

let result;
if (action === 'archive') {
result = await Meteor.call('archiveRoom', rid);
result = await Meteor.callAsync('archiveRoom', rid);
} else {
result = await Meteor.call('unarchiveRoom', rid);
result = await Meteor.callAsync('unarchiveRoom', rid);
}

return API.v1.success({ result });
Expand Down
4 changes: 2 additions & 2 deletions apps/meteor/app/api/server/v1/settings.ts
Expand Up @@ -106,7 +106,7 @@ API.v1.addRoute(
throw new Meteor.Error('error-name-param-not-provided', 'The parameter "name" is required');
}

await Meteor.call('addOAuthService', this.bodyParams.name, this.userId);
await Meteor.callAsync('addOAuthService', this.bodyParams.name, this.userId);

return API.v1.success();
},
Expand Down Expand Up @@ -177,7 +177,7 @@ API.v1.addRoute(

if (isSettingAction(setting) && isSettingsUpdatePropsActions(this.bodyParams) && this.bodyParams.execute) {
// execute the configured method
Meteor.call(setting.value);
await Meteor.callAsync(setting.value);
return API.v1.success();
}

Expand Down
6 changes: 3 additions & 3 deletions apps/meteor/app/api/server/v1/subscriptions.ts
Expand Up @@ -28,7 +28,7 @@ API.v1.addRoute(
updatedSinceDate = new Date(updatedSince as string);
}

const result = await Meteor.call('subscriptions/get', updatedSinceDate);
const result = await Meteor.callAsync('subscriptions/get', updatedSinceDate);

return API.v1.success(
Array.isArray(result)
Expand Down Expand Up @@ -96,8 +96,8 @@ API.v1.addRoute(
validateParams: isSubscriptionsUnreadProps,
},
{
post() {
Meteor.call('unreadMessages', (this.bodyParams as any).firstUnreadMessage, (this.bodyParams as any).roomId);
async post() {
await Meteor.callAsync('unreadMessages', (this.bodyParams as any).firstUnreadMessage, (this.bodyParams as any).roomId);

return API.v1.success();
},
Expand Down
12 changes: 6 additions & 6 deletions apps/meteor/app/api/server/v1/teams.ts
Expand Up @@ -136,9 +136,9 @@ API.v1.addRoute(
const rooms = await Team.getMatchingTeamRooms(team._id, roomsToRemove);

if (rooms.length) {
rooms.forEach((room) => {
Meteor.call('eraseRoom', room);
});
for await (const room of rooms) {
await Meteor.callAsync('eraseRoom', room);
}
}

await Promise.all([Team.unsetTeamIdOfRooms(this.userId, team._id), Team.removeAllMembersFromTeam(team._id)]);
Expand Down Expand Up @@ -618,9 +618,9 @@ API.v1.addRoute(

// If we got a list of rooms to delete along with the team, remove them first
if (rooms.length) {
rooms.forEach((room) => {
Meteor.call('eraseRoom', room);
});
for await (const room of rooms) {
await Meteor.callAsync('eraseRoom', room);
}
}

// Move every other room back to the workspace
Expand Down

0 comments on commit a6ae079

Please sign in to comment.