| | | 1 | | using System.Data.Common; |
| | | 2 | | using System.Text.Json; |
| | | 3 | | using Dapper; |
| | | 4 | | using GistBackend.Exceptions; |
| | | 5 | | using GistBackend.Types; |
| | | 6 | | using GistBackend.Utils; |
| | | 7 | | using Microsoft.Extensions.Logging; |
| | | 8 | | using Microsoft.Extensions.Options; |
| | | 9 | | using MySqlConnector; |
| | | 10 | | using static GistBackend.Utils.LogEvents; |
| | | 11 | | |
| | | 12 | | namespace GistBackend.Handlers.MariaDbHandler; |
| | | 13 | | |
| | | 14 | | public interface IMariaDbHandler { |
| | | 15 | | Task< TransactionHandle> OpenTransactionAsync(CancellationToken ct); |
| | | 16 | | Task<RssFeedInfo?> GetFeedInfoByRssUrlAsync(Uri rssUrl, CancellationToken ct); |
| | | 17 | | Task<int> InsertFeedInfoAsync(RssFeedInfo feedInfo, CancellationToken ct); |
| | | 18 | | Task UpdateFeedInfoAsync(RssFeedInfo feedInfo, CancellationToken ct); |
| | | 19 | | Task<Gist?> GetGistByReferenceAsync(string reference, CancellationToken ct); |
| | | 20 | | Task<ConstructedGist?> GetConstructedGistByReference(string reference, LanguageMode? languageMode, CancellationToken |
| | | 21 | | Task<int> InsertGistAsync(Gist gist, CancellationToken ct); |
| | | 22 | | Task<int> InsertGistAsync(Gist gist, TransactionHandle handle, CancellationToken ct); |
| | | 23 | | Task<int> InsertDisabledGistAsync(DisabledGist gist, CancellationToken ct); |
| | | 24 | | Task UpdateDisabledGistAsync(DisabledGist gist, CancellationToken ct); |
| | | 25 | | Task InsertSummaryAsync(Summary summary, CancellationToken ct); |
| | | 26 | | Task InsertSummaryAsync(Summary summary, TransactionHandle handle, CancellationToken ct); |
| | | 27 | | Task UpdateGistAsync(Gist gist, TransactionHandle handle, CancellationToken ct); |
| | | 28 | | Task UpdateSummaryAsync(Summary summary, TransactionHandle handle, CancellationToken ct); |
| | | 29 | | Task<bool> DailyRecapExistsAsync(CancellationToken ct); |
| | | 30 | | Task<bool> WeeklyRecapExistsAsync(CancellationToken ct); |
| | | 31 | | Task<List<ConstructedGist>> GetConstructedGistsOfLastDayAsync(CancellationToken ct); |
| | | 32 | | Task<List<ConstructedGist>> GetConstructedGistsOfLastWeekAsync(CancellationToken ct); |
| | | 33 | | Task<int> InsertDailyRecapAsync(RecapAIResponse recapAIResponse, CancellationToken ct); |
| | | 34 | | Task<int> InsertWeeklyRecapAsync(RecapAIResponse recapAIResponse, CancellationToken ct); |
| | | 35 | | Task<List<Gist>> GetAllGistsAsync(CancellationToken ct); |
| | | 36 | | Task<bool> EnsureCorrectDisabledStateForGistAsync(int gistId, bool disabled, CancellationToken ct); |
| | | 37 | | Task<List<ConstructedGist>> GetPreviousConstructedGistsAsync(int take, int? lastGistId, IEnumerable<string> tags, |
| | | 38 | | string? searchQuery, IEnumerable<int> disabledFeeds, LanguageMode? languageMode, bool? includeSponsoredContent, |
| | | 39 | | CancellationToken ct); |
| | | 40 | | Task<ConstructedGist?> GetConstructedGistByIdAsync(int id, LanguageMode? languageMode, CancellationToken ct); |
| | | 41 | | Task<List<RssFeedInfo>> GetAllFeedInfosAsync(CancellationToken ct); |
| | | 42 | | Task<SerializedRecap?> GetLatestRecapAsync(RecapType recapType, CancellationToken ct); |
| | | 43 | | Task<bool> IsChatRegisteredAsync(long chatId, CancellationToken ct); |
| | | 44 | | Task RegisterChatAsync(long chatId, CancellationToken ct); |
| | | 45 | | Task DeregisterChatAsync(long chatId, CancellationToken ct); |
| | | 46 | | Task<List<Chat>> GetAllChatsAsync(CancellationToken ct); |
| | | 47 | | Task<List<ConstructedGist>> GetNextFiveConstructedGistsAsync(int lastGistId, LanguageMode languageMode, |
| | | 48 | | CancellationToken ct); |
| | | 49 | | Task SetGistIdLastSentForChatAsync(long chatId, int gistId, CancellationToken ct); |
| | | 50 | | } |
| | | 51 | | |
| | | 52 | | public class MariaDbHandler : IMariaDbHandler |
| | | 53 | | { |
| | | 54 | | private readonly string _connectionString; |
| | | 55 | | private readonly IDateTimeHandler _dateTimeHandler; |
| | | 56 | | private readonly ILogger<MariaDbHandler>? _logger; |
| | | 57 | | |
| | 128 | 58 | | public MariaDbHandler(IOptions<MariaDbHandlerOptions> options, |
| | 128 | 59 | | IDateTimeHandler dateTimeHandler, |
| | 128 | 60 | | ILogger<MariaDbHandler>? logger) |
| | | 61 | | { |
| | 128 | 62 | | _dateTimeHandler = dateTimeHandler; |
| | 128 | 63 | | _logger = logger; |
| | 128 | 64 | | _connectionString = options.Value.GetConnectionString(); |
| | 128 | 65 | | SqlMapper.AddTypeHandler(new UriTypeHandler()); |
| | 128 | 66 | | } |
| | | 67 | | |
| | | 68 | | public async Task< TransactionHandle> OpenTransactionAsync(CancellationToken ct) |
| | | 69 | | { |
| | | 70 | | try |
| | | 71 | | { |
| | 627 | 72 | | var connection = await GetOpenConnectionAsync(ct); |
| | 627 | 73 | | return new TransactionHandle(connection, await connection.BeginTransactionAsync(ct)); |
| | | 74 | | } |
| | 0 | 75 | | catch (MySqlException e) |
| | | 76 | | { |
| | 0 | 77 | | _logger?.LogError(OpeningTransactionFailed, e, "Opening transaction failed"); |
| | 0 | 78 | | throw; |
| | | 79 | | } |
| | 627 | 80 | | } |
| | | 81 | | |
| | | 82 | | public async Task CommitTransactionAsync(DbTransaction transaction, CancellationToken ct) |
| | | 83 | | { |
| | | 84 | | try |
| | | 85 | | { |
| | 623 | 86 | | await transaction.CommitAsync(ct); |
| | 1246 | 87 | | if (transaction.Connection is null) return; |
| | 0 | 88 | | await transaction.Connection.CloseAsync(); |
| | 0 | 89 | | } |
| | 0 | 90 | | catch (MySqlException e) |
| | | 91 | | { |
| | 0 | 92 | | _logger?.LogError(CommittingTransactionFailed, e, "Committing transaction failed"); |
| | 0 | 93 | | throw; |
| | | 94 | | } |
| | 623 | 95 | | } |
| | | 96 | | |
| | | 97 | | public async Task<RssFeedInfo?> GetFeedInfoByRssUrlAsync(Uri rssUrl, CancellationToken ct) |
| | | 98 | | { |
| | | 99 | | const string query = "SELECT Title, RssUrl, Language, Type, Id FROM Feeds WHERE RssUrl = @RssUrl"; |
| | 3 | 100 | | var command = new CommandDefinition(query, new { RssUrl = rssUrl }, cancellationToken: ct); |
| | | 101 | | |
| | | 102 | | try |
| | | 103 | | { |
| | 3 | 104 | | await using var connection = await GetOpenConnectionAsync(ct); |
| | 3 | 105 | | return await connection.QueryFirstOrDefaultAsync<RssFeedInfo>(command).WithDeadlockRetry(_logger); |
| | 0 | 106 | | } |
| | 0 | 107 | | catch (MySqlException e) |
| | | 108 | | { |
| | 0 | 109 | | _logger?.LogError(GettingFeedInfoByUrlFailed, e, "Getting feedInfo by rssUrl failed"); |
| | 0 | 110 | | throw; |
| | | 111 | | } |
| | 3 | 112 | | } |
| | | 113 | | |
| | | 114 | | public async Task<int> InsertFeedInfoAsync(RssFeedInfo feedInfo, CancellationToken ct) |
| | | 115 | | { |
| | | 116 | | const string query = """ |
| | | 117 | | INSERT INTO Feeds (Title, RssUrl, Language, Type) |
| | | 118 | | VALUES (@Title, @RssUrl, @Language, @Type); |
| | | 119 | | SELECT LAST_INSERT_ID(); |
| | | 120 | | """; |
| | 87 | 121 | | var command = new CommandDefinition(query, feedInfo, cancellationToken: ct); |
| | | 122 | | |
| | 87 | 123 | | await using var connection = await GetOpenConnectionAsync(ct); |
| | | 124 | | try |
| | | 125 | | { |
| | 87 | 126 | | return await connection.ExecuteScalarAsync<int>(command).WithDeadlockRetry(_logger); |
| | | 127 | | } |
| | 1 | 128 | | catch (MySqlException e) |
| | | 129 | | { |
| | 1 | 130 | | _logger?.LogError(InsertingFeedInfoFailed, e, "Inserting FeedInfo failed"); |
| | 1 | 131 | | throw; |
| | | 132 | | } |
| | 86 | 133 | | } |
| | | 134 | | |
| | | 135 | | public async Task UpdateFeedInfoAsync(RssFeedInfo feedInfo, CancellationToken ct) |
| | | 136 | | { |
| | | 137 | | const string query = |
| | | 138 | | "UPDATE Feeds SET Title = @Title, Language = @Language, Type = @Type WHERE RssUrl = @RssUrl"; |
| | 3 | 139 | | var command = new CommandDefinition(query, feedInfo, cancellationToken: ct); |
| | | 140 | | |
| | 3 | 141 | | await using var connection = await GetOpenConnectionAsync(ct); |
| | | 142 | | try |
| | | 143 | | { |
| | 3 | 144 | | var rowsAffected = await connection.ExecuteAsync(command).WithDeadlockRetry(_logger); |
| | 4 | 145 | | if (rowsAffected != 1) throw new DatabaseOperationException("Did not successfully update feed info"); |
| | 2 | 146 | | } |
| | 1 | 147 | | catch (Exception e) when (e is MySqlException or DatabaseOperationException) |
| | | 148 | | { |
| | 1 | 149 | | _logger?.LogError(UpdatingFeedInfoFailed, e, "Updating FeedInfo failed"); |
| | 1 | 150 | | throw; |
| | | 151 | | } |
| | 2 | 152 | | } |
| | | 153 | | |
| | | 154 | | public async Task<Gist?> GetGistByReferenceAsync(string reference, CancellationToken ct) |
| | | 155 | | { |
| | | 156 | | const string query = """ |
| | | 157 | | SELECT Reference, FeedId, Author, IsSponsoredContent, Published, Updated, Url, Tags, Id |
| | | 158 | | FROM Gists WHERE Reference = @Reference |
| | | 159 | | """; |
| | 3 | 160 | | var command = new CommandDefinition(query, new { Reference = reference }, cancellationToken: ct); |
| | | 161 | | |
| | | 162 | | try |
| | | 163 | | { |
| | 3 | 164 | | await using var connection = await GetOpenConnectionAsync(ct); |
| | 3 | 165 | | return await connection.QueryFirstOrDefaultAsync<Gist>(command).WithDeadlockRetry(_logger); |
| | 0 | 166 | | } |
| | 0 | 167 | | catch (MySqlException e) |
| | | 168 | | { |
| | 0 | 169 | | _logger?.LogError(GettingGistByReferenceFailed, e, "Getting gist by reference failed"); |
| | 0 | 170 | | throw; |
| | | 171 | | } |
| | 3 | 172 | | } |
| | | 173 | | |
| | | 174 | | public async Task<ConstructedGist?> GetConstructedGistByReference(string reference, LanguageMode? languageMode, |
| | | 175 | | CancellationToken ct) |
| | | 176 | | { |
| | 3 | 177 | | var parameters = new DynamicParameters(); |
| | 3 | 178 | | var query = $""" |
| | 3 | 179 | | SELECT |
| | 3 | 180 | | g.Id as Id, |
| | 3 | 181 | | g.Reference as Reference, |
| | 3 | 182 | | f.Title as FeedTitle, |
| | 3 | 183 | | f.RssUrl as FeedUrl, |
| | 3 | 184 | | f.Type as FeedType, |
| | 3 | 185 | | s.Title as Title, |
| | 3 | 186 | | g.Author as Author, |
| | 3 | 187 | | g.IsSponsoredContent as IsSponsoredContent, |
| | 3 | 188 | | g.Url as Url, |
| | 3 | 189 | | DATE_FORMAT(g.Published, '%Y-%m-%dT%H:%i:%s.%fZ') as Published, |
| | 3 | 190 | | DATE_FORMAT(g.Updated, '%Y-%m-%dT%H:%i:%s.%fZ') as Updated, |
| | 3 | 191 | | s.SummaryText as Summary, |
| | 3 | 192 | | g.Tags as Tags |
| | 3 | 193 | | FROM Gists g |
| | 3 | 194 | | INNER JOIN Feeds f ON g.FeedId = f.Id |
| | 3 | 195 | | INNER JOIN Summaries s ON s.GistId = g.Id |
| | 3 | 196 | | WHERE g.Reference = @Reference AND {GetLanguageModeConstraint(parameters, languageMode)} |
| | 3 | 197 | | """; |
| | 3 | 198 | | parameters.Add("Reference", reference); |
| | 3 | 199 | | var command = new CommandDefinition(query, parameters, cancellationToken: ct); |
| | | 200 | | |
| | | 201 | | try |
| | | 202 | | { |
| | 3 | 203 | | await using var connection = await GetOpenConnectionAsync(ct); |
| | 3 | 204 | | return await connection.QueryFirstOrDefaultAsync<ConstructedGist>(command).WithDeadlockRetry(_logger); |
| | 0 | 205 | | } |
| | 0 | 206 | | catch (MySqlException e) |
| | | 207 | | { |
| | 0 | 208 | | _logger?.LogError(GettingGistByReferenceFailed, e, "Getting gist by reference failed"); |
| | 0 | 209 | | throw; |
| | | 210 | | } |
| | 3 | 211 | | } |
| | | 212 | | |
| | | 213 | | public async Task<int> InsertGistAsync(Gist gist, CancellationToken ct) |
| | | 214 | | { |
| | | 215 | | try |
| | | 216 | | { |
| | 268 | 217 | | await using var handle = await OpenTransactionAsync(ct); |
| | 268 | 218 | | var gistId = await InsertGistAsync(gist, handle, ct); |
| | 267 | 219 | | await CommitTransactionAsync(handle.Transaction, ct); |
| | 267 | 220 | | return gistId; |
| | 1 | 221 | | } catch (MySqlException e) |
| | | 222 | | { |
| | 1 | 223 | | _logger?.LogError(InsertingGistFailed, e, "Inserting Gist failed"); |
| | 1 | 224 | | throw; |
| | | 225 | | } |
| | 267 | 226 | | } |
| | | 227 | | |
| | | 228 | | public async Task<int> InsertGistAsync(Gist gist, TransactionHandle handle, CancellationToken ct) |
| | | 229 | | { |
| | | 230 | | const string query = """ |
| | | 231 | | INSERT INTO Gists |
| | | 232 | | (Reference, FeedId, Author, IsSponsoredContent, Published, Updated, Url, Tags) |
| | | 233 | | VALUES ( |
| | | 234 | | @Reference, @FeedId, @Author, @IsSponsoredContent, @Published, @Updated, @Url, @Tags |
| | | 235 | | ); |
| | | 236 | | SELECT LAST_INSERT_ID(); |
| | | 237 | | """; |
| | 268 | 238 | | var command = new CommandDefinition(query, gist, handle.Transaction, cancellationToken: ct); |
| | | 239 | | |
| | | 240 | | try |
| | | 241 | | { |
| | 268 | 242 | | return await handle.Connection.ExecuteScalarAsync<int>(command).WithDeadlockRetry(_logger); |
| | | 243 | | } |
| | 1 | 244 | | catch (MySqlException e) |
| | | 245 | | { |
| | 1 | 246 | | _logger?.LogError(InsertingGistFailed, e, "Inserting Gist failed"); |
| | 1 | 247 | | throw; |
| | | 248 | | } |
| | 267 | 249 | | } |
| | | 250 | | |
| | | 251 | | public async Task<int> InsertDisabledGistAsync(DisabledGist gist, CancellationToken ct) |
| | | 252 | | { |
| | | 253 | | const string query = """ |
| | | 254 | | INSERT INTO Gists |
| | | 255 | | (Reference, FeedId, Author, IsSponsoredContent, Published, Updated, Url, Tags, Disabled) |
| | | 256 | | VALUES ( |
| | | 257 | | @Reference, @FeedId, @Author, @IsSponsoredContent, @Published, @Updated, @Url, @Tags, TRUE |
| | | 258 | | ); |
| | | 259 | | SELECT LAST_INSERT_ID(); |
| | | 260 | | """; |
| | 1 | 261 | | var command = new CommandDefinition(query, gist, cancellationToken: ct); |
| | | 262 | | |
| | | 263 | | try |
| | | 264 | | { |
| | 1 | 265 | | await using var connection = await GetOpenConnectionAsync(ct); |
| | 1 | 266 | | return await connection.ExecuteScalarAsync<int>(command).WithDeadlockRetry(_logger); |
| | 0 | 267 | | } |
| | 0 | 268 | | catch (MySqlException e) |
| | | 269 | | { |
| | 0 | 270 | | _logger?.LogError(InsertingGistFailed, e, "Inserting Gist failed"); |
| | 0 | 271 | | throw; |
| | | 272 | | } |
| | 1 | 273 | | } |
| | | 274 | | |
| | | 275 | | public async Task InsertSummaryAsync(Summary summary, CancellationToken ct) |
| | | 276 | | { |
| | | 277 | | try |
| | | 278 | | { |
| | 355 | 279 | | await using var handle = await OpenTransactionAsync(ct); |
| | 355 | 280 | | await InsertSummaryAsync(summary, handle, ct); |
| | 354 | 281 | | await CommitTransactionAsync(handle.Transaction, ct); |
| | 354 | 282 | | } |
| | 1 | 283 | | catch (MySqlException e) |
| | | 284 | | { |
| | 1 | 285 | | _logger?.LogError(InsertingSummaryFailed, e, "Inserting Summary failed"); |
| | 1 | 286 | | throw; |
| | | 287 | | } |
| | 354 | 288 | | } |
| | | 289 | | |
| | | 290 | | public async Task InsertSummaryAsync(Summary summary, TransactionHandle handle, CancellationToken ct) |
| | | 291 | | { |
| | | 292 | | const string query = """ |
| | | 293 | | INSERT INTO Summaries (GistId, Language, IsTranslated, Title, SummaryText) |
| | | 294 | | VALUES (@GistId, @Language, @IsTranslated, @Title, @SummaryText); |
| | | 295 | | """; |
| | 355 | 296 | | var command = new CommandDefinition(query, summary, handle.Transaction, cancellationToken: ct); |
| | | 297 | | |
| | | 298 | | try |
| | | 299 | | { |
| | 355 | 300 | | await handle.Connection.ExecuteAsync(command); |
| | 354 | 301 | | } |
| | 1 | 302 | | catch (MySqlException e) |
| | | 303 | | { |
| | 1 | 304 | | _logger?.LogError(InsertingSummaryFailed, e, "Inserting Summary failed"); |
| | 1 | 305 | | throw; |
| | | 306 | | } |
| | 354 | 307 | | } |
| | | 308 | | |
| | | 309 | | public async Task UpdateGistAsync(Gist gist, CancellationToken ct) |
| | | 310 | | { |
| | | 311 | | try |
| | | 312 | | { |
| | 2 | 313 | | await using var handle = await OpenTransactionAsync(ct); |
| | 2 | 314 | | await UpdateGistAsync(gist, handle, ct); |
| | 1 | 315 | | await CommitTransactionAsync(handle.Transaction, ct); |
| | 1 | 316 | | } |
| | 1 | 317 | | catch (Exception e) when (e is MySqlException or DatabaseOperationException) |
| | | 318 | | { |
| | 1 | 319 | | _logger?.LogError(UpdatingGistFailed, e, "Updating gist failed"); |
| | 1 | 320 | | throw; |
| | | 321 | | } |
| | 1 | 322 | | } |
| | | 323 | | |
| | | 324 | | public async Task UpdateGistAsync(Gist gist, TransactionHandle handle, CancellationToken ct) |
| | | 325 | | { |
| | | 326 | | const string query = """ |
| | | 327 | | UPDATE Gists |
| | | 328 | | SET FeedId = @FeedId, Author = @Author, IsSponsoredContent = @IsSponsoredContent, |
| | | 329 | | Published = @Published, Updated = @Updated, Url = @Url, Tags = @Tags |
| | | 330 | | WHERE Reference = @Reference; |
| | | 331 | | """; |
| | 2 | 332 | | var command = new CommandDefinition(query, gist, handle.Transaction, cancellationToken: ct); |
| | | 333 | | |
| | | 334 | | try |
| | | 335 | | { |
| | 2 | 336 | | var rowsAffected = await handle.Connection.ExecuteAsync(command).WithDeadlockRetry(_logger); |
| | 3 | 337 | | if (rowsAffected != 1) throw new DatabaseOperationException("Did not successfully update gist"); |
| | 1 | 338 | | } |
| | 1 | 339 | | catch (Exception e) when (e is MySqlException or DatabaseOperationException) |
| | | 340 | | { |
| | 1 | 341 | | _logger?.LogError(UpdatingGistFailed, e, "Updating gist failed"); |
| | 1 | 342 | | throw; |
| | | 343 | | } |
| | 1 | 344 | | } |
| | | 345 | | |
| | | 346 | | public async Task UpdateDisabledGistAsync(DisabledGist gist, CancellationToken ct) |
| | | 347 | | { |
| | | 348 | | const string query = """ |
| | | 349 | | UPDATE Gists |
| | | 350 | | SET FeedId = @FeedId, Author = @Author, IsSponsoredContent = @IsSponsoredContent, |
| | | 351 | | Published = @Published, Updated = @Updated, Url = @Url, Tags = @Tags, Disabled = TRUE |
| | | 352 | | WHERE Reference = @Reference; |
| | | 353 | | """; |
| | 1 | 354 | | var command = new CommandDefinition(query, gist, cancellationToken: ct); |
| | | 355 | | |
| | | 356 | | try |
| | | 357 | | { |
| | 1 | 358 | | await using var connection = await GetOpenConnectionAsync(ct); |
| | 1 | 359 | | var rowsAffected = await connection.ExecuteAsync(command).WithDeadlockRetry(_logger); |
| | 1 | 360 | | if (rowsAffected != 1) throw new DatabaseOperationException("Did not successfully update gist"); |
| | 1 | 361 | | } |
| | 0 | 362 | | catch (MySqlException e) |
| | | 363 | | { |
| | 0 | 364 | | _logger?.LogError(UpdatingGistFailed, e, "Updating Gist failed"); |
| | 0 | 365 | | throw; |
| | | 366 | | } |
| | 1 | 367 | | } |
| | | 368 | | |
| | | 369 | | public async Task UpdateSummaryAsync(Summary summary, TransactionHandle handle, CancellationToken ct) |
| | | 370 | | { |
| | | 371 | | const string query = """ |
| | | 372 | | UPDATE Summaries |
| | | 373 | | SET Title = @Title, SummaryText = @SummaryText |
| | | 374 | | WHERE GistId = @GistId AND Language = @Language; |
| | | 375 | | """; |
| | 2 | 376 | | var command = new CommandDefinition(query, summary, handle.Transaction, cancellationToken: ct); |
| | | 377 | | |
| | | 378 | | try |
| | | 379 | | { |
| | 2 | 380 | | var rowsAffected = await handle.Connection.ExecuteAsync(command).WithDeadlockRetry(_logger); |
| | 3 | 381 | | if (rowsAffected != 1) throw new DatabaseOperationException("Did not successfully update summary"); |
| | 1 | 382 | | } |
| | 1 | 383 | | catch (Exception e) when (e is MySqlException or DatabaseOperationException) |
| | | 384 | | { |
| | 1 | 385 | | _logger?.LogError(UpdatingSummaryFailed, e, "Updating summary failed"); |
| | 1 | 386 | | throw; |
| | | 387 | | } |
| | 1 | 388 | | } |
| | | 389 | | |
| | 4 | 390 | | public Task<bool> DailyRecapExistsAsync(CancellationToken ct) => RecapExistsAsync(RecapType.Daily, ct); |
| | | 391 | | |
| | 4 | 392 | | public Task<bool> WeeklyRecapExistsAsync(CancellationToken ct) => RecapExistsAsync(RecapType.Weekly, ct); |
| | | 393 | | |
| | | 394 | | private async Task<bool> RecapExistsAsync(RecapType recapType, CancellationToken ct) |
| | | 395 | | { |
| | 8 | 396 | | var query = $"SELECT COUNT(id) FROM Recaps{recapType.ToTypeString()}" + |
| | 8 | 397 | | " WHERE Created >= @StartOfDay AND Created < @StartOfNextDay"; |
| | 8 | 398 | | var startOfDay = _dateTimeHandler.GetUtcNow().Date; |
| | 8 | 399 | | var command = new CommandDefinition(query, |
| | 8 | 400 | | new { StartOfDay = startOfDay, StartOfNextDay = startOfDay.AddDays(1) }, |
| | 8 | 401 | | cancellationToken: ct); |
| | | 402 | | |
| | | 403 | | try |
| | | 404 | | { |
| | 8 | 405 | | await using var connection = await GetOpenConnectionAsync(ct); |
| | 8 | 406 | | var recapCount = await connection.QuerySingleAsync<int>(command).WithDeadlockRetry(_logger); |
| | 8 | 407 | | return recapCount switch { |
| | 4 | 408 | | 0 => false, |
| | 2 | 409 | | 1 => true, |
| | 2 | 410 | | _ => throw new DatabaseOperationException("Found multiple recaps in database") |
| | 8 | 411 | | }; |
| | 0 | 412 | | } |
| | 2 | 413 | | catch (Exception e) when (e is MySqlException or DatabaseOperationException) |
| | | 414 | | { |
| | 2 | 415 | | _logger?.LogError(CheckIfRecapExistsFailed, e, "Check if the {RecapType} recap exists failed", |
| | 2 | 416 | | recapType.ToTypeString()); |
| | 2 | 417 | | throw; |
| | | 418 | | } |
| | 6 | 419 | | } |
| | | 420 | | |
| | | 421 | | public Task<List<ConstructedGist>> GetConstructedGistsOfLastDayAsync(CancellationToken ct) => |
| | 3 | 422 | | GetGistsWithFeedOfLastDaysAsync(1, ct); |
| | | 423 | | |
| | | 424 | | public Task<List<ConstructedGist>> GetConstructedGistsOfLastWeekAsync(CancellationToken ct) => |
| | 3 | 425 | | GetGistsWithFeedOfLastDaysAsync(7, ct); |
| | | 426 | | |
| | | 427 | | private async Task<List<ConstructedGist>> GetGistsWithFeedOfLastDaysAsync(int days, CancellationToken ct) |
| | | 428 | | { |
| | 6 | 429 | | var parameters = new DynamicParameters(); |
| | 6 | 430 | | var query = $""" |
| | 6 | 431 | | SELECT |
| | 6 | 432 | | g.Id as Id, |
| | 6 | 433 | | g.Reference as Reference, |
| | 6 | 434 | | f.Title as FeedTitle, |
| | 6 | 435 | | f.RssUrl as FeedUrl, |
| | 6 | 436 | | f.Type as FeedType, |
| | 6 | 437 | | s.Title as Title, |
| | 6 | 438 | | g.Author as Author, |
| | 6 | 439 | | g.IsSponsoredContent as IsSponsoredContent, |
| | 6 | 440 | | g.Url as Url, |
| | 6 | 441 | | DATE_FORMAT(g.Published, '%Y-%m-%dT%H:%i:%s.%fZ') as Published, |
| | 6 | 442 | | DATE_FORMAT(g.Updated, '%Y-%m-%dT%H:%i:%s.%fZ') as Updated, |
| | 6 | 443 | | s.SummaryText as Summary, |
| | 6 | 444 | | g.Tags as Tags |
| | 6 | 445 | | FROM Gists g |
| | 6 | 446 | | INNER JOIN Feeds f ON g.FeedId = f.Id |
| | 6 | 447 | | INNER JOIN Summaries s ON s.GistId = g.Id |
| | 6 | 448 | | WHERE {GetLanguageModeConstraint(parameters, LanguageMode.Original)} AND g.IsSponsoredContent IS FALSE |
| | 6 | 449 | | AND Updated >= @EarliestUpdated AND Updated <= @Now |
| | 6 | 450 | | """; |
| | 6 | 451 | | var now = _dateTimeHandler.GetUtcNow(); |
| | 6 | 452 | | var earliestUpdated = now.AddDays(-days); |
| | 6 | 453 | | parameters.Add("Now", now); |
| | 6 | 454 | | parameters.Add("EarliestUpdated", earliestUpdated); |
| | 6 | 455 | | var command = new CommandDefinition(query, parameters, cancellationToken: ct); |
| | | 456 | | |
| | | 457 | | try |
| | | 458 | | { |
| | 6 | 459 | | await using var connection = await GetOpenConnectionAsync(ct); |
| | 6 | 460 | | return (await connection.QueryAsync<ConstructedGist>(command).WithDeadlockRetry(_logger)).ToList(); |
| | 0 | 461 | | } |
| | 0 | 462 | | catch (MySqlException e) |
| | | 463 | | { |
| | 0 | 464 | | _logger?.LogError(GettingGistsForRecapFailed, e, "Getting the gists of the last {Days} days failed", days); |
| | 0 | 465 | | throw; |
| | | 466 | | } |
| | 6 | 467 | | } |
| | | 468 | | |
| | | 469 | | public Task<int> InsertDailyRecapAsync(RecapAIResponse recapAIResponse, CancellationToken ct) => |
| | 10 | 470 | | InsertRecapAsync(RecapType.Daily, recapAIResponse, ct); |
| | | 471 | | |
| | | 472 | | public Task<int> InsertWeeklyRecapAsync(RecapAIResponse recapAIResponse, CancellationToken ct) => |
| | 10 | 473 | | InsertRecapAsync(RecapType.Weekly, recapAIResponse, ct); |
| | | 474 | | |
| | | 475 | | private async Task<int> InsertRecapAsync(RecapType recapType, RecapAIResponse recapAIResponse, CancellationToken ct) |
| | | 476 | | { |
| | 20 | 477 | | var query = $""" |
| | 20 | 478 | | INSERT INTO Recaps{recapType.ToTypeString()} (Created, RecapEn, RecapDe) |
| | 20 | 479 | | VALUES (@Created, @RecapEn, @RecapDe); |
| | 20 | 480 | | SELECT LAST_INSERT_ID(); |
| | 20 | 481 | | """; |
| | 20 | 482 | | var serializedRecap = new SerializedRecap( |
| | 20 | 483 | | _dateTimeHandler.GetUtcNow(), |
| | 20 | 484 | | JsonSerializer.Serialize(recapAIResponse.RecapSectionsEnglish, SerializerDefaults.JsonOptions), |
| | 20 | 485 | | JsonSerializer.Serialize(recapAIResponse.RecapSectionsGerman, SerializerDefaults.JsonOptions) |
| | 20 | 486 | | ); |
| | 20 | 487 | | var command = new CommandDefinition(query, serializedRecap, cancellationToken: ct); |
| | | 488 | | |
| | | 489 | | try |
| | | 490 | | { |
| | 20 | 491 | | await using var connection = await GetOpenConnectionAsync(ct); |
| | 20 | 492 | | return await connection.ExecuteScalarAsync<int>(command).WithDeadlockRetry(_logger); |
| | 0 | 493 | | } |
| | 0 | 494 | | catch (MySqlException e) |
| | | 495 | | { |
| | 0 | 496 | | _logger?.LogError(e, "Inserting {RecapType} recap failed", recapType.ToTypeString()); |
| | 0 | 497 | | throw; |
| | | 498 | | } |
| | 20 | 499 | | } |
| | | 500 | | |
| | | 501 | | public async Task<List<Gist>> GetAllGistsAsync(CancellationToken ct) |
| | | 502 | | { |
| | | 503 | | const string query = |
| | | 504 | | "SELECT Reference, FeedId, Author, IsSponsoredContent, Published, Updated, Url, Tags, Id FROM Gists"; |
| | 2 | 505 | | var command = new CommandDefinition(query, cancellationToken: ct); |
| | | 506 | | |
| | | 507 | | try |
| | | 508 | | { |
| | 2 | 509 | | await using var connection = await GetOpenConnectionAsync(ct); |
| | 2 | 510 | | return (await connection.QueryAsync<Gist>(command).WithDeadlockRetry(_logger)).ToList(); |
| | 0 | 511 | | } |
| | 0 | 512 | | catch (MySqlException e) |
| | | 513 | | { |
| | 0 | 514 | | _logger?.LogError(GettingAllGistsFailed, e, "Getting all gists failed"); |
| | 0 | 515 | | throw; |
| | | 516 | | } |
| | 2 | 517 | | } |
| | | 518 | | |
| | | 519 | | public async Task<bool> EnsureCorrectDisabledStateForGistAsync(int gistId, bool disabled, CancellationToken ct) |
| | | 520 | | { |
| | 18 | 521 | | if (await GetDisabledStateForGistAsync(gistId, ct) == disabled) return true; |
| | | 522 | | const string query = "UPDATE Gists SET Disabled = @Disabled WHERE Id = @GistId"; |
| | 14 | 523 | | var command = new CommandDefinition(query, new { Disabled = disabled, GistId = gistId }, cancellationToken: ct); |
| | | 524 | | |
| | | 525 | | try |
| | | 526 | | { |
| | 14 | 527 | | await using var connection = await GetOpenConnectionAsync(ct); |
| | 14 | 528 | | var rowsAffected = await connection.ExecuteAsync(command).WithDeadlockRetry(_logger); |
| | 14 | 529 | | if (rowsAffected != 1) throw new DatabaseOperationException("Did not successfully set gist disabled state"); |
| | 14 | 530 | | } |
| | 0 | 531 | | catch (Exception e) when (e is MySqlException or DatabaseOperationException) |
| | | 532 | | { |
| | 0 | 533 | | _logger?.LogError(EnsuringCorrectDisabledFailed, e, "Ensuring correct disabled state for gist failed"); |
| | 0 | 534 | | throw; |
| | | 535 | | } |
| | | 536 | | |
| | 14 | 537 | | _logger?.LogInformation(ChangedDisabledStateOfGistInDb, |
| | 14 | 538 | | "Changed disabled state of gist with ID {GistId} to {Disabled}", gistId, disabled); |
| | 14 | 539 | | return false; |
| | 16 | 540 | | } |
| | | 541 | | |
| | | 542 | | private async Task<bool> GetDisabledStateForGistAsync(int gistId, CancellationToken ct) |
| | | 543 | | { |
| | | 544 | | const string query = "SELECT Disabled FROM Gists WHERE Id = @GistId"; |
| | 16 | 545 | | var command = new CommandDefinition(query, new { GistId = gistId }, cancellationToken: ct); |
| | | 546 | | |
| | | 547 | | try |
| | | 548 | | { |
| | 16 | 549 | | await using var connection = await GetOpenConnectionAsync(ct); |
| | 16 | 550 | | return await connection.QuerySingleAsync<bool>(command).WithDeadlockRetry(_logger); |
| | 0 | 551 | | } |
| | 0 | 552 | | catch (MySqlException e) |
| | | 553 | | { |
| | 0 | 554 | | _logger?.LogError(GettingDisabledStateFailed, e, "Getting disabled state for gist failed"); |
| | 0 | 555 | | throw; |
| | | 556 | | } |
| | 16 | 557 | | } |
| | | 558 | | |
| | | 559 | | public async Task<List<ConstructedGist>> GetPreviousConstructedGistsAsync(int take, int? lastGistId, IEnumerable<str |
| | | 560 | | string? searchQuery, IEnumerable<int> disabledFeeds, LanguageMode? languageMode, bool? includeSponsoredContent, |
| | | 561 | | CancellationToken ct) |
| | | 562 | | { |
| | 52 | 563 | | var parameters = new DynamicParameters(); |
| | 52 | 564 | | var constraints = new List<string> { |
| | 52 | 565 | | "Disabled IS FALSE", |
| | 52 | 566 | | GetLanguageModeConstraint(parameters, languageMode) |
| | 52 | 567 | | }; |
| | | 568 | | |
| | 52 | 569 | | AddSponsoredContentConstraint(constraints, includeSponsoredContent); |
| | 52 | 570 | | AddLastGistIdConstraint(parameters, constraints, lastGistId); |
| | 52 | 571 | | AddSearchQueryConstraint(parameters, constraints, searchQuery); |
| | 52 | 572 | | AddTagsConstraint(parameters, constraints, tags); |
| | 52 | 573 | | AddDisabledFeedsConstraint(parameters, constraints, disabledFeeds); |
| | 52 | 574 | | parameters.Add("Take", take); |
| | | 575 | | |
| | 52 | 576 | | var constraintsTemplate = string.Join(" AND ", constraints); |
| | | 577 | | |
| | 52 | 578 | | var query = $""" |
| | 52 | 579 | | SELECT |
| | 52 | 580 | | g.Id as Id, |
| | 52 | 581 | | g.Reference as Reference, |
| | 52 | 582 | | f.Title as FeedTitle, |
| | 52 | 583 | | f.RssUrl as FeedUrl, |
| | 52 | 584 | | f.Type as FeedType, |
| | 52 | 585 | | s.Title as Title, |
| | 52 | 586 | | g.Author as Author, |
| | 52 | 587 | | g.IsSponsoredContent as IsSponsoredContent, |
| | 52 | 588 | | g.Url as Url, |
| | 52 | 589 | | DATE_FORMAT(g.Published, '%Y-%m-%dT%H:%i:%s.%fZ') as Published, |
| | 52 | 590 | | DATE_FORMAT(g.Updated, '%Y-%m-%dT%H:%i:%s.%fZ') as Updated, |
| | 52 | 591 | | s.SummaryText as Summary, |
| | 52 | 592 | | g.Tags as Tags |
| | 52 | 593 | | FROM Gists g |
| | 52 | 594 | | INNER JOIN Feeds f ON g.FeedId = f.Id |
| | 52 | 595 | | INNER JOIN Summaries s ON s.GistId = g.Id |
| | 52 | 596 | | WHERE {constraintsTemplate} |
| | 52 | 597 | | ORDER BY g.id DESC LIMIT @Take |
| | 52 | 598 | | """; |
| | | 599 | | |
| | 52 | 600 | | var command = new CommandDefinition(query, parameters, cancellationToken: ct); |
| | | 601 | | try |
| | | 602 | | { |
| | 52 | 603 | | await using var connection = await GetOpenConnectionAsync(ct); |
| | 52 | 604 | | return (await connection.QueryAsync<ConstructedGist>(command).WithDeadlockRetry(_logger)).ToList(); |
| | 0 | 605 | | } |
| | 0 | 606 | | catch (MySqlException e) |
| | | 607 | | { |
| | 0 | 608 | | _logger?.LogError(GettingPreviousGistsWithFeedFailed, e, "Getting previous gists with feed failed"); |
| | 0 | 609 | | throw; |
| | | 610 | | } |
| | 52 | 611 | | } |
| | | 612 | | |
| | | 613 | | private static string GetLanguageModeConstraint(DynamicParameters parameters, LanguageMode? languageMode) |
| | | 614 | | { |
| | 70 | 615 | | languageMode ??= LanguageMode.Original; |
| | | 616 | | switch (languageMode) |
| | | 617 | | { |
| | | 618 | | case LanguageMode.Original: |
| | 69 | 619 | | return "s.IsTranslated IS FALSE"; |
| | | 620 | | case LanguageMode.En or LanguageMode.De: |
| | 1 | 621 | | var language = languageMode == LanguageMode.De ? Language.De : Language.En; |
| | 1 | 622 | | parameters.Add("Language", language); |
| | 1 | 623 | | return "s.Language = @Language"; |
| | | 624 | | default: |
| | 0 | 625 | | throw new ArgumentOutOfRangeException(nameof(languageMode), languageMode, null); |
| | | 626 | | } |
| | | 627 | | } |
| | | 628 | | |
| | | 629 | | private static void AddLastGistIdConstraint(DynamicParameters parameters, List<string> constraints, int? lastGistId) |
| | | 630 | | { |
| | 52 | 631 | | constraints.Add("g.Id < @LastGistId"); |
| | 52 | 632 | | parameters.Add("LastGistId", lastGistId ?? int.MaxValue); |
| | 52 | 633 | | } |
| | | 634 | | |
| | | 635 | | private static void AddSearchQueryConstraint(DynamicParameters parameters, List<string> constraints, string? searchQ |
| | | 636 | | { |
| | 52 | 637 | | var parsedSearchQuery = ParseSearchQuery(searchQuery); |
| | 114 | 638 | | for (var i = 0; i < parsedSearchQuery.Count; i++) |
| | | 639 | | { |
| | 5 | 640 | | parameters.Add($"SearchQuery{i}", parsedSearchQuery[i]); |
| | 5 | 641 | | constraints.Add($"(LOWER(s.Title) LIKE @SearchQuery{i} OR LOWER(s.SummaryText) LIKE @SearchQuery{i})"); |
| | | 642 | | } |
| | 52 | 643 | | } |
| | | 644 | | |
| | 52 | 645 | | private static List<string> ParseSearchQuery(string? searchQuery) => string.IsNullOrWhiteSpace(searchQuery) |
| | 52 | 646 | | ? [] |
| | 52 | 647 | | : searchQuery |
| | 52 | 648 | | .Split(' ') |
| | 6 | 649 | | .Where(word => !string.IsNullOrWhiteSpace(word)) |
| | 5 | 650 | | .Select(word => word.Trim().ToLowerInvariant()) |
| | 5 | 651 | | .Select(word => $"%{word}%") |
| | 52 | 652 | | .ToList(); |
| | | 653 | | |
| | | 654 | | private static void AddTagsConstraint(DynamicParameters parameters, List<string> constraints, IEnumerable<string> ta |
| | | 655 | | { |
| | 52 | 656 | | var parsedTags = ParseTags(tags); |
| | 114 | 657 | | for (var i = 0; i < parsedTags.Count; i++) |
| | | 658 | | { |
| | 5 | 659 | | parameters.Add($"Tags{i}", parsedTags[i]); |
| | 5 | 660 | | constraints.Add($"g.Tags REGEXP @Tags{i}"); |
| | | 661 | | } |
| | 52 | 662 | | } |
| | | 663 | | |
| | 52 | 664 | | private static List<string> ParseTags(IEnumerable<string> tags) => tags |
| | 5 | 665 | | .Where(tag => !string.IsNullOrWhiteSpace(tag)) |
| | 5 | 666 | | .Select(tag => $@"\b{tag}\b") |
| | 52 | 667 | | .ToList(); |
| | | 668 | | |
| | | 669 | | private static void AddDisabledFeedsConstraint(DynamicParameters parameters, List<string> constraints, |
| | | 670 | | IEnumerable<int> disabledFeeds) |
| | | 671 | | { |
| | 52 | 672 | | parameters.Add("DisabledFeeds", disabledFeeds); |
| | 52 | 673 | | constraints.Add("g.FeedId NOT IN @DisabledFeeds"); |
| | 52 | 674 | | } |
| | | 675 | | |
| | | 676 | | private static void AddSponsoredContentConstraint(List<string> constraints, bool? includeSponsoredContent) |
| | | 677 | | { |
| | 103 | 678 | | if (includeSponsoredContent is not true) constraints.Add("g.IsSponsoredContent IS FALSE"); |
| | 52 | 679 | | } |
| | | 680 | | |
| | | 681 | | public async Task<ConstructedGist?> GetConstructedGistByIdAsync(int id, LanguageMode? languageMode, CancellationToke |
| | | 682 | | { |
| | 5 | 683 | | var parameters = new DynamicParameters(); |
| | 5 | 684 | | var query = $""" |
| | 5 | 685 | | SELECT |
| | 5 | 686 | | g.Id as Id, |
| | 5 | 687 | | g.Reference as Reference, |
| | 5 | 688 | | f.Title as FeedTitle, |
| | 5 | 689 | | f.RssUrl as FeedUrl, |
| | 5 | 690 | | f.Type as FeedType, |
| | 5 | 691 | | s.Title as Title, |
| | 5 | 692 | | g.Author as Author, |
| | 5 | 693 | | g.IsSponsoredContent as IsSponsoredContent, |
| | 5 | 694 | | g.Url as Url, |
| | 5 | 695 | | DATE_FORMAT(g.Published, '%Y-%m-%dT%H:%i:%s.%fZ') as Published, |
| | 5 | 696 | | DATE_FORMAT(g.Updated, '%Y-%m-%dT%H:%i:%s.%fZ') as Updated, |
| | 5 | 697 | | s.SummaryText as Summary, |
| | 5 | 698 | | g.Tags as Tags |
| | 5 | 699 | | FROM Gists g |
| | 5 | 700 | | INNER JOIN Feeds f ON g.FeedId = f.Id |
| | 5 | 701 | | INNER JOIN Summaries s ON s.GistId = g.Id |
| | 5 | 702 | | WHERE g.Id = @Id AND {GetLanguageModeConstraint(parameters, languageMode)} |
| | 5 | 703 | | """; |
| | 5 | 704 | | parameters.Add("Id", id); |
| | 5 | 705 | | var command = new CommandDefinition(query, parameters, cancellationToken: ct); |
| | | 706 | | |
| | | 707 | | try |
| | | 708 | | { |
| | 5 | 709 | | await using var connection = await GetOpenConnectionAsync(ct); |
| | 5 | 710 | | return await connection.QuerySingleOrDefaultAsync<ConstructedGist>(command).WithDeadlockRetry(_logger); |
| | 0 | 711 | | } |
| | 0 | 712 | | catch (MySqlException e) |
| | | 713 | | { |
| | 0 | 714 | | _logger?.LogError(GettingGistByReferenceFailed, e, "Getting gist by ID failed"); |
| | 0 | 715 | | throw; |
| | | 716 | | } |
| | 5 | 717 | | } |
| | | 718 | | |
| | | 719 | | public async Task<List<RssFeedInfo>> GetAllFeedInfosAsync(CancellationToken ct) |
| | | 720 | | { |
| | | 721 | | const string query = "SELECT Title, RssUrl, Language, Type, Id FROM Feeds"; |
| | 4 | 722 | | var command = new CommandDefinition(query, cancellationToken: ct); |
| | | 723 | | |
| | | 724 | | try |
| | | 725 | | { |
| | 4 | 726 | | await using var connection = await GetOpenConnectionAsync(ct); |
| | 4 | 727 | | return (await connection.QueryAsync<RssFeedInfo>(command).WithDeadlockRetry(_logger)).ToList(); |
| | 0 | 728 | | } |
| | 0 | 729 | | catch (MySqlException e) |
| | | 730 | | { |
| | 0 | 731 | | _logger?.LogError(GettingAllFeedInfosFailed, e, "Getting all feed infos failed"); |
| | 0 | 732 | | throw; |
| | | 733 | | } |
| | 4 | 734 | | } |
| | | 735 | | |
| | | 736 | | public async Task<SerializedRecap?> GetLatestRecapAsync(RecapType recapType, CancellationToken ct) |
| | | 737 | | { |
| | 4 | 738 | | var query = $"SELECT Created, RecapEn, RecapDe, Id FROM Recaps{recapType.ToTypeString()} ORDER BY Created DESC L |
| | 4 | 739 | | var command = new CommandDefinition(query, cancellationToken: ct); |
| | | 740 | | |
| | | 741 | | try |
| | | 742 | | { |
| | 4 | 743 | | await using var connection = await GetOpenConnectionAsync(ct); |
| | 4 | 744 | | var serializedRecap = await connection.QuerySingleOrDefaultAsync<SerializedRecap>(command) |
| | 4 | 745 | | .WithDeadlockRetry(_logger); |
| | 6 | 746 | | if (serializedRecap is not null) return serializedRecap; |
| | 2 | 747 | | _logger?.LogInformation(NoRecapFound, "No {RecapType} recap found in database", |
| | 2 | 748 | | recapType.ToTypeString()); |
| | 2 | 749 | | return null; |
| | 0 | 750 | | } |
| | 0 | 751 | | catch (MySqlException e) |
| | | 752 | | { |
| | 0 | 753 | | _logger?.LogError(GettingLatestRecapFailed, e, "Getting latest {RecapType} recap failed", |
| | 0 | 754 | | recapType.ToTypeString()); |
| | 0 | 755 | | throw; |
| | | 756 | | } |
| | 4 | 757 | | } |
| | | 758 | | |
| | | 759 | | public async Task<bool> IsChatRegisteredAsync(long chatId, CancellationToken ct) |
| | | 760 | | { |
| | | 761 | | const string query = "SELECT COUNT(Id) FROM Chats WHERE Id = @ChatId"; |
| | 3 | 762 | | var command = new CommandDefinition(query, new { ChatId = chatId }, cancellationToken: ct); |
| | | 763 | | |
| | | 764 | | try |
| | | 765 | | { |
| | 3 | 766 | | await using var connection = await GetOpenConnectionAsync(ct); |
| | 3 | 767 | | var count = await connection.ExecuteScalarAsync<int>(command).WithDeadlockRetry(_logger); |
| | 3 | 768 | | if (count > 1) |
| | | 769 | | { |
| | 0 | 770 | | throw new DatabaseOperationException($"Found multiple chats with the same ID {chatId} in database"); |
| | | 771 | | } |
| | 3 | 772 | | return count > 0; |
| | 0 | 773 | | } |
| | 0 | 774 | | catch (MySqlException e) |
| | | 775 | | { |
| | 0 | 776 | | _logger?.LogError(ChatRegisterCheckFailed, e, "Checking if chat is registered failed"); |
| | 0 | 777 | | throw; |
| | | 778 | | } |
| | 3 | 779 | | } |
| | | 780 | | |
| | | 781 | | public async Task RegisterChatAsync(long chatId, CancellationToken ct) |
| | | 782 | | { |
| | | 783 | | const string query = "INSERT INTO Chats (Id, GistIdLastSent) VALUES (@ChatId, @GistIdLastSent)"; |
| | 32 | 784 | | var mostRecentGistWithFeed = await GetMostRecentGistWithFeedAsync(ct); |
| | | 785 | | // Default to 0 if no gists are found, so that the first gist will be sent |
| | | 786 | | // otherwise set it to 5 less than the most recent gist ID to send the last 5 gists |
| | 32 | 787 | | var gistIdLastSent = mostRecentGistWithFeed?.Id - 5 ?? 0; |
| | 32 | 788 | | var command = new CommandDefinition(query, new { ChatId = chatId, GistIdLastSent = gistIdLastSent }, |
| | 32 | 789 | | cancellationToken: ct); |
| | | 790 | | |
| | | 791 | | try |
| | | 792 | | { |
| | 32 | 793 | | await using var connection = await GetOpenConnectionAsync(ct); |
| | 32 | 794 | | await connection.ExecuteAsync(command).WithDeadlockRetry(_logger); |
| | 31 | 795 | | _logger?.LogInformation(ChatRegistered, |
| | 31 | 796 | | "Chat with ID {ChatId} registered with GistIdLastSend {GistIdLastSend}", chatId, gistIdLastSent); |
| | 31 | 797 | | } |
| | 1 | 798 | | catch (MySqlException e) |
| | | 799 | | { |
| | 1 | 800 | | _logger?.LogError(RegisteringChatFailed, e, |
| | 1 | 801 | | "Registering chat with ID {ChatId} and GistIdLastSend {GistIdLastSend} failed", chatId, gistIdLastSent); |
| | 1 | 802 | | throw; |
| | | 803 | | } |
| | 31 | 804 | | } |
| | | 805 | | |
| | | 806 | | private async Task<ConstructedGist?> GetMostRecentGistWithFeedAsync(CancellationToken ct) |
| | | 807 | | { |
| | 32 | 808 | | var gistsWithFeed = await GetPreviousConstructedGistsAsync(1, null, [], null, [], null, false, ct); |
| | 34 | 809 | | if (gistsWithFeed.Count != 0) return gistsWithFeed.Single(); |
| | 30 | 810 | | _logger?.LogInformation(NoRecentGistFound, "No recent gist found in database"); |
| | 30 | 811 | | return null; |
| | 32 | 812 | | } |
| | | 813 | | |
| | | 814 | | public async Task DeregisterChatAsync(long chatId, CancellationToken ct) |
| | | 815 | | { |
| | | 816 | | const string query = "DELETE FROM Chats WHERE Id = @ChatId"; |
| | 3 | 817 | | var command = new CommandDefinition(query, new { ChatId = chatId }, cancellationToken: ct); |
| | | 818 | | |
| | | 819 | | try |
| | | 820 | | { |
| | 3 | 821 | | await using var connection = await GetOpenConnectionAsync(ct); |
| | 3 | 822 | | var rowsAffected = await connection.ExecuteAsync(command).WithDeadlockRetry(_logger); |
| | | 823 | | switch (rowsAffected) |
| | | 824 | | { |
| | | 825 | | case 0: |
| | 1 | 826 | | throw new DatabaseOperationException($"No chat with ID {chatId} found to deregister"); |
| | | 827 | | case > 1: |
| | 0 | 828 | | throw new DatabaseOperationException($"Deregistered multiple chats with the same ID {chatId}"); |
| | | 829 | | default: |
| | 2 | 830 | | _logger?.LogInformation(ChatDeregistered, "Chat with ID {ChatId} deregistered", chatId); |
| | | 831 | | break; |
| | | 832 | | } |
| | 2 | 833 | | } |
| | 0 | 834 | | catch (MySqlException e) |
| | | 835 | | { |
| | 0 | 836 | | _logger?.LogError(DeregisteringChatFailed, e, "Deregistering chat with ID {ChatId} failed", chatId); |
| | 0 | 837 | | throw; |
| | | 838 | | } |
| | 2 | 839 | | } |
| | | 840 | | |
| | | 841 | | public async Task<List<Chat>> GetAllChatsAsync(CancellationToken ct) |
| | | 842 | | { |
| | | 843 | | const string query = "SELECT Id, GistIdLastSent FROM Chats"; |
| | 3 | 844 | | var command = new CommandDefinition(query, cancellationToken: ct); |
| | | 845 | | |
| | | 846 | | try |
| | | 847 | | { |
| | 3 | 848 | | await using var connection = await GetOpenConnectionAsync(ct); |
| | 3 | 849 | | return (await connection.QueryAsync<Chat>(command).WithDeadlockRetry(_logger)).ToList(); |
| | 0 | 850 | | } |
| | 0 | 851 | | catch (MySqlException e) |
| | | 852 | | { |
| | 0 | 853 | | _logger?.LogError(GettingAllChatsFailed, e, "Getting all chats failed"); |
| | 0 | 854 | | throw; |
| | | 855 | | } |
| | 3 | 856 | | } |
| | | 857 | | |
| | | 858 | | public async Task<List<ConstructedGist>> GetNextFiveConstructedGistsAsync(int lastGistId, LanguageMode languageMode, |
| | | 859 | | CancellationToken ct) |
| | | 860 | | { |
| | 4 | 861 | | var parameters = new DynamicParameters(); |
| | 4 | 862 | | var query = $""" |
| | 4 | 863 | | SELECT |
| | 4 | 864 | | g.Id as Id, |
| | 4 | 865 | | g.Reference as Reference, |
| | 4 | 866 | | f.Title as FeedTitle, |
| | 4 | 867 | | f.RssUrl as FeedUrl, |
| | 4 | 868 | | f.Type as FeedType, |
| | 4 | 869 | | s.Title as Title, |
| | 4 | 870 | | g.Author as Author, |
| | 4 | 871 | | g.IsSponsoredContent as IsSponsoredContent, |
| | 4 | 872 | | g.Url as Url, |
| | 4 | 873 | | DATE_FORMAT(g.Published, '%Y-%m-%dT%H:%i:%s.%fZ') as Published, |
| | 4 | 874 | | DATE_FORMAT(g.Updated, '%Y-%m-%dT%H:%i:%s.%fZ') as Updated, |
| | 4 | 875 | | s.SummaryText as Summary, |
| | 4 | 876 | | g.Tags as Tags |
| | 4 | 877 | | FROM Gists g |
| | 4 | 878 | | INNER JOIN Feeds f ON g.FeedId = f.Id |
| | 4 | 879 | | INNER JOIN Summaries s ON s.GistId = g.Id |
| | 4 | 880 | | WHERE g.Id > @LastGistId AND g.Disabled IS FALSE AND {GetLanguageModeConstraint(parameters, languageMode)} |
| | 4 | 881 | | AND g.IsSponsoredContent IS FALSE |
| | 4 | 882 | | ORDER BY g.Id ASC LIMIT 5 |
| | 4 | 883 | | """; |
| | 4 | 884 | | parameters.Add("LastGistId", lastGistId); |
| | 4 | 885 | | var command = new CommandDefinition(query, parameters, cancellationToken: ct); |
| | | 886 | | |
| | | 887 | | try |
| | | 888 | | { |
| | 4 | 889 | | await using var connection = await GetOpenConnectionAsync(ct); |
| | 4 | 890 | | return (await connection.QueryAsync<ConstructedGist>(command).WithDeadlockRetry(_logger)).ToList(); |
| | 0 | 891 | | } |
| | 0 | 892 | | catch (MySqlException e) |
| | | 893 | | { |
| | 0 | 894 | | _logger?.LogError(GettingNextFiveGistsWithFeedFailed, e, |
| | 0 | 895 | | "Getting next gists with feed with lastGistId {LastGistId} failed", lastGistId); |
| | 0 | 896 | | throw; |
| | | 897 | | } |
| | 4 | 898 | | } |
| | | 899 | | |
| | | 900 | | public async Task SetGistIdLastSentForChatAsync(long chatId, int gistId, CancellationToken ct) |
| | | 901 | | { |
| | | 902 | | const string query = "UPDATE Chats SET GistIdLastSent = @GistIdLastSent WHERE Id = @ChatId"; |
| | 3 | 903 | | var command = new CommandDefinition(query, new { GistIdLastSent = gistId, ChatId = chatId }, |
| | 3 | 904 | | cancellationToken: ct); |
| | | 905 | | |
| | | 906 | | try |
| | | 907 | | { |
| | 3 | 908 | | await using var connection = await GetOpenConnectionAsync(ct); |
| | 3 | 909 | | var rowsAffected = await connection.ExecuteAsync(command).WithDeadlockRetry(_logger); |
| | 3 | 910 | | if (rowsAffected != 1) |
| | 1 | 911 | | throw new DatabaseOperationException( |
| | 1 | 912 | | $"Did not successfully set GistIdLastSent for Chat {chatId} to {gistId}"); |
| | 2 | 913 | | } |
| | 0 | 914 | | catch (MySqlException e) |
| | | 915 | | { |
| | 0 | 916 | | _logger?.LogError(SettingGistIdLastSentFailed, e, |
| | 0 | 917 | | "Setting GistIdLastSent for chat with ID {ChatId} to {GistId} failed", chatId, gistId); |
| | | 918 | | |
| | 0 | 919 | | throw; |
| | | 920 | | } |
| | 2 | 921 | | } |
| | | 922 | | |
| | | 923 | | private async Task<MySqlConnection> GetOpenConnectionAsync(CancellationToken ct) |
| | | 924 | | { |
| | 907 | 925 | | MySqlConnection? connection = null; |
| | | 926 | | try |
| | | 927 | | { |
| | 907 | 928 | | connection = new MySqlConnection(_connectionString); |
| | 907 | 929 | | await connection.OpenAsync(ct); |
| | 907 | 930 | | return connection; |
| | | 931 | | } |
| | 0 | 932 | | catch (Exception e) |
| | | 933 | | { |
| | 0 | 934 | | _logger?.LogError(DatabaseConnectionFailed, e, "Failed to connect to database"); |
| | 0 | 935 | | if (connection is not null) await connection.DisposeAsync(); |
| | 0 | 936 | | throw; |
| | | 937 | | } |
| | 907 | 938 | | } |
| | | 939 | | } |