Added additional logging and implemented post handling if the message is already approved
This commit is contained in:
@@ -37,10 +37,18 @@ namespace mstdnCats.Services
|
||||
var mediaAttachment = post.MediaAttachments.FirstOrDefault(m => m.MediaId == mediaId);
|
||||
if (mediaAttachment != null)
|
||||
{
|
||||
// Check if the media attachment is already approved
|
||||
bool isAlreadyApproved = mediaAttachment.Approved;
|
||||
if (isAlreadyApproved){
|
||||
await _bot.AnswerCallbackQueryAsync(callbackQuery.Id, "Media attachment is already approved.",true);
|
||||
return;
|
||||
}
|
||||
|
||||
mediaAttachment.Approved = true;
|
||||
|
||||
bool updated = await _db.UpdateOneAsync(p => p.mstdnPostId == post.mstdnPostId, post);
|
||||
|
||||
|
||||
if (updated)
|
||||
{
|
||||
// Send the media attachment to the channel
|
||||
|
@@ -11,6 +11,7 @@ namespace mstdnCats.Services
|
||||
{
|
||||
public static async Task<List<MediaAttachment>> checkAndInsertPostsAsync(IDocumentCollection<Post> _db, TelegramBotClient _bot, List<Post> fetchedPosts, ILogger<MastodonBot>? logger)
|
||||
{
|
||||
|
||||
// Get existing posts
|
||||
var existingPosts = _db.AsQueryable().Select(x => x.mstdnPostId).ToArray();
|
||||
logger?.LogInformation($"Recieved posts to proccess: {fetchedPosts.Count} - total existing posts: {existingPosts.Length}");
|
||||
@@ -21,13 +22,24 @@ namespace mstdnCats.Services
|
||||
// Check if post already exists
|
||||
if (!existingPosts.Contains(post.mstdnPostId) && post.MediaAttachments.Count > 0)
|
||||
{
|
||||
|
||||
// Send approve or reject message to admin
|
||||
foreach (var media in post.MediaAttachments)
|
||||
{
|
||||
try
|
||||
{
|
||||
await _bot.SendPhotoAsync(DotNetEnv.Env.GetString("ADMIN_NUMID"), media.PreviewUrl, caption: $"<a href=\"" + post.Url + "\"> Mastodon </a>", parseMode: ParseMode.Html
|
||||
, replyMarkup: new InlineKeyboardMarkup().AddButton("Approve", $"approve-{media.MediaId}").AddButton("Reject", $"reject-{media.MediaId}"));
|
||||
|
||||
}
|
||||
catch (System.Exception ex)
|
||||
{
|
||||
logger?.LogError("Error while sending message to admin: " + ex.Message + " - Media URL: " + media.PreviewUrl);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
// Insert post
|
||||
await _db.InsertOneAsync(post);
|
||||
newPosts++;
|
||||
|
Reference in New Issue
Block a user