2021-01-22 21:23:27 -05:00
|
|
|
|
using System;
|
|
|
|
|
using System.Linq;
|
2020-07-22 19:27:25 -04:00
|
|
|
|
using System.Threading;
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
using BirdsiteLive.DAL.Contracts;
|
|
|
|
|
using BirdsiteLive.Pipeline.Contracts;
|
|
|
|
|
using BirdsiteLive.Pipeline.Models;
|
2021-02-15 18:54:52 -05:00
|
|
|
|
using Microsoft.Extensions.Logging;
|
2020-07-22 19:27:25 -04:00
|
|
|
|
|
|
|
|
|
namespace BirdsiteLive.Pipeline.Processors
|
|
|
|
|
{
|
|
|
|
|
public class SaveProgressionProcessor : ISaveProgressionProcessor
|
|
|
|
|
{
|
|
|
|
|
private readonly ITwitterUserDal _twitterUserDal;
|
2021-02-15 18:54:52 -05:00
|
|
|
|
private readonly ILogger<SaveProgressionProcessor> _logger;
|
|
|
|
|
|
2020-07-22 19:27:25 -04:00
|
|
|
|
#region Ctor
|
2021-02-15 18:54:52 -05:00
|
|
|
|
public SaveProgressionProcessor(ITwitterUserDal twitterUserDal, ILogger<SaveProgressionProcessor> logger)
|
2020-07-22 19:27:25 -04:00
|
|
|
|
{
|
|
|
|
|
_twitterUserDal = twitterUserDal;
|
2021-02-15 18:54:52 -05:00
|
|
|
|
_logger = logger;
|
2020-07-22 19:27:25 -04:00
|
|
|
|
}
|
|
|
|
|
#endregion
|
|
|
|
|
|
2021-09-05 13:58:33 -04:00
|
|
|
|
public async Task ProcessAsync(UserWithDataToSync userWithTweetsToSync, CancellationToken ct)
|
2020-07-22 19:27:25 -04:00
|
|
|
|
{
|
2021-02-15 18:54:52 -05:00
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
if (userWithTweetsToSync.Tweets.Length == 0)
|
|
|
|
|
{
|
|
|
|
|
_logger.LogWarning("No tweets synchronized");
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
if(userWithTweetsToSync.Followers.Length == 0)
|
|
|
|
|
{
|
|
|
|
|
_logger.LogWarning("No Followers found for {User}", userWithTweetsToSync.User.Acct);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var userId = userWithTweetsToSync.User.Id;
|
|
|
|
|
var followingSyncStatuses = userWithTweetsToSync.Followers.Select(x => x.FollowingsSyncStatus[userId]).ToList();
|
|
|
|
|
|
|
|
|
|
if (followingSyncStatuses.Count == 0)
|
|
|
|
|
{
|
|
|
|
|
_logger.LogWarning("No Followers sync found for {User}, Id: {UserId}", userWithTweetsToSync.User.Acct, userId);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var lastPostedTweet = userWithTweetsToSync.Tweets.Select(x => x.Id).Max();
|
|
|
|
|
var minimumSync = followingSyncStatuses.Min();
|
|
|
|
|
var now = DateTime.UtcNow;
|
2021-09-05 13:58:33 -04:00
|
|
|
|
await _twitterUserDal.UpdateTwitterUserAsync(userId, lastPostedTweet, minimumSync, userWithTweetsToSync.User.FetchingErrorCount, now);
|
2021-02-15 18:54:52 -05:00
|
|
|
|
}
|
|
|
|
|
catch (Exception e)
|
|
|
|
|
{
|
|
|
|
|
_logger.LogError(e, "SaveProgressionProcessor.ProcessAsync() Exception");
|
|
|
|
|
throw;
|
|
|
|
|
}
|
2020-07-22 19:27:25 -04:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|