Skip to content

Commit

Permalink
Updated packages
Browse files Browse the repository at this point in the history
  • Loading branch information
Eptagone committed May 5, 2022
1 parent ee933f4 commit 5326558
Show file tree
Hide file tree
Showing 7 changed files with 66 additions and 23 deletions.
37 changes: 21 additions & 16 deletions src/SauceNAO.LocalWebhook/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@
builder.Services.AddControllers();

// Configure database context
var connectionString = builder.Configuration.GetConnectionString("SauceNAO");
builder.Services.AddDbContext<SauceNaoContext>(options => options.UseSqlite(connectionString));
// builder.Services.AddDbContext<SauceNaoContext>(options => options.UseSqlServer(connectionString));
var connectionString = builder.Configuration.GetConnectionString("SNAO");
// builder.Services.AddDbContext<SauceNaoContext>(options => options.UseSqlite(connectionString));
builder.Services.AddDbContext<SauceNaoContext>(options => options.UseSqlServer(connectionString));

// Configure cache context
var cacheConnection = $"Data Source={Path.GetTempFileName()}"; // Get connection string for cache
Expand All @@ -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;
}

Expand Down Expand Up @@ -97,16 +100,18 @@
#endif
}

// Create cache file
using (var scope = app.Services.CreateScope())
{
// Create cache file
using var context = scope.ServiceProvider.GetRequiredService<CacheDbContext>();
context.Database.EnsureCreated();
// Initialize bot
_ = scope.ServiceProvider.GetRequiredService<SnaoBotProperties>();
}

// Configure the HTTP request pipeline.

// app.UseHttpsRedirection();
app.UseHttpsRedirection();

app.UseAuthorization();

Expand Down
3 changes: 2 additions & 1 deletion src/SauceNAO.LocalWebhook/SauceNAO.LocalWebhook.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@

<ItemGroup>
<PackageReference Include="Microsoft.EntityFrameworkCore.Sqlite" Version="6.0.4" />
<PackageReference Include="Ngrok.AgentAPI" Version="0.1.0" />
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="6.0.4" />
<PackageReference Include="Ngrok.AgentAPI" Version="0.2.0" />
</ItemGroup>

<ItemGroup>
Expand Down
2 changes: 1 addition & 1 deletion src/SauceNao.Core/API/Types/ResultData.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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")]
Expand Down
6 changes: 2 additions & 4 deletions src/SauceNao.Core/Bot/SauceNaoBot.CommandHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
{
Expand Down
4 changes: 4 additions & 0 deletions src/SauceNao.Core/Bot/SauceNaoBot.InlineHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,11 @@ void AddUserSauces(IEnumerable<SuccessfulSauce> 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);
}
Expand Down
16 changes: 16 additions & 0 deletions src/SauceNao.Core/Data/IUserRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,5 +22,21 @@ public interface IUserRepository : IRepository<UserData>
/// <param name="isPrivate">Is private.</param>
/// <param name="cancellationToken">The cancellation token.</param>
Task<UserData> GetUserAsync(ITelegramUser telegramUser, [Optional] CancellationToken cancellationToken);

/// <summary>
/// Insert a new sauce in the user's sauce history.
/// </summary>
/// <param name="userId">Unique identifier for the user.</param>
/// <param name="userSauce">User sauce.</param>
/// <returns><see cref="UserSauce"/></returns>
UserSauce InsertSauce(long userId, UserSauce userSauce);
/// <summary>
/// Insert a new sauce in the user's sauce history.
/// </summary>
/// <param name="userId">Unique identifier for the user.</param>
/// <param name="userSauce">User sauce.</param>
/// <param name="cancellationToken">The cancellation token.</param>
/// <returns><see cref="UserSauce"/></returns>
Task<UserSauce> InsertSauceAsync(long userId, UserSauce userSauce, [Optional] CancellationToken cancellationToken);
}
}
21 changes: 20 additions & 1 deletion src/SauceNao.Infrastructure/Data/UserRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,9 @@ public UserRepository(SauceNaoContext context) : base(context)

public IQueryable<UserData> 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)
Expand Down Expand Up @@ -64,5 +66,22 @@ public async Task<UserData> 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<UserSauce> InsertSauceAsync(long userId, UserSauce userSauce, [Optional] CancellationToken cancellationToken)
{
userSauce.UserId = userId;
Context.Add(userSauce);
await Context.SaveChangesAsync(cancellationToken).ConfigureAwait(false);
return userSauce;
}
}
}

0 comments on commit 5326558

Please sign in to comment.