Compare commits

..

No commits in common. "597470fd6996d2e7ca8bd2c827447b59a8270111" and "7b1407c2cbb3c4587d43e1b36aa709f09523ba5b" have entirely different histories.

15 changed files with 195 additions and 212 deletions

View File

@ -1,130 +0,0 @@
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<DiscordNotificationController> _logger;
private readonly IDiscordWebHooksRepository _discord;
private readonly ISourcesRepository _sources;
private readonly IDiscordNotificationRepository _discordNotification;
public DiscordNotificationController(ILogger<DiscordNotificationController> logger, IDiscordWebHooksRepository discord, ISourcesRepository sources, IDiscordNotificationRepository discordNotification)
{
_logger = logger;
_discord = discord;
_sources = sources;
_discordNotification = discordNotification;
}
[HttpGet(Name = "ListSubscriptions")]
public IEnumerable<DiscordNotificationDto> List(int page)
{
var res = new List<DiscordNotificationDto>();
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<DiscordNotificationDto> GetByDiscordId(Guid id)
{
var res = new List<DiscordNotificationDto>();
var items = _discordNotification.ListByWebhook(id);
foreach (var item in items) res.Add(DiscordNotificationDto.Convert(item));
return res;
}
[HttpGet("by/sourceId/{id}")]
public IEnumerable<DiscordNotificationDto> GetBySourceId(Guid id)
{
var res = new List<DiscordNotificationDto>();
var items = _discordNotification.ListBySourceId(id);
foreach (var item in items) res.Add(DiscordNotificationDto.Convert(item));
return res;
}
[HttpPost(Name = "New Subscription")]
public ActionResult<DiscordNotificationDto> 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<DiscordNotificationDto> 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();
}
}

View File

@ -0,0 +1,133 @@
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<SubscriptionsController> _logger;
private readonly IDiscordWebHooksRepository _discord;
private readonly ISourcesRepository _sources;
private readonly ISubscriptionRepository _subscription;
public SubscriptionsController(ILogger<SubscriptionsController> logger, IDiscordWebHooksRepository discord, ISourcesRepository sources, ISubscriptionRepository subscription)
{
_logger = logger;
_discord = discord;
_sources = sources;
_subscription = subscription;
}
[HttpGet(Name = "ListSubscriptions")]
public IEnumerable<SubscriptionDto> List(int page)
{
var res = new List<SubscriptionDto>();
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<SubscriptionDto> GetByDiscordId(Guid id)
{
var res = new List<SubscriptionDto>();
var items = _subscription.ListByWebhook(id);
foreach (var item in items) res.Add(SubscriptionDto.Convert(item));
return res;
}
[HttpGet("by/sourceId/{id}")]
public IEnumerable<SubscriptionDto> GetBySourceId(Guid id)
{
var res = new List<SubscriptionDto>();
var items = _subscription.ListBySourceId(id);
foreach (var item in items) res.Add(SubscriptionDto.Convert(item));
return res;
}
[HttpPost(Name = "New Subscription")]
public ActionResult<SubscriptionDto> 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<SubscriptionDto> 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();
}
}

View File

@ -48,7 +48,7 @@ builder.Services.AddScoped<IDiscordQueueRepository, DiscordQueueTable>();
builder.Services.AddScoped<IDiscordWebHooksRepository, DiscordWebhooksTable>();
builder.Services.AddScoped<IIconsRepository, IconsTable>();
builder.Services.AddScoped<ISourcesRepository, SourcesTable>();
builder.Services.AddScoped<IDiscordNotificationRepository, DiscordNotificationTable>();
builder.Services.AddScoped<ISubscriptionRepository, SubscriptionsTable>();
// Configure Identity
builder.Services.AddScoped<IIdentityService, IdentityService>();

View File

@ -10,12 +10,11 @@ namespace Newsbot.Collector.Database;
public class DatabaseContext : IdentityDbContext
{
public DbSet<ArticlesEntity> Articles { get; set; } = null!;
public DbSet<DiscordNotificationEntity> DiscordNotification { get; set; } = null!;
public DbSet<DiscordQueueEntity> DiscordQueue { get; set; } = null!;
public DbSet<DiscordWebhookEntity> DiscordWebhooks { get; set; } = null!;
public DbSet<IconEntity> Icons { get; set; } = null!;
public DbSet<SourceEntity> Sources { get; set; } = null!;
public DbSet<SubscriptionEntity> Subscriptions { get; set; } = null!;
//public DbSet<UserEntity> Users { get; set; } = null!;

View File

@ -8,78 +8,78 @@ using Npgsql;
namespace Newsbot.Collector.Database.Repositories;
public class DiscordNotificationTable : IDiscordNotificationRepository
public class SubscriptionsTable : ISubscriptionRepository
{
//private readonly string _connectionString;
private DatabaseContext _context;
public DiscordNotificationTable(string connectionString)
public SubscriptionsTable(string connectionString)
{
//_connectionString = connectionString;
_context = new DatabaseContext(connectionString);
}
public DiscordNotificationTable(DatabaseContext context)
public SubscriptionsTable(DatabaseContext context)
{
_context = context;
}
public DiscordNotificationEntity New(DiscordNotificationEntity model)
public SubscriptionEntity New(SubscriptionEntity model)
{
model.Id = new Guid();
//using var context = new DatabaseContext(_connectionString);
_context.DiscordNotification.Add(model);
_context.Subscriptions.Add(model);
_context.SaveChanges();
return model;
}
public List<DiscordNotificationEntity> List(int page = 0, int count = 25)
public List<SubscriptionEntity> List(int page = 0, int count = 25)
{
//using var context = new DatabaseContext(_connectionString);
return _context.DiscordNotification.Skip(page * count).Take(count).ToList();
return _context.Subscriptions.Skip(page * count).Take(count).ToList();
}
public List<DiscordNotificationEntity> ListBySourceId(Guid id, int page = 0, int count = 25)
public List<SubscriptionEntity> ListBySourceId(Guid id, int page = 0, int count = 25)
{
//using var context = new DatabaseContext(_connectionString);
return _context.DiscordNotification.Where(f => f.SourceId.Equals(id))
return _context.Subscriptions.Where(f => f.SourceId.Equals(id))
.Skip(page * count)
.ToList();
}
public List<DiscordNotificationEntity> ListByWebhook(Guid id, int page = 0, int count = 25)
public List<SubscriptionEntity> ListByWebhook(Guid id, int page = 0, int count = 25)
{
//using var context = new DatabaseContext(_connectionString);
return _context.DiscordNotification.Where(f => f.DiscordWebHookId.Equals(id)).Skip(page * count).ToList();
return _context.Subscriptions.Where(f => f.DiscordWebHookId.Equals(id)).Skip(page * count).ToList();
}
public DiscordNotificationEntity GetById(Guid id)
public SubscriptionEntity GetById(Guid id)
{
//using var context = new DatabaseContext(_connectionString);
var res = _context.DiscordNotification
var res = _context.Subscriptions
.FirstOrDefault(f => f.Id.Equals(id));
return res ??= new DiscordNotificationEntity();
return res ??= new SubscriptionEntity();
}
public DiscordNotificationEntity GetByWebhookAndSource(Guid webhookId, Guid sourceId)
public SubscriptionEntity GetByWebhookAndSource(Guid webhookId, Guid sourceId)
{
//using var context = new DatabaseContext(_connectionString);
var res = _context.DiscordNotification
var res = _context.Subscriptions
.Where(f => f.DiscordWebHookId.Equals(webhookId))
.FirstOrDefault(f => f.SourceId.Equals(sourceId));
return res ??= new DiscordNotificationEntity();
return res ??= new SubscriptionEntity();
}
public void Delete(Guid id)
{
//using var context = new DatabaseContext(_connectionString);
var res = _context.DiscordNotification.FirstOrDefault(f => f.Id.Equals(id));
var res = _context.Subscriptions.FirstOrDefault(f => f.Id.Equals(id));
if (res is null)
{
return;
}
_context.DiscordNotification.Remove(res);
_context.Subscriptions.Remove(res);
_context.SaveChanges();
}

View File

@ -3,7 +3,7 @@ using Newsbot.Collector.Domain.Models;
namespace Newsbot.Collector.Domain.Dto;
public class DiscordNotificationDetailsDto
public class SubscriptionDetailsDto
{
public Guid Id { get; set; }
public bool CodeAllowReleases { get; set; }
@ -11,14 +11,14 @@ public class DiscordNotificationDetailsDto
public SourceDto? Source { get; set; }
public DiscordWebHookDto? DiscordWebHook { get; set; }
public static DiscordNotificationDetailsDto Convert(DiscordNotificationEntity discordNotification, SourceEntity source,
public static SubscriptionDetailsDto Convert(SubscriptionEntity subscription, SourceEntity source,
DiscordWebhookEntity discord)
{
return new DiscordNotificationDetailsDto
return new SubscriptionDetailsDto
{
Id = discordNotification.Id,
CodeAllowCommits = discordNotification.CodeAllowCommits,
CodeAllowReleases = discordNotification.CodeAllowReleases,
Id = subscription.Id,
CodeAllowCommits = subscription.CodeAllowCommits,
CodeAllowReleases = subscription.CodeAllowReleases,
Source = SourceDto.Convert(source),
DiscordWebHook = DiscordWebHookDto.Convert(discord)
};

View File

@ -3,7 +3,7 @@ using Newsbot.Collector.Domain.Models;
namespace Newsbot.Collector.Domain.Dto;
public class DiscordNotificationDto
public class SubscriptionDto
{
public Guid Id { get; set; }
public Guid SourceId { get; set; }
@ -11,9 +11,9 @@ public class DiscordNotificationDto
public bool CodeAllowReleases { get; set; }
public bool CodeAllowCommits { get; set; }
public static DiscordNotificationDto Convert(DiscordNotificationEntity model)
public static SubscriptionDto Convert(SubscriptionEntity model)
{
return new DiscordNotificationDto
return new SubscriptionDto
{
Id = model.Id,
SourceId = model.SourceId,

View File

@ -1,6 +1,6 @@
namespace Newsbot.Collector.Domain.Entities;
public class DiscordNotificationEntity
public class SubscriptionEntity
{
public Guid Id { get; set; }
public bool CodeAllowReleases { get; set; }

View File

@ -2,7 +2,7 @@ using Newsbot.Collector.Domain.Models;
namespace Newsbot.Collector.Domain.Interfaces;
public interface IDiscordClient
public interface IDiscordNotificatioClient
{
void SendMessage(DiscordMessage payload);
}

View File

@ -1,18 +0,0 @@
using Newsbot.Collector.Domain.Entities;
using Newsbot.Collector.Domain.Models;
namespace Newsbot.Collector.Domain.Interfaces;
public interface IDiscordNotificationRepository
{
DiscordNotificationEntity New(DiscordNotificationEntity model);
List<DiscordNotificationEntity> List(int page = 0, int count = 25);
List<DiscordNotificationEntity> ListBySourceId(Guid id, int page = 0, int count = 25);
List<DiscordNotificationEntity> ListByWebhook(Guid id, int page = 0, int count = 25);
DiscordNotificationEntity GetById(Guid id);
DiscordNotificationEntity GetByWebhookAndSource(Guid webhookId, Guid sourceId);
void Delete(Guid id);
}

View File

@ -0,0 +1,18 @@
using Newsbot.Collector.Domain.Entities;
using Newsbot.Collector.Domain.Models;
namespace Newsbot.Collector.Domain.Interfaces;
public interface ISubscriptionRepository
{
SubscriptionEntity New(SubscriptionEntity model);
List<SubscriptionEntity> List(int page = 0, int count = 25);
List<SubscriptionEntity> ListBySourceId(Guid id, int page = 0, int count = 25);
List<SubscriptionEntity> ListByWebhook(Guid id, int page = 0, int count = 25);
SubscriptionEntity GetById(Guid id);
SubscriptionEntity GetByWebhookAndSource(Guid webhookId, Guid sourceId);
void Delete(Guid id);
}

View File

@ -1,4 +1,3 @@
using Newsbot.Collector.Database;
using Newsbot.Collector.Database.Repositories;
using Newsbot.Collector.Domain.Entities;
using Newsbot.Collector.Domain.Interfaces;
@ -33,13 +32,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 IDiscordNotificationRepository _subs;
private ISubscriptionRepository _subs;
private IDiscordWebHooksRepository _webhook;
public DiscordNotificationJob()
@ -48,19 +47,18 @@ public class DiscordNotificationJob
_article = new ArticlesTable("");
_webhook = new DiscordWebhooksTable("");
_sources = new SourcesTable("");
_subs = new DiscordNotificationTable("");
_subs = new SubscriptionsTable("");
_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 DiscordNotificationTable(options.ConnectionString ?? "");
_subs = new SubscriptionsTable(options.ConnectionString ?? "");
_icons = new IconsTable(options.ConnectionString ?? "");
_logger = JobLogger.GetLogger(options.OpenTelemetry ?? "", JobName);
@ -78,7 +76,6 @@ 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");
@ -124,7 +121,7 @@ public class DiscordNotificationJob
_queue.Delete(request.Id);
}
public void SendSubscriptionNotification(Guid requestId, ArticlesEntity articleDetails, SourceEntity sourceDetails, IconEntity sourceIcon, DiscordNotificationEntity sub)
public void SendSubscriptionNotification(Guid requestId, ArticlesEntity articleDetails, SourceEntity sourceDetails, IconEntity sourceIcon, SubscriptionEntity sub)
{
// Check if the subscription code flags
// If the article is a code commit and the subscription does not want them, skip.
@ -137,7 +134,7 @@ public class DiscordNotificationJob
var discordDetails = _webhook.GetById(sub.DiscordWebHookId);
if (discordDetails.Enabled == false) return;
var client = new DiscordClient(discordDetails.Url);
var client = new DiscordWebhookClient(discordDetails.Url);
try
{
client.SendMessage(GenerateDiscordMessage(sourceDetails, articleDetails, sourceIcon));

View File

@ -6,16 +6,16 @@ using Newtonsoft.Json;
namespace Newsbot.Collector.Services.Notifications.Discord;
public class DiscordClient : IDiscordClient
public class DiscordWebhookClient : IDiscordNotificatioClient
{
private readonly string[] _webhooks;
public DiscordClient(string webhook)
public DiscordWebhookClient(string webhook)
{
_webhooks = new[] { webhook };
}
public DiscordClient(string[] webhooks)
public DiscordWebhookClient(string[] webhooks)
{
_webhooks = webhooks;
}

View File

@ -10,7 +10,7 @@ public class DiscordNotificationJobTest
public void PostTestMessage()
{
var uri = "";
var webhookClient = new DiscordClient(uri);
var webhookClient = new DiscordWebhookClient(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 DiscordNotificationEntity
new SubscriptionEntity
{
CodeAllowCommits = false,
CodeAllowReleases = true

View File

@ -1,16 +0,0 @@
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();
}
}