Add more async database functions

This commit is contained in:
2024-12-16 14:26:06 +03:30
parent dc38ce927f
commit 24d5175e7b
3 changed files with 9 additions and 7 deletions

View File

@@ -16,14 +16,14 @@ public class HandleDbBackup
{
logger?.LogInformation("Backup requested");
var json = _db.Find(new BsonDocument()).ToList().ToJson();
var json = (await _db.Find(new BsonDocument()).ToListAsync()).ToJson();
var bytes = Encoding.UTF8.GetBytes(json);
var stream = new MemoryStream(bytes);
await _bot.SendDocument(adminId, InputFile.FromStream(stream, "backup.json"),
"Backup of your collection\nCreated at " +
DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss" + "\nCurrent post count: " + _db.CountDocuments(new BsonDocument())),
DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss" + "\nCurrent post count: " + _db.CountDocumentsAsync(new BsonDocument())),
ParseMode.Html);
logger?.LogInformation("Backup sent");
}

View File

@@ -1,5 +1,6 @@
using Microsoft.Extensions.Logging;
using MongoDB.Driver;
using MongoDB.Driver.Linq;
using mstdnCats.Models;
using Telegram.Bot;
using Telegram.Bot.Types;
@@ -17,9 +18,9 @@ public class HandleStartMessage
(callbackQuery != null ? "Callback" : "Start command"));
// choose all media attachments that are approved
var mediaAttachmentsToSelect = _db.AsQueryable()
var mediaAttachmentsToSelect = await _db.AsQueryable()
.Where(post => post.MediaAttachments.Any(media => media.Approved))
.ToList();
.ToListAsync();
// select random approved media attachment
var selectedMediaAttachment = mediaAttachmentsToSelect[new Random().Next(mediaAttachmentsToSelect.Count)];

View File

@@ -1,6 +1,7 @@
using CatsOfMastodonBot.Models;
using Microsoft.Extensions.Logging;
using MongoDB.Driver;
using MongoDB.Driver.Linq;
using mstdnCats.Models;
using Telegram.Bot;
using Telegram.Bot.Types.Enums;
@@ -16,9 +17,9 @@ public class ProcessPosts
var config = ConfigData.fetchData();
// Get existing posts
var existingPosts = _db.AsQueryable().Select(x => x.mstdnPostId).ToArray();
var existingPosts = await _db.AsQueryable().Select(x => x.mstdnPostId).ToListAsync();
logger?.LogInformation(
$"Recieved posts to proccess: {fetchedPosts.Count} - total existing posts: {existingPosts.Length}");
$"Recieved posts to proccess: {fetchedPosts.Count} - total existing posts: {existingPosts.Count}");
var newPosts = 0;
// Process posts
List<Post> validPosts = new();