diff --git a/src/BirdsiteLive.Pipeline/BirdsiteLive.Pipeline.csproj b/src/BirdsiteLive.Pipeline/BirdsiteLive.Pipeline.csproj index 89f6d5d..6b8b510 100644 --- a/src/BirdsiteLive.Pipeline/BirdsiteLive.Pipeline.csproj +++ b/src/BirdsiteLive.Pipeline/BirdsiteLive.Pipeline.csproj @@ -1,4 +1,4 @@ - + netstandard2.0 diff --git a/src/BirdsiteLive.Pipeline/Processors/SendTweetsToFollowersProcessor.cs b/src/BirdsiteLive.Pipeline/Processors/SendTweetsToFollowersProcessor.cs index 8c5f6b9..29bfb12 100644 --- a/src/BirdsiteLive.Pipeline/Processors/SendTweetsToFollowersProcessor.cs +++ b/src/BirdsiteLive.Pipeline/Processors/SendTweetsToFollowersProcessor.cs @@ -10,6 +10,7 @@ using BirdsiteLive.DAL.Models; using BirdsiteLive.Domain; using BirdsiteLive.Pipeline.Contracts; using BirdsiteLive.Pipeline.Models; +using BirdsiteLive.Pipeline.Processors.SubTasks; using BirdsiteLive.Twitter; using BirdsiteLive.Twitter.Models; using Tweetinvi.Models; @@ -18,16 +19,14 @@ namespace BirdsiteLive.Pipeline.Processors { public class SendTweetsToFollowersProcessor : ISendTweetsToFollowersProcessor { - private readonly IActivityPubService _activityPubService; - private readonly IStatusService _statusService; - private readonly IFollowersDal _followersDal; + private readonly ISendTweetsToInboxTask _sendTweetsToInboxTask; + private readonly ISendTweetsToSharedInboxTask _sendTweetsToSharedInbox; #region Ctor - public SendTweetsToFollowersProcessor(IActivityPubService activityPubService, IFollowersDal followersDal, IStatusService statusService) + public SendTweetsToFollowersProcessor(ISendTweetsToInboxTask sendTweetsToInboxTask, ISendTweetsToSharedInboxTask sendTweetsToSharedInbox) { - _activityPubService = activityPubService; - _followersDal = followersDal; - _statusService = statusService; + _sendTweetsToInboxTask = sendTweetsToInboxTask; + _sendTweetsToSharedInbox = sendTweetsToSharedInbox; } #endregion @@ -58,7 +57,7 @@ namespace BirdsiteLive.Pipeline.Processors { try { - await ProcessInstanceFollowersWithSharedInbox(tweets, user, followersPerInstance); + await _sendTweetsToSharedInbox.ExecuteAsync(tweets, user, followersPerInstance); } catch (Exception e) { @@ -67,58 +66,14 @@ namespace BirdsiteLive.Pipeline.Processors } } } - - private async Task ProcessInstanceFollowersWithSharedInbox(ExtractedTweet[] tweets, SyncTwitterUser user, - IGrouping followersPerInstance) - { - var userId = user.Id; - var host = followersPerInstance.Key; - var groupedFollowers = followersPerInstance.ToList(); - var inbox = groupedFollowers.First().SharedInboxRoute; - - var fromStatusId = groupedFollowers - .Max(x => x.FollowingsSyncStatus[userId]); - - var tweetsToSend = tweets - .Where(x => x.Id > fromStatusId) - .OrderBy(x => x.Id) - .ToList(); - - var syncStatus = fromStatusId; - try - { - foreach (var tweet in tweetsToSend) - { - var note = _statusService.GetStatus(user.Acct, tweet); - var result = - await _activityPubService.PostNewNoteActivity(note, user.Acct, tweet.Id.ToString(), host, inbox); - - if (result == HttpStatusCode.Accepted) - syncStatus = tweet.Id; - else - throw new Exception("Posting new note activity failed"); - } - } - finally - { - if (syncStatus != fromStatusId) - { - foreach (var f in groupedFollowers) - { - f.FollowingsSyncStatus[userId] = syncStatus; - await _followersDal.UpdateFollowerAsync(f); - } - } - } - } - + private async Task ProcessFollowersWithInbox(ExtractedTweet[] tweets, List followerWtInbox, SyncTwitterUser user) { foreach (var follower in followerWtInbox) { try { - await ProcessFollowerWithInboxAsync(tweets, follower, user); + await _sendTweetsToInboxTask.ExecuteAsync(tweets, follower, user); } catch (Exception e) { @@ -127,43 +82,5 @@ namespace BirdsiteLive.Pipeline.Processors } } } - - private async Task ProcessFollowerWithInboxAsync(IEnumerable tweets, Follower follower, SyncTwitterUser user) - { - var userId = user.Id; - var fromStatusId = follower.FollowingsSyncStatus[userId]; - var tweetsToSend = tweets - .Where(x => x.Id > fromStatusId) - .OrderBy(x => x.Id) - .ToList(); - - var inbox = follower.InboxRoute; - //var inbox = string.IsNullOrWhiteSpace(follower.SharedInboxRoute) - // ? follower.InboxRoute - // : follower.SharedInboxRoute; - - var syncStatus = fromStatusId; - try - { - foreach (var tweet in tweetsToSend) - { - var note = _statusService.GetStatus(user.Acct, tweet); - var result = await _activityPubService.PostNewNoteActivity(note, user.Acct, tweet.Id.ToString(), follower.Host, inbox); - - if (result == HttpStatusCode.Accepted) - syncStatus = tweet.Id; - else - throw new Exception("Posting new note activity failed"); - } - } - finally - { - if (syncStatus != fromStatusId) - { - follower.FollowingsSyncStatus[userId] = syncStatus; - await _followersDal.UpdateFollowerAsync(follower); - } - } - } } } \ No newline at end of file diff --git a/src/BirdsiteLive.Pipeline/Processors/SubTasks/SendTweetsToInboxTask.cs b/src/BirdsiteLive.Pipeline/Processors/SubTasks/SendTweetsToInboxTask.cs new file mode 100644 index 0000000..3624f45 --- /dev/null +++ b/src/BirdsiteLive.Pipeline/Processors/SubTasks/SendTweetsToInboxTask.cs @@ -0,0 +1,71 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Net; +using System.Threading.Tasks; +using BirdsiteLive.DAL.Contracts; +using BirdsiteLive.DAL.Models; +using BirdsiteLive.Domain; +using BirdsiteLive.Twitter.Models; + +namespace BirdsiteLive.Pipeline.Processors.SubTasks +{ + public interface ISendTweetsToInboxTask + { + Task ExecuteAsync(IEnumerable tweets, Follower follower, SyncTwitterUser user); + } + + public class SendTweetsToInboxTask : ISendTweetsToInboxTask + { + private readonly IActivityPubService _activityPubService; + private readonly IStatusService _statusService; + private readonly IFollowersDal _followersDal; + + #region Ctor + public SendTweetsToInboxTask(IActivityPubService activityPubService, IStatusService statusService, IFollowersDal followersDal) + { + _activityPubService = activityPubService; + _statusService = statusService; + _followersDal = followersDal; + } + #endregion + + public async Task ExecuteAsync(IEnumerable tweets, Follower follower, SyncTwitterUser user) + { + var userId = user.Id; + var fromStatusId = follower.FollowingsSyncStatus[userId]; + var tweetsToSend = tweets + .Where(x => x.Id > fromStatusId) + .OrderBy(x => x.Id) + .ToList(); + + var inbox = follower.InboxRoute; + //var inbox = string.IsNullOrWhiteSpace(follower.SharedInboxRoute) + // ? follower.InboxRoute + // : follower.SharedInboxRoute; + + var syncStatus = fromStatusId; + try + { + foreach (var tweet in tweetsToSend) + { + var note = _statusService.GetStatus(user.Acct, tweet); + var result = await _activityPubService.PostNewNoteActivity(note, user.Acct, tweet.Id.ToString(), follower.Host, inbox); + + if (result == HttpStatusCode.Accepted) + syncStatus = tweet.Id; + else + throw new Exception("Posting new note activity failed"); + } + } + finally + { + if (syncStatus != fromStatusId) + { + follower.FollowingsSyncStatus[userId] = syncStatus; + await _followersDal.UpdateFollowerAsync(follower); + } + } + } + } +} \ No newline at end of file diff --git a/src/BirdsiteLive.Pipeline/Processors/SubTasks/SendTweetsToSharedInboxTask.cs b/src/BirdsiteLive.Pipeline/Processors/SubTasks/SendTweetsToSharedInboxTask.cs new file mode 100644 index 0000000..0aeafd6 --- /dev/null +++ b/src/BirdsiteLive.Pipeline/Processors/SubTasks/SendTweetsToSharedInboxTask.cs @@ -0,0 +1,75 @@ +using System; +using System.Linq; +using System.Net; +using System.Threading.Tasks; +using BirdsiteLive.DAL.Contracts; +using BirdsiteLive.DAL.Models; +using BirdsiteLive.Domain; +using BirdsiteLive.Twitter.Models; + +namespace BirdsiteLive.Pipeline.Processors.SubTasks +{ + public interface ISendTweetsToSharedInboxTask + { + Task ExecuteAsync(ExtractedTweet[] tweets, SyncTwitterUser user, IGrouping followersPerInstance); + } + + public class SendTweetsToSharedInboxTask : ISendTweetsToSharedInboxTask + { + private readonly IStatusService _statusService; + private readonly IActivityPubService _activityPubService; + private readonly IFollowersDal _followersDal; + + #region Ctor + public SendTweetsToSharedInboxTask(IActivityPubService activityPubService, IStatusService statusService, IFollowersDal followersDal) + { + _activityPubService = activityPubService; + _statusService = statusService; + _followersDal = followersDal; + } + #endregion + + public async Task ExecuteAsync(ExtractedTweet[] tweets, SyncTwitterUser user, IGrouping followersPerInstance) + { + var userId = user.Id; + var host = followersPerInstance.Key; + var groupedFollowers = followersPerInstance.ToList(); + var inbox = groupedFollowers.First().SharedInboxRoute; + + var fromStatusId = groupedFollowers + .Max(x => x.FollowingsSyncStatus[userId]); + + var tweetsToSend = tweets + .Where(x => x.Id > fromStatusId) + .OrderBy(x => x.Id) + .ToList(); + + var syncStatus = fromStatusId; + try + { + foreach (var tweet in tweetsToSend) + { + var note = _statusService.GetStatus(user.Acct, tweet); + var result = + await _activityPubService.PostNewNoteActivity(note, user.Acct, tweet.Id.ToString(), host, inbox); + + if (result == HttpStatusCode.Accepted) + syncStatus = tweet.Id; + else + throw new Exception("Posting new note activity failed"); + } + } + finally + { + if (syncStatus != fromStatusId) + { + foreach (var f in groupedFollowers) + { + f.FollowingsSyncStatus[userId] = syncStatus; + await _followersDal.UpdateFollowerAsync(f); + } + } + } + } + } +} \ No newline at end of file