Newsbot.Collector/Newsbot.Collector.Services/Jobs/DiscordNotificationJob.cs
James Tombleson af2f700e41
Features/discord errors (#20)
* catching another http error code.

* more discord logging
2023-04-03 06:46:35 -07:00

181 lines
6.0 KiB
C#

using Newsbot.Collector.Database.Repositories;
using Newsbot.Collector.Domain.Interfaces;
using Newsbot.Collector.Domain.Models;
using Newsbot.Collector.Domain.Models.Config;
using Newsbot.Collector.Services.Notifications.Discord;
using Newtonsoft.Json.Linq;
using Serilog;
namespace Newsbot.Collector.Services.Jobs;
public class DiscordNotificationJobOptions
{
public string? ConnectionString { get; init; }
public string? OpenTelemetry { get; init; }
public bool IsEnabled { get; init; }
}
public class DiscordNotificationJob
{
private const string JobName = "DiscordNotifications";
private IDiscordQueueRepository _queue;
private IArticlesRepository _article;
private IDiscordWebHooksRepository _webhook;
private ISourcesRepository _sources;
private ISubscriptionRepository _subs;
private IIconsRepository _icons;
private ILogger _logger;
public DiscordNotificationJob()
{
_queue = new DiscordQueueTable("");
_article = new ArticlesTable("");
_webhook = new DiscordWebhooksTable("");
_sources = new SourcesTable("");
_subs = new SubscriptionsTable("");
_icons = new IconsTable("");
_logger = JobLogger.GetLogger("", JobName);
}
public void InitAndExecute(DiscordNotificationJobOptions options)
{
_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 ?? "");
_icons = new IconsTable(options.ConnectionString ?? "");
_logger = JobLogger.GetLogger(options.OpenTelemetry ?? "", JobName);
if (!options.IsEnabled)
{
_logger.Warning($"{JobName} - Going to exit because feature flag is off.");
return;
}
_logger.Information($"{JobName} - Starting up the job.");
Execute();
}
private void Execute()
{
//collect all the new requests
var requests = _queue.List(25);
_logger.Debug($"{JobName} - Collected {requests.Count} items to send");
foreach (var request in requests)
{
_logger.Debug($"{JobName} - Processing {request.ID}");
// Get all details on the article in the queue
var articleDetails = _article.GetById(request.ArticleID);
// Get the details of the source
var sourceDetails = _sources.GetByID(articleDetails.SourceID);
if (sourceDetails.ID == Guid.Empty)
{
_logger.Error($"{JobName} - Article ({articleDetails.ID}) was linked to a empty Source ID. Removing from the queue.");
_queue.Delete(request.ID);
continue;
}
var sourceIcon = new IconModel();
try
{
sourceIcon = _icons.GetBySourceId(sourceDetails.ID);
}
catch
{
_logger.Warning($"{JobName} - Source ID '{sourceDetails.ID}' is missing an icon.");
}
// 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;
}
var client = new DiscordWebhookClient(discordDetails.Url);
try
{
client.SendMessage(GenerateDiscordMessage(sourceDetails, articleDetails, sourceIcon));
}
catch (Exception e)
{
_logger.Error($"Failed to post message to Discord. {e}");
_logger.Debug($"Queue Record: {request.ID}");
_logger.Debug($"Article: {articleDetails.ID}");
_logger.Debug($"Source: {sourceDetails.ID}");
_logger.Debug($"Subscription: {sub.ID}");
}
Thread.Sleep(3000);
}
_logger.Debug($"{JobName} - Removing {request.ID} from the queue.");
_queue.Delete(request.ID);
}
_logger.Information($"{JobName} - Loop has been completed.");
}
public DiscordMessage GenerateDiscordMessage(SourceModel source, ArticlesModel article, IconModel icon)
{
var embed = new DiscordMessageEmbed
{
Title = article.Title,
Color = DiscordMessageEmbedColors.Red,
Description = article.Description,
Author = new DiscordMessageEmbedAuthor
{
Name = article.AuthorName,
IconUrl = icon.FileName
},
Footer = new DiscordMessageEmbedFooter
{
Text = "Brought to you by Newsbot",
},
Fields = new DiscordMessageEmbedField[]
{
new DiscordMessageEmbedField
{
Name = "Link",
Value = article.URL,
Inline = false,
}
}
};
if (article.URL is not null && article.URL != "")
{
embed.Url = article.URL;
}
if (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
}
};
}
}