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 Importer classes to typescript #29714

Merged
merged 38 commits into from
Dec 6, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
38 commits
Select commit Hold shift + click to select a range
2e8596e
chore: convert importer classes to typescript
pierre-lehnen-rc Jul 4, 2023
1729093
types
pierre-lehnen-rc Jul 4, 2023
66f68c6
removed raw impots model
pierre-lehnen-rc Jul 7, 2023
f766427
converting each importer to TS
pierre-lehnen-rc Jul 10, 2023
8c6d014
temp
pierre-lehnen-rc Jul 11, 2023
b496a67
import service
pierre-lehnen-rc Jul 13, 2023
8e400d8
endpoints
pierre-lehnen-rc Jul 13, 2023
3141bfa
fix total count
pierre-lehnen-rc Jul 13, 2023
4188910
Changed `v1/users.info` to allow searching by import id
pierre-lehnen-rc Jul 14, 2023
0e348bf
Merge remote-tracking branch 'origin/develop' into chore/convert-impo…
pierre-lehnen-rc Jul 14, 2023
42e2f71
Merge branch 'develop' into chore/convert-importer-to-ts
pierre-lehnen-rc Jul 17, 2023
7654bbd
Merge branch 'develop' into chore/convert-importer-to-ts
pierre-lehnen-rc Jul 19, 2023
4f589c7
types
pierre-lehnen-rc Jul 19, 2023
c503739
Merge branch 'develop' into chore/convert-importer-to-ts
pierre-lehnen-rc Aug 3, 2023
8bf5980
removed old changesets for fixes that were already merged
pierre-lehnen-rc Aug 3, 2023
48a070a
remove unused file
pierre-lehnen-rc Aug 3, 2023
a0de7c0
removed unneeded eslint-disable instructions
pierre-lehnen-rc Aug 3, 2023
837dac3
some types
pierre-lehnen-rc Aug 3, 2023
d0b2b36
types
pierre-lehnen-rc Aug 3, 2023
e879953
fixed import order
pierre-lehnen-rc Aug 7, 2023
edfac05
Merge remote-tracking branch 'origin/develop' into chore/convert-impo…
pierre-lehnen-rc Oct 16, 2023
76243f7
removing unneeded changes
pierre-lehnen-rc Oct 16, 2023
fdaf549
removed variable requires
pierre-lehnen-rc Oct 16, 2023
338bab5
Rename modules to match classes' names
tassoevan Oct 31, 2023
cbc37db
Merge branch 'develop' of github.com:RocketChat/Rocket.Chat into chor…
tassoevan Oct 31, 2023
591e156
Remove some unsoundness
tassoevan Nov 1, 2023
0d36659
Merge branch 'develop' of github.com:RocketChat/Rocket.Chat into chor…
tassoevan Nov 1, 2023
ec39bf0
Add type for Slack structures
tassoevan Nov 1, 2023
6844529
Remove unused fields from `Importer`
tassoevan Nov 1, 2023
5535fe1
Dismiss expandable method bindings
tassoevan Nov 1, 2023
e756766
Rename class
tassoevan Nov 1, 2023
5c30cd6
Merge branch 'develop' of github.com:RocketChat/Rocket.Chat into chor…
tassoevan Nov 1, 2023
dd1bb63
Respect naming convention in parameters
tassoevan Nov 1, 2023
8b12012
Merge branch 'develop' of github.com:RocketChat/Rocket.Chat into chor…
tassoevan Nov 1, 2023
9c10835
Move `Importers` initialization
tassoevan Nov 1, 2023
6db97fa
Migrate components to TS
tassoevan Nov 3, 2023
614dc7f
Merge branch 'develop' of github.com:RocketChat/Rocket.Chat into chor…
tassoevan Nov 3, 2023
da29bab
Merge branch 'develop' into chore/convert-importer-to-ts
sampaiodiego Dec 4, 2023
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
28 changes: 23 additions & 5 deletions apps/meteor/app/api/server/v1/import.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,13 @@ import {
isDownloadPendingFilesParamsPOST,
isDownloadPendingAvatarsParamsPOST,
isGetCurrentImportOperationParamsGET,
isImportersListParamsGET,
isImportAddUsersParamsPOST,
} from '@rocket.chat/rest-typings';
import { Meteor } from 'meteor/meteor';

import { PendingAvatarImporter } from '../../../importer-pending-avatars/server/PendingAvatarImporter';
import { PendingFileImporter } from '../../../importer-pending-files/server/PendingFileImporter';
import { Importers } from '../../../importer/server';
import {
executeUploadImportFile,
Expand Down Expand Up @@ -136,9 +139,8 @@ API.v1.addRoute(
}

const operation = await Import.newOperation(this.userId, importer.name, importer.key);

importer.instance = new importer.importer(importer, operation); // eslint-disable-line new-cap
const count = await importer.instance.prepareFileCount();
const instance = new PendingFileImporter(importer, operation);
const count = await instance.prepareFileCount();

return API.v1.success({
count,
Expand All @@ -162,8 +164,8 @@ API.v1.addRoute(
}

const operation = await Import.newOperation(this.userId, importer.name, importer.key);
importer.instance = new importer.importer(importer, operation); // eslint-disable-line new-cap
const count = await importer.instance.prepareFileCount();
const instance = new PendingAvatarImporter(importer, operation);
const count = await instance.prepareFileCount();

return API.v1.success({
count,
Expand All @@ -189,6 +191,22 @@ API.v1.addRoute(
},
);

API.v1.addRoute(
'importers.list',
{
authRequired: true,
validateParams: isImportersListParamsGET,
permissionsRequired: ['run-import'],
},
{
async get() {
const importers = Importers.getAllVisible().map(({ key, name }) => ({ key, name }));

return API.v1.success(importers);
},
},
);

API.v1.addRoute(
'import.clear',
{
Expand Down
4 changes: 0 additions & 4 deletions apps/meteor/app/importer-csv/client/adder.js

This file was deleted.

1 change: 0 additions & 1 deletion apps/meteor/app/importer-csv/client/index.ts

This file was deleted.

12 changes: 0 additions & 12 deletions apps/meteor/app/importer-csv/lib/info.js

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,18 +1,23 @@
import type { IImport } from '@rocket.chat/core-typings';
import { Settings, Users } from '@rocket.chat/models';
import { Random } from '@rocket.chat/random';
import { parse } from 'csv-parse/lib/sync';

import { Base, ProgressStep, ImporterWebsocket } from '../../importer/server';
import { Importer, ProgressStep, ImporterWebsocket } from '../../importer/server';
import type { IConverterOptions } from '../../importer/server/classes/ImportDataConverter';
import type { ImporterProgress } from '../../importer/server/classes/ImporterProgress';
import type { ImporterInfo } from '../../importer/server/definitions/ImporterInfo';

export class CsvImporter extends Base {
constructor(info, importRecord, converterOptions = {}) {
super(info, importRecord, converterOptions);
export class CsvImporter extends Importer {
private csvParser: (csv: string) => string[];

const { parse } = require('csv-parse/lib/sync');
constructor(info: ImporterInfo, importRecord: IImport, converterOptions: IConverterOptions = {}) {
super(info, importRecord, converterOptions);

this.csvParser = parse;
}

async prepareUsingLocalFile(fullFilePath) {
async prepareUsingLocalFile(fullFilePath: string): Promise<ImporterProgress> {
this.logger.debug('start preparing import operation');
await this.converter.clearImportData();

Expand Down Expand Up @@ -40,17 +45,21 @@ export class CsvImporter extends Base {
let messagesCount = 0;
let usersCount = 0;
let channelsCount = 0;
const dmRooms = new Map();
const roomIds = new Map();
const usedUsernames = new Set();
const availableUsernames = new Set();

const getRoomId = (roomName) => {
if (!roomIds.has(roomName)) {
roomIds.set(roomName, Random.id());
const dmRooms = new Set<string>();
const roomIds = new Map<string, string>();
const usedUsernames = new Set<string>();
const availableUsernames = new Set<string>();

const getRoomId = (roomName: string) => {
const roomId = roomIds.get(roomName);

if (roomId === undefined) {
const fallbackRoomId = Random.id();
roomIds.set(roomName, fallbackRoomId);
return fallbackRoomId;
}

return roomIds.get(roomName);
return roomId;
};

for await (const entry of zip.getEntries()) {
Expand Down Expand Up @@ -149,7 +158,7 @@ export class CsvImporter extends Base {
continue;
}

let data;
let data: { username: string; ts: string; text: string; otherUsername?: string; isDirect?: true }[];
const msgGroupData = item[1].split('.')[0]; // messages
let isDirect = false;

Expand All @@ -173,6 +182,10 @@ export class CsvImporter extends Base {

if (isDirect) {
for await (const msg of data) {
if (!msg.otherUsername) {
continue;
}

const sourceId = [msg.username, msg.otherUsername].sort().join('/');

if (!dmRooms.has(sourceId)) {
Expand All @@ -182,7 +195,7 @@ export class CsvImporter extends Base {
t: 'd',
});

dmRooms.set(sourceId, true);
dmRooms.add(sourceId);
}

const newMessage = {
Expand Down Expand Up @@ -217,8 +230,6 @@ export class CsvImporter extends Base {
}

await super.updateRecord({ 'count.messages': messagesCount, 'messagesstatus': null });
increaseProgressCount();
continue;
}

increaseProgressCount();
Expand Down
9 changes: 6 additions & 3 deletions apps/meteor/app/importer-csv/server/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
import { Importers } from '../../importer/server';
import { CsvImporterInfo } from '../lib/info';
import { CsvImporter } from './importer';
import { CsvImporter } from './CsvImporter';

Importers.add(new CsvImporterInfo(), CsvImporter);
Importers.add({
key: 'csv',
name: 'CSV',
importer: CsvImporter,
});
4 changes: 0 additions & 4 deletions apps/meteor/app/importer-hipchat-enterprise/client/adder.ts

This file was deleted.

This file was deleted.

12 changes: 0 additions & 12 deletions apps/meteor/app/importer-hipchat-enterprise/lib/info.ts

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,10 @@ import { Readable } from 'stream';
import { Settings } from '@rocket.chat/models';
import { Meteor } from 'meteor/meteor';

import { Base, ProgressStep } from '../../importer/server';
import { Importer, ProgressStep } from '../../importer/server';

export class HipChatEnterpriseImporter extends Base {
/** @deprecated HipChat was discontinued at 2019-02-15 */
export class HipChatEnterpriseImporter extends Importer {
constructor(info, importRecord, converterOptions = {}) {
super(info, importRecord, converterOptions);

Expand Down
9 changes: 6 additions & 3 deletions apps/meteor/app/importer-hipchat-enterprise/server/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
import { Importers } from '../../importer/server';
import { HipChatEnterpriseImporterInfo } from '../lib/info';
import { HipChatEnterpriseImporter } from './importer';
import { HipChatEnterpriseImporter } from './HipChatEnterpriseImporter';

Importers.add(new HipChatEnterpriseImporterInfo(), HipChatEnterpriseImporter);
Importers.add({
key: 'hipchatenterprise',
name: 'HipChat (tar.gz)',
importer: HipChatEnterpriseImporter,
});
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
import { Users } from '@rocket.chat/models';

import { Base, ProgressStep, Selection } from '../../importer/server';
import { Importer, ProgressStep, Selection } from '../../importer/server';
import type { ImporterProgress } from '../../importer/server/classes/ImporterProgress';
import { setAvatarFromServiceWithValidation } from '../../lib/server/functions/setUserAvatar';

export class PendingAvatarImporter extends Base {
export class PendingAvatarImporter extends Importer {
async prepareFileCount() {
this.logger.debug('start preparing import operation');
await super.updateProgress(ProgressStep.PREPARING_STARTED);

const users = await Users.findAllUsersWithPendingAvatar();
const users = Users.findAllUsersWithPendingAvatar();
const fileCount = await users.count();

if (fileCount === 0) {
Expand All @@ -19,26 +20,26 @@ export class PendingAvatarImporter extends Base {
await this.updateRecord({ 'count.messages': fileCount, 'messagesstatus': null });
await this.addCountToTotal(fileCount);

const fileData = new Selection(this.name, [], [], fileCount);
const fileData = new Selection(this.info.name, [], [], fileCount);
await this.updateRecord({ fileData });

await super.updateProgress(ProgressStep.IMPORTING_FILES);
setImmediate(() => {
this.startImport(fileData);
void this.startImport(fileData);
});

return fileCount;
}

async startImport() {
const pendingFileUserList = await Users.findAllUsersWithPendingAvatar();
async startImport(importSelection: Selection): Promise<ImporterProgress> {
const pendingFileUserList = Users.findAllUsersWithPendingAvatar();
try {
for await (const user of pendingFileUserList) {
try {
const { _pendingAvatarUrl: url, name, _id } = user;

try {
if (!url || !url.startsWith('http')) {
if (!url?.startsWith('http')) {
continue;
}

Expand All @@ -57,9 +58,9 @@ export class PendingAvatarImporter extends Base {
}
} catch (error) {
// If the cursor expired, restart the method
if (error && error.codeName === 'CursorNotFound') {
if (this.isCursorNotFoundError(error)) {
this.logger.info('CursorNotFound');
return this.startImport();
return this.startImport(importSelection);
}

await super.updateProgress(ProgressStep.ERROR);
Expand Down
10 changes: 7 additions & 3 deletions apps/meteor/app/importer-pending-avatars/server/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
import { Importers } from '../../importer/server';
import { PendingAvatarImporter } from './importer';
import { PendingAvatarImporterInfo } from './info';
import { PendingAvatarImporter } from './PendingAvatarImporter';

Importers.add(new PendingAvatarImporterInfo(), PendingAvatarImporter);
Importers.add({
key: 'pending-avatars',
name: 'Pending Avatars',
visible: false,
importer: PendingAvatarImporter,
});
7 changes: 0 additions & 7 deletions apps/meteor/app/importer-pending-avatars/server/info.js

This file was deleted.

Loading