You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

60 lines
2.3 KiB

  1. using System;
  2. using System.Linq;
  3. using System.Threading;
  4. using System.Threading.Tasks;
  5. using BirdsiteLive.DAL.Contracts;
  6. using BirdsiteLive.Pipeline.Contracts;
  7. using BirdsiteLive.Pipeline.Models;
  8. using Microsoft.Extensions.Logging;
  9. namespace BirdsiteLive.Pipeline.Processors
  10. {
  11. public class SaveProgressionProcessor : ISaveProgressionProcessor
  12. {
  13. private readonly ITwitterUserDal _twitterUserDal;
  14. private readonly ILogger<SaveProgressionProcessor> _logger;
  15. #region Ctor
  16. public SaveProgressionProcessor(ITwitterUserDal twitterUserDal, ILogger<SaveProgressionProcessor> logger)
  17. {
  18. _twitterUserDal = twitterUserDal;
  19. _logger = logger;
  20. }
  21. #endregion
  22. public async Task ProcessAsync(UserWithDataToSync userWithTweetsToSync, CancellationToken ct)
  23. {
  24. try
  25. {
  26. if (userWithTweetsToSync.Tweets.Length == 0)
  27. {
  28. _logger.LogWarning("No tweets synchronized");
  29. return;
  30. }
  31. if(userWithTweetsToSync.Followers.Length == 0)
  32. {
  33. _logger.LogWarning("No Followers found for {User}", userWithTweetsToSync.User.Acct);
  34. return;
  35. }
  36. var userId = userWithTweetsToSync.User.Id;
  37. var followingSyncStatuses = userWithTweetsToSync.Followers.Select(x => x.FollowingsSyncStatus[userId]).ToList();
  38. if (followingSyncStatuses.Count == 0)
  39. {
  40. _logger.LogWarning("No Followers sync found for {User}, Id: {UserId}", userWithTweetsToSync.User.Acct, userId);
  41. return;
  42. }
  43. var lastPostedTweet = userWithTweetsToSync.Tweets.Select(x => x.Id).Max();
  44. var minimumSync = followingSyncStatuses.Min();
  45. var now = DateTime.UtcNow;
  46. await _twitterUserDal.UpdateTwitterUserAsync(userId, lastPostedTweet, minimumSync, userWithTweetsToSync.User.FetchingErrorCount, now);
  47. }
  48. catch (Exception e)
  49. {
  50. _logger.LogError(e, "SaveProgressionProcessor.ProcessAsync() Exception");
  51. throw;
  52. }
  53. }
  54. }
  55. }