cloutier--bird.makeup/src/BirdsiteLive.Pipeline/Processors/SendTweetsToFollowersProcessor.cs

135 lines
5.5 KiB
C#
Raw Normal View History

2020-07-22 19:27:25 -04:00
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Threading;
2020-07-18 23:35:19 -04:00
using System.Threading.Tasks;
2020-08-12 18:34:01 -04:00
using System.Xml;
using BirdsiteLive.Common.Settings;
using BirdsiteLive.DAL.Contracts;
2020-07-22 19:27:25 -04:00
using BirdsiteLive.DAL.Models;
using BirdsiteLive.Domain;
using BirdsiteLive.Moderation.Actions;
2020-07-18 23:35:19 -04:00
using BirdsiteLive.Pipeline.Contracts;
using BirdsiteLive.Pipeline.Models;
using BirdsiteLive.Pipeline.Processors.SubTasks;
using BirdsiteLive.Twitter;
2020-07-22 20:19:40 -04:00
using BirdsiteLive.Twitter.Models;
2021-01-16 00:34:09 -05:00
using Microsoft.Extensions.Logging;
2020-07-18 23:35:19 -04:00
namespace BirdsiteLive.Pipeline.Processors
{
public class SendTweetsToFollowersProcessor : ISendTweetsToFollowersProcessor
{
private readonly ISendTweetsToInboxTask _sendTweetsToInboxTask;
private readonly ISendTweetsToSharedInboxTask _sendTweetsToSharedInbox;
2021-09-10 20:47:02 -04:00
private readonly IFollowersDal _followersDal;
private readonly InstanceSettings _instanceSettings;
2021-01-16 00:34:09 -05: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 00:34:09 -05:00
_logger = logger;
_instanceSettings = instanceSettings;
_removeFollowerAction = removeFollowerAction;
2021-09-10 20:47:02 -04:00
_followersDal = followersDal;
}
#endregion
public async Task<UserWithDataToSync> ProcessAsync(UserWithDataToSync userWithTweetsToSync, CancellationToken ct)
2020-07-18 23:35:19 -04:00
{
var user = userWithTweetsToSync.User;
2020-08-12 18:34:01 -04:00
// Process Shared Inbox
var followersWtSharedInbox = userWithTweetsToSync.Followers
.Where(x => !string.IsNullOrWhiteSpace(x.SharedInboxRoute))
.ToList();
2021-09-10 20:47:02 -04:00
await ProcessFollowersWithSharedInboxAsync(userWithTweetsToSync.Tweets, followersWtSharedInbox, user);
2020-08-12 18:34:01 -04:00
// Process Inbox
var followerWtInbox = userWithTweetsToSync.Followers
.Where(x => string.IsNullOrWhiteSpace(x.SharedInboxRoute))
.ToList();
2021-09-10 20:47:02 -04:00
await ProcessFollowersWithInboxAsync(userWithTweetsToSync.Tweets, followerWtInbox, user);
2020-08-12 18:34:01 -04:00
return userWithTweetsToSync;
}
2021-09-10 20:47:02 -04:00
private async Task ProcessFollowersWithSharedInboxAsync(ExtractedTweet[] tweets, List<Follower> followers, SyncTwitterUser user)
2020-08-12 18:34:01 -04:00
{
var followersPerInstances = followers.GroupBy(x => x.Host);
2022-12-28 10:30:58 -05:00
List<Task> todo = new List<Task>();
2020-08-12 18:34:01 -04:00
foreach (var followersPerInstance in followersPerInstances)
{
2022-12-28 10:30:58 -05:00
var t = Task.Run( async () =>
2020-07-22 19:27:25 -04:00
{
2022-12-28 10:30:58 -05:00
try
{
_logger.LogInformation("Sending " + tweets.Length + " tweets from user " + user.Acct + " to instance " + followersPerInstance.Key);
await _sendTweetsToSharedInbox.ExecuteAsync(tweets, user, followersPerInstance.Key, followersPerInstance.ToArray());
2021-09-10 20:47:02 -04:00
2022-12-28 10:30:58 -05:00
foreach (var f in followersPerInstance)
await ProcessWorkingUserAsync(f);
}
catch (Exception e)
{
var follower = followersPerInstance.First();
_logger.LogError(e, "Posting to {Host}{Route} failed", follower.Host, follower.SharedInboxRoute);
2021-09-10 20:47:02 -04:00
2022-12-28 10:30:58 -05:00
foreach (var f in followersPerInstance)
await ProcessFailingUserAsync(f);
}
});
todo.Add(t);
2020-07-22 19:27:25 -04:00
}
2022-12-28 10:30:58 -05:00
await Task.WhenAll(todo);
2020-08-12 18:34:01 -04:00
}
2021-09-10 20:47:02 -04:00
private async Task ProcessFollowersWithInboxAsync(ExtractedTweet[] tweets, List<Follower> followerWtInbox, SyncTwitterUser user)
2020-08-12 18:34:01 -04:00
{
foreach (var follower in followerWtInbox)
{
try
{
await _sendTweetsToInboxTask.ExecuteAsync(tweets, follower, user);
2021-09-10 20:47:02 -04:00
await ProcessWorkingUserAsync(follower);
2020-08-12 18:34:01 -04:00
}
catch (Exception e)
{
2021-01-16 00:34:09 -05:00
_logger.LogError(e, "Posting to {Host}{Route} failed", follower.Host, follower.InboxRoute);
2021-09-10 20:47:02 -04:00
await ProcessFailingUserAsync(follower);
2020-08-12 18:34:01 -04:00
}
}
2020-07-22 19:27:25 -04:00
}
2021-09-10 20:47:02 -04: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-10 20:47:02 -04:00
}
2020-07-18 23:35:19 -04:00
}
}