perf: 优化导入的设计

This commit is contained in:
Clansty 2022-03-08 23:22:15 +08:00
parent 8771bd9490
commit 321e867d81
No known key found for this signature in database
GPG Key ID: 05F8479BA63A8E92
3 changed files with 113 additions and 103 deletions

View File

@ -11,8 +11,6 @@ import os from 'os';
import TelegramChat from './TelegramChat';
import TelegramSession from '../models/TelegramSession';
import { LogLevel } from 'telegram/extensions/Logger';
import { CustomFile } from 'telegram/client/uploads';
import { TelegramImportSession } from './TelegramImportSession';
type MessageHandler = (message: Api.Message) => Promise<boolean | void>;
type ServiceMessageHandler = (message: Api.MessageService) => Promise<boolean | void>;
@ -171,18 +169,4 @@ export default class Telegram {
const newChat = updates.chats[0];
return new TelegramChat(this, this.client, newChat, this.waitForMessageHelper);
}
public async startImportSession(chat: TelegramChat, textFile: CustomFile, mediaCount: number) {
const init = await this.client.invoke(
new Api.messages.InitHistoryImport({
peer: chat.entity,
file: await this.client.uploadFile({
file: textFile,
workers: 1,
}),
mediaCount,
}),
);
return new TelegramImportSession(chat, this.client, init.id);
}
}

View File

@ -7,6 +7,7 @@ import { CustomFile } from 'telegram/client/uploads';
import Telegram from './Telegram';
import createPaginatedInlineSelector from '../utils/paginatedInlineSelector';
import inlineDigitInput from '../utils/inlineDigitInput';
import { TelegramImportSession } from './TelegramImportSession';
export default class TelegramChat {
public readonly inputPeer: Api.TypeInputPeer;
@ -204,4 +205,23 @@ export default class TelegramChat {
})
);
}
public async startImportSession(textFile: CustomFile, mediaCount: number) {
await this.client.invoke(
new Api.messages.CheckHistoryImportPeer({
peer: this.entity,
})
);
const init = await this.client.invoke(
new Api.messages.InitHistoryImport({
peer: this.entity,
file: await this.client.uploadFile({
file: textFile,
workers: 1,
}),
mediaCount,
}),
);
return new TelegramImportSession(this, this.client, init.id);
}
}

View File

@ -135,11 +135,11 @@ export default {
console.log('正在准备导入…');
const newChat = await telegram.createChat(chatName);
const txtBuffer = Buffer.from(output, 'utf-8');
try {
const newChat = await telegram.createChat(chatName);
const importSession = await telegram.startImportSession(
newChat,
const importSession = await newChat.startImportSession(
new CustomFile('record.txt', txtBuffer.length, '', txtBuffer),
files.size,
);
@ -228,8 +228,14 @@ export default {
}
await importSession.finish();
await tmpDir.cleanup();
console.log('导入成功!');
}
catch (e) {
console.error('错误', e);
const dumpPath = path.join(outputPath, 'record');
await fsP.writeFile(dumpPath, txtBuffer);
console.log('临时文件位置', outputPath);
}
},
};