added shared inbox publication

This commit is contained in:
Nicolas Constant 2020-08-12 18:34:01 -04:00
parent d5276c120e
commit afa05a72d2
No known key found for this signature in database
GPG key ID: 1E9F677FB01A5688

View file

@ -4,6 +4,7 @@ using System.Linq;
using System.Net; using System.Net;
using System.Threading; using System.Threading;
using System.Threading.Tasks; using System.Threading.Tasks;
using System.Xml;
using BirdsiteLive.DAL.Contracts; using BirdsiteLive.DAL.Contracts;
using BirdsiteLive.DAL.Models; using BirdsiteLive.DAL.Models;
using BirdsiteLive.Domain; using BirdsiteLive.Domain;
@ -33,13 +34,31 @@ namespace BirdsiteLive.Pipeline.Processors
public async Task<UserWithTweetsToSync> ProcessAsync(UserWithTweetsToSync userWithTweetsToSync, CancellationToken ct) public async Task<UserWithTweetsToSync> ProcessAsync(UserWithTweetsToSync userWithTweetsToSync, CancellationToken ct)
{ {
var user = userWithTweetsToSync.User; var user = userWithTweetsToSync.User;
var userId = user.Id;
foreach (var follower in userWithTweetsToSync.Followers) // 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)
{ {
try try
{ {
await ProcessFollowerAsync(userWithTweetsToSync.Tweets, follower, userId, user); await ProcessInstanceFollowersWithSharedInbox(tweets, user, followersPerInstance);
} }
catch (Exception e) catch (Exception e)
{ {
@ -47,17 +66,81 @@ namespace BirdsiteLive.Pipeline.Processors
//TODO handle error //TODO handle error
} }
} }
return userWithTweetsToSync;
} }
private async Task ProcessFollowerAsync(IEnumerable<ExtractedTweet> tweets, Follower follower, int userId, SyncTwitterUser user) 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
}
}
}
private async Task ProcessFollowerWithInboxAsync(IEnumerable<ExtractedTweet> tweets, Follower follower, SyncTwitterUser user)
{
var userId = user.Id;
var fromStatusId = follower.FollowingsSyncStatus[userId]; var fromStatusId = follower.FollowingsSyncStatus[userId];
var tweetsToSend = tweets.Where(x => x.Id > fromStatusId).OrderBy(x => x.Id).ToList(); var tweetsToSend = tweets
var inbox = string.IsNullOrWhiteSpace(follower.SharedInboxRoute) .Where(x => x.Id > fromStatusId)
? follower.InboxRoute .OrderBy(x => x.Id)
: follower.SharedInboxRoute; .ToList();
var inbox = follower.InboxRoute;
//var inbox = string.IsNullOrWhiteSpace(follower.SharedInboxRoute)
// ? follower.InboxRoute
// : follower.SharedInboxRoute;
var syncStatus = fromStatusId; var syncStatus = fromStatusId;
try try
@ -67,7 +150,7 @@ namespace BirdsiteLive.Pipeline.Processors
var note = _statusService.GetStatus(user.Acct, tweet); var note = _statusService.GetStatus(user.Acct, tweet);
var result = await _activityPubService.PostNewNoteActivity(note, user.Acct, tweet.Id.ToString(), follower.Host, inbox); var result = await _activityPubService.PostNewNoteActivity(note, user.Acct, tweet.Id.ToString(), follower.Host, inbox);
if (result == HttpStatusCode.Accepted) if (result == HttpStatusCode.Accepted)
syncStatus = tweet.Id; syncStatus = tweet.Id;
else else
throw new Exception("Posting new note activity failed"); throw new Exception("Posting new note activity failed");