2020-07-22 02:11:44 -04:00
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Net;
|
|
|
|
|
using System.Threading;
|
2020-07-18 23:35:19 -04:00
|
|
|
|
using System.Threading.Tasks;
|
2020-07-22 02:11:44 -04:00
|
|
|
|
using BirdsiteLive.DAL.Contracts;
|
|
|
|
|
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-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;
|
|
|
|
|
private readonly IUserService _userService;
|
|
|
|
|
private readonly IFollowersDal _followersDal;
|
|
|
|
|
private readonly ITwitterUserDal _twitterUserDal;
|
|
|
|
|
|
|
|
|
|
#region Ctor
|
|
|
|
|
public SendTweetsToFollowersProcessor(IActivityPubService activityPubService, IUserService userService, IFollowersDal followersDal, ITwitterUserDal twitterUserDal)
|
|
|
|
|
{
|
|
|
|
|
_activityPubService = activityPubService;
|
|
|
|
|
_userService = userService;
|
|
|
|
|
_followersDal = followersDal;
|
|
|
|
|
_twitterUserDal = twitterUserDal;
|
|
|
|
|
}
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
|
|
public async Task 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;
|
|
|
|
|
var userId = user.Id;
|
|
|
|
|
|
|
|
|
|
foreach (var follower in userWithTweetsToSync.Followers)
|
|
|
|
|
{
|
|
|
|
|
var fromStatusId = follower.FollowingsSyncStatus[userId];
|
|
|
|
|
var tweetsToSend = userWithTweetsToSync.Tweets.Where(x => x.Id > fromStatusId).OrderBy(x => x.Id).ToList();
|
|
|
|
|
|
|
|
|
|
var syncStatus = fromStatusId;
|
|
|
|
|
foreach (var tweet in tweetsToSend)
|
|
|
|
|
{
|
|
|
|
|
var note = _userService.GetStatus(user.Acct, tweet);
|
|
|
|
|
var result = await _activityPubService.PostNewNoteActivity(note, user.Acct, tweet.Id.ToString(), follower.Host, follower.InboxUrl);
|
|
|
|
|
if (result == HttpStatusCode.Accepted)
|
|
|
|
|
syncStatus = tweet.Id;
|
|
|
|
|
else
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
follower.FollowingsSyncStatus[userId] = syncStatus;
|
|
|
|
|
await _followersDal.UpdateFollowerAsync(follower);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var lastPostedTweet = userWithTweetsToSync.Tweets.Select(x => x.Id).Max();
|
|
|
|
|
var minimumSync = userWithTweetsToSync.Followers.Select(x => x.FollowingsSyncStatus[userId]).Min();
|
|
|
|
|
await _twitterUserDal.UpdateTwitterUserAsync(userId, lastPostedTweet, minimumSync);
|
2020-07-18 23:35:19 -04:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|