diff --git a/apps/meteor/app/importer-csv/server/importer.js b/apps/meteor/app/importer-csv/server/importer.js index 7e6f68ca733cad8a28707edc6e10473103b66b7e..c10f134d80d6c4d093c9448fe20ad5c27209ced0 100644 --- a/apps/meteor/app/importer-csv/server/importer.js +++ b/apps/meteor/app/importer-csv/server/importer.js @@ -53,173 +53,169 @@ export class CsvImporter extends Base { return roomIds.get(roomName); }; - zip.forEach((entry) => { - Promise.await( - (async () => { - this.logger.debug(`Entry: ${entry.entryName}`); - - // Ignore anything that has `__MACOSX` in it's name, as sadly these things seem to mess everything up - if (entry.entryName.indexOf('__MACOSX') > -1) { - this.logger.debug(`Ignoring the file: ${entry.entryName}`); - return increaseProgressCount(); - } + for await (const entry of zip.getEntries()) { + this.logger.debug(`Entry: ${entry.entryName}`); - // Directories are ignored, since they are "virtual" in a zip file - if (entry.isDirectory) { - this.logger.debug(`Ignoring the directory entry: ${entry.entryName}`); - return increaseProgressCount(); - } + // Ignore anything that has `__MACOSX` in it's name, as sadly these things seem to mess everything up + if (entry.entryName.indexOf('__MACOSX') > -1) { + this.logger.debug(`Ignoring the file: ${entry.entryName}`); + return increaseProgressCount(); + } - // Parse the channels - if (entry.entryName.toLowerCase() === 'channels.csv') { - await super.updateProgress(ProgressStep.PREPARING_CHANNELS); - const parsedChannels = this.csvParser(entry.getData().toString()); - channelsCount = parsedChannels.length; - - for await (const c of parsedChannels) { - const name = c[0].trim(); - const id = getRoomId(name); - const creator = c[1].trim(); - const isPrivate = c[2].trim().toLowerCase() === 'private'; - const members = c[3] - .trim() - .split(';') - .map((m) => m.trim()) - .filter((m) => m); + // Directories are ignored, since they are "virtual" in a zip file + if (entry.isDirectory) { + this.logger.debug(`Ignoring the directory entry: ${entry.entryName}`); + return increaseProgressCount(); + } - await this.converter.addChannel({ - importIds: [id], - u: { - _id: creator, - }, - name, - users: members, - t: isPrivate ? 'p' : 'c', - }); - } + // Parse the channels + if (entry.entryName.toLowerCase() === 'channels.csv') { + await super.updateProgress(ProgressStep.PREPARING_CHANNELS); + const parsedChannels = this.csvParser(entry.getData().toString()); + channelsCount = parsedChannels.length; + + for await (const c of parsedChannels) { + const name = c[0].trim(); + const id = getRoomId(name); + const creator = c[1].trim(); + const isPrivate = c[2].trim().toLowerCase() === 'private'; + const members = c[3] + .trim() + .split(';') + .map((m) => m.trim()) + .filter((m) => m); + + await this.converter.addChannel({ + importIds: [id], + u: { + _id: creator, + }, + name, + users: members, + t: isPrivate ? 'p' : 'c', + }); + } - await super.updateRecord({ 'count.channels': channelsCount }); - return increaseProgressCount(); - } + await super.updateRecord({ 'count.channels': channelsCount }); + return increaseProgressCount(); + } - // Parse the users - if (entry.entryName.toLowerCase() === 'users.csv') { - await super.updateProgress(ProgressStep.PREPARING_USERS); - const parsedUsers = this.csvParser(entry.getData().toString()); - usersCount = parsedUsers.length; + // Parse the users + if (entry.entryName.toLowerCase() === 'users.csv') { + await super.updateProgress(ProgressStep.PREPARING_USERS); + const parsedUsers = this.csvParser(entry.getData().toString()); + usersCount = parsedUsers.length; + + for await (const u of parsedUsers) { + const username = u[0].trim(); + availableUsernames.add(username); + + const email = u[1].trim(); + const name = u[2].trim(); + + await this.converter.addUser({ + importIds: [username], + emails: [email], + username, + name, + }); + } - for await (const u of parsedUsers) { - const username = u[0].trim(); - availableUsernames.add(username); + await super.updateRecord({ 'count.users': usersCount }); + return increaseProgressCount(); + } - const email = u[1].trim(); - const name = u[2].trim(); + // Parse the messages + if (entry.entryName.indexOf('/') > -1) { + if (this.progress.step !== ProgressStep.PREPARING_MESSAGES) { + await super.updateProgress(ProgressStep.PREPARING_MESSAGES); + } - await this.converter.addUser({ - importIds: [username], - emails: [email], - username, - name, - }); - } + const item = entry.entryName.split('/'); // random/messages.csv + const folderName = item[0]; // random - await super.updateRecord({ 'count.users': usersCount }); - return increaseProgressCount(); - } + let msgs = []; - // Parse the messages - if (entry.entryName.indexOf('/') > -1) { - if (this.progress.step !== ProgressStep.PREPARING_MESSAGES) { - await super.updateProgress(ProgressStep.PREPARING_MESSAGES); - } + try { + msgs = this.csvParser(entry.getData().toString()); + } catch (e) { + this.logger.warn(`The file ${entry.entryName} contains invalid syntax`, e); + return increaseProgressCount(); + } - const item = entry.entryName.split('/'); // random/messages.csv - const folderName = item[0]; // random + let data; + const msgGroupData = item[1].split('.')[0]; // messages + let isDirect = false; + + if (folderName.toLowerCase() === 'directmessages') { + isDirect = true; + data = msgs.map((m) => ({ + username: m[0], + ts: m[2], + text: m[3], + otherUsername: m[1], + isDirect: true, + })); + } else { + data = msgs.map((m) => ({ username: m[0], ts: m[1], text: m[2] })); + } - let msgs = []; + messagesCount += data.length; + const channelName = `${folderName}/${msgGroupData}`; - try { - msgs = this.csvParser(entry.getData().toString()); - } catch (e) { - this.logger.warn(`The file ${entry.entryName} contains invalid syntax`, e); - return increaseProgressCount(); - } + await super.updateRecord({ messagesstatus: channelName }); - let data; - const msgGroupData = item[1].split('.')[0]; // messages - let isDirect = false; - - if (folderName.toLowerCase() === 'directmessages') { - isDirect = true; - data = msgs.map((m) => ({ - username: m[0], - ts: m[2], - text: m[3], - otherUsername: m[1], - isDirect: true, - })); - } else { - data = msgs.map((m) => ({ username: m[0], ts: m[1], text: m[2] })); - } + if (isDirect) { + for await (const msg of data) { + const sourceId = [msg.username, msg.otherUsername].sort().join('/'); + + if (!dmRooms.has(sourceId)) { + await this.converter.addChannel({ + importIds: [sourceId], + users: [msg.username, msg.otherUsername], + t: 'd', + }); - messagesCount += data.length; - const channelName = `${folderName}/${msgGroupData}`; - - await super.updateRecord({ messagesstatus: channelName }); - - if (isDirect) { - for await (const msg of data) { - const sourceId = [msg.username, msg.otherUsername].sort().join('/'); - - if (!dmRooms.has(sourceId)) { - await this.converter.addChannel({ - importIds: [sourceId], - users: [msg.username, msg.otherUsername], - t: 'd', - }); - - dmRooms.set(sourceId, true); - } - - const newMessage = { - rid: sourceId, - u: { - _id: msg.username, - }, - ts: new Date(parseInt(msg.ts)), - msg: msg.text, - }; - - usedUsernames.add(msg.username); - usedUsernames.add(msg.otherUsername); - await this.converter.addMessage(newMessage); - } - } else { - const rid = getRoomId(folderName); - - for await (const msg of data) { - const newMessage = { - rid, - u: { - _id: msg.username, - }, - ts: new Date(parseInt(msg.ts)), - msg: msg.text, - }; - - usedUsernames.add(msg.username); - await this.converter.addMessage(newMessage); - } + dmRooms.set(sourceId, true); } - await super.updateRecord({ 'count.messages': messagesCount, 'messagesstatus': null }); - return increaseProgressCount(); + const newMessage = { + rid: sourceId, + u: { + _id: msg.username, + }, + ts: new Date(parseInt(msg.ts)), + msg: msg.text, + }; + + usedUsernames.add(msg.username); + usedUsernames.add(msg.otherUsername); + await this.converter.addMessage(newMessage); } - })(), - ); + } else { + const rid = getRoomId(folderName); + + for await (const msg of data) { + const newMessage = { + rid, + u: { + _id: msg.username, + }, + ts: new Date(parseInt(msg.ts)), + msg: msg.text, + }; + + usedUsernames.add(msg.username); + await this.converter.addMessage(newMessage); + } + } + + await super.updateRecord({ 'count.messages': messagesCount, 'messagesstatus': null }); + return increaseProgressCount(); + } increaseProgressCount(); - }); + } if (usersCount) { await Settings.incrementValueById('CSV_Importer_Count', usersCount);