Maybe fixing the diplicate image problem?

This commit is contained in:
2024-11-13 21:15:14 +03:30
parent 71be9a43e1
commit d35dcd9a54

View File

@@ -1,5 +1,6 @@
using CatsOfMastodonBot.Models; using CatsOfMastodonBot.Models;
using JsonFlatFileDataStore; using JsonFlatFileDataStore;
using Microsoft.AspNetCore.Mvc.Diagnostics;
using Microsoft.Extensions.Logging; using Microsoft.Extensions.Logging;
using mstdnCats.Models; using mstdnCats.Models;
using Telegram.Bot; using Telegram.Bot;
@@ -27,12 +28,15 @@ namespace mstdnCats.Services
string mediaId = parts[1]; string mediaId = parts[1];
var post = _db.AsQueryable().FirstOrDefault(p => p.MediaAttachments.Any(m => m.MediaId == mediaId)); var post = _db.AsQueryable().FirstOrDefault(p => p.MediaAttachments.Any(m => m.MediaId == mediaId));
// Extract Only media attachments into a list of media attachments
if (post == null) if (post == null)
{ {
logger?.LogInformation("No matching post found."); logger?.LogInformation("No matching post found.");
return; return;
} }
var allMediaAttachments = _db.AsQueryable().SelectMany(p => p.MediaAttachments).ToList();
// Approve the media attachment // Approve the media attachment
if (action == "approve") if (action == "approve")
@@ -55,7 +59,7 @@ namespace mstdnCats.Services
if (updated) if (updated)
{ {
// Send the media attachment to the channel // Send the media attachment to the channel
await _bot.SendPhoto(config.CHANNEL_NUMID, post.MediaAttachments.First().Url, caption: $"Post from " + $"<a href=\"" + post.Account.Url + "\">" + post.Account.DisplayName + " </a>", parseMode: ParseMode.Html await _bot.SendPhoto(config.CHANNEL_NUMID, allMediaAttachments.First(m => m.MediaId == mediaId).Url, caption: $"Post from " + $"<a href=\"" + post.Account.Url + "\">" + post.Account.DisplayName + " </a>", parseMode: ParseMode.Html
, replyMarkup: new InlineKeyboardMarkup(InlineKeyboardButton.WithUrl("View on Mastodon", post.Url))); , replyMarkup: new InlineKeyboardMarkup(InlineKeyboardButton.WithUrl("View on Mastodon", post.Url)));
await _bot.AnswerCallbackQuery(callbackQuery.Id, "Media attachment approved and sent to channel."); await _bot.AnswerCallbackQuery(callbackQuery.Id, "Media attachment approved and sent to channel.");