2020-07-22 19:27:25 -04:00
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Linq;
|
2020-07-22 02:11:44 -04:00
|
|
|
|
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;
|
2020-07-22 02:11:44 -04:00
|
|
|
|
using BirdsiteLive.DAL.Contracts;
|
2020-07-22 19:27:25 -04:00
|
|
|
|
using BirdsiteLive.DAL.Models;
|
2020-07-22 02:11:44 -04:00
|
|
|
|
using BirdsiteLive.Domain;
|
2020-07-18 23:35:19 -04:00
|
|
|
|
using BirdsiteLive.Pipeline.Contracts;
|
|
|
|
|
using BirdsiteLive.Pipeline.Models;
|
2020-07-22 02:11:44 -04:00
|
|
|
|
using BirdsiteLive.Twitter;
|
2020-07-22 20:19:40 -04:00
|
|
|
|
using BirdsiteLive.Twitter.Models;
|
2020-07-22 19:27:25 -04:00
|
|
|
|
using Tweetinvi.Models;
|
2020-07-18 23:35:19 -04:00
|
|
|
|
|
|
|
|
|
namespace BirdsiteLive.Pipeline.Processors
|
|
|
|
|
{
|
|
|
|
|
public class SendTweetsToFollowersProcessor : ISendTweetsToFollowersProcessor
|
|
|
|
|
{
|
2020-07-22 02:11:44 -04:00
|
|
|
|
private readonly IActivityPubService _activityPubService;
|
2020-07-22 19:27:25 -04:00
|
|
|
|
private readonly IStatusService _statusService;
|
2020-07-22 02:11:44 -04:00
|
|
|
|
private readonly IFollowersDal _followersDal;
|
|
|
|
|
|
|
|
|
|
#region Ctor
|
2020-07-22 19:27:25 -04:00
|
|
|
|
public SendTweetsToFollowersProcessor(IActivityPubService activityPubService, IFollowersDal followersDal, IStatusService statusService)
|
2020-07-22 02:11:44 -04:00
|
|
|
|
{
|
|
|
|
|
_activityPubService = activityPubService;
|
|
|
|
|
_followersDal = followersDal;
|
2020-07-22 19:27:25 -04:00
|
|
|
|
_statusService = statusService;
|
2020-07-22 02:11:44 -04:00
|
|
|
|
}
|
|
|
|
|
#endregion
|
|
|
|
|
|
2020-07-22 19:27:25 -04:00
|
|
|
|
public async Task<UserWithTweetsToSync> ProcessAsync(UserWithTweetsToSync userWithTweetsToSync, CancellationToken ct)
|
2020-07-18 23:35:19 -04:00
|
|
|
|
{
|
2020-07-22 02:11:44 -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();
|
|
|
|
|
await ProcessFollowersWithSharedInbox(userWithTweetsToSync.Tweets, followersWtSharedInbox, user);
|
|
|
|
|
|
|
|
|
|
// Process Inbox
|
|
|
|
|
var followerWtInbox = userWithTweetsToSync.Followers
|
|
|
|
|
.Where(x => string.IsNullOrWhiteSpace(x.SharedInboxRoute))
|
|
|
|
|
.ToList();
|
|
|
|
|
await ProcessFollowersWithInbox(userWithTweetsToSync.Tweets, followerWtInbox, user);
|
|
|
|
|
|
|
|
|
|
return userWithTweetsToSync;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private async Task ProcessFollowersWithSharedInbox(ExtractedTweet[] tweets, List<Follower> followers, SyncTwitterUser user)
|
|
|
|
|
{
|
|
|
|
|
var followersPerInstances = followers.GroupBy(x => x.Host);
|
|
|
|
|
|
|
|
|
|
foreach (var followersPerInstance in followersPerInstances)
|
2020-07-22 02:11:44 -04:00
|
|
|
|
{
|
2020-07-22 19:27:25 -04:00
|
|
|
|
try
|
|
|
|
|
{
|
2020-08-12 18:34:01 -04:00
|
|
|
|
await ProcessInstanceFollowersWithSharedInbox(tweets, user, followersPerInstance);
|
2020-07-22 19:27:25 -04:00
|
|
|
|
}
|
|
|
|
|
catch (Exception e)
|
|
|
|
|
{
|
|
|
|
|
Console.WriteLine(e);
|
|
|
|
|
//TODO handle error
|
|
|
|
|
}
|
|
|
|
|
}
|
2020-08-12 18:34:01 -04:00
|
|
|
|
}
|
2020-07-22 19:27:25 -04:00
|
|
|
|
|
2020-08-12 18:34:01 -04:00
|
|
|
|
private async Task ProcessInstanceFollowersWithSharedInbox(ExtractedTweet[] tweets, SyncTwitterUser user,
|
|
|
|
|
IGrouping<string, Follower> 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<Follower> followerWtInbox, SyncTwitterUser user)
|
|
|
|
|
{
|
|
|
|
|
foreach (var follower in followerWtInbox)
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
await ProcessFollowerWithInboxAsync(tweets, follower, user);
|
|
|
|
|
}
|
|
|
|
|
catch (Exception e)
|
|
|
|
|
{
|
|
|
|
|
Console.WriteLine(e);
|
|
|
|
|
//TODO handle error
|
|
|
|
|
}
|
|
|
|
|
}
|
2020-07-22 19:27:25 -04:00
|
|
|
|
}
|
|
|
|
|
|
2020-08-12 18:34:01 -04:00
|
|
|
|
private async Task ProcessFollowerWithInboxAsync(IEnumerable<ExtractedTweet> tweets, Follower follower, SyncTwitterUser user)
|
2020-07-22 19:27:25 -04:00
|
|
|
|
{
|
2020-08-12 18:34:01 -04:00
|
|
|
|
var userId = user.Id;
|
2020-07-22 19:27:25 -04:00
|
|
|
|
var fromStatusId = follower.FollowingsSyncStatus[userId];
|
2020-08-12 18:34:01 -04:00
|
|
|
|
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;
|
2020-07-22 02:11:44 -04:00
|
|
|
|
|
2020-07-22 19:27:25 -04:00
|
|
|
|
var syncStatus = fromStatusId;
|
|
|
|
|
try
|
|
|
|
|
{
|
2020-07-22 02:11:44 -04:00
|
|
|
|
foreach (var tweet in tweetsToSend)
|
|
|
|
|
{
|
2020-07-22 19:27:25 -04:00
|
|
|
|
var note = _statusService.GetStatus(user.Acct, tweet);
|
2020-08-10 20:04:12 -04:00
|
|
|
|
var result = await _activityPubService.PostNewNoteActivity(note, user.Acct, tweet.Id.ToString(), follower.Host, inbox);
|
2020-07-22 19:27:25 -04:00
|
|
|
|
|
2020-08-12 18:34:01 -04:00
|
|
|
|
if (result == HttpStatusCode.Accepted)
|
2020-07-22 02:11:44 -04:00
|
|
|
|
syncStatus = tweet.Id;
|
|
|
|
|
else
|
2020-07-22 19:27:25 -04:00
|
|
|
|
throw new Exception("Posting new note activity failed");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
finally
|
|
|
|
|
{
|
|
|
|
|
if (syncStatus != fromStatusId)
|
|
|
|
|
{
|
|
|
|
|
follower.FollowingsSyncStatus[userId] = syncStatus;
|
|
|
|
|
await _followersDal.UpdateFollowerAsync(follower);
|
2020-07-22 02:11:44 -04:00
|
|
|
|
}
|
|
|
|
|
}
|
2020-07-18 23:35:19 -04:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|