Newsbot.Collector/Newsbot.Collector.Services/Jobs/DiscordNotificationJob.cs

157 lines
5.0 KiB
C#
Raw Normal View History

2023-03-05 20:12:59 -08:00
using Newsbot.Collector.Database.Repositories;
using Newsbot.Collector.Domain.Interfaces;
using Newsbot.Collector.Domain.Models;
using Newsbot.Collector.Domain.Models.Config;
2023-03-05 20:12:59 -08:00
using Newsbot.Collector.Services.Notifications.Discord;
using Serilog;
2023-03-05 20:12:59 -08:00
namespace Newsbot.Collector.Services.Jobs;
public class DiscordNotificationJobOptions
{
public ConfigSectionConnectionStrings? ConnectionStrings { get; set; }
public ConfigSectionNotificationsDiscord? Config { get; set; }
2023-03-05 20:12:59 -08:00
}
public class DiscordNotificationJob
2023-03-05 20:12:59 -08:00
{
private IDiscordQueueRepository _queue;
private IArticlesRepository _article;
private IDiscordWebHooksRepository _webhook;
private ISourcesRepository _sources;
private ISubscriptionRepository _subs;
private IIconsRepository _icons;
2023-03-05 20:12:59 -08:00
public DiscordNotificationJob()
2023-03-05 20:12:59 -08:00
{
_queue = new DiscordQueueTable("");
_article = new ArticlesTable("");
_webhook = new DiscordWebhooksTable("");
_sources = new SourcesTable("");
_subs = new SubscriptionsTable("");
_icons = new IconsTable("");
2023-03-05 20:12:59 -08:00
}
public void InitAndExecute(DiscordNotificationJobOptions options)
2023-03-05 20:12:59 -08:00
{
options.ConnectionStrings ??= new ConfigSectionConnectionStrings();
options.Config ??= new ConfigSectionNotificationsDiscord();
_queue = new DiscordQueueTable(options.ConnectionStrings.Database ?? "");
_article = new ArticlesTable(options.ConnectionStrings.Database ?? "");
_webhook = new DiscordWebhooksTable(options.ConnectionStrings.Database ?? "");
_sources = new SourcesTable(options.ConnectionStrings.Database ?? "");
_subs = new SubscriptionsTable(options.ConnectionStrings.Database ?? "");
_icons = new IconsTable(options.ConnectionStrings.Database ?? "");
Execute();
2023-03-05 20:12:59 -08:00
}
private void Execute()
{
//collect all the new requests
var requests = _queue.List(25);
foreach (var request in requests)
{
// Get all details on the article in the queue
var articleDetails = _article.GetById(request.ArticleID);
// Get the deatils of the source
var sourceDetails = _sources.GetByID(articleDetails.SourceID);
if (sourceDetails.ID == Guid.Empty)
{
Log.Error($"DiscordNotificationJob - Article ({articleDetails.ID}) was linked to a empty Source ID. Removing from the queue");
_queue.Delete(request.ID);
continue;
}
2023-03-05 20:12:59 -08:00
var sourceIcon = _icons.GetBySourceId(sourceDetails.ID);
2023-03-05 20:12:59 -08:00
// Find all the subscriptions for that source
var allSubscriptions = _subs.ListBySourceID(sourceDetails.ID);
foreach (var sub in allSubscriptions)
{
// find the discord webhooks we need to post to
var discordDetails = _webhook.GetByID(sub.DiscordWebHookID);
if (discordDetails.Enabled == false)
{
continue;
}
2023-03-05 20:12:59 -08:00
var client = new DiscordWebhookClient(discordDetails.Url);
try
{
client.SendMessage(GenerateDiscordMessage(sourceDetails, articleDetails, sourceIcon));
}
catch (Exception e)
{
Log.Error($"Failed to post message to Discord. {e}");
continue;
}
Thread.Sleep(3000);
2023-03-05 20:12:59 -08:00
}
_queue.Delete(request.ID);
2023-03-05 20:12:59 -08:00
}
}
public DiscordMessage GenerateDiscordMessage(SourceModel source, ArticlesModel article, IconModel icon)
2023-03-05 20:12:59 -08:00
{
var embed = new DiscordMessageEmbed
{
Title = article.Title,
Color = DiscordMessageEmbedColors.Red,
Description = article.Description,
Author = new DiscordMessageEmbedAuthor
{
Name = article.AuthorName,
IconUrl = icon.FileName
2023-03-05 20:12:59 -08:00
},
Footer = new DiscordMessageEmbedFooter
{
Text = "Brought to you by Newsbot",
},
Fields = new DiscordMessageEmbedField[]
{
new DiscordMessageEmbedField
{
Name = "Link",
Value = article.URL,
Inline = false,
}
2023-03-05 20:12:59 -08:00
}
};
if (article.URL is not null && article.URL != "")
{
embed.Url = article.URL;
}
if (article.Thumbnail is not null && article.Thumbnail != "")
{
embed.Image = new DiscordMessageEmbedImage
{
Url = article.Thumbnail
};
}
if (article.AuthorImage is not null && article.AuthorImage != "")
{
embed.Author.IconUrl = article.AuthorImage;
}
return new DiscordMessage
{
Embeds = new DiscordMessageEmbed[]
{
embed
}
};
}
}