diff --git a/src/SauceNAO.LocalWebhook/Program.cs b/src/SauceNAO.LocalWebhook/Program.cs index b911859..701f8ef 100644 --- a/src/SauceNAO.LocalWebhook/Program.cs +++ b/src/SauceNAO.LocalWebhook/Program.cs @@ -14,9 +14,9 @@ builder.Services.AddControllers(); // Configure database context -var connectionString = builder.Configuration.GetConnectionString("SauceNAO"); -builder.Services.AddDbContext(options => options.UseSqlite(connectionString)); -// builder.Services.AddDbContext(options => options.UseSqlServer(connectionString)); +var connectionString = builder.Configuration.GetConnectionString("SNAO"); +// builder.Services.AddDbContext(options => options.UseSqlite(connectionString)); +builder.Services.AddDbContext(options => options.UseSqlServer(connectionString)); // Configure cache context var cacheConnection = $"Data Source={Path.GetTempFileName()}"; // Get connection string for cache @@ -37,19 +37,22 @@ var tunnelName = ngrok["TunnelName"] ?? "SnaoTunnel"; var tunnel = agent.ListTunnels().Tunnels.FirstOrDefault(t => t.Name == tunnelName); - if (tunnel == null) + if (tunnel != null) { - var port = ngrok["Port"]; - var hostheader = string.Format("localhost:{0}", port); - var address = string.Format("http://{0}", hostheader); - - var tunnelConfig = new TunnelConfiguration(tunnelName, "http", address) - { - HostHeader = hostheader, - BindTls = "true" - }; - tunnel = agent.StartTunnel(tunnelConfig); + agent.StopTunnel(tunnelName); } + + var port = ngrok["Port"]; + var hostheader = string.Format("localhost:{0}", port); + var address = string.Format("https://{0}", hostheader); + + var tunnelConfig = new HttpTunnelConfiguration(tunnelName, address) + { + HostHeader = hostheader, + Schemes = new string[] { "https" } + }; + tunnel = agent.StartTunnel(tunnelConfig); + appUrl = tunnel.PublicUrl; } @@ -97,16 +100,18 @@ #endif } -// Create cache file using (var scope = app.Services.CreateScope()) { + // Create cache file using var context = scope.ServiceProvider.GetRequiredService(); context.Database.EnsureCreated(); + // Initialize bot + _ = scope.ServiceProvider.GetRequiredService(); } // Configure the HTTP request pipeline. -// app.UseHttpsRedirection(); +app.UseHttpsRedirection(); app.UseAuthorization(); diff --git a/src/SauceNAO.LocalWebhook/SauceNAO.LocalWebhook.csproj b/src/SauceNAO.LocalWebhook/SauceNAO.LocalWebhook.csproj index 64c799b..0e950bc 100644 --- a/src/SauceNAO.LocalWebhook/SauceNAO.LocalWebhook.csproj +++ b/src/SauceNAO.LocalWebhook/SauceNAO.LocalWebhook.csproj @@ -9,7 +9,8 @@ - + + diff --git a/src/SauceNao.Core/API/Types/ResultData.cs b/src/SauceNao.Core/API/Types/ResultData.cs index cad8b0f..fcf951a 100644 --- a/src/SauceNao.Core/API/Types/ResultData.cs +++ b/src/SauceNao.Core/API/Types/ResultData.cs @@ -61,7 +61,7 @@ public sealed class ResultData [JsonPropertyName("konachan_id")] public uint KonachanId { get; set; } [JsonPropertyName("md_id")] - public uint MdId { get; set; } + public string MdId { get; set; } [JsonPropertyName("mu_id")] public uint MuId { get; set; } [JsonPropertyName("mal_id")] diff --git a/src/SauceNao.Core/Bot/SauceNaoBot.CommandHandler.cs b/src/SauceNao.Core/Bot/SauceNaoBot.CommandHandler.cs index 7c2cd30..d21903d 100644 --- a/src/SauceNao.Core/Bot/SauceNaoBot.CommandHandler.cs +++ b/src/SauceNao.Core/Bot/SauceNaoBot.CommandHandler.cs @@ -622,8 +622,7 @@ private async Task SauceAsync(Message message, CancellationToken cancellationTok // save sauce to db and update user's search history sauce = new SuccessfulSauce(sauceResult, targetMedia, Date); await _db.Sauces.InsertAsync(sauce, cancellationToken).ConfigureAwait(false); - User.UserSauces.Add(new UserSauce(sauce.Key, Date)); - await _db.Users.UpdateAsync(User, cancellationToken).ConfigureAwait(false); + await _db.Users.InsertSauceAsync(User.Id, new UserSauce(sauce.Key, Date), cancellationToken).ConfigureAwait(false); break; case SauceStatus.NotFound: if (!Properties.WebhookMode) // Local Mode @@ -658,8 +657,7 @@ private async Task SauceAsync(Message message, CancellationToken cancellationTok if (userSauce == default) { userSauce = new UserSauce(sauce.Key, Date); - User.UserSauces.Add(userSauce); - await _db.Users.UpdateAsync(User, cancellationToken).ConfigureAwait(false); + await _db.Users.InsertSauceAsync(User.Id, userSauce, cancellationToken).ConfigureAwait(false); } else { diff --git a/src/SauceNao.Core/Bot/SauceNaoBot.InlineHandler.cs b/src/SauceNao.Core/Bot/SauceNaoBot.InlineHandler.cs index 610ce92..4d2daef 100644 --- a/src/SauceNao.Core/Bot/SauceNaoBot.InlineHandler.cs +++ b/src/SauceNao.Core/Bot/SauceNaoBot.InlineHandler.cs @@ -140,7 +140,11 @@ void AddUserSauces(IEnumerable sauces) { NextOffset = results.Count < 10 ? string.Empty : offset.ToString(), IsPersonal = true, +#if DEBUG + CacheTime = 16 +#else CacheTime = 480 +#endif }; await Api.AnswerInlineQueryAsync(answeriquery, cancellationToken: cancellationToken).ConfigureAwait(false); } diff --git a/src/SauceNao.Core/Data/IUserRepository.cs b/src/SauceNao.Core/Data/IUserRepository.cs index 90285ee..5aac6f1 100644 --- a/src/SauceNao.Core/Data/IUserRepository.cs +++ b/src/SauceNao.Core/Data/IUserRepository.cs @@ -22,5 +22,21 @@ public interface IUserRepository : IRepository /// Is private. /// The cancellation token. Task GetUserAsync(ITelegramUser telegramUser, [Optional] CancellationToken cancellationToken); + + /// + /// Insert a new sauce in the user's sauce history. + /// + /// Unique identifier for the user. + /// User sauce. + /// + UserSauce InsertSauce(long userId, UserSauce userSauce); + /// + /// Insert a new sauce in the user's sauce history. + /// + /// Unique identifier for the user. + /// User sauce. + /// The cancellation token. + /// + Task InsertSauceAsync(long userId, UserSauce userSauce, [Optional] CancellationToken cancellationToken); } } diff --git a/src/SauceNao.Infrastructure/Data/UserRepository.cs b/src/SauceNao.Infrastructure/Data/UserRepository.cs index 8e22502..6622ed1 100644 --- a/src/SauceNao.Infrastructure/Data/UserRepository.cs +++ b/src/SauceNao.Infrastructure/Data/UserRepository.cs @@ -18,7 +18,9 @@ public UserRepository(SauceNaoContext context) : base(context) public IQueryable GetAllUsers() { - return Context.Users.AsNoTracking().Include(u => u.UserSauces).ThenInclude(u => u.Sauce).AsQueryable(); + return Context.Users.AsNoTracking() + .Include(u => u.UserSauces) + .ThenInclude(u => u.Sauce).AsQueryable(); } public UserData GetUser(ITelegramUser telegramUser) @@ -64,5 +66,22 @@ public async Task GetUserAsync(ITelegramUser telegramUser, [Optional] } return user; } + + public UserSauce InsertSauce(long userId, UserSauce userSauce) + { + userSauce.UserId = userId; + Context.Add(userSauce); + Context.SaveChanges(); + + return userSauce; + } + + public async Task InsertSauceAsync(long userId, UserSauce userSauce, [Optional] CancellationToken cancellationToken) + { + userSauce.UserId = userId; + Context.Add(userSauce); + await Context.SaveChangesAsync(cancellationToken).ConfigureAwait(false); + return userSauce; + } } }