Now not accepting posts from bot accounts - improved error handling
This commit is contained in:
1 parent
79deb67848
commit
972167634f
3 files changed
+14
-6
No files matched your search
@@ -26,6 +26,8 @@ namespace mstdnCats.Models
|
|||||||
public required string Acct { get; set; }
|
public required string Acct { get; set; }
|
||||||
[JsonPropertyName("display_name")]
|
[JsonPropertyName("display_name")]
|
||||||
public required string DisplayName { get; set; }
|
public required string DisplayName { get; set; }
|
||||||
|
[JsonPropertyName("bot")]
|
||||||
|
public required Boolean IsBot { get; set; }
|
||||||
[JsonPropertyName("url")]
|
[JsonPropertyName("url")]
|
||||||
public required string Url { get; set; }
|
public required string Url { get; set; }
|
||||||
[JsonPropertyName("avatar_static")]
|
[JsonPropertyName("avatar_static")]
|
||||||
|
|||||||
@@ -6,7 +6,6 @@ namespace mstdnCats.Services
|
|||||||
{
|
{
|
||||||
public sealed class PostResolver
|
public sealed class PostResolver
|
||||||
{
|
{
|
||||||
|
|
||||||
public static async Task<List<Post>?> GetPostsAsync(string tag, ILogger<MastodonBot>? logger, string instance)
|
public static async Task<List<Post>?> GetPostsAsync(string tag, ILogger<MastodonBot>? logger, string instance)
|
||||||
{
|
{
|
||||||
// Get posts
|
// Get posts
|
||||||
@@ -16,20 +15,27 @@ namespace mstdnCats.Services
|
|||||||
var response = await _httpClient.GetAsync(requestUrl);
|
var response = await _httpClient.GetAsync(requestUrl);
|
||||||
|
|
||||||
// Print out ratelimit info
|
// 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
|
// Check if response is ok
|
||||||
if (
|
if (
|
||||||
response.StatusCode == System.Net.HttpStatusCode.OK ||
|
response.StatusCode == System.Net.HttpStatusCode.OK ||
|
||||||
response.Content.Headers.ContentType.MediaType.Contains("application/json") ||
|
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
|
// Deserialize response based on 'Post' model
|
||||||
return JsonSerializer.Deserialize<List<Post>>(await response.Content.ReadAsStringAsync());
|
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)
|
foreach (Post post in fetchedPosts)
|
||||||
{
|
{
|
||||||
// Check if post already exists
|
// 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
|
// Send approve or reject message to admin
|
||||||
|
|||||||
Reference in new issue
Block a user