2020-08-12 19:05:01 -04:00
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Net;
|
|
|
|
|
using System.Threading.Tasks;
|
2021-01-22 18:31:30 -05:00
|
|
|
|
using BirdsiteLive.Common.Settings;
|
2020-08-12 19:05:01 -04:00
|
|
|
|
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;
|
2020-08-12 19:05:01 -04:00
|
|
|
|
|
|
|
|
|
namespace BirdsiteLive.Pipeline.Processors.SubTasks
|
|
|
|
|
{
|
|
|
|
|
public interface ISendTweetsToInboxTask
|
|
|
|
|
{
|
|
|
|
|
Task ExecuteAsync(IEnumerable<ExtractedTweet> tweets, Follower follower, SyncTwitterUser user);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public class SendTweetsToInboxTask : ISendTweetsToInboxTask
|
|
|
|
|
{
|
|
|
|
|
private readonly IActivityPubService _activityPubService;
|
|
|
|
|
private readonly IStatusService _statusService;
|
|
|
|
|
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<SendTweetsToInboxTask> _logger;
|
|
|
|
|
|
2020-08-12 19:05:01 -04:00
|
|
|
|
|
|
|
|
|
#region Ctor
|
2021-01-22 18:31:30 -05:00
|
|
|
|
public SendTweetsToInboxTask(IActivityPubService activityPubService, IStatusService statusService, IFollowersDal followersDal, InstanceSettings settings, ILogger<SendTweetsToInboxTask> logger)
|
2020-08-12 19:05:01 -04:00
|
|
|
|
{
|
|
|
|
|
_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;
|
2020-08-12 19:05:01 -04:00
|
|
|
|
}
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
|
|
public async Task ExecuteAsync(IEnumerable<ExtractedTweet> tweets, Follower follower, SyncTwitterUser user)
|
|
|
|
|
{
|
|
|
|
|
var userId = user.Id;
|
|
|
|
|
var fromStatusId = follower.FollowingsSyncStatus[userId];
|
|
|
|
|
var tweetsToSend = tweets
|
|
|
|
|
.Where(x => x.Id > fromStatusId)
|
|
|
|
|
.OrderBy(x => x.Id)
|
|
|
|
|
.ToList();
|
|
|
|
|
|
|
|
|
|
var inbox = follower.InboxRoute;
|
|
|
|
|
|
|
|
|
|
var syncStatus = fromStatusId;
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
foreach (var tweet in tweetsToSend)
|
|
|
|
|
{
|
2021-01-19 22:15:19 -05:00
|
|
|
|
try
|
|
|
|
|
{
|
2021-01-22 18:31:30 -05:00
|
|
|
|
if (!tweet.IsReply ||
|
|
|
|
|
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(), follower.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-16 00:34:09 -05:00
|
|
|
|
syncStatus = tweet.Id;
|
2020-08-12 19:05:01 -04:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
finally
|
|
|
|
|
{
|
|
|
|
|
if (syncStatus != fromStatusId)
|
|
|
|
|
{
|
|
|
|
|
follower.FollowingsSyncStatus[userId] = syncStatus;
|
|
|
|
|
await _followersDal.UpdateFollowerAsync(follower);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|