Switch MAX bridge to PyMax only
This commit is contained in:
@@ -1,5 +1,8 @@
|
||||
using Microsoft.AspNetCore.SignalR;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.Extensions.Options;
|
||||
using QMax.Api.Configuration;
|
||||
using QMax.Api.Contracts;
|
||||
using QMax.Api.Data;
|
||||
using QMax.Api.Data.Entities;
|
||||
using QMax.Api.Infrastructure.Hubs;
|
||||
@@ -29,10 +32,41 @@ public sealed class MaxBridgeSyncService(
|
||||
catch (Exception ex)
|
||||
{
|
||||
logger.LogWarning(ex, "MAX sync failed.");
|
||||
await PublishCurrentMaxStatusAsync(cancellationToken);
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
private async Task PublishCurrentMaxStatusAsync(CancellationToken cancellationToken)
|
||||
{
|
||||
try
|
||||
{
|
||||
var status = await maxBridgeClient.GetStatusAsync(cancellationToken);
|
||||
await using var scope = scopeFactory.CreateAsyncScope();
|
||||
var db = scope.ServiceProvider.GetRequiredService<QMaxDbContext>();
|
||||
var options = scope.ServiceProvider.GetRequiredService<IOptions<QMaxOptions>>().Value;
|
||||
var state = await db.MaxAccountStates.FirstOrDefaultAsync(x => x.Id == 1, cancellationToken);
|
||||
if (state is null)
|
||||
{
|
||||
state = new MaxAccountState { Id = 1 };
|
||||
db.MaxAccountStates.Add(state);
|
||||
}
|
||||
|
||||
state.PhoneNumber = options.MaxPhoneNumber;
|
||||
state.Status = status.Status;
|
||||
state.IsAuthorized = status.IsAuthorized;
|
||||
state.LastUrl = status.Url;
|
||||
state.LastError = status.LastError;
|
||||
state.UpdatedAt = DateTimeOffset.UtcNow;
|
||||
await db.SaveChangesAsync(cancellationToken);
|
||||
await hubContext.Clients.All.SendAsync("MaxStatusChanged", ToDto(status), cancellationToken);
|
||||
}
|
||||
catch (Exception statusError)
|
||||
{
|
||||
logger.LogDebug(statusError, "Unable to publish current MAX status after sync failure.");
|
||||
}
|
||||
}
|
||||
|
||||
public async Task<int> SyncChatHistoryAsync(string externalChatId, string? chatUrl, CancellationToken cancellationToken)
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(externalChatId))
|
||||
@@ -1387,6 +1421,11 @@ public sealed class MaxBridgeSyncService(
|
||||
};
|
||||
}
|
||||
|
||||
private static MaxBridgeStatusDto ToDto(MaxBridgeStatus status)
|
||||
{
|
||||
return new MaxBridgeStatusDto(status.Mode, status.IsAuthorized, status.LoginStage, status.Status, status.Url, status.Title, status.LastError, status.UpdatedAt);
|
||||
}
|
||||
|
||||
private sealed record LastKnownMessageSnapshot(
|
||||
MessageDirection Direction,
|
||||
string? Text,
|
||||
|
||||
Reference in New Issue
Block a user