2020-08-12 19:05:01 -04:00
|
|
|
|
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
|
|
|
|
|
{
|
2020-08-12 20:23:19 -04:00
|
|
|
|
Task ExecuteAsync(ExtractedTweet[] tweets, SyncTwitterUser user, string host, Follower[] followersPerInstance);
|
2020-08-12 19:05:01 -04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
2020-08-12 20:23:19 -04:00
|
|
|
|
public async Task ExecuteAsync(ExtractedTweet[] tweets, SyncTwitterUser user, string host, Follower[] followersPerInstance)
|
2020-08-12 19:05:01 -04:00
|
|
|
|
{
|
|
|
|
|
var userId = user.Id;
|
2020-08-12 20:23:19 -04:00
|
|
|
|
var inbox = followersPerInstance.First().SharedInboxRoute;
|
2020-08-12 19:05:01 -04:00
|
|
|
|
|
2020-08-12 20:23:19 -04:00
|
|
|
|
var fromStatusId = followersPerInstance
|
2020-08-12 19:05:01 -04:00
|
|
|
|
.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);
|
|
|
|
|
|
2021-01-07 19:37:09 -05:00
|
|
|
|
if (result == HttpStatusCode.Accepted || result == HttpStatusCode.OK)
|
2020-08-12 19:05:01 -04:00
|
|
|
|
syncStatus = tweet.Id;
|
|
|
|
|
else
|
|
|
|
|
throw new Exception("Posting new note activity failed");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
finally
|
|
|
|
|
{
|
|
|
|
|
if (syncStatus != fromStatusId)
|
|
|
|
|
{
|
2020-08-12 20:23:19 -04:00
|
|
|
|
foreach (var f in followersPerInstance)
|
2020-08-12 19:05:01 -04:00
|
|
|
|
{
|
|
|
|
|
f.FollowingsSyncStatus[userId] = syncStatus;
|
|
|
|
|
await _followersDal.UpdateFollowerAsync(f);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|