Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion apps/meteor/app/api/server/v1/chat.ts
Original file line number Diff line number Diff line change
Expand Up @@ -375,7 +375,7 @@ API.v1.addRoute(
_id: msg._id,
msg: msgFromBody,
rid: msg.rid,
customFields: this.bodyParams.customFields as Record<string, any> | undefined,
...(this.bodyParams.customFields && { customFields: this.bodyParams.customFields }),
},
this.bodyParams.previewUrls,
),
Expand Down
6 changes: 3 additions & 3 deletions apps/meteor/app/api/server/v1/teams.ts
Original file line number Diff line number Diff line change
Expand Up @@ -441,9 +441,9 @@ API.v1.addRoute(
const canSeeAllMembers = await hasPermissionAsync(this.userId, 'view-all-teams', team.roomId);

const query = {
username: username ? new RegExp(escapeRegExp(username), 'i') : undefined,
name: name ? new RegExp(escapeRegExp(name), 'i') : undefined,
status: status ? { $in: status as UserStatus[] } : undefined,
...(username && { username: new RegExp(escapeRegExp(username), 'i') }),
...(name && { name: new RegExp(escapeRegExp(name), 'i') }),
...(status && { status: { $in: status as UserStatus[] } }),
};

const { records, total } = await Team.members(this.userId, team._id, canSeeAllMembers, { offset, count }, query);
Expand Down
65 changes: 32 additions & 33 deletions apps/meteor/app/apps/server/converters/messages.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { isMessageFromVisitor } from '@rocket.chat/core-typings';
import { Messages, Rooms, Users } from '@rocket.chat/models';
import { Random } from '@rocket.chat/random';
import { removeEmpty } from '@rocket.chat/tools';

import { cachedFunction } from './cachedFunction';
import { transformMappedData } from './transformMappedData';
Expand Down Expand Up @@ -236,39 +237,37 @@ export class AppMessagesConverter {
}

return attachments.map((attachment) =>
Object.assign(
{
collapsed: attachment.collapsed,
color: attachment.color,
text: attachment.text,
ts: attachment.timestamp ? attachment.timestamp.toJSON() : attachment.timestamp,
message_link: attachment.timestampLink,
thumb_url: attachment.thumbnailUrl,
author_name: attachment.author ? attachment.author.name : undefined,
author_link: attachment.author ? attachment.author.link : undefined,
author_icon: attachment.author ? attachment.author.icon : undefined,
title: attachment.title ? attachment.title.value : undefined,
title_link: attachment.title ? attachment.title.link : undefined,
title_link_download: attachment.title ? attachment.title.displayDownloadLink : undefined,
image_dimensions: attachment.imageDimensions,
image_preview: attachment.imagePreview,
image_url: attachment.imageUrl,
image_type: attachment.imageType,
image_size: attachment.imageSize,
audio_url: attachment.audioUrl,
audio_type: attachment.audioType,
audio_size: attachment.audioSize,
video_url: attachment.videoUrl,
video_type: attachment.videoType,
video_size: attachment.videoSize,
fields: attachment.fields,
button_alignment: attachment.actionButtonsAlignment,
actions: attachment.actions,
type: attachment.type,
description: attachment.description,
},
attachment._unmappedProperties_,
),
removeEmpty({
collapsed: attachment.collapsed,
color: attachment.color,
text: attachment.text,
ts: attachment.timestamp ? attachment.timestamp.toJSON() : attachment.timestamp,
message_link: attachment.timestampLink,
thumb_url: attachment.thumbnailUrl,
author_name: attachment.author ? attachment.author.name : undefined,
author_link: attachment.author ? attachment.author.link : undefined,
author_icon: attachment.author ? attachment.author.icon : undefined,
title: attachment.title ? attachment.title.value : undefined,
title_link: attachment.title ? attachment.title.link : undefined,
title_link_download: attachment.title ? attachment.title.displayDownloadLink : undefined,
image_dimensions: attachment.imageDimensions,
image_preview: attachment.imagePreview,
image_url: attachment.imageUrl,
image_type: attachment.imageType,
image_size: attachment.imageSize,
audio_url: attachment.audioUrl,
audio_type: attachment.audioType,
audio_size: attachment.audioSize,
video_url: attachment.videoUrl,
video_type: attachment.videoType,
video_size: attachment.videoSize,
fields: attachment.fields,
button_alignment: attachment.actionButtonsAlignment,
actions: attachment.actions,
type: attachment.type,
description: attachment.description,
...attachment._unmappedProperties_,
}),
);
}

Expand Down
203 changes: 112 additions & 91 deletions apps/meteor/app/apps/server/converters/rooms.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,120 +20,147 @@ export class AppRoomsConverter {
return this.convertRoom(room);
}

async convertAppRoom(room, isPartial = false) {
if (!room) {
return undefined;
async __getCreator(user) {
if (!user) {
return;
}

let u;
if (room.creator) {
const creator = await Users.findOneById(room.creator.id);
u = {
_id: creator._id,
username: creator.username,
name: creator.name,
};
const creator = await Users.findOneById(user, { projection: { _id: 1, username: 1, name: 1 } });
if (!creator) {
return;
}

let v;
if (room.visitor) {
const visitor = await LivechatVisitors.findOneEnabledById(room.visitor.id);
return {
_id: creator._id,
username: creator.username,
name: creator.name,
};
}

const { lastMessageTs, phone } = room.visitorChannelInfo;
async __getVisitor({ visitor: roomVisitor, visitorChannelInfo }) {
if (!roomVisitor) {
return;
}

v = {
_id: visitor._id,
username: visitor.username,
token: visitor.token,
status: visitor.status || 'online',
...(lastMessageTs && { lastMessageTs }),
...(phone && { phone }),
};
const visitor = await LivechatVisitors.findOneEnabledById(roomVisitor.id);
if (!visitor) {
return;
}

let departmentId;
if (room.department) {
const department = await LivechatDepartment.findOneById(room.department.id, { projection: { _id: 1 } });
departmentId = department._id;
const { lastMessageTs, phone } = visitorChannelInfo;

return {
_id: visitor._id,
username: visitor.username,
token: visitor.token,
status: visitor.status || 'online',
...(lastMessageTs && { lastMessageTs }),
...(phone && { phone }),
};
}

async __getUserIdAndUsername(uid) {
if (!uid) {
return;
}

const user = await Users.findOneById(uid, { projection: { _id: 1, username: 1 } });
if (!user) {
return;
}

return {
_id: user._id,
username: user.username,
};
}

async __getRoomCloser(room, v) {
if (!room.closedBy) {
return;
}

let servedBy;
if (room.servedBy) {
const user = await Users.findOneById(room.servedBy.id);
servedBy = {
if (room.closer === 'user') {
const user = await Users.findOneById(room.closedBy.id, { projection: { _id: 1, username: 1 } });
if (!user) {
return;
}

return {
_id: user._id,
username: user.username,
};
}

let closedBy;
if (room.closedBy) {
if (room.closer === 'user') {
const user = await Users.findOneById(room.closedBy.id);
closedBy = {
_id: user._id,
username: user.username,
};
} else if (room.closer === 'visitor') {
closedBy = {
_id: v._id,
username: v.username,
};
}
if (room.closer === 'visitor' && v) {
return {
_id: v._id,
username: v.username,
};
}
}

let contactId;
if (room.contact?._id) {
const contact = await LivechatContacts.findOneById(room.contact._id, { projection: { _id: 1 } });
contactId = contact._id;
// TODO do we really need this?
async __getContactId({ contact }) {
if (!contact?._id) {
return;
}
const contactFromDb = await LivechatContacts.findOneById(contact._id, { projection: { _id: 1 } });
return contactFromDb?._id;
}

let _default;
if (typeof room.isDefault !== 'undefined') {
_default = room.isDefault;
// TODO do we really need this?
async __getDepartment({ department }) {
if (!department) {
return;
}
const dept = await LivechatDepartment.findOneById(department.id, { projection: { _id: 1 } });
return dept?._id;
}

let ro;
if (typeof room.isReadOnly !== 'undefined') {
ro = room.isReadOnly;
async convertAppRoom(room, isPartial = false) {
if (!room) {
return undefined;
}

let sysMes;
if (typeof room.displaySystemMessages !== 'undefined') {
sysMes = room.displaySystemMessages;
}
const u = await this.__getCreator(room.creator?.id);

let msgs;
if (typeof room.messageCount !== 'undefined') {
msgs = room.messageCount;
}
const v = await this.__getVisitor(room);

const departmentId = await this.__getDepartment(room);

const servedBy = await this.__getUserIdAndUsername(room.servedBy);

const closedBy = await this.__getRoomCloser(room, v);

const contactId = await this.__getContactId(room);

const newRoom = {
...(room.id && { _id: room.id }),
fname: room.displayName,
name: room.slugifiedName,
t: room.type,
u,
v,
ro,
sysMes,
msgs,
departmentId,
servedBy,
closedBy,
members: room.members,
uids: room.userIds,
default: _default,
waitingResponse: typeof room.isWaitingResponse === 'undefined' ? undefined : !!room.isWaitingResponse,
open: typeof room.isOpen === 'undefined' ? undefined : !!room.isOpen,
ts: room.createdAt,
msgs: room.messageCount || 0,
_updatedAt: room.updatedAt,
closedAt: room.closedAt,
lm: room.lastModifiedAt,
customFields: room.customFields,
livechatData: room.livechatData,
prid: typeof room.parentRoom === 'undefined' ? undefined : room.parentRoom.id,
contactId,
...(room.displayName && { fname: room.displayName }),
...(room.type !== 'd' && { name: room.slugifiedName }),
...(room.members && { members: room.members }),
...(typeof room.isDefault !== 'undefined' && { default: room.isDefault }),
...(typeof room.isReadOnly !== 'undefined' && { ro: room.isReadOnly }),
...(typeof room.displaySystemMessages !== 'undefined' && { sysMes: room.displaySystemMessages }),
...(u && { u }),
...(v && { v }),
...(departmentId && { departmentId }),
...(servedBy && { servedBy }),
...(closedBy && { closedBy }),
...(room.userIds && { uids: room.userIds }),
...(typeof room.isWaitingResponse !== 'undefined' && { waitingResponse: !!room.isWaitingResponse }),
...(typeof room.isOpen !== 'undefined' && { open: !!room.isOpen }),
...(room.closedAt && { closedAt: room.closedAt }),
...(room.lastModifiedAt && { lm: room.lastModifiedAt }),
...(room.customFields && { customFields: room.customFields }),
...(room.livechatData && { livechatData: room.livechatData }),
...(typeof room.parentRoom !== 'undefined' && { prid: room.parentRoom.id }),
...(contactId && { contactId }),
...(room._USERNAMES && { _USERNAMES: room._USERNAMES }),
...(room.source && {
source: {
Expand All @@ -142,13 +169,7 @@ export class AppRoomsConverter {
}),
};

if (isPartial) {
Object.entries(newRoom).forEach(([key, value]) => {
if (typeof value === 'undefined') {
delete newRoom[key];
}
});
} else {
if (!isPartial) {
Object.assign(newRoom, room._unmappedProperties_);
}

Expand Down
5 changes: 3 additions & 2 deletions apps/meteor/app/apps/server/converters/users.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { UserStatusConnection, UserType } from '@rocket.chat/apps-engine/definition/users';
import { Users } from '@rocket.chat/models';
import { removeEmpty } from '@rocket.chat/tools';

export class AppUsersConverter {
constructor(orch) {
Expand Down Expand Up @@ -56,7 +57,7 @@ export class AppUsersConverter {
return undefined;
}

return {
return removeEmpty({
_id: user.id,
username: user.username,
emails: user.emails,
Expand All @@ -71,7 +72,7 @@ export class AppUsersConverter {
_updatedAt: user.updatedAt,
lastLogin: user.lastLoginAt,
appId: user.appId,
};
});
}

_convertUserTypeToEnum(type) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,12 @@ Meteor.methods<ServerMethods>({
});
}

if (!keyPair.public_key || !keyPair.private_key) {
throw new Meteor.Error('error-invalid-keys', 'Invalid keys', {
method: 'e2e.setUserPublicAndPrivateKeys',
});
}

if (!keyPair.force) {
const keys = await Users.fetchKeysByUserId(userId);

Expand Down
Loading