From 58229b134865915322f0336d25158a72502d3780 Mon Sep 17 00:00:00 2001 From: James Tombleson Date: Sun, 9 Jul 2023 21:29:33 -0700 Subject: [PATCH] Moving the Subscriptions over to DiscordNotifications as this will be the automated posting compared to user feeds --- .../DiscordNotificationController.cs | 130 +++++++++++++++++ .../Controllers/SubscriptionsController.cs | 133 ------------------ Newsbot.Collector.Api/Program.cs | 2 +- Newsbot.Collector.Database/DatabaseContext.cs | 3 +- ...nsTable.cs => DiscordNotificationTable.cs} | 38 ++--- ...to.cs => DiscordNotificationDetailsDto.cs} | 12 +- ...iptionDto.cs => DiscordNotificationDto.cs} | 6 +- ...Entity.cs => DiscordNotificationEntity.cs} | 2 +- .../IDiscordNotificationRepository.cs | 18 +++ .../Interfaces/ISubscriptionsRepository.cs | 18 --- .../Jobs/DiscordNotificationJob.cs | 17 ++- .../Jobs/DiscordNotificationJobTest.cs | 4 +- .../Tables/DiscordQueueTableTests.cs | 16 +++ 13 files changed, 208 insertions(+), 191 deletions(-) create mode 100644 Newsbot.Collector.Api/Controllers/DiscordNotificationController.cs delete mode 100644 Newsbot.Collector.Api/Controllers/SubscriptionsController.cs rename Newsbot.Collector.Database/Repositories/{SubscriptionsTable.cs => DiscordNotificationTable.cs} (56%) rename Newsbot.Collector.Domain/Dto/{SubscriptionDetailsDto.cs => DiscordNotificationDetailsDto.cs} (57%) rename Newsbot.Collector.Domain/Dto/{SubscriptionDto.cs => DiscordNotificationDto.cs} (79%) rename Newsbot.Collector.Domain/Entities/{SubscriptionEntity.cs => DiscordNotificationEntity.cs} (87%) create mode 100644 Newsbot.Collector.Domain/Interfaces/IDiscordNotificationRepository.cs delete mode 100644 Newsbot.Collector.Domain/Interfaces/ISubscriptionsRepository.cs create mode 100644 Newsbot.Collector.Tests/Tables/DiscordQueueTableTests.cs diff --git a/Newsbot.Collector.Api/Controllers/DiscordNotificationController.cs b/Newsbot.Collector.Api/Controllers/DiscordNotificationController.cs new file mode 100644 index 0000000..c2c8e55 --- /dev/null +++ b/Newsbot.Collector.Api/Controllers/DiscordNotificationController.cs @@ -0,0 +1,130 @@ +using Microsoft.AspNetCore.Authentication.JwtBearer; +using Microsoft.AspNetCore.Authorization; +using Microsoft.AspNetCore.Mvc; +using Newsbot.Collector.Domain.Dto; +using Newsbot.Collector.Domain.Entities; +using Newsbot.Collector.Domain.Interfaces; + +namespace Newsbot.Collector.Api.Controllers; + +[ApiController] +[Route("api/subscriptions")] +[Authorize(AuthenticationSchemes = JwtBearerDefaults.AuthenticationScheme)] +public class DiscordNotificationController : ControllerBase +{ + private readonly ILogger _logger; + private readonly IDiscordWebHooksRepository _discord; + private readonly ISourcesRepository _sources; + private readonly IDiscordNotificationRepository _discordNotification; + + public DiscordNotificationController(ILogger logger, IDiscordWebHooksRepository discord, ISourcesRepository sources, IDiscordNotificationRepository discordNotification) + { + _logger = logger; + _discord = discord; + _sources = sources; + _discordNotification = discordNotification; + } + + [HttpGet(Name = "ListSubscriptions")] + public IEnumerable List(int page) + { + var res = new List(); + var items = _discordNotification.List(page); + foreach (var item in items) res.Add(DiscordNotificationDto.Convert(item)); + return res; + } + + [HttpGet("{id}")] + public DiscordNotificationDto GetById(Guid id) + { + return DiscordNotificationDto.Convert(_discordNotification.GetById(id)); + } + + [HttpGet("{id}/details")] + public DiscordNotificationDetailsDto GetDetailsById(Guid id) + { + var sub = _discordNotification.GetById(id); + var webhook = _discord.GetById(sub.DiscordWebHookId); + var source = _sources.GetById(sub.SourceId); + + return DiscordNotificationDetailsDto.Convert(sub, source, webhook); + } + + [HttpPost("{id}/delete")] + public void DeleteById(Guid id) + { + _discordNotification.Delete(id); + } + + [HttpGet("by/discordId/{id}")] + public IEnumerable GetByDiscordId(Guid id) + { + var res = new List(); + var items = _discordNotification.ListByWebhook(id); + foreach (var item in items) res.Add(DiscordNotificationDto.Convert(item)); + return res; + } + + [HttpGet("by/sourceId/{id}")] + public IEnumerable GetBySourceId(Guid id) + { + var res = new List(); + var items = _discordNotification.ListBySourceId(id); + foreach (var item in items) res.Add(DiscordNotificationDto.Convert(item)); + return res; + } + + [HttpPost(Name = "New Subscription")] + public ActionResult New(Guid sourceId, Guid discordId) + { + if (sourceId == Guid.Empty) return new BadRequestResult(); + if (discordId == Guid.Empty) return new BadRequestResult(); + + var exists = _discordNotification.GetByWebhookAndSource(discordId, sourceId); + if (exists.Id != Guid.Empty) return DiscordNotificationDto.Convert(exists); + + var discord = _discord.GetById(discordId); + if (discord.Id == Guid.Empty) return new BadRequestResult(); + + var source = _sources.GetById(sourceId); + if (source.Id == Guid.Empty) return new BadRequestResult(); + + var item = _discordNotification.New(new DiscordNotificationEntity + { + Id = Guid.NewGuid(), + SourceId = sourceId, + DiscordWebHookId = discordId, + CodeAllowCommits = false, + CodeAllowReleases = false + }); + + return DiscordNotificationDto.Convert(item); + } + + [HttpPost("new/codeproject")] + public ActionResult NewCodeProjectSubscription(Guid sourceId, Guid discordId, bool allowReleases, + bool allowCommits) + { + if (sourceId == Guid.Empty) return new BadRequestResult(); + if (discordId == Guid.Empty) return new BadRequestResult(); + + var exists = _discordNotification.GetByWebhookAndSource(discordId, sourceId); + if (exists.Id != Guid.Empty) return DiscordNotificationDto.Convert(exists); + + var discord = _discord.GetById(discordId); + if (discord.Id == Guid.Empty) return new BadRequestResult(); + + var source = _sources.GetById(sourceId); + if (source.Id == Guid.Empty) return new BadRequestResult(); + + var sub = _discordNotification.New(new DiscordNotificationEntity + { + DiscordWebHookId = discordId, + SourceId = sourceId, + CodeAllowCommits = allowCommits, + CodeAllowReleases = allowReleases + }); + + return new DiscordNotificationDto(); + } +} \ No newline at end of file diff --git a/Newsbot.Collector.Api/Controllers/SubscriptionsController.cs b/Newsbot.Collector.Api/Controllers/SubscriptionsController.cs deleted file mode 100644 index e8dafdd..0000000 --- a/Newsbot.Collector.Api/Controllers/SubscriptionsController.cs +++ /dev/null @@ -1,133 +0,0 @@ -using Microsoft.AspNetCore.Authentication.JwtBearer; -using Microsoft.AspNetCore.Authorization; -using Microsoft.AspNetCore.Mvc; -using Microsoft.Extensions.Options; -using Newsbot.Collector.Database.Repositories; -using Newsbot.Collector.Domain.Dto; -using Newsbot.Collector.Domain.Entities; -using Newsbot.Collector.Domain.Interfaces; -using Newsbot.Collector.Domain.Models; - -namespace Newsbot.Collector.Api.Controllers; - -[ApiController] -[Route("api/subscriptions")] -[Authorize(AuthenticationSchemes = JwtBearerDefaults.AuthenticationScheme)] -public class SubscriptionsController : ControllerBase -{ - private readonly ILogger _logger; - private readonly IDiscordWebHooksRepository _discord; - private readonly ISourcesRepository _sources; - private readonly ISubscriptionRepository _subscription; - - public SubscriptionsController(ILogger logger, IDiscordWebHooksRepository discord, ISourcesRepository sources, ISubscriptionRepository subscription) - { - _logger = logger; - _discord = discord; - _sources = sources; - _subscription = subscription; - } - - [HttpGet(Name = "ListSubscriptions")] - public IEnumerable List(int page) - { - var res = new List(); - var items = _subscription.List(page); - foreach (var item in items) res.Add(SubscriptionDto.Convert(item)); - return res; - } - - [HttpGet("{id}")] - public SubscriptionDto GetById(Guid id) - { - return SubscriptionDto.Convert(_subscription.GetById(id)); - } - - [HttpGet("{id}/details")] - public SubscriptionDetailsDto GetDetailsById(Guid id) - { - var sub = _subscription.GetById(id); - var webhook = _discord.GetById(sub.DiscordWebHookId); - var source = _sources.GetById(sub.SourceId); - - return SubscriptionDetailsDto.Convert(sub, source, webhook); - } - - [HttpPost("{id}/delete")] - public void DeleteById(Guid id) - { - _subscription.Delete(id); - } - - [HttpGet("by/discordid/{id}")] - public IEnumerable GetByDiscordId(Guid id) - { - var res = new List(); - var items = _subscription.ListByWebhook(id); - foreach (var item in items) res.Add(SubscriptionDto.Convert(item)); - return res; - } - - [HttpGet("by/sourceId/{id}")] - public IEnumerable GetBySourceId(Guid id) - { - var res = new List(); - var items = _subscription.ListBySourceId(id); - foreach (var item in items) res.Add(SubscriptionDto.Convert(item)); - return res; - } - - [HttpPost(Name = "New Subscription")] - public ActionResult New(Guid sourceId, Guid discordId) - { - if (sourceId == Guid.Empty) return new BadRequestResult(); - if (discordId == Guid.Empty) return new BadRequestResult(); - - var exists = _subscription.GetByWebhookAndSource(discordId, sourceId); - if (exists.Id != Guid.Empty) return SubscriptionDto.Convert(exists); - - var discord = _discord.GetById(discordId); - if (discord.Id == Guid.Empty) return new BadRequestResult(); - - var source = _sources.GetById(sourceId); - if (source.Id == Guid.Empty) return new BadRequestResult(); - - var item = _subscription.New(new SubscriptionEntity - { - Id = Guid.NewGuid(), - SourceId = sourceId, - DiscordWebHookId = discordId, - CodeAllowCommits = false, - CodeAllowReleases = false - }); - - return SubscriptionDto.Convert(item); - } - - [HttpPost("new/codeproject")] - public ActionResult NewCodeProjectSubscription(Guid sourceId, Guid discordId, bool allowReleases, - bool allowCommits) - { - if (sourceId == Guid.Empty) return new BadRequestResult(); - if (discordId == Guid.Empty) return new BadRequestResult(); - - var exists = _subscription.GetByWebhookAndSource(discordId, sourceId); - if (exists.Id != Guid.Empty) return SubscriptionDto.Convert(exists); - - var discord = _discord.GetById(discordId); - if (discord.Id == Guid.Empty) return new BadRequestResult(); - - var source = _sources.GetById(sourceId); - if (source.Id == Guid.Empty) return new BadRequestResult(); - - var sub = _subscription.New(new SubscriptionEntity - { - DiscordWebHookId = discordId, - SourceId = sourceId, - CodeAllowCommits = allowCommits, - CodeAllowReleases = allowReleases - }); - - return new SubscriptionDto(); - } -} \ No newline at end of file diff --git a/Newsbot.Collector.Api/Program.cs b/Newsbot.Collector.Api/Program.cs index ee12181..b6d9ec4 100644 --- a/Newsbot.Collector.Api/Program.cs +++ b/Newsbot.Collector.Api/Program.cs @@ -48,7 +48,7 @@ builder.Services.AddScoped(); builder.Services.AddScoped(); builder.Services.AddScoped(); builder.Services.AddScoped(); -builder.Services.AddScoped(); +builder.Services.AddScoped(); // Configure Identity builder.Services.AddScoped(); diff --git a/Newsbot.Collector.Database/DatabaseContext.cs b/Newsbot.Collector.Database/DatabaseContext.cs index c4d8710..6cb50cc 100644 --- a/Newsbot.Collector.Database/DatabaseContext.cs +++ b/Newsbot.Collector.Database/DatabaseContext.cs @@ -10,11 +10,12 @@ namespace Newsbot.Collector.Database; public class DatabaseContext : IdentityDbContext { public DbSet Articles { get; set; } = null!; + + public DbSet DiscordNotification { get; set; } = null!; public DbSet DiscordQueue { get; set; } = null!; public DbSet DiscordWebhooks { get; set; } = null!; public DbSet Icons { get; set; } = null!; public DbSet Sources { get; set; } = null!; - public DbSet Subscriptions { get; set; } = null!; //public DbSet Users { get; set; } = null!; diff --git a/Newsbot.Collector.Database/Repositories/SubscriptionsTable.cs b/Newsbot.Collector.Database/Repositories/DiscordNotificationTable.cs similarity index 56% rename from Newsbot.Collector.Database/Repositories/SubscriptionsTable.cs rename to Newsbot.Collector.Database/Repositories/DiscordNotificationTable.cs index 871a6d5..9132d8b 100644 --- a/Newsbot.Collector.Database/Repositories/SubscriptionsTable.cs +++ b/Newsbot.Collector.Database/Repositories/DiscordNotificationTable.cs @@ -8,78 +8,78 @@ using Npgsql; namespace Newsbot.Collector.Database.Repositories; -public class SubscriptionsTable : ISubscriptionRepository +public class DiscordNotificationTable : IDiscordNotificationRepository { //private readonly string _connectionString; private DatabaseContext _context; - public SubscriptionsTable(string connectionString) + public DiscordNotificationTable(string connectionString) { //_connectionString = connectionString; _context = new DatabaseContext(connectionString); } - public SubscriptionsTable(DatabaseContext context) + public DiscordNotificationTable(DatabaseContext context) { _context = context; } - public SubscriptionEntity New(SubscriptionEntity model) + public DiscordNotificationEntity New(DiscordNotificationEntity model) { model.Id = new Guid(); //using var context = new DatabaseContext(_connectionString); - _context.Subscriptions.Add(model); + _context.DiscordNotification.Add(model); _context.SaveChanges(); return model; } - public List List(int page = 0, int count = 25) + public List List(int page = 0, int count = 25) { //using var context = new DatabaseContext(_connectionString); - return _context.Subscriptions.Skip(page * count).Take(count).ToList(); + return _context.DiscordNotification.Skip(page * count).Take(count).ToList(); } - public List ListBySourceId(Guid id, int page = 0, int count = 25) + public List ListBySourceId(Guid id, int page = 0, int count = 25) { //using var context = new DatabaseContext(_connectionString); - return _context.Subscriptions.Where(f => f.SourceId.Equals(id)) + return _context.DiscordNotification.Where(f => f.SourceId.Equals(id)) .Skip(page * count) .ToList(); } - public List ListByWebhook(Guid id, int page = 0, int count = 25) + public List ListByWebhook(Guid id, int page = 0, int count = 25) { //using var context = new DatabaseContext(_connectionString); - return _context.Subscriptions.Where(f => f.DiscordWebHookId.Equals(id)).Skip(page * count).ToList(); + return _context.DiscordNotification.Where(f => f.DiscordWebHookId.Equals(id)).Skip(page * count).ToList(); } - public SubscriptionEntity GetById(Guid id) + public DiscordNotificationEntity GetById(Guid id) { //using var context = new DatabaseContext(_connectionString); - var res = _context.Subscriptions + var res = _context.DiscordNotification .FirstOrDefault(f => f.Id.Equals(id)); - return res ??= new SubscriptionEntity(); + return res ??= new DiscordNotificationEntity(); } - public SubscriptionEntity GetByWebhookAndSource(Guid webhookId, Guid sourceId) + public DiscordNotificationEntity GetByWebhookAndSource(Guid webhookId, Guid sourceId) { //using var context = new DatabaseContext(_connectionString); - var res = _context.Subscriptions + var res = _context.DiscordNotification .Where(f => f.DiscordWebHookId.Equals(webhookId)) .FirstOrDefault(f => f.SourceId.Equals(sourceId)); - return res ??= new SubscriptionEntity(); + return res ??= new DiscordNotificationEntity(); } public void Delete(Guid id) { //using var context = new DatabaseContext(_connectionString); - var res = _context.Subscriptions.FirstOrDefault(f => f.Id.Equals(id)); + var res = _context.DiscordNotification.FirstOrDefault(f => f.Id.Equals(id)); if (res is null) { return; } - _context.Subscriptions.Remove(res); + _context.DiscordNotification.Remove(res); _context.SaveChanges(); } diff --git a/Newsbot.Collector.Domain/Dto/SubscriptionDetailsDto.cs b/Newsbot.Collector.Domain/Dto/DiscordNotificationDetailsDto.cs similarity index 57% rename from Newsbot.Collector.Domain/Dto/SubscriptionDetailsDto.cs rename to Newsbot.Collector.Domain/Dto/DiscordNotificationDetailsDto.cs index 90ae0af..1eaab30 100644 --- a/Newsbot.Collector.Domain/Dto/SubscriptionDetailsDto.cs +++ b/Newsbot.Collector.Domain/Dto/DiscordNotificationDetailsDto.cs @@ -3,7 +3,7 @@ using Newsbot.Collector.Domain.Models; namespace Newsbot.Collector.Domain.Dto; -public class SubscriptionDetailsDto +public class DiscordNotificationDetailsDto { public Guid Id { get; set; } public bool CodeAllowReleases { get; set; } @@ -11,14 +11,14 @@ public class SubscriptionDetailsDto public SourceDto? Source { get; set; } public DiscordWebHookDto? DiscordWebHook { get; set; } - public static SubscriptionDetailsDto Convert(SubscriptionEntity subscription, SourceEntity source, + public static DiscordNotificationDetailsDto Convert(DiscordNotificationEntity discordNotification, SourceEntity source, DiscordWebhookEntity discord) { - return new SubscriptionDetailsDto + return new DiscordNotificationDetailsDto { - Id = subscription.Id, - CodeAllowCommits = subscription.CodeAllowCommits, - CodeAllowReleases = subscription.CodeAllowReleases, + Id = discordNotification.Id, + CodeAllowCommits = discordNotification.CodeAllowCommits, + CodeAllowReleases = discordNotification.CodeAllowReleases, Source = SourceDto.Convert(source), DiscordWebHook = DiscordWebHookDto.Convert(discord) }; diff --git a/Newsbot.Collector.Domain/Dto/SubscriptionDto.cs b/Newsbot.Collector.Domain/Dto/DiscordNotificationDto.cs similarity index 79% rename from Newsbot.Collector.Domain/Dto/SubscriptionDto.cs rename to Newsbot.Collector.Domain/Dto/DiscordNotificationDto.cs index 56b7efb..634ac39 100644 --- a/Newsbot.Collector.Domain/Dto/SubscriptionDto.cs +++ b/Newsbot.Collector.Domain/Dto/DiscordNotificationDto.cs @@ -3,7 +3,7 @@ using Newsbot.Collector.Domain.Models; namespace Newsbot.Collector.Domain.Dto; -public class SubscriptionDto +public class DiscordNotificationDto { public Guid Id { get; set; } public Guid SourceId { get; set; } @@ -11,9 +11,9 @@ public class SubscriptionDto public bool CodeAllowReleases { get; set; } public bool CodeAllowCommits { get; set; } - public static SubscriptionDto Convert(SubscriptionEntity model) + public static DiscordNotificationDto Convert(DiscordNotificationEntity model) { - return new SubscriptionDto + return new DiscordNotificationDto { Id = model.Id, SourceId = model.SourceId, diff --git a/Newsbot.Collector.Domain/Entities/SubscriptionEntity.cs b/Newsbot.Collector.Domain/Entities/DiscordNotificationEntity.cs similarity index 87% rename from Newsbot.Collector.Domain/Entities/SubscriptionEntity.cs rename to Newsbot.Collector.Domain/Entities/DiscordNotificationEntity.cs index ae405ed..efe4105 100644 --- a/Newsbot.Collector.Domain/Entities/SubscriptionEntity.cs +++ b/Newsbot.Collector.Domain/Entities/DiscordNotificationEntity.cs @@ -1,6 +1,6 @@ namespace Newsbot.Collector.Domain.Entities; -public class SubscriptionEntity +public class DiscordNotificationEntity { public Guid Id { get; set; } public bool CodeAllowReleases { get; set; } diff --git a/Newsbot.Collector.Domain/Interfaces/IDiscordNotificationRepository.cs b/Newsbot.Collector.Domain/Interfaces/IDiscordNotificationRepository.cs new file mode 100644 index 0000000..c71e137 --- /dev/null +++ b/Newsbot.Collector.Domain/Interfaces/IDiscordNotificationRepository.cs @@ -0,0 +1,18 @@ +using Newsbot.Collector.Domain.Entities; +using Newsbot.Collector.Domain.Models; + +namespace Newsbot.Collector.Domain.Interfaces; + +public interface IDiscordNotificationRepository +{ + DiscordNotificationEntity New(DiscordNotificationEntity model); + + List List(int page = 0, int count = 25); + List ListBySourceId(Guid id, int page = 0, int count = 25); + List ListByWebhook(Guid id, int page = 0, int count = 25); + + DiscordNotificationEntity GetById(Guid id); + DiscordNotificationEntity GetByWebhookAndSource(Guid webhookId, Guid sourceId); + + void Delete(Guid id); +} \ No newline at end of file diff --git a/Newsbot.Collector.Domain/Interfaces/ISubscriptionsRepository.cs b/Newsbot.Collector.Domain/Interfaces/ISubscriptionsRepository.cs deleted file mode 100644 index c955bbf..0000000 --- a/Newsbot.Collector.Domain/Interfaces/ISubscriptionsRepository.cs +++ /dev/null @@ -1,18 +0,0 @@ -using Newsbot.Collector.Domain.Entities; -using Newsbot.Collector.Domain.Models; - -namespace Newsbot.Collector.Domain.Interfaces; - -public interface ISubscriptionRepository -{ - SubscriptionEntity New(SubscriptionEntity model); - - List List(int page = 0, int count = 25); - List ListBySourceId(Guid id, int page = 0, int count = 25); - List ListByWebhook(Guid id, int page = 0, int count = 25); - - SubscriptionEntity GetById(Guid id); - SubscriptionEntity GetByWebhookAndSource(Guid webhookId, Guid sourceId); - - void Delete(Guid id); -} \ No newline at end of file diff --git a/Newsbot.Collector.Services/Jobs/DiscordNotificationJob.cs b/Newsbot.Collector.Services/Jobs/DiscordNotificationJob.cs index 6ae432e..9544c63 100644 --- a/Newsbot.Collector.Services/Jobs/DiscordNotificationJob.cs +++ b/Newsbot.Collector.Services/Jobs/DiscordNotificationJob.cs @@ -1,3 +1,4 @@ +using Newsbot.Collector.Database; using Newsbot.Collector.Database.Repositories; using Newsbot.Collector.Domain.Entities; using Newsbot.Collector.Domain.Interfaces; @@ -32,13 +33,13 @@ public class DiscordNotificationJobOptions public class DiscordNotificationJob { private const string JobName = "DiscordNotifications"; + private ILogger _logger; + //private DatabaseContext _databaseContext; private IArticlesRepository _article; private IIconsRepository _icons; - private ILogger _logger; - private IDiscordQueueRepository _queue; private ISourcesRepository _sources; - private ISubscriptionRepository _subs; + private IDiscordNotificationRepository _subs; private IDiscordWebHooksRepository _webhook; public DiscordNotificationJob() @@ -47,18 +48,19 @@ public class DiscordNotificationJob _article = new ArticlesTable(""); _webhook = new DiscordWebhooksTable(""); _sources = new SourcesTable(""); - _subs = new SubscriptionsTable(""); + _subs = new DiscordNotificationTable(""); _icons = new IconsTable(""); _logger = JobLogger.GetLogger("", JobName); } public void InitAndExecute(DiscordNotificationJobOptions options) { + //_databaseContext = new DatabaseContext(options.ConnectionString ?? ""); _queue = new DiscordQueueTable(options.ConnectionString ?? ""); _article = new ArticlesTable(options.ConnectionString ?? ""); _webhook = new DiscordWebhooksTable(options.ConnectionString ?? ""); _sources = new SourcesTable(options.ConnectionString ?? ""); - _subs = new SubscriptionsTable(options.ConnectionString ?? ""); + _subs = new DiscordNotificationTable(options.ConnectionString ?? ""); _icons = new IconsTable(options.ConnectionString ?? ""); _logger = JobLogger.GetLogger(options.OpenTelemetry ?? "", JobName); @@ -76,6 +78,7 @@ public class DiscordNotificationJob private void Execute() { //collect all the new requests + var requests = _queue.List(100); _logger.Debug($"{JobName} - Collected {requests.Count} items to send"); @@ -121,7 +124,7 @@ public class DiscordNotificationJob _queue.Delete(request.Id); } - public void SendSubscriptionNotification(Guid requestId, ArticlesEntity articleDetails, SourceEntity sourceDetails, IconEntity sourceIcon, SubscriptionEntity sub) + public void SendSubscriptionNotification(Guid requestId, ArticlesEntity articleDetails, SourceEntity sourceDetails, IconEntity sourceIcon, DiscordNotificationEntity sub) { // Check if the subscription code flags // If the article is a code commit and the subscription does not want them, skip. @@ -134,7 +137,7 @@ public class DiscordNotificationJob var discordDetails = _webhook.GetById(sub.DiscordWebHookId); if (discordDetails.Enabled == false) return; - var client = new DiscordWebhookClient(discordDetails.Url); + var client = new DiscordClient(discordDetails.Url); try { client.SendMessage(GenerateDiscordMessage(sourceDetails, articleDetails, sourceIcon)); diff --git a/Newsbot.Collector.Tests/Jobs/DiscordNotificationJobTest.cs b/Newsbot.Collector.Tests/Jobs/DiscordNotificationJobTest.cs index a3b3c71..ed50aae 100644 --- a/Newsbot.Collector.Tests/Jobs/DiscordNotificationJobTest.cs +++ b/Newsbot.Collector.Tests/Jobs/DiscordNotificationJobTest.cs @@ -10,7 +10,7 @@ public class DiscordNotificationJobTest public void PostTestMessage() { var uri = ""; - var webhookClient = new DiscordWebhookClient(uri); + var webhookClient = new DiscordClient(uri); var client = new DiscordNotificationJob(); var msg = client.GenerateDiscordMessage(new SourceEntity @@ -78,7 +78,7 @@ public class DiscordNotificationJobTest Id = Guid.NewGuid(), FileName = "https://www.redditstatic.com/desktop2x/img/favicon/android-icon-192x192.png" }, - new SubscriptionEntity + new DiscordNotificationEntity { CodeAllowCommits = false, CodeAllowReleases = true diff --git a/Newsbot.Collector.Tests/Tables/DiscordQueueTableTests.cs b/Newsbot.Collector.Tests/Tables/DiscordQueueTableTests.cs new file mode 100644 index 0000000..5629d51 --- /dev/null +++ b/Newsbot.Collector.Tests/Tables/DiscordQueueTableTests.cs @@ -0,0 +1,16 @@ +using Microsoft.Extensions.Configuration; +using Newsbot.Collector.Database.Repositories; +using Newsbot.Collector.Domain.Consts; + +namespace Newsbot.Collector.Tests.Tables; + +public class DiscordQueueTableTests +{ + [Fact] + public void TableListsData() + { + var str = TestHelper.LoadConfig().GetConnectionString("Database") ?? ""; + var context = new DiscordQueueTable(str); + var results = context.List(); + } +} \ No newline at end of file