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
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 } from '@rocket.chat/core-typings';
import { SortOptionObject } from 'mongodb';

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

export async function findEmojisCustom({
query = {},
pagination: { offset, count, sort },
}: {
query: {};
ggazzo marked this conversation as resolved.
Show resolved Hide resolved
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.

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

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

export const getUploadFormData = async ({
request,
}: {
request: Request;
}): Promise<{ file: Readable; filename: string; encoding: string; mimetype: string; fileBuffer: Buffer }> =>
new Promise((resolve, reject) => {
const bb = busboy({ headers: request.headers, defParamCharset: 'utf8' });

const fields: { file: Readable; filename: string; encoding: string; mimetype: string; fileBuffer: Buffer } = Object.create(null);

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 (fields.hasOwnProperty(fieldname)) {
return reject('Just 1 file is allowed');
}

fields[fieldname] = {
file,
filename,
encoding,
mimetype,
fileBuffer: Buffer.concat(fileData),
};
});
},
);

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

bb.on('finish', () => resolve(fields));

request.pipe(bb);
});
Original file line number Diff line number Diff line change
@@ -1,8 +1,23 @@
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');
Expand All @@ -12,7 +27,7 @@ export async function findMentionedMessages({ uid, roomId, pagination: { offset,
throw new Error('invalid-user');
}

const cursor = await Messages.findVisibleByMentionAndRoomId(user.username, roomId, {
const cursor = await Messages.findVisibleByMentionAndRoomId((user as IUser).username, roomId, {
ggazzo marked this conversation as resolved.
Show resolved Hide resolved
sort: sort || { ts: -1 },
skip: offset,
limit: count,
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
Original file line number Diff line number Diff line change
@@ -1,15 +1,32 @@
import { IMessage, IRoom } from '@rocket.chat/core-typings';

import { hasPermissionAsync, hasAtLeastOnePermissionAsync } from '../../../authorization/server/functions/hasPermission';
import { Rooms } from '../../../models/server/raw';
import { Subscriptions } from '../../../models/server';
import { adminFields } from '../../../../lib/rooms/adminFields';

export async function findAdminRooms({ uid, filter, types = [], pagination: { offset, count, sort } }) {
export async function findAdminRooms({
uid,
filter,
types = [],
pagination: { offset, count, sort },
}: {
uid: string;
filter: string;
types: string[];
pagination: { offset: number; count: number; sort: [string, number][] };
}): Promise<{
rooms: IRoom[];
count: number;
offset: number;
total: number;
}> {
if (!(await hasPermissionAsync(uid, 'view-room-administration'))) {
throw new Error('error-not-authorized');
}
const name = filter && filter.trim();
const discussion = types && types.includes('discussions');
const includeTeams = types && types.includes('teams');
const name = filter?.trim();
const discussion = types?.includes('discussions');
const includeTeams = types?.includes('teams');
const showOnlyTeams = types.length === 1 && types.includes('teams');
const typesToRemove = ['discussions', 'teams'];
const showTypes = Array.isArray(types) ? types.filter((type) => !typesToRemove.includes(type)) : [];
Expand Down Expand Up @@ -41,15 +58,15 @@ export async function findAdminRooms({ uid, filter, types = [], pagination: { of
};
}

export async function findAdminRoom({ uid, rid }) {
export async function findAdminRoom({ uid, rid }: { uid: string; rid: string }): Promise<unknown> {
if (!(await hasPermissionAsync(uid, 'view-room-administration'))) {
throw new Error('error-not-authorized');
}

return Rooms.findOneById(rid, { fields: adminFields });
}

export async function findChannelAndPrivateAutocomplete({ uid, selector }) {
export async function findChannelAndPrivateAutocomplete({ uid, selector }: { uid: string; selector: { name: string } }): Promise<unknown> {
const options = {
fields: {
_id: 1,
Expand All @@ -66,7 +83,7 @@ export async function findChannelAndPrivateAutocomplete({ uid, selector }) {

const userRoomsIds = Subscriptions.cachedFindByUserId(uid, { fields: { rid: 1 } })
.fetch()
.map((item) => item.rid);
.map((item: IMessage) => item.rid);
ggazzo marked this conversation as resolved.
Show resolved Hide resolved

const rooms = await Rooms.findRoomsWithoutDiscussionsByRoomIds(selector.name, userRoomsIds, options).toArray();

Expand All @@ -75,7 +92,7 @@ export async function findChannelAndPrivateAutocomplete({ uid, selector }) {
};
}

export async function findAdminRoomsAutocomplete({ uid, selector }) {
export async function findAdminRoomsAutocomplete({ uid, selector }: { uid: string; selector: { name: string } }): Promise<unknown> {
if (!(await hasAtLeastOnePermissionAsync(uid, ['view-room-administration', 'can-audit']))) {
throw new Error('error-not-authorized');
}
Expand All @@ -100,10 +117,21 @@ export async function findAdminRoomsAutocomplete({ uid, selector }) {
};
}

export async function findChannelAndPrivateAutocompleteWithPagination({ uid, selector, pagination: { offset, count, sort } }) {
export async function findChannelAndPrivateAutocompleteWithPagination({
uid,
selector,
pagination: { offset, count, sort },
}: {
uid: string;
selector: { name: string };
pagination: { offset: number; count: number; sort: [string, number][] };
}): Promise<{
items: IRoom[];
total: number;
}> {
const userRoomsIds = Subscriptions.cachedFindByUserId(uid, { fields: { rid: 1 } })
.fetch()
.map((item) => item.rid);
.map((item: IMessage) => item.rid);
ggazzo marked this conversation as resolved.
Show resolved Hide resolved

const options = {
fields: {
Expand All @@ -129,7 +157,9 @@ export async function findChannelAndPrivateAutocompleteWithPagination({ uid, sel
};
}

export async function findRoomsAvailableForTeams({ uid, name }) {
export async function findRoomsAvailableForTeams({ uid, name }: { uid: string; name: string }): Promise<{
items: IMessage[];
}> {
const options = {
fields: {
_id: 1,
Expand All @@ -146,7 +176,7 @@ export async function findRoomsAvailableForTeams({ uid, name }) {

const userRooms = Subscriptions.findByUserIdAndRoles(uid, ['owner'], { fields: { rid: 1 } })
.fetch()
.map((item) => item.rid);
.map((item: IMessage) => item.rid);
ggazzo marked this conversation as resolved.
Show resolved Hide resolved

const rooms = await Rooms.findChannelAndGroupListWithoutTeamsByNameStartingByOwner(uid, name, userRooms, options).toArray();

Expand Down
Loading