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

[FIX] Importers failing when usernames exists but cases don't match and improve the importer framework's performance #8966

Merged
merged 16 commits into from
Dec 6, 2017
Merged
Show file tree
Hide file tree
Changes from 3 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
4 changes: 4 additions & 0 deletions packages/rocketchat-importer-csv/client/adder.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import { Importers } from 'meteor/rocketchat:importer';
import { CsvImporterInfo } from '../info';

Importers.add(new CsvImporterInfo());
10 changes: 10 additions & 0 deletions packages/rocketchat-importer-csv/info.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { ImporterInfo } from 'meteor/rocketchat:importer';

export class CsvImporterInfo extends ImporterInfo {
constructor() {
super('csv', 'CSV', 'application/zip', [{
text: 'Importer_CSV_Information',
href: 'https://rocket.chat/docs/administrator-guides/import/csv/'
}]);
}
}
10 changes: 0 additions & 10 deletions packages/rocketchat-importer-csv/main.js

This file was deleted.

12 changes: 10 additions & 2 deletions packages/rocketchat-importer-csv/package.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,17 @@ Package.onUse(function(api) {
'rocketchat:lib',
'rocketchat:importer'
]);

api.use('rocketchat:logger', 'server');
api.addFiles('server.js', 'server');
api.addFiles('main.js', ['client', 'server']);

// Importer information to both server and client
api.addFiles('info.js');

// Server files
api.addFiles(['server/importer.js', 'server/adder.js'], 'server');

// Client files
api.addFiles('client/adder.js', 'client');
});

Npm.depends({
Expand Down
5 changes: 5 additions & 0 deletions packages/rocketchat-importer-csv/server/adder.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { Importers } from 'meteor/rocketchat:importer';
import { CsvImporterInfo } from '../info';
import { CsvImporter } from './importer';

Importers.add(new CsvImporterInfo(), CsvImporter);
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
/* globals Importer */

Importer.CSV = class ImporterCSV extends Importer.Base {
constructor(name, descriptionI18N, mimeType) {
super(name, descriptionI18N, mimeType);
this.logger.debug('Constructed a new CSV Importer.');
import {
Base,
ProgressStep,
Selection,
SelectionChannel,
SelectionUser
} from 'meteor/rocketchat:importer';

export class CsvImporter extends Base {
constructor(info) {
super(info);

this.csvParser = Npm.require('csv-parse/lib/sync');
this.messages = new Map();
Expand Down Expand Up @@ -36,7 +41,7 @@ Importer.CSV = class ImporterCSV extends Importer.Base {

//Parse the channels
if (entry.entryName.toLowerCase() === 'channels.csv') {
super.updateProgress(Importer.ProgressStep.PREPARING_CHANNELS);
super.updateProgress(ProgressStep.PREPARING_CHANNELS);
const parsedChannels = this.csvParser(entry.getData().toString());
tempChannels = parsedChannels.map((c) => {
return {
Expand All @@ -52,7 +57,7 @@ Importer.CSV = class ImporterCSV extends Importer.Base {

//Parse the users
if (entry.entryName.toLowerCase() === 'users.csv') {
super.updateProgress(Importer.ProgressStep.PREPARING_USERS);
super.updateProgress(ProgressStep.PREPARING_USERS);
const parsedUsers = this.csvParser(entry.getData().toString());
tempUsers = parsedUsers.map((u) => { return { id: u[0].trim().replace('.', '_'), username: u[0].trim(), email: u[1].trim(), name: u[2].trim() }; });
continue;
Expand Down Expand Up @@ -96,7 +101,7 @@ Importer.CSV = class ImporterCSV extends Importer.Base {
super.addCountToTotal(tempChannels.length);

// Save the messages records to the import record for `startImport` usage
super.updateProgress(Importer.ProgressStep.PREPARING_MESSAGES);
super.updateProgress(ProgressStep.PREPARING_MESSAGES);
let messagesCount = 0;
for (const [channel, messagesMap] of tempMessages.entries()) {
if (!this.messages.get(channel)) {
Expand All @@ -107,8 +112,8 @@ Importer.CSV = class ImporterCSV extends Importer.Base {
messagesCount += msgs.length;
super.updateRecord({ 'messagesstatus': `${ channel }/${ msgGroupData }` });

if (Importer.Base.getBSONSize(msgs) > Importer.Base.MaxBSONSize) {
Importer.Base.getBSONSafeArraysFromAnArray(msgs).forEach((splitMsg, i) => {
if (Base.getBSONSize(msgs) > Base.getMaxBSONSize()) {
Base.getBSONSafeArraysFromAnArray(msgs).forEach((splitMsg, i) => {
const messagesId = this.collection.insert({ 'import': this.importRecord._id, 'importer': this.name, 'type': 'messages', 'name': `${ channel }/${ msgGroupData }.${ i }`, 'messages': splitMsg });
this.messages.get(channel).set(`${ msgGroupData }.${ i }`, this.collection.findOne(messagesId));
});
Expand All @@ -125,16 +130,16 @@ Importer.CSV = class ImporterCSV extends Importer.Base {
//Ensure we have at least a single user, channel, or message
if (tempUsers.length === 0 && tempChannels.length === 0 && messagesCount === 0) {
this.logger.error('No users, channels, or messages found in the import file.');
super.updateProgress(Importer.ProgressStep.ERROR);
super.updateProgress(ProgressStep.ERROR);
return super.getProgress();
}

const selectionUsers = tempUsers.map((u) => new Importer.SelectionUser(u.id, u.username, u.email, false, false, true));
const selectionChannels = tempChannels.map((c) => new Importer.SelectionChannel(c.id, c.name, false, true, c.isPrivate));
const selectionUsers = tempUsers.map((u) => new SelectionUser(u.id, u.username, u.email, false, false, true));
const selectionChannels = tempChannels.map((c) => new SelectionChannel(c.id, c.name, false, true, c.isPrivate));
const selectionMessages = this.importRecord.count.messages;

super.updateProgress(Importer.ProgressStep.USER_SELECTION);
return new Importer.Selection(this.name, selectionUsers, selectionChannels, selectionMessages);
super.updateProgress(ProgressStep.USER_SELECTION);
return new Selection(this.name, selectionUsers, selectionChannels, selectionMessages);
}

startImport(importSelection) {
Expand Down Expand Up @@ -163,7 +168,7 @@ Importer.CSV = class ImporterCSV extends Importer.Base {

const startedByUserId = Meteor.userId();
Meteor.defer(() => {
super.updateProgress(Importer.ProgressStep.IMPORTING_USERS);
super.updateProgress(ProgressStep.IMPORTING_USERS);
//Import the users
for (const u of this.users.users) {
if (!u.do_import) {
Expand Down Expand Up @@ -198,7 +203,7 @@ Importer.CSV = class ImporterCSV extends Importer.Base {
this.collection.update({ _id: this.users._id }, { $set: { 'users': this.users.users }});

//Import the channels
super.updateProgress(Importer.ProgressStep.IMPORTING_CHANNELS);
super.updateProgress(ProgressStep.IMPORTING_CHANNELS);
for (const c of this.channels.channels) {
if (!c.do_import) {
continue;
Expand Down Expand Up @@ -277,7 +282,7 @@ Importer.CSV = class ImporterCSV extends Importer.Base {


//Import the Messages
super.updateProgress(Importer.ProgressStep.IMPORTING_MESSAGES);
super.updateProgress(ProgressStep.IMPORTING_MESSAGES);
for (const [ch, messagesMap] of this.messages.entries()) {
const csvChannel = this.getChannelFromName(ch);
if (!csvChannel || !csvChannel.do_import) {
Expand Down Expand Up @@ -325,8 +330,8 @@ Importer.CSV = class ImporterCSV extends Importer.Base {
});
}

super.updateProgress(Importer.ProgressStep.FINISHING);
super.updateProgress(Importer.ProgressStep.DONE);
super.updateProgress(ProgressStep.FINISHING);
super.updateProgress(ProgressStep.DONE);
const timeTook = Date.now() - started;
this.logger.log(`CSV Import took ${ timeTook } milliseconds.`);
});
Expand All @@ -335,11 +340,11 @@ Importer.CSV = class ImporterCSV extends Importer.Base {
}

getSelection() {
const selectionUsers = this.users.users.map((u) => new Importer.SelectionUser(u.id, u.username, u.email, false, false, true));
const selectionChannels = this.channels.channels.map((c) => new Importer.SelectionChannel(c.id, c.name, false, true, c.isPrivate));
const selectionUsers = this.users.users.map((u) => new SelectionUser(u.id, u.username, u.email, false, false, true));
const selectionChannels = this.channels.channels.map((c) => new SelectionChannel(c.id, c.name, false, true, c.isPrivate));
const selectionMessages = this.importRecord.count.messages;

return new Importer.Selection(this.name, selectionUsers, selectionChannels, selectionMessages);
return new Selection(this.name, selectionUsers, selectionChannels, selectionMessages);
}

getChannelFromName(channelName) {
Expand All @@ -357,4 +362,4 @@ Importer.CSV = class ImporterCSV extends Importer.Base {
}
}
}
};
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import { Importers } from 'meteor/rocketchat:importer';
import { HipChatEnterpriseImporterInfo } from '../info';

Importers.add(new HipChatEnterpriseImporterInfo());
15 changes: 15 additions & 0 deletions packages/rocketchat-importer-hipchat-enterprise/info.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { ImporterInfo } from 'meteor/rocketchat:importer';

export class HipChatEnterpriseImporterInfo extends ImporterInfo {
constructor() {
super('hipchatenterprise', 'HipChat Enterprise', 'application/gzip', [
{
text: 'Importer_HipChatEnterprise_Information',
href: 'https://rocket.chat/docs/administrator-guides/import/hipchat/enterprise/'
}, {
text: 'Importer_HipChatEnterprise_BetaWarning',
href: 'https://github.com/RocketChat/Rocket.Chat/issues/new'
}
]);
}
}
15 changes: 0 additions & 15 deletions packages/rocketchat-importer-hipchat-enterprise/main.js

This file was deleted.

11 changes: 9 additions & 2 deletions packages/rocketchat-importer-hipchat-enterprise/package.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,15 @@ Package.onUse(function(api) {
]);

api.use('rocketchat:logger', 'server');
api.addFiles('server.js', 'server');
api.addFiles('main.js', ['client', 'server']);

// Importer information to both server and client
api.addFiles('info.js');

// Server files
api.addFiles(['server/importer.js', 'server/adder.js'], 'server');

// Client files
api.addFiles('client/adder.js', 'client');
});

Npm.depends({
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { Importers } from 'meteor/rocketchat:importer';
import { HipChatEnterpriseImporterInfo } from '../info';
import { HipChatEnterpriseImporter } from './importer';

Importers.add(new HipChatEnterpriseImporterInfo(), HipChatEnterpriseImporter);
Loading