Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Chore: convert apps/meteor/app/api/server/lib/ files to TS #25840

Merged
merged 15 commits into from
Jun 15, 2022
8 changes: 1 addition & 7 deletions apps/meteor/app/api/server/api.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import type {
} from '@rocket.chat/rest-typings';
import type { IUser, IMethodConnection, IRoom } from '@rocket.chat/core-typings';
import type { ValidateFunction } from 'ajv';
import type { Request } from 'express';

import { ITwoFactorOptions } from '../../2fa/server/code';

Expand Down Expand Up @@ -70,13 +71,6 @@ type Options = (
validateParams?: ValidateFunction;
};

type Request = {
method: 'GET' | 'POST' | 'PUT' | 'DELETE';
url: string;
headers: Record<string, string>;
body: any;
};

type PartialThis = {
readonly request: Request & { query: Record<string, string> };
};
Expand Down
2 changes: 1 addition & 1 deletion apps/meteor/app/api/server/helpers/parseJsonQuery.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ API.helperMethods.set(
if (typeof fields === 'object') {
let nonSelectableFields = Object.keys(API.v1.defaultFieldsToExclude);
if (this.request.route.includes('/v1/users.')) {
const getFields = () =>
const getFields = (): string[] =>
Object.keys(
hasPermission(this.userId, 'view-full-other-user-info')
? API.v1.limitedUserFieldsToExcludeIfIsPrivilegedUser
Expand Down
20 changes: 0 additions & 20 deletions apps/meteor/app/api/server/lib/emoji-custom.js

This file was deleted.

34 changes: 34 additions & 0 deletions apps/meteor/app/api/server/lib/emoji-custom.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import { IEmojiCustom, ILivechatDepartmentRecord } from '@rocket.chat/core-typings';
import { FilterQuery, SortOptionObject } from 'mongodb';

import { EmojiCustom } from '../../../models/server/raw';

export async function findEmojisCustom({
query = {},
pagination: { offset, count, sort },
}: {
query: FilterQuery<ILivechatDepartmentRecord>;
pagination: { offset: number; count: number; sort: SortOptionObject<IEmojiCustom> };
}): Promise<{
emojis: IEmojiCustom[];
count: number;
offset: any;
total: number;
}> {
const cursor = EmojiCustom.find(query, {
sort: sort || { name: 1 },
skip: offset,
limit: count,
});

const total = await cursor.count();

const emojis = await cursor.toArray();

return {
emojis,
count: emojis.length,
offset,
total,
};
}
36 changes: 0 additions & 36 deletions apps/meteor/app/api/server/lib/getUploadFormData.js

This file was deleted.

89 changes: 89 additions & 0 deletions apps/meteor/app/api/server/lib/getUploadFormData.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
import { Readable } from 'stream';

import { Request } from 'express';
import busboy from 'busboy';
import { ValidateFunction } from 'ajv';

type UploadResult = {
file: Readable;
filename: string;
encoding: string;
mimetype: string;
fileBuffer: Buffer;
};

export const getUploadFormData = async <T extends string, K, V extends ValidateFunction<K>>(
{ request }: { request: Request },
options: {
field?: T;
validate?: V;
} = {},
): Promise<
[
UploadResult,
K extends unknown
? {
[k: string]: string;
}
: K,
T,
]
> =>
new Promise((resolve, reject) => {
const bb = busboy({ headers: request.headers, defParamCharset: 'utf8' });
const fields: { [K: string]: string } = Object.create(null);

let uploadedFile: UploadResult | undefined;

let assetName: T | undefined;

bb.on(
'file',
(
fieldname: string,
file: Readable,
{ filename, encoding, mimeType: mimetype }: { filename: string; encoding: string; mimeType: string },
) => {
const fileData: Uint8Array[] = [];

file.on('data', (data: any) => fileData.push(data));

file.on('end', () => {
if (uploadedFile) {
return reject('Just 1 file is allowed');
}
if (options.field && fieldname !== options.field) {
return reject('Invalid field name');
}
uploadedFile = {
file,
filename,
encoding,
mimetype,
fileBuffer: Buffer.concat(fileData),
};

assetName = fieldname as T;
});
},
);

bb.on('field', (fieldname, value) => {
fields[fieldname] = value;
});

bb.on('finish', () => {
if (!uploadedFile || !assetName) {
return reject('No file uploaded');
}
if (options.validate === undefined) {
return resolve([uploadedFile, fields, assetName]);
}
if (!options.validate(fields)) {
return reject(`Invalid fields${options.validate.errors?.join(', ')}`);
}
return resolve([uploadedFile, fields, assetName]);
});

request.pipe(bb);
});
Original file line number Diff line number Diff line change
@@ -1,13 +1,28 @@
import { IMessage, IUser } from '@rocket.chat/core-typings';

import { canAccessRoomAsync } from '../../../authorization/server/functions/canAccessRoom';
import { Rooms, Messages, Users } from '../../../models/server/raw';
import { getValue } from '../../../settings/server/raw';

export async function findMentionedMessages({ uid, roomId, pagination: { offset, count, sort } }) {
export async function findMentionedMessages({
uid,
roomId,
pagination: { offset, count, sort },
}: {
uid: string;
roomId: string;
pagination: { offset: number; count: number; sort: [string, number][] };
}): Promise<{
messages: IMessage[];
count: number;
offset: number;
total: number;
}> {
const room = await Rooms.findOneById(roomId);
if (!(await canAccessRoomAsync(room, { _id: uid }))) {
throw new Error('error-not-allowed');
}
const user = await Users.findOneById(uid, { fields: { username: 1 } });
const user: IUser | null = await Users.findOneById(uid, { fields: { username: 1 } });
if (!user) {
throw new Error('invalid-user');
}
Expand All @@ -30,7 +45,20 @@ export async function findMentionedMessages({ uid, roomId, pagination: { offset,
};
}

export async function findStarredMessages({ uid, roomId, pagination: { offset, count, sort } }) {
export async function findStarredMessages({
uid,
roomId,
pagination: { offset, count, sort },
}: {
uid: string;
roomId: string;
pagination: { offset: number; count: number; sort: [string, number][] };
}): Promise<{
messages: IMessage[];
count: number;
offset: any;
total: number;
}> {
const room = await Rooms.findOneById(roomId);
if (!(await canAccessRoomAsync(room, { _id: uid }))) {
throw new Error('error-not-allowed');
Expand Down Expand Up @@ -58,7 +86,7 @@ export async function findStarredMessages({ uid, roomId, pagination: { offset, c
};
}

export async function findSnippetedMessageById({ uid, messageId }) {
export async function findSnippetedMessageById({ uid, messageId }: { uid: string; messageId: string }): Promise<IMessage> {
if (!(await getValue('Message_AllowSnippeting'))) {
throw new Error('error-not-allowed');
}
Expand All @@ -83,12 +111,23 @@ export async function findSnippetedMessageById({ uid, messageId }) {
throw new Error('error-not-allowed');
}

return {
message: snippet,
};
return snippet;
}

export async function findSnippetedMessages({ uid, roomId, pagination: { offset, count, sort } }) {
export async function findSnippetedMessages({
uid,
roomId,
pagination: { offset, count, sort },
}: {
uid: string;
roomId: string;
pagination: { offset: number; count: number; sort: [string, number][] };
}): Promise<{
messages: IMessage[];
count: number;
offset: number;
total: number;
}> {
if (!(await getValue('Message_AllowSnippeting'))) {
throw new Error('error-not-allowed');
}
Expand Down Expand Up @@ -116,7 +155,22 @@ export async function findSnippetedMessages({ uid, roomId, pagination: { offset,
};
}

export async function findDiscussionsFromRoom({ uid, roomId, text, pagination: { offset, count, sort } }) {
export async function findDiscussionsFromRoom({
uid,
roomId,
text,
pagination: { offset, count, sort },
}: {
uid: string;
roomId: string;
text: string;
pagination: { offset: number; count: number; sort: [string, number][] };
}): Promise<{
messages: IMessage[];
count: number;
offset: number;
total: number;
}> {
const room = await Rooms.findOneById(roomId);

if (!(await canAccessRoomAsync(room, { _id: uid }))) {
Expand Down
Loading