Now not accepting posts from bot accounts - improved error handling
This commit is contained in:
@@ -6,7 +6,6 @@ namespace mstdnCats.Services
|
||||
{
|
||||
public sealed class PostResolver
|
||||
{
|
||||
|
||||
public static async Task<List<Post>?> GetPostsAsync(string tag, ILogger<MastodonBot>? logger, string instance)
|
||||
{
|
||||
// Get posts
|
||||
@@ -16,20 +15,27 @@ namespace mstdnCats.Services
|
||||
var response = await _httpClient.GetAsync(requestUrl);
|
||||
|
||||
// Print out ratelimit info
|
||||
logger?.LogInformation("Remaining requests: " + response.Headers.GetValues("X-RateLimit-Remaining").First() + "time to reset: " + response.Headers.GetValues("X-RateLimit-Reset").First());
|
||||
logger?.LogInformation("Remaining requests: " +
|
||||
response.Headers.GetValues("X-RateLimit-Remaining").First() + "time to reset: " +
|
||||
response.Headers.GetValues("X-RateLimit-Reset").First());
|
||||
|
||||
// Check if response is ok
|
||||
if (
|
||||
response.StatusCode == System.Net.HttpStatusCode.OK ||
|
||||
response.Content.Headers.ContentType.MediaType.Contains("application/json") ||
|
||||
response.Headers.TryGetValues("X-RateLimit-Remaining", out var remaining) && int.Parse(remaining.First()) != 0
|
||||
)
|
||||
response.Headers.TryGetValues("X-RateLimit-Remaining", out var remaining) &&
|
||||
int.Parse(remaining.First()) != 0
|
||||
)
|
||||
{
|
||||
// Deserialize response based on 'Post' model
|
||||
return JsonSerializer.Deserialize<List<Post>>(await response.Content.ReadAsStringAsync());
|
||||
}
|
||||
|
||||
else return null;
|
||||
else
|
||||
{
|
||||
logger?.LogCritical("Error while getting posts: " + response.StatusCode);
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
@@ -22,7 +22,7 @@ namespace mstdnCats.Services
|
||||
foreach (Post post in fetchedPosts)
|
||||
{
|
||||
// Check if post already exists
|
||||
if (!existingPosts.Contains(post.mstdnPostId) && post.MediaAttachments.Count > 0)
|
||||
if (!existingPosts.Contains(post.mstdnPostId) && post.MediaAttachments.Count > 0 && post.Account.IsBot == false)
|
||||
{
|
||||
|
||||
// Send approve or reject message to admin
|
||||
|
Reference in New Issue
Block a user