Разделение
This commit is contained in:
@@ -4,17 +4,17 @@ using System.IO.Compression;
|
||||
using System.Linq;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
using Knot.Contracts.Conversations.Application.Abstractions;
|
||||
using Knot.Contracts.Conversations.Domain;
|
||||
using Knot.Contracts.Messaging.Application.Abstractions;
|
||||
using Knot.Contracts.Messaging.Domain;
|
||||
using Knot.Modules.TelegramImport.Application.Abstractions;
|
||||
using Knot.Modules.TelegramImport.Application.TelegramImport;
|
||||
using Knot.Modules.TelegramImport.Infrastructure.Background;
|
||||
using Knot.Shared.Kernel.Storage;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using Microsoft.Extensions.Hosting;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using Knot.Modules.TelegramImport.Application.Abstractions;
|
||||
using Knot.Modules.TelegramImport.Application.TelegramImport;
|
||||
using Knot.Modules.Messaging.Application.Abstractions;
|
||||
using Knot.Modules.Conversations.Application.Abstractions;
|
||||
using Knot.Modules.Messaging.Domain;
|
||||
using Knot.Modules.Conversations.Domain;
|
||||
using Knot.Shared.Kernel.Storage;
|
||||
using Knot.Modules.TelegramImport.Infrastructure.Background;
|
||||
|
||||
namespace Knot.Modules.TelegramImport.Infrastructure.Background;
|
||||
|
||||
@@ -34,7 +34,7 @@ public class TelegramImportWorker : BackgroundService
|
||||
protected override async Task ExecuteAsync(CancellationToken stoppingToken)
|
||||
{
|
||||
_logger.LogInformation("Telegram Import Worker started.");
|
||||
|
||||
|
||||
// В реальном проекте здесь будет чтение из Channels или RabbitMQ
|
||||
// Для примера оставим заглушку цикла
|
||||
while (!stoppingToken.IsCancellationRequested)
|
||||
@@ -55,33 +55,33 @@ public class TelegramImportWorker : BackgroundService
|
||||
var jobInfo = new ImportJobInfo { JobId = jobId, Status = ImportJobStatus.Processing };
|
||||
_jobStore.AddOrUpdate(jobInfo);
|
||||
|
||||
try
|
||||
try
|
||||
{
|
||||
using var archive = ZipFile.OpenRead(zipPath);
|
||||
var entries = archive.Entries.Where(e => e.Name.StartsWith("messages") && e.Name.EndsWith(".html")).ToList();
|
||||
using var archive = ZipFile.OpenRead(zipPath);
|
||||
var entries = archive.Entries.Where(e => e.Name.StartsWith("messages") && e.Name.EndsWith(".html")).ToList();
|
||||
|
||||
// 1. Создание чата (уже было в оригинале, но здесь в фоне)
|
||||
Guid targetChatId = Guid.NewGuid(); // Упростим логику для демонстрации рефакторинга
|
||||
// 1. Создание чата (уже было в оригинале, но здесь в фоне)
|
||||
Guid targetChatId = Guid.NewGuid(); // Упростим логику для демонстрации рефакторинга
|
||||
|
||||
foreach (var entry in entries)
|
||||
{
|
||||
using var stream = entry.Open();
|
||||
var messages = await parser.ParseMessagesAsync(stream, "", ct);
|
||||
|
||||
foreach (var m in messages)
|
||||
{
|
||||
Guid senderGuid = request.Mapping.TryGetValue(m.SenderName ?? "", out var sid) ? sid : request.CurrentUserId;
|
||||
|
||||
var textMsg = new TextMessage(Guid.NewGuid(), targetChatId, senderGuid, m.Content, null, null, null, m.CreatedAt, true);
|
||||
msgRepo.Add(textMsg);
|
||||
|
||||
jobInfo.ProcessedMessages++;
|
||||
_jobStore.AddOrUpdate(jobInfo);
|
||||
}
|
||||
await uow.SaveChangesAsync(ct);
|
||||
}
|
||||
|
||||
jobInfo.Status = ImportJobStatus.Completed;
|
||||
foreach (var entry in entries)
|
||||
{
|
||||
using var stream = entry.Open();
|
||||
var messages = await parser.ParseMessagesAsync(stream, "", ct);
|
||||
|
||||
foreach (var m in messages)
|
||||
{
|
||||
Guid senderGuid = request.Mapping.TryGetValue(m.SenderName ?? "", out var sid) ? sid : request.CurrentUserId;
|
||||
|
||||
var textMsg = new TextMessage(Guid.NewGuid(), targetChatId, senderGuid, m.Content, null, null, null, m.CreatedAt, true);
|
||||
msgRepo.Add(textMsg);
|
||||
|
||||
jobInfo.ProcessedMessages++;
|
||||
_jobStore.AddOrUpdate(jobInfo);
|
||||
}
|
||||
await uow.SaveChangesAsync(ct);
|
||||
}
|
||||
|
||||
jobInfo.Status = ImportJobStatus.Completed;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user