added support for .env file and improved validation

This commit is contained in:
2024-09-14 21:34:21 +03:30
parent 2f2ab5f944
commit 77f1a931fd
6 changed files with 67 additions and 31 deletions

21
Services/CheckEnv.cs Normal file
View File

@@ -0,0 +1,21 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
namespace mstdnCats.Services
{
public class CheckEnv
{
public static Boolean IsValid(){
if (DotNetEnv.Env.GetString("DB_NAME") == null ||
DotNetEnv.Env.GetString("BOT_TOKEN") == null ||
DotNetEnv.Env.GetString("TAG") == null ||
DotNetEnv.Env.GetString("CHANNEL_NUMID") == null ||
DotNetEnv.Env.GetString("ADMIN_NUMID") == null ){
return false;
}
return true;
}
}
}

View File

@@ -9,19 +9,22 @@ namespace mstdnCats.Services
{
// Setup DB
IDocumentCollection<Post>? collection = null;
try
if (_dbname != null)
{
// Initialize DB
var store = new DataStore($"{_dbname}.json", minifyJson: false);
collection = store.GetCollection<Post>();
}
catch
{
return Task.FromResult<IDocumentCollection<Post>>(null);
}
try
{
// Initialize DB
var store = new DataStore($"{_dbname}.json", minifyJson: false);
collection = store.GetCollection<Post>();
}
catch
{
return Task.FromResult<IDocumentCollection<Post>>(null);
}
// Return collection
// Return collection
return Task.FromResult(collection);
}
return Task.FromResult(collection);
}
}

View File

@@ -44,7 +44,7 @@ namespace mstdnCats.Services
if (updated)
{
// Send the media attachment to the channel
await _bot.SendPhotoAsync(Environment.GetEnvironmentVariable("CHANNEL_NUMID"), post.MediaAttachments.First().Url, caption: $"Post from " + $"<a href=\"" + post.Account.Url + "\">" + post.Account.DisplayName + " </a>", parseMode: ParseMode.Html
await _bot.SendPhotoAsync(DotNetEnv.Env.GetString("CHANNEL_NUMID"), post.MediaAttachments.First().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)));
await _bot.AnswerCallbackQueryAsync(callbackQuery.Id, "Media attachment approved and sent to channel.");

View File

@@ -24,7 +24,7 @@ namespace mstdnCats.Services
// Send approve or reject message to admin
foreach (var media in post.MediaAttachments)
{
await _bot.SendPhotoAsync(Environment.GetEnvironmentVariable("ADMIN_NUMID"), media.PreviewUrl, caption: $"<a href=\"" + post.Url + "\"> Mastodon </a>", parseMode: ParseMode.Html
await _bot.SendPhotoAsync(DotNetEnv.Env.GetString("ADMIN_NUMID"), media.PreviewUrl, caption: $"<a href=\"" + post.Url + "\"> Mastodon </a>", parseMode: ParseMode.Html
, replyMarkup: new InlineKeyboardMarkup().AddButton("Approve", $"approve-{media.MediaId}").AddButton("Reject", $"reject-{media.MediaId}"));
}