Finished the migration to MongoDb (NOT TESTED)

This commit is contained in:
2024-12-15 17:32:26 +03:30
parent fbe0d500d9
commit ef8c7f2ee9
6 changed files with 48 additions and 30 deletions

View File

@@ -2,6 +2,7 @@ using CatsOfMastodonBot.Models;
using JsonFlatFileDataStore;
using Microsoft.AspNetCore.Mvc.Diagnostics;
using Microsoft.Extensions.Logging;
using MongoDB.Driver;
using mstdnCats.Models;
using Telegram.Bot;
using Telegram.Bot.Types;
@@ -12,10 +13,10 @@ namespace mstdnCats.Services
{
public class HandlePostAction
{
public static async Task HandleCallbackQuery(CallbackQuery callbackQuery, IDocumentCollection<Post> _db, TelegramBotClient _bot, ILogger<MastodonBot>? logger)
public static async Task HandleCallbackQuery(CallbackQuery callbackQuery, IMongoCollection<Post> _db, TelegramBotClient _bot, ILogger<MastodonBot>? logger)
{
var config = ConfigData.fetchData();
// Extract media ID from callback query data
string[] parts = callbackQuery.Data.Split('-');
if (parts.Length != 2)
@@ -27,16 +28,14 @@ namespace mstdnCats.Services
string action = parts[0];
string mediaId = parts[1];
var post = _db.AsQueryable().FirstOrDefault(p => p.MediaAttachments.Any(m => m.MediaId == mediaId));
// Extract Only media attachments into a list of media attachments
var filter = Builders<Post>.Filter.Eq("MediaAttachments.MediaId", mediaId);
var post = await _db.Find(filter).FirstOrDefaultAsync();
if (post == null)
{
logger?.LogInformation("No matching post found.");
return;
}
var allMediaAttachments = _db.AsQueryable().SelectMany(p => p.MediaAttachments).ToList();
// Approve the media attachment
if (action == "approve")
@@ -45,21 +44,22 @@ namespace mstdnCats.Services
if (mediaAttachment != null)
{
// Check if the media attachment is already approved
if (mediaAttachment.Approved){
await _bot.AnswerCallbackQuery(callbackQuery.Id, "Media attachment is already approved.",true);
if (mediaAttachment.Approved)
{
await _bot.AnswerCallbackQuery(callbackQuery.Id, "Media attachment is already approved.", true);
return;
}
mediaAttachment.Approved = true;
bool updated = await _db.UpdateOneAsync(p => p.mstdnPostId == post.mstdnPostId, post);
// Update the media attachment
var update = Builders<Post>.Update.Set("MediaAttachments.$.Approved", true);
var result = await _db.UpdateOneAsync(filter, update);
if (updated)
if (result.ModifiedCount > 0)
{
try
{
// Send the media attachment to the channel
var allMediaAttachments = post.MediaAttachments.ToList();
await _bot.SendPhoto(config.CHANNEL_NUMID,
allMediaAttachments.First(m => m.MediaId == mediaId).Url,
caption: $"Post from " + $"<a href=\"" + post.Account.Url + "\">" +
@@ -81,8 +81,6 @@ namespace mstdnCats.Services
{
logger?.LogError($"Failed to update the media attachment {mediaId}. Record might not exist or was not found.");
}
}
else
{
@@ -103,19 +101,26 @@ namespace mstdnCats.Services
}
else
{
post.MediaAttachments.RemoveAll(m => m.MediaId == mediaId);
await _db.UpdateOneAsync(p => p.mstdnPostId == post.mstdnPostId, post);
await _bot.AnswerCallbackQuery(callbackQuery.Id, "Media attachment rejected.");
await _bot.DeleteMessage(callbackQuery.Message.Chat.Id, callbackQuery.Message.Id);
var update = Builders<Post>.Update.PullFilter("MediaAttachments", Builders<MediaAttachment>.Filter.Eq("MediaId", mediaId));
var result = await _db.UpdateOneAsync(filter, update);
logger?.LogTrace($"Media attachment {mediaId} removed from post {post.mstdnPostId}.");
if (result.ModifiedCount > 0)
{
await _bot.AnswerCallbackQuery(callbackQuery.Id, "Media attachment rejected.");
await _bot.DeleteMessage(callbackQuery.Message.Chat.Id, callbackQuery.Message.Id);
logger?.LogTrace($"Media attachment {mediaId} removed from post {post.mstdnPostId}.");
}
else
{
logger?.LogError($"Failed to remove media attachment {mediaId} from post {post.mstdnPostId}.");
}
}
}
else
{
logger?.LogError("Invalid action specified.");
}
}
}
}