Add more async database functions
This commit is contained in:
		@@ -15,15 +15,15 @@ public class HandleDbBackup
 | 
			
		||||
        string adminId, IMongoCollection<Post> _db)
 | 
			
		||||
    {
 | 
			
		||||
        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");
 | 
			
		||||
    }
 | 
			
		||||
 
 | 
			
		||||
@@ -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)];
 | 
			
		||||
 
 | 
			
		||||
@@ -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();
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user