BirdsiteLive/src/BirdsiteLive.Pipeline/Processors/Federation/SendTweetsToFollowersProces...

129 lines
5.1 KiB
C#
Raw Normal View History

2020-07-23 01:27:25 +02:00
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Threading;
2020-07-19 05:35:19 +02:00
using System.Threading.Tasks;
2020-08-13 00:34:01 +02:00
using System.Xml;
using BirdsiteLive.Common.Settings;
using BirdsiteLive.DAL.Contracts;
2020-07-23 01:27:25 +02:00
using BirdsiteLive.DAL.Models;
using BirdsiteLive.Domain;
using BirdsiteLive.Moderation.Actions;
using BirdsiteLive.Pipeline.Contracts.Federation;
2020-07-19 05:35:19 +02:00
using BirdsiteLive.Pipeline.Models;
using BirdsiteLive.Pipeline.Processors.SubTasks;
using BirdsiteLive.Twitter;
2020-07-23 02:19:40 +02:00
using BirdsiteLive.Twitter.Models;
2021-01-16 06:34:09 +01:00
using Microsoft.Extensions.Logging;
2020-07-23 01:27:25 +02:00
using Tweetinvi.Models;
2020-07-19 05:35:19 +02:00
namespace BirdsiteLive.Pipeline.Processors.Federation
2020-07-19 05:35:19 +02:00
{
public class SendTweetsToFollowersProcessor : ISendTweetsToFollowersProcessor
{
private readonly ISendTweetsToInboxTask _sendTweetsToInboxTask;
private readonly ISendTweetsToSharedInboxTask _sendTweetsToSharedInbox;
2021-09-11 02:47:02 +02:00
private readonly IFollowersDal _followersDal;
private readonly InstanceSettings _instanceSettings;
2021-01-16 06:34:09 +01:00
private readonly ILogger<SendTweetsToFollowersProcessor> _logger;
private readonly IRemoveFollowerAction _removeFollowerAction;
#region Ctor
public SendTweetsToFollowersProcessor(ISendTweetsToInboxTask sendTweetsToInboxTask, ISendTweetsToSharedInboxTask sendTweetsToSharedInbox, IFollowersDal followersDal, ILogger<SendTweetsToFollowersProcessor> logger, InstanceSettings instanceSettings, IRemoveFollowerAction removeFollowerAction)
{
_sendTweetsToInboxTask = sendTweetsToInboxTask;
_sendTweetsToSharedInbox = sendTweetsToSharedInbox;
2021-01-16 06:34:09 +01:00
_logger = logger;
_instanceSettings = instanceSettings;
_removeFollowerAction = removeFollowerAction;
2021-09-11 02:47:02 +02:00
_followersDal = followersDal;
}
#endregion
public async Task<UserWithDataToSync> ProcessAsync(UserWithDataToSync userWithTweetsToSync, CancellationToken ct)
2020-07-19 05:35:19 +02:00
{
var user = userWithTweetsToSync.User;
2020-08-13 00:34:01 +02:00
// Process Shared Inbox
var followersWtSharedInbox = userWithTweetsToSync.Followers
.Where(x => !string.IsNullOrWhiteSpace(x.SharedInboxRoute))
.ToList();
2021-09-11 02:47:02 +02:00
await ProcessFollowersWithSharedInboxAsync(userWithTweetsToSync.Tweets, followersWtSharedInbox, user);
2020-08-13 00:34:01 +02:00
// Process Inbox
var followerWtInbox = userWithTweetsToSync.Followers
.Where(x => string.IsNullOrWhiteSpace(x.SharedInboxRoute))
.ToList();
2021-09-11 02:47:02 +02:00
await ProcessFollowersWithInboxAsync(userWithTweetsToSync.Tweets, followerWtInbox, user);
2020-08-13 00:34:01 +02:00
return userWithTweetsToSync;
}
2021-09-11 02:47:02 +02:00
private async Task ProcessFollowersWithSharedInboxAsync(ExtractedTweet[] tweets, List<Follower> followers, SyncTwitterUser user)
2020-08-13 00:34:01 +02:00
{
var followersPerInstances = followers.GroupBy(x => x.Host);
foreach (var followersPerInstance in followersPerInstances)
{
2020-07-23 01:27:25 +02:00
try
{
2020-08-13 02:23:19 +02:00
await _sendTweetsToSharedInbox.ExecuteAsync(tweets, user, followersPerInstance.Key, followersPerInstance.ToArray());
2021-09-11 02:47:02 +02:00
2021-09-11 05:28:36 +02:00
foreach (var f in followersPerInstance)
2021-09-11 02:47:02 +02:00
await ProcessWorkingUserAsync(f);
2020-07-23 01:27:25 +02:00
}
catch (Exception e)
{
2021-01-16 06:34:09 +01:00
var follower = followersPerInstance.First();
_logger.LogError(e, "Posting to {Host}{Route} failed", follower.Host, follower.SharedInboxRoute);
2021-09-11 02:47:02 +02:00
foreach (var f in followersPerInstance)
await ProcessFailingUserAsync(f);
2020-07-23 01:27:25 +02:00
}
}
2020-08-13 00:34:01 +02:00
}
2021-09-11 02:47:02 +02:00
private async Task ProcessFollowersWithInboxAsync(ExtractedTweet[] tweets, List<Follower> followerWtInbox, SyncTwitterUser user)
2020-08-13 00:34:01 +02:00
{
foreach (var follower in followerWtInbox)
{
try
{
await _sendTweetsToInboxTask.ExecuteAsync(tweets, follower, user);
2021-09-11 02:47:02 +02:00
await ProcessWorkingUserAsync(follower);
2020-08-13 00:34:01 +02:00
}
catch (Exception e)
{
2021-01-16 06:34:09 +01:00
_logger.LogError(e, "Posting to {Host}{Route} failed", follower.Host, follower.InboxRoute);
2021-09-11 02:47:02 +02:00
await ProcessFailingUserAsync(follower);
2020-08-13 00:34:01 +02:00
}
}
2020-07-23 01:27:25 +02:00
}
2021-09-11 02:47:02 +02:00
private async Task ProcessWorkingUserAsync(Follower follower)
{
if (follower.PostingErrorCount > 0)
{
follower.PostingErrorCount = 0;
await _followersDal.UpdateFollowerAsync(follower);
}
}
private async Task ProcessFailingUserAsync(Follower follower)
{
follower.PostingErrorCount++;
if (follower.PostingErrorCount > _instanceSettings.FailingFollowerCleanUpThreshold
&& _instanceSettings.FailingFollowerCleanUpThreshold > 0
|| follower.PostingErrorCount > 2147483600)
{
await _removeFollowerAction.ProcessAsync(follower);
}
else
{
await _followersDal.UpdateFollowerAsync(follower);
}
2021-09-11 02:47:02 +02:00
}
2020-07-19 05:35:19 +02:00
}
}