diff --git a/Models/Post.cs b/Models/Post.cs index 692af38..e3871ef 100755 --- a/Models/Post.cs +++ b/Models/Post.cs @@ -29,7 +29,7 @@ public class Account } public class MediaAttachment -{ +{ [JsonPropertyName("id")] public required string MediaId { get; set; } [JsonPropertyName("type")] public required string Type { get; set; } [JsonPropertyName("url")] public required string Url { get; set; } diff --git a/Program.cs b/Program.cs index ff10449..790bad2 100755 --- a/Program.cs +++ b/Program.cs @@ -1,5 +1,7 @@ using CatsOfMastodonBot.Models; using CatsOfMastodonBot.Services; +using Microsoft.AspNetCore.Hosting; +using Microsoft.Extensions.Hosting; using Microsoft.Extensions.Logging; using mstdnCats.Services; using Telegram.Bot; @@ -30,6 +32,21 @@ public class MastodonBot var db = await DbInitializer.SetupDb(config.MONGODB_CONNECTION_STRING, config.DB_NAME); logger.LogInformation("DB setup done"); + // Web server setup + var host = Host.CreateDefaultBuilder() + .ConfigureWebHostDefaults(webBuilder => + { + webBuilder.UseKestrel(options => + { + options.ListenAnyIP(5005); // Listen on port 5005 + }); + ServerStartup.Serverstartup(db); + webBuilder.UseStartup(); + }) + .Build(); + + await host.RunAsync(); + // Setup bot var bot = new TelegramBotClient(config.BOT_TOKEN); @@ -41,13 +58,14 @@ public class MastodonBot logger.LogInformation("Setup complete"); logger.LogInformation($"Bot is running as {me.FirstName} - instance: {config.INSTANCE}"); - // Handle bot updates + // Handle bot updates - For glass buttons functionality async Task OnUpdate(Update update) { switch (update) { case { CallbackQuery: { } callbackQuery }: { + // Send a new cat picture if (callbackQuery.Data == "new_random") { await HandleStartMessage.HandleStartMessageAsync(callbackQuery.Message, bot, db, logger, @@ -55,11 +73,14 @@ public class MastodonBot break; } - else + // Approve or reject a post + else if (callbackQuery.Data.Contains("approve-") || callbackQuery.Data.Contains("reject-")) { await HandlePostAction.HandleCallbackQuery(callbackQuery, db, bot, logger); break; } + + break; } default: logger.LogInformation($"Received unhandled update {update.Type}"); break; } @@ -72,14 +93,16 @@ public class MastodonBot { if (message.Text == "/start" && message.Chat.Type == ChatType.Private) await HandleStartMessage.HandleStartMessageAsync(message, bot, db, logger); - else if (message.Text == "/backup") + + else if (message.Text == "/backup" && message.Chat.Type == ChatType.Private) await HandleDbBackup.HandleDbBackupAsync(bot, logger, config.DB_NAME, config.ADMIN_NUMID, db); + // Send a message to prompt user to send /start and recieve their cat photo only if its from a telegram user and not a channel else if (message.Chat.Type == ChatType.Private) await HandleStartMessage.HandleStartMessageAsync(message, bot, db, logger); } - // Set a timer to fetch and process posts every 15 minutes + // Set a timer to fetch and process posts every 10 minutes _postFetchTimer = new Timer(async _ => await RunCheck.runAsync(db, bot, config.TAG, logger, config.INSTANCE), null, TimeSpan.Zero, TimeSpan.FromMinutes(10)); // Another timer to automatically backup the DB every 6 hour diff --git a/Web/ServerStartup.cs b/Web/ServerStartup.cs new file mode 100755 index 0000000..5c9739c --- /dev/null +++ b/Web/ServerStartup.cs @@ -0,0 +1,63 @@ +using System.Reflection; +using System.Text; +using Microsoft.AspNetCore.Builder; +using Microsoft.AspNetCore.Hosting; +using Microsoft.AspNetCore.Http; +using MongoDB.Driver; +using MongoDB.Driver.Linq; +using mstdnCats.Models; + +namespace mstdnCats.Services; + +public class ServerStartup +{ + private static IMongoCollection _db; + public static void Serverstartup(IMongoCollection db) + { + _db = db; + } + public void Configure(IApplicationBuilder app, IWebHostEnvironment env) + { + app.UseRouting(); + + app.UseEndpoints(endpoints => + { + endpoints.MapGet("/", async context => + { + var assembly = Assembly.GetEntryAssembly(); + var resourceName = "mstdnCats.Web.wwwroot.index.html"; // Full resource name + + using (var stream = assembly.GetManifestResourceStream(resourceName)) + { + if (stream == null) + { + context.Response.StatusCode = 500; + await context.Response.WriteAsync("Something went wrong in our side."); + return; + } + + using (var reader = new StreamReader(stream, Encoding.UTF8)) + { + context.Response.ContentType = "text/html"; + await context.Response.WriteAsync(reader.ReadToEnd()); + } + } + }); + + endpoints.MapGet("/api/gimme", async context => + { + // Api endpoint + // Choose all media attachments that are approved + var mediaAttachmentsToSelect = await _db.AsQueryable() + .Where(post => post.MediaAttachments.Any(media => media.Approved)) + .ToListAsync(); + + // Select random approved media attachment + var selectedPost = mediaAttachmentsToSelect[new Random().Next(mediaAttachmentsToSelect.Count)]; + + // Send as JSON + await context.Response.WriteAsJsonAsync(selectedPost); + }); + }); + } +} \ No newline at end of file diff --git a/Web/wwwroot/index.html b/Web/wwwroot/index.html new file mode 100755 index 0000000..af44201 --- /dev/null +++ b/Web/wwwroot/index.html @@ -0,0 +1,293 @@ + + + + + + Cats of Mastodon! + + +

Cats of Mastodon!

+

Welcome to Daily Catventures! Get your daily dose of purr-fectly adorable feline fun! ✨
Posts gathered across Mastodon 🤝

+ +
+
+
+ 's cat! +
+
+ Cat Photo +
+
+
+ View on Mastodon + +
+
+
+
+ +
+ © 2024 Mahdium + mahdium.ir + Source Code +
+ + + diff --git a/mstdnCats.csproj b/mstdnCats.csproj index 26a7f11..6673f58 100755 --- a/mstdnCats.csproj +++ b/mstdnCats.csproj @@ -15,4 +15,11 @@ + + + + + + +