cloutier--bird.makeup/src/BirdsiteLive.Pipeline/Processors/SubTasks/SendTweetsToSharedInboxTask.cs

100 lines
3.9 KiB
C#
Raw Normal View History

using System;
using System.Linq;
using System.Net;
using System.Threading.Tasks;
2021-01-22 18:31:30 -05:00
using BirdsiteLive.Common.Settings;
using BirdsiteLive.DAL.Contracts;
using BirdsiteLive.DAL.Models;
using BirdsiteLive.Domain;
using BirdsiteLive.Twitter.Models;
2021-01-19 22:15:19 -05:00
using Microsoft.Extensions.Logging;
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);
}
public class SendTweetsToSharedInboxTask : ISendTweetsToSharedInboxTask
{
private readonly IStatusService _statusService;
private readonly IActivityPubService _activityPubService;
private readonly IFollowersDal _followersDal;
2021-01-22 18:31:30 -05:00
private readonly InstanceSettings _settings;
2021-01-19 22:15:19 -05:00
private readonly ILogger<SendTweetsToSharedInboxTask> _logger;
2021-01-16 00:34:09 -05:00
#region Ctor
2021-01-22 18:31:30 -05:00
public SendTweetsToSharedInboxTask(IActivityPubService activityPubService, IStatusService statusService, IFollowersDal followersDal, InstanceSettings settings, ILogger<SendTweetsToSharedInboxTask> logger)
{
_activityPubService = activityPubService;
_statusService = statusService;
_followersDal = followersDal;
2021-01-22 18:31:30 -05:00
_settings = settings;
2021-01-19 22:15:19 -05:00
_logger = logger;
}
#endregion
2020-08-12 20:23:19 -04:00
public async Task ExecuteAsync(ExtractedTweet[] tweets, SyncTwitterUser user, string host, Follower[] followersPerInstance)
{
var userId = user.Id;
2020-08-12 20:23:19 -04:00
var inbox = followersPerInstance.First().SharedInboxRoute;
2020-08-12 20:23:19 -04:00
var fromStatusId = followersPerInstance
.Max(x => x.FollowingsSyncStatus[userId]);
var tweetsToSend = tweets
.Where(x => x.Id > fromStatusId)
.OrderBy(x => x.Id)
.ToList();
2022-05-08 13:51:35 -04:00
_logger.LogInformation("After filtering, there were " + tweetsToSend.Count() + " tweets left to send");
var syncStatus = fromStatusId;
try
{
foreach (var tweet in tweetsToSend)
{
2021-01-19 22:15:19 -05:00
try
{
2022-05-09 20:31:18 -04:00
if (tweet.IsRetweet)
{
var note = _statusService.GetStatus(user.Acct, tweet);
await _activityPubService.PostNewActivity(note, user.Acct, "Announce", tweet.Id.ToString(), host, inbox);
}
else if (!tweet.IsReply ||
2021-01-22 18:31:30 -05:00
tweet.IsReply && tweet.IsThread ||
_settings.PublishReplies)
{
var note = _statusService.GetStatus(user.Acct, tweet);
2022-05-09 20:31:18 -04:00
await _activityPubService.PostNewActivity(note, user.Acct, "Create", tweet.Id.ToString(), host, inbox);
2021-01-22 18:31:30 -05:00
}
2021-01-19 22:15:19 -05:00
}
catch (ArgumentException e)
{
if (e.Message.Contains("Invalid pattern") && e.Message.Contains("at offset")) //Regex exception
{
_logger.LogError(e, "Can't parse {MessageContent} from Tweet {Id}", tweet.MessageContent, tweet.Id);
}
else
{
throw;
}
}
2021-01-22 18:31:30 -05:00
2021-01-16 00:34:09 -05:00
syncStatus = tweet.Id;
}
}
finally
{
if (syncStatus != fromStatusId)
{
2020-08-12 20:23:19 -04:00
foreach (var f in followersPerInstance)
{
f.FollowingsSyncStatus[userId] = syncStatus;
await _followersDal.UpdateFollowerAsync(f);
}
}
}
}
}
}