Skip to content

Commit

Permalink
Merge branch 'develop' into refactor/test-pageobjects
Browse files Browse the repository at this point in the history
  • Loading branch information
souzaramon authored Jul 13, 2022
2 parents 8080885 + bf6068e commit 9ee843e
Show file tree
Hide file tree
Showing 77 changed files with 425 additions and 878 deletions.
1 change: 0 additions & 1 deletion apps/meteor/app/api/server/api.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,6 @@ type ActionThis<TMethod extends Method, TPathPattern extends PathPattern, TOptio
getUserInfo(
me: IUser,
): TOptions extends { authRequired: true } ? UserInfo : TOptions extends { authOrAnonRequired: true } ? UserInfo | undefined : undefined;
insertUserObject<T>({ object, userId }: { object: { [key: string]: unknown }; userId: string }): { [key: string]: unknown } & T;
composeRoomWithLastMessage(room: IRoom, userId: string): IRoom;
} & (TOptions extends { authRequired: true }
? {
Expand Down
19 changes: 19 additions & 0 deletions apps/meteor/app/api/server/helpers/addUserToFileObj.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { IUpload, IUser } from '@rocket.chat/core-typings';
import { Users } from '@rocket.chat/models';

export async function addUserToFileObj(files: IUpload[]): Promise<(IUpload & { user?: Pick<IUser, '_id' | 'name' | 'username'> })[]> {
const uids = files.map(({ userId }) => userId).filter(Boolean);

const users = await Users.findByIds(uids, { projection: { name: 1, username: 1 } }).toArray();

return files.map((file) => {
const user = users.find(({ _id: userId }) => file.userId && userId === file.userId);
if (!user) {
return file;
}
return {
...file,
user,
};
});
}
21 changes: 0 additions & 21 deletions apps/meteor/app/api/server/helpers/insertUserObject.ts

This file was deleted.

1 change: 0 additions & 1 deletion apps/meteor/app/api/server/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import './helpers/getLoggedInUser';
import './helpers/getPaginationItems';
import './helpers/getUserFromParams';
import './helpers/getUserInfo';
import './helpers/insertUserObject';
import './helpers/isUserFromParams';
import './helpers/parseJsonQuery';
import './helpers/requestParams';
Expand Down
11 changes: 3 additions & 8 deletions apps/meteor/app/api/server/v1/channels.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import { API } from '../api';
import { settings } from '../../../settings/server';
import { Team } from '../../../../server/sdk';
import { findUsersOfRoom } from '../../../../server/lib/findUsersOfRoom';
import { addUserToFileObj } from '../helpers/addUserToFileObj';

// Returns the channel IF found otherwise it will return the failure of why it didn't. Check the `statusCode` property
function findChannelByIdOrName({ params, checkedArchived = true, userId }) {
Expand Down Expand Up @@ -276,12 +277,6 @@ API.v1.addRoute(
params: this.requestParams(),
checkedArchived: false,
});
const addUserObjectToEveryObject = (file) => {
if (file.userId) {
file = this.insertUserObject({ object: file, userId: file.userId });
}
return file;
};

if (!canAccessRoom(findResult, { _id: this.userId })) {
return API.v1.unauthorized();
Expand All @@ -299,10 +294,10 @@ API.v1.addRoute(
projection: fields,
});

const [files, total] = await Promise.all([cursor.map(addUserObjectToEveryObject).toArray(), totalCount]);
const [files, total] = await Promise.all([cursor.toArray(), totalCount]);

return API.v1.success({
files,
files: await addUserToFileObj(files),
count: files.length,
offset,
total,
Expand Down
9 changes: 2 additions & 7 deletions apps/meteor/app/api/server/v1/groups.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import { normalizeMessagesForUser } from '../../../utils/server/lib/normalizeMes
import { API } from '../api';
import { Team } from '../../../../server/sdk';
import { findUsersOfRoom } from '../../../../server/lib/findUsersOfRoom';
import { addUserToFileObj } from '../helpers/addUserToFileObj';

// Returns the private group subscription IF found otherwise it will return the failure of why it didn't. Check the `statusCode` property
export function findPrivateGroupByIdOrName({ params, userId, checkedArchived = true }) {
Expand Down Expand Up @@ -339,12 +340,6 @@ API.v1.addRoute(
userId: this.userId,
checkedArchived: false,
});
const addUserObjectToEveryObject = (file) => {
if (file.userId) {
file = this.insertUserObject({ object: file, userId: file.userId });
}
return file;
};

const { offset, count } = this.getPaginationItems();
const { sort, fields, query } = this.parseJsonQuery();
Expand All @@ -361,7 +356,7 @@ API.v1.addRoute(
const [files, total] = await Promise.all([cursor.toArray(), totalCount]);

return API.v1.success({
files: files.map(addUserObjectToEveryObject),
files: await addUserToFileObj(files),
count: files.length,
offset,
total,
Expand Down
20 changes: 4 additions & 16 deletions apps/meteor/app/api/server/v1/im.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/**
* Docs: https://github.com/RocketChat/developer-docs/blob/master/reference/api/rest-api/endpoints/team-collaboration-endpoints/im-endpoints
*/
import type { IMessage, IRoom, ISetting, ISubscription, IUpload, IUser } from '@rocket.chat/core-typings';
import type { IMessage, IRoom, ISetting, ISubscription, IUpload } from '@rocket.chat/core-typings';
import {
isDmDeleteProps,
isDmFileProps,
Expand All @@ -20,6 +20,7 @@ import { normalizeMessagesForUser } from '../../../utils/server/lib/normalizeMes
import { API } from '../api';
import { getRoomByNameOrIdWithOptionToJoin } from '../../../lib/server/functions/getRoomByNameOrIdWithOptionToJoin';
import { createDirectMessage } from '../../../../server/methods/createDirectMessage';
import { addUserToFileObj } from '../helpers/addUserToFileObj';

interface IImFilesObject extends IUpload {
userId: string;
Expand Down Expand Up @@ -223,23 +224,10 @@ API.v1.addRoute(
projection: fields,
});

const [files, total] = await Promise.all([
cursor
.map((file): IImFilesObject | (IImFilesObject & { user: Pick<IUser, '_id' | 'name' | 'username'> }) => {
if (file.userId) {
return this.insertUserObject<IImFilesObject & { user: Pick<IUser, '_id' | 'name' | 'username'> }>({
object: { ...file },
userId: file.userId,
});
}
return file;
})
.toArray(),
totalCount,
]);
const [files, total] = await Promise.all([cursor.toArray(), totalCount]);

return API.v1.success({
files,
files: await addUserToFileObj(files),
count: files.length,
offset,
total,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,11 @@ Meteor.startup(function () {
Imports.invalidateOperationsExceptId(idToKeep);

// Clean up all the raw import data, except for the last operation
runDrop(() => RawImports.model.rawCollection().remove({ import: { $ne: idToKeep } }));
runDrop(() => RawImports.model.rawCollection().deleteMany({ import: { $ne: idToKeep } }));
} else {
Imports.invalidateAllOperations();

// Clean up all the raw import data
runDrop(() => RawImports.model.rawCollection().remove({}));
runDrop(() => RawImports.model.rawCollection().deleteMany({}));
}
});
20 changes: 10 additions & 10 deletions apps/meteor/app/integrations/server/api/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ function getIntegrationScript(integration) {
const script = integration.scriptCompiled;
const { sandbox, store } = buildSandbox();
try {
incomingLogger.info({ msg: 'Will evaluate script of Trigger', name: integration.name });
incomingLogger.info({ msg: 'Will evaluate script of Trigger', integration: integration.name });
incomingLogger.debug(script);

const vmScript = new VMScript(`${script}; Script;`, 'script.js');
Expand All @@ -89,20 +89,20 @@ function getIntegrationScript(integration) {
} catch (err) {
incomingLogger.error({
msg: 'Error evaluating Script in Trigger',
name: integration.name,
integration: integration.name,
script,
err,
});
throw API.v1.failure('error-evaluating-script');
}

incomingLogger.error({ msg: 'Class "Script" not in Trigger', name: integration.name });
incomingLogger.error({ msg: 'Class "Script" not in Trigger', integration: integration.name });
throw API.v1.failure('class-script-not-found');
}

function createIntegration(options, user) {
incomingLogger.info({ msg: 'Add integration', name: options.name });
incomingLogger.debug(options);
incomingLogger.info({ msg: 'Add integration', integration: options.name });
incomingLogger.debug({ options });

Meteor.runAsUser(user._id, function () {
switch (options.event) {
Expand Down Expand Up @@ -139,7 +139,7 @@ function createIntegration(options, user) {

function removeIntegration(options, user) {
incomingLogger.info('Remove integration');
incomingLogger.debug(options);
incomingLogger.debug({ options });

const integrationToRemove = Promise.await(Integrations.findOneByUrl(options.target_url));
if (!integrationToRemove) {
Expand All @@ -152,7 +152,7 @@ function removeIntegration(options, user) {
}

function executeIntegrationRest() {
incomingLogger.info({ msg: 'Post integration:', name: this.integration.name });
incomingLogger.info({ msg: 'Post integration:', integration: this.integration.name });
incomingLogger.debug({ urlParams: this.urlParams, bodyParams: this.bodyParams });

if (this.integration.enabled !== true) {
Expand Down Expand Up @@ -230,7 +230,7 @@ function executeIntegrationRest() {
if (!result) {
incomingLogger.debug({
msg: 'Process Incoming Request result of Trigger has no data',
name: this.integration.name,
integration: this.integration.name,
});
return API.v1.success();
}
Expand All @@ -246,13 +246,13 @@ function executeIntegrationRest() {

incomingLogger.debug({
msg: 'Process Incoming Request result of Trigger',
name: this.integration.name,
integration: this.integration.name,
result: this.bodyParams,
});
} catch (err) {
incomingLogger.error({
msg: 'Error running Script in Trigger',
name: this.integration.name,
integration: this.integration.name,
script: this.integration.scriptCompiled,
err,
});
Expand Down
10 changes: 5 additions & 5 deletions apps/meteor/app/integrations/server/lib/triggerHandler.js
Original file line number Diff line number Diff line change
Expand Up @@ -274,7 +274,7 @@ export class RocketChatIntegrationHandler {
const { store, sandbox } = this.buildSandbox();

try {
outgoingLogger.info({ msg: 'Will evaluate script of Trigger', name: integration.name });
outgoingLogger.info({ msg: 'Will evaluate script of Trigger', integration: integration.name });
outgoingLogger.debug(script);

const vmScript = new VMScript(`${script}; Script;`, 'script.js');
Expand All @@ -296,7 +296,7 @@ export class RocketChatIntegrationHandler {
} catch (err) {
outgoingLogger.error({
msg: 'Error evaluating Script in Trigger',
name: integration.name,
integration: integration.name,
script,
err,
});
Expand Down Expand Up @@ -385,12 +385,12 @@ export class RocketChatIntegrationHandler {
});
outgoingLogger.error({
msg: 'Error running Script in the Integration',
name: integration.name,
integration: integration.name,
err,
});
outgoingLogger.debug({
msg: 'Error running Script in the Integration',
name: integration.name,
integration: integration.name,
script: integration.scriptCompiled,
}); // Only output the compiled script if debugging is enabled, so the logs don't get spammed.
}
Expand Down Expand Up @@ -708,7 +708,7 @@ export class RocketChatIntegrationHandler {
this.updateHistory({ historyId, step: 'mapped-args-to-data', data, triggerWord: word });

outgoingLogger.info(`Will be executing the Integration "${trigger.name}" to the url: ${url}`);
outgoingLogger.debug(data);
outgoingLogger.debug({ data });

let opts = {
params: {},
Expand Down
2 changes: 1 addition & 1 deletion apps/meteor/app/livechat/server/lib/QueueManager.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ export const QueueManager = {
);
logger.debug(`Generated inquiry for visitor ${guest._id} with id ${inquiry._id} [Not queued]`);

LivechatRooms.updateRoomCount();
await LivechatRooms.updateRoomCount();

await queueInquiry(room, inquiry, agent);
logger.debug(`Inquiry ${inquiry._id} queued`);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { Meteor } from 'meteor/meteor';
import _ from 'underscore';

import { Base } from './_Base';
Expand All @@ -14,9 +13,6 @@ export class LivechatDepartmentAgents extends Base {
this.tryEnsureIndex({ departmentEnabled: 1 });
this.tryEnsureIndex({ agentId: 1 });
this.tryEnsureIndex({ username: 1 });

const collectionObj = this.model.rawCollection();
this.findAndModify = Meteor.wrapAsync(collectionObj.findAndModify, collectionObj);
}

findByDepartmentId(departmentId) {
Expand Down Expand Up @@ -96,7 +92,7 @@ export class LivechatDepartmentAgents extends Base {

const collectionObj = this.model.rawCollection();

const agent = Promise.await(collectionObj.findAndModify(query, sort, update));
const agent = Promise.await(collectionObj.findOneAndUpdate(query, update, { sort, returnNewDocument: 'after' }));
if (agent && agent.value) {
return {
agentId: agent.value.agentId,
Expand Down Expand Up @@ -188,7 +184,7 @@ export class LivechatDepartmentAgents extends Base {
},
};

const bot = this.findAndModify(query, sort, update);
const bot = this.model.rawCollection().findOneAndUpdate(query, update, { sort, returnNewDocument: 'after' });
if (bot && bot.value) {
return {
agentId: bot.value.agentId,
Expand Down
14 changes: 4 additions & 10 deletions apps/meteor/app/models/server/models/LivechatRooms.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import s from 'underscore.string';
import _ from 'underscore';
import { Settings } from '@rocket.chat/models';

import { Base } from './_Base';
import Rooms from './Rooms';
import Settings from './Settings';

export class LivechatRooms extends Base {
constructor(...args) {
Expand Down Expand Up @@ -35,11 +35,6 @@ export class LivechatRooms extends Base {
);
}

findLivechat(filter = {}, offset = 0, limit = 20) {
const query = Object.assign(filter, { t: 'l' });
return this.find(query, { sort: { ts: -1 }, offset, limit });
}

findOneByIdOrName(_idOrName, options) {
const query = {
t: 'l',
Expand Down Expand Up @@ -258,7 +253,7 @@ export class LivechatRooms extends Base {
return this.findOne(query, options);
}

updateRoomCount = function () {
updateRoomCount = async function () {
const query = {
_id: 'Livechat_Room_Count',
};
Expand All @@ -269,9 +264,8 @@ export class LivechatRooms extends Base {
},
};

const livechatCount = Settings.findAndModify(query, null, update);

return livechatCount.value.value;
const livechatCount = await Settings.findOneAndUpdate(query, update, { returnDocument: 'after' });
return livechatCount.value;
};

findOpenByVisitorToken(visitorToken, options) {
Expand Down
5 changes: 0 additions & 5 deletions apps/meteor/app/models/server/models/Settings.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import { Meteor } from 'meteor/meteor';

import { Base } from './_Base';

export class Settings extends Base {
Expand All @@ -8,9 +6,6 @@ export class Settings extends Base {

this.tryEnsureIndex({ blocked: 1 }, { sparse: 1 });
this.tryEnsureIndex({ hidden: 1 }, { sparse: 1 });

const collectionObj = this.model.rawCollection();
this.findAndModify = Meteor.wrapAsync(collectionObj.findAndModify, collectionObj);
}

// FIND
Expand Down
Loading

0 comments on commit 9ee843e

Please sign in to comment.