diff --git a/src/BirdsiteLive.Domain/ActivityPubService.cs b/src/BirdsiteLive.Domain/ActivityPubService.cs index c402790..e18193e 100644 --- a/src/BirdsiteLive.Domain/ActivityPubService.cs +++ b/src/BirdsiteLive.Domain/ActivityPubService.cs @@ -18,7 +18,7 @@ namespace BirdsiteLive.Domain { Task GetUser(string objectId); Task PostDataAsync(T data, string targetHost, string actorUrl, string inbox = null); - Task PostNewActivity(Note note, string username, string activityType, string noteId, string targetHost, + Task PostNewActivity(ActivityCreateNote note, string username, string noteId, string targetHost, string targetInbox); } @@ -57,35 +57,11 @@ namespace BirdsiteLive.Domain return actor; } - public async Task PostNewActivity(Note note, string username, string activityType, string noteId, string targetHost, string targetInbox) + public async Task PostNewActivity(ActivityCreateNote noteActivity, string username, string noteId, string targetHost, string targetInbox) { try { var actor = UrlFactory.GetActorUrl(_instanceSettings.Domain, username); - String noteUri; - if (activityType == "Create") - { - noteUri = UrlFactory.GetNoteUrl(_instanceSettings.Domain, username, noteId); - } else - { - noteUri = UrlFactory.GetNoteUrl(_instanceSettings.Domain, username, note.announceId); - } - - var now = DateTime.UtcNow; - var nowString = now.ToString("s") + "Z"; - - var noteActivity = new ActivityCreateNote() - { - context = "https://www.w3.org/ns/activitystreams", - id = $"{noteUri}/activity", - type = activityType, - actor = actor, - published = nowString, - - to = new[] {$"{actor}/followers"}, - cc = note.cc, - apObject = note - }; await PostDataAsync(noteActivity, targetHost, actor, targetInbox); } diff --git a/src/BirdsiteLive.Domain/Repository/PublicationRepository.cs b/src/BirdsiteLive.Domain/Repository/PublicationRepository.cs deleted file mode 100644 index 042aeb2..0000000 --- a/src/BirdsiteLive.Domain/Repository/PublicationRepository.cs +++ /dev/null @@ -1,40 +0,0 @@ -using System.Linq; -using BirdsiteLive.Common.Settings; -using BirdsiteLive.Domain.Tools; - -namespace BirdsiteLive.Domain.Repository -{ - public interface IPublicationRepository - { - bool IsUnlisted(string twitterAcct); - bool IsSensitive(string twitterAcct); - } - - public class PublicationRepository : IPublicationRepository - { - private readonly string[] _unlistedAccounts; - private readonly string[] _sensitiveAccounts; - - #region Ctor - public PublicationRepository(InstanceSettings settings) - { - _unlistedAccounts = PatternsParser.Parse(settings.UnlistedTwitterAccounts); - _sensitiveAccounts = PatternsParser.Parse(settings.SensitiveTwitterAccounts); - } - #endregion - - public bool IsUnlisted(string twitterAcct) - { - if (_unlistedAccounts == null || !_unlistedAccounts.Any()) return false; - - return _unlistedAccounts.Contains(twitterAcct.ToLowerInvariant()); - } - - public bool IsSensitive(string twitterAcct) - { - if (_sensitiveAccounts == null || !_sensitiveAccounts.Any()) return false; - - return _sensitiveAccounts.Contains(twitterAcct.ToLowerInvariant()); - } - } -} diff --git a/src/BirdsiteLive.Domain/StatusService.cs b/src/BirdsiteLive.Domain/StatusService.cs index a10d8b5..080317b 100644 --- a/src/BirdsiteLive.Domain/StatusService.cs +++ b/src/BirdsiteLive.Domain/StatusService.cs @@ -3,6 +3,7 @@ using System.Collections.Generic; using System.IO; using System.Linq; using System.Text.RegularExpressions; +using System.Threading.Tasks; using BirdsiteLive.ActivityPub; using BirdsiteLive.ActivityPub.Converters; using BirdsiteLive.ActivityPub.Models; @@ -17,6 +18,7 @@ namespace BirdsiteLive.Domain public interface IStatusService { Note GetStatus(string username, ExtractedTweet tweet); + ActivityCreateNote GetActivity(string username, ExtractedTweet tweet); } public class StatusService : IStatusService @@ -24,15 +26,13 @@ namespace BirdsiteLive.Domain private readonly InstanceSettings _instanceSettings; private readonly IStatusExtractor _statusExtractor; private readonly IExtractionStatisticsHandler _statisticsHandler; - private readonly IPublicationRepository _publicationRepository; #region Ctor - public StatusService(InstanceSettings instanceSettings, IStatusExtractor statusExtractor, IExtractionStatisticsHandler statisticsHandler, IPublicationRepository publicationRepository) + public StatusService(InstanceSettings instanceSettings, IStatusExtractor statusExtractor, IExtractionStatisticsHandler statisticsHandler) { _instanceSettings = instanceSettings; _statusExtractor = statusExtractor; _statisticsHandler = statisticsHandler; - _publicationRepository = publicationRepository; } #endregion @@ -50,15 +50,9 @@ namespace BirdsiteLive.Domain var to = $"{actorUrl}/followers"; - var isUnlisted = _publicationRepository.IsUnlisted(username); var cc = new string[0]; - if (isUnlisted) - cc = new[] {"https://www.w3.org/ns/activitystreams#Public"}; string summary = null; - var sensitive = _publicationRepository.IsSensitive(username); - if (sensitive) - summary = "Potential Content Warning"; var extractedTags = _statusExtractor.Extract(tweet.MessageContent); _statisticsHandler.ExtractedStatus(extractedTags.tags.Count(x => x.type == "Mention")); @@ -90,7 +84,7 @@ namespace BirdsiteLive.Domain to = new[] { to }, cc = cc, - sensitive = sensitive, + sensitive = false, summary = summary, content = $"

{content}

", attachment = Convert(tweet.Media), @@ -99,6 +93,40 @@ namespace BirdsiteLive.Domain return note; } + public ActivityCreateNote GetActivity(string username, ExtractedTweet tweet) + { + var note = GetStatus(username, tweet); + var actor = UrlFactory.GetActorUrl(_instanceSettings.Domain, username); + String noteUri; + string activityType; + if (tweet.IsRetweet) + { + noteUri = UrlFactory.GetNoteUrl(_instanceSettings.Domain, username, tweet.Id.ToString()); + activityType = "Announce"; + } else + { + noteUri = UrlFactory.GetNoteUrl(_instanceSettings.Domain, username, tweet.Id.ToString()); + activityType = "Create"; + } + + var now = DateTime.UtcNow; + var nowString = now.ToString("s") + "Z"; + + var noteActivity = new ActivityCreateNote() + { + context = "https://www.w3.org/ns/activitystreams", + id = $"{noteUri}/activity", + type = activityType, + actor = actor, + published = nowString, + + to = new[] {$"{actor}/followers"}, + cc = note.cc, + apObject = note + }; + + return noteActivity; + } private Attachment[] Convert(ExtractedMedia[] media) { diff --git a/src/BirdsiteLive.Pipeline/Processors/SubTasks/SendTweetsToInboxTask.cs b/src/BirdsiteLive.Pipeline/Processors/SubTasks/SendTweetsToInboxTask.cs index c76428c..ec98bd8 100644 --- a/src/BirdsiteLive.Pipeline/Processors/SubTasks/SendTweetsToInboxTask.cs +++ b/src/BirdsiteLive.Pipeline/Processors/SubTasks/SendTweetsToInboxTask.cs @@ -55,13 +55,8 @@ namespace BirdsiteLive.Pipeline.Processors.SubTasks { try { - if (!tweet.IsReply || - tweet.IsReply && tweet.IsThread || - _settings.PublishReplies) - { - var note = _statusService.GetStatus(user.Acct, tweet); - await _activityPubService.PostNewActivity(note, user.Acct, "Create", tweet.Id.ToString(), follower.Host, inbox); - } + var activity = _statusService.GetActivity(user.Acct, tweet); + await _activityPubService.PostNewActivity(activity, user.Acct, tweet.Id.ToString(), follower.Host, inbox); } catch (ArgumentException e) { diff --git a/src/BirdsiteLive.Pipeline/Processors/SubTasks/SendTweetsToSharedInboxTask.cs b/src/BirdsiteLive.Pipeline/Processors/SubTasks/SendTweetsToSharedInboxTask.cs index 8dfebf9..72a7c41 100644 --- a/src/BirdsiteLive.Pipeline/Processors/SubTasks/SendTweetsToSharedInboxTask.cs +++ b/src/BirdsiteLive.Pipeline/Processors/SubTasks/SendTweetsToSharedInboxTask.cs @@ -56,18 +56,8 @@ namespace BirdsiteLive.Pipeline.Processors.SubTasks { try { - 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 || - tweet.IsReply && tweet.IsThread || - _settings.PublishReplies) - { - var note = _statusService.GetStatus(user.Acct, tweet); - await _activityPubService.PostNewActivity(note, user.Acct, "Create", tweet.Id.ToString(), host, inbox); - } + var activity = _statusService.GetActivity(user.Acct, tweet); + await _activityPubService.PostNewActivity(activity, user.Acct, tweet.Id.ToString(), host, inbox); } catch (ArgumentException e) { diff --git a/src/BirdsiteLive/Controllers/UsersController.cs b/src/BirdsiteLive/Controllers/UsersController.cs index bb70627..5ce6179 100644 --- a/src/BirdsiteLive/Controllers/UsersController.cs +++ b/src/BirdsiteLive/Controllers/UsersController.cs @@ -171,6 +171,24 @@ namespace BirdsiteLive.Controllers return View(displayTweet); } + [Route("/users/{id}/statuses/{statusId}/activity")] + public async Task Activity(string id, string statusId) + { + if (!long.TryParse(statusId, out var parsedStatusId)) + return NotFound(); + + var tweet = await _twitterTweetService.GetTweetAsync(parsedStatusId); + if (tweet == null) + return NotFound(); + + var user = await _twitterUserService.GetUserAsync(id); + + var status = _statusService.GetActivity(id, tweet); + + var jsonApUser = JsonConvert.SerializeObject(status); + return Content(jsonApUser, "application/activity+json; charset=utf-8"); + } + [Route("/users/{id}/inbox")] [HttpPost] public async Task Inbox() diff --git a/src/Tests/BirdsiteLive.Domain.Tests/StatusServiceTests.cs b/src/Tests/BirdsiteLive.Domain.Tests/StatusServiceTests.cs index e6178c3..c7a23d5 100644 --- a/src/Tests/BirdsiteLive.Domain.Tests/StatusServiceTests.cs +++ b/src/Tests/BirdsiteLive.Domain.Tests/StatusServiceTests.cs @@ -1,7 +1,11 @@ using System; using BirdsiteLive.Common.Settings; +using BirdsiteLive.Domain.Statistics; +using BirdsiteLive.Domain.Tools; using BirdsiteLive.Twitter.Models; +using Microsoft.Extensions.Logging; using Microsoft.VisualStudio.TestTools.UnitTesting; +using Moq; namespace BirdsiteLive.Domain.Tests { @@ -20,29 +24,32 @@ namespace BirdsiteLive.Domain.Tests } #endregion -// [TestMethod] -// public void ExtractMentionsTest() -// { -// #region Stubs -// var username = "MyUserName"; -// var extractedTweet = new ExtractedTweet -// { -// Id = 124L, -// CreatedAt = DateTime.UtcNow, -// MessageContent = @"Getting ready for the weekend...have a great one everyone! -//⁠ -//Photo by Tim Tronckoe | @timtronckoe -//⁠ -//#archenemy #michaelamott #alissawhitegluz #jeffloomis #danielerlandsson #sharleedangelo⁠" -// }; -// #endregion + [TestMethod] + public void ActivityTest() + { + #region Stubs + var username = "MyUserName"; + var extractedTweet = new ExtractedTweet + { + Id = 124L, + CreatedAt = DateTime.UtcNow, + MessageContent = @"Getting ready for the weekend...have a great one everyone! +⁠ +Photo by Tim Tronckoe | @timtronckoe +⁠ +#archenemy #michaelamott #alissawhitegluz #jeffloomis #danielerlandsson #sharleedangelo⁠" + }; + #endregion -// var service = new StatusService(_settings); -// var result = service.GetStatus(username, extractedTweet); + var logger1 = new Mock>(); + var statusExtractor = new StatusExtractor(_settings, logger1.Object); + var stats = new Mock(); + var service = new StatusService(_settings, statusExtractor, stats.Object); + var result = service.GetActivity(username, extractedTweet); -// #region Validations + #region Validations -// #endregion -// } + #endregion + } } } diff --git a/src/Tests/BirdsiteLive.Pipeline.Tests/Processors/SubTasks/SendTweetsToInboxTaskTests.cs b/src/Tests/BirdsiteLive.Pipeline.Tests/Processors/SubTasks/SendTweetsToInboxTaskTests.cs index 63d41c1..e8a6fbf 100644 --- a/src/Tests/BirdsiteLive.Pipeline.Tests/Processors/SubTasks/SendTweetsToInboxTaskTests.cs +++ b/src/Tests/BirdsiteLive.Pipeline.Tests/Processors/SubTasks/SendTweetsToInboxTaskTests.cs @@ -3,6 +3,7 @@ using System.Collections.Generic; using System.Net; using System.Net.Http; using System.Threading.Tasks; +using BirdsiteLive.ActivityPub; using BirdsiteLive.ActivityPub.Models; using BirdsiteLive.Common.Settings; using BirdsiteLive.DAL.Contracts; @@ -37,6 +38,9 @@ namespace BirdsiteLive.Pipeline.Tests.Processors.SubTasks { id = noteId }; + var activity = new ActivityCreateNote() { + apObject = note + }; var twitterHandle = "Test"; var twitterUserId = 7; @@ -63,12 +67,11 @@ namespace BirdsiteLive.Pipeline.Tests.Processors.SubTasks #endregion #region Mocks - var activityPubService = new Mock(MockBehavior.Strict); + var activityPubService = new Mock(); activityPubService .Setup(x => x.PostNewActivity( - It.Is(y => y.id == noteId), + It.Is(y => y.apObject.id == noteId), It.Is(y => y == twitterHandle), - "Create", It.Is(y => y == tweetId.ToString()), It.Is(y => y == host), It.Is(y => y == inbox))) @@ -76,10 +79,10 @@ namespace BirdsiteLive.Pipeline.Tests.Processors.SubTasks var statusServiceMock = new Mock(MockBehavior.Strict); statusServiceMock - .Setup(x => x.GetStatus( + .Setup(x => x.GetActivity( It.Is(y => y == twitterHandle), It.Is(y => y.Id == tweetId))) - .Returns(note); + .Returns(activity); var followersDalMock = new Mock(MockBehavior.Strict); followersDalMock @@ -120,6 +123,10 @@ namespace BirdsiteLive.Pipeline.Tests.Processors.SubTasks { id = noteId }; + var activity = new ActivityCreateNote() + { + apObject = note + }; var twitterHandle = "Test"; var twitterUserId = 7; @@ -139,15 +146,26 @@ namespace BirdsiteLive.Pipeline.Tests.Processors.SubTasks FollowingsSyncStatus = new Dictionary { { twitterUserId, 9 } } }; - var settings = new InstanceSettings - { - PublishReplies = false - }; + var settings = new InstanceSettings { }; #endregion #region Mocks var activityPubService = new Mock(MockBehavior.Strict); + activityPubService + .Setup(x => x.PostNewActivity( + It.Is(y => y.apObject.id == noteId), + It.Is(y => y == twitterHandle), + It.Is(y => y == tweetId.ToString()), + It.Is(y => y == host), + It.Is(y => y == inbox))) + .Returns(Task.CompletedTask); + var statusServiceMock = new Mock(MockBehavior.Strict); + statusServiceMock + .Setup(x => x.GetActivity( + It.Is(y => y == twitterHandle), + It.Is(y => y.Id == tweetId))) + .Returns(activity); var followersDalMock = new Mock(MockBehavior.Strict); followersDalMock @@ -188,6 +206,10 @@ namespace BirdsiteLive.Pipeline.Tests.Processors.SubTasks { id = noteId }; + var activity = new ActivityCreateNote() + { + apObject = note + }; var twitterHandle = "Test"; var twitterUserId = 7; @@ -217,9 +239,8 @@ namespace BirdsiteLive.Pipeline.Tests.Processors.SubTasks var activityPubService = new Mock(MockBehavior.Strict); activityPubService .Setup(x => x.PostNewActivity( - It.Is(y => y.id == noteId), + It.Is(y => y.apObject.id == noteId), It.Is(y => y == twitterHandle), - "Create", It.Is(y => y == tweetId.ToString()), It.Is(y => y == host), It.Is(y => y == inbox))) @@ -227,10 +248,10 @@ namespace BirdsiteLive.Pipeline.Tests.Processors.SubTasks var statusServiceMock = new Mock(MockBehavior.Strict); statusServiceMock - .Setup(x => x.GetStatus( + .Setup(x => x.GetActivity( It.Is(y => y == twitterHandle), It.Is(y => y.Id == tweetId))) - .Returns(note); + .Returns(activity); var followersDalMock = new Mock(MockBehavior.Strict); followersDalMock @@ -271,6 +292,10 @@ namespace BirdsiteLive.Pipeline.Tests.Processors.SubTasks { id = noteId }; + var activity = new ActivityCreateNote() + { + apObject = note + }; var twitterHandle = "Test"; var twitterUserId = 7; @@ -300,9 +325,8 @@ namespace BirdsiteLive.Pipeline.Tests.Processors.SubTasks var activityPubService = new Mock(MockBehavior.Strict); activityPubService .Setup(x => x.PostNewActivity( - It.Is(y => y.id == noteId), + It.Is(y => y.apObject.id == noteId), It.Is(y => y == twitterHandle), - "Create", It.Is(y => y == tweetId.ToString()), It.Is(y => y == host), It.Is(y => y == inbox))) @@ -310,10 +334,10 @@ namespace BirdsiteLive.Pipeline.Tests.Processors.SubTasks var statusServiceMock = new Mock(MockBehavior.Strict); statusServiceMock - .Setup(x => x.GetStatus( + .Setup(x => x.GetActivity( It.Is(y => y == twitterHandle), It.Is(y => y.Id == tweetId))) - .Returns(note); + .Returns(activity); var followersDalMock = new Mock(MockBehavior.Strict); followersDalMock @@ -380,9 +404,8 @@ namespace BirdsiteLive.Pipeline.Tests.Processors.SubTasks { activityPubService .Setup(x => x.PostNewActivity( - It.Is(y => y.id == tweetId.ToString()), + It.Is(y => y.apObject.id == tweetId.ToString()), It.Is(y => y == twitterHandle), - "Create", It.Is(y => y == tweetId.ToString()), It.Is(y => y == host), It.Is(y => y == inbox))) @@ -393,10 +416,10 @@ namespace BirdsiteLive.Pipeline.Tests.Processors.SubTasks foreach (var tweetId in new[] { tweetId2, tweetId3 }) { statusServiceMock - .Setup(x => x.GetStatus( + .Setup(x => x.GetActivity( It.Is(y => y == twitterHandle), It.Is(y => y.Id == tweetId))) - .Returns(new Note { id = tweetId.ToString() }); + .Returns(new ActivityCreateNote { apObject = new Note { id = tweetId.ToString() }}); } var followersDalMock = new Mock(MockBehavior.Strict); @@ -464,9 +487,8 @@ namespace BirdsiteLive.Pipeline.Tests.Processors.SubTasks activityPubService .Setup(x => x.PostNewActivity( - It.Is(y => y.id == tweetId2.ToString()), + It.Is(y => y.apObject.id == tweetId2.ToString()), It.Is(y => y == twitterHandle), - "Create", It.Is(y => y == tweetId2.ToString()), It.Is(y => y == host), It.Is(y => y == inbox))) @@ -474,9 +496,8 @@ namespace BirdsiteLive.Pipeline.Tests.Processors.SubTasks activityPubService .Setup(x => x.PostNewActivity( - It.Is(y => y.id == tweetId3.ToString()), + It.Is(y => y.apObject.id == tweetId3.ToString()), It.Is(y => y == twitterHandle), - "Create", It.Is(y => y == tweetId3.ToString()), It.Is(y => y == host), It.Is(y => y == inbox))) @@ -486,10 +507,10 @@ namespace BirdsiteLive.Pipeline.Tests.Processors.SubTasks foreach (var tweetId in new[] { tweetId2, tweetId3 }) { statusServiceMock - .Setup(x => x.GetStatus( + .Setup(x => x.GetActivity( It.Is(y => y == twitterHandle), It.Is(y => y.Id == tweetId))) - .Returns(new Note { id = tweetId.ToString() }); + .Returns(new ActivityCreateNote { apObject = new Note { id = tweetId.ToString() }}); } var followersDalMock = new Mock(MockBehavior.Strict); @@ -565,7 +586,7 @@ namespace BirdsiteLive.Pipeline.Tests.Processors.SubTasks var statusServiceMock = new Mock(MockBehavior.Strict); statusServiceMock - .Setup(x => x.GetStatus( + .Setup(x => x.GetActivity( It.Is(y => y == twitterHandle), It.Is(y => y.Id == tweetId))) .Throws(new ArgumentException("Invalid pattern blabla at offset 9")); @@ -589,74 +610,5 @@ namespace BirdsiteLive.Pipeline.Tests.Processors.SubTasks #endregion } - [TestMethod] - [ExpectedException(typeof(ArgumentException))] - public async Task ExecuteAsync_SingleTweet_ArgumentException_Test() - { - #region Stubs - var tweetId = 10; - var tweets = new List - { - new ExtractedTweet - { - Id = tweetId, - } - }; - - var twitterHandle = "Test"; - var twitterUserId = 7; - var twitterUser = new SyncTwitterUser - { - Id = twitterUserId, - Acct = twitterHandle - }; - - var host = "domain.ext"; - var inbox = "/user/inbox"; - var follower = new Follower - { - Id = 1, - Host = host, - InboxRoute = inbox, - FollowingsSyncStatus = new Dictionary { { twitterUserId, 9 } } - }; - - var settings = new InstanceSettings - { - PublishReplies = false - }; - #endregion - - #region Mocks - var activityPubService = new Mock(MockBehavior.Strict); - - var statusServiceMock = new Mock(MockBehavior.Strict); - statusServiceMock - .Setup(x => x.GetStatus( - It.Is(y => y == twitterHandle), - It.Is(y => y.Id == tweetId))) - .Throws(new ArgumentException()); - - var followersDalMock = new Mock(MockBehavior.Strict); - - var loggerMock = new Mock>(); - #endregion - - var task = new SendTweetsToInboxTask(activityPubService.Object, statusServiceMock.Object, followersDalMock.Object, settings, loggerMock.Object); - - try - { - await task.ExecuteAsync(tweets.ToArray(), follower, twitterUser); - - } - finally - { - #region Validations - activityPubService.VerifyAll(); - statusServiceMock.VerifyAll(); - followersDalMock.VerifyAll(); - #endregion - } - } } } \ No newline at end of file diff --git a/src/Tests/BirdsiteLive.Pipeline.Tests/Processors/SubTasks/SendTweetsToSharedInboxTests.cs b/src/Tests/BirdsiteLive.Pipeline.Tests/Processors/SubTasks/SendTweetsToSharedInboxTests.cs index 9a8a6db..acba7fd 100644 --- a/src/Tests/BirdsiteLive.Pipeline.Tests/Processors/SubTasks/SendTweetsToSharedInboxTests.cs +++ b/src/Tests/BirdsiteLive.Pipeline.Tests/Processors/SubTasks/SendTweetsToSharedInboxTests.cs @@ -4,6 +4,7 @@ using System.Linq; using System.Net; using System.Net.Http; using System.Threading.Tasks; +using BirdsiteLive.ActivityPub; using BirdsiteLive.ActivityPub.Models; using BirdsiteLive.Common.Settings; using BirdsiteLive.DAL.Contracts; @@ -38,6 +39,10 @@ namespace BirdsiteLive.Pipeline.Tests.Processors.SubTasks { id = noteId }; + var activity = new ActivityCreateNote() + { + apObject = note + }; var twitterHandle = "Test"; var twitterUserId = 7; @@ -84,9 +89,8 @@ namespace BirdsiteLive.Pipeline.Tests.Processors.SubTasks var activityPubService = new Mock(MockBehavior.Strict); activityPubService .Setup(x => x.PostNewActivity( - It.Is(y => y.id == noteId), + It.Is(y => y.apObject.id == noteId), It.Is(y => y == twitterHandle), - "Create", It.Is(y => y == tweetId.ToString()), It.Is(y => y == host), It.Is(y => y == inbox))) @@ -94,100 +98,10 @@ namespace BirdsiteLive.Pipeline.Tests.Processors.SubTasks var statusServiceMock = new Mock(MockBehavior.Strict); statusServiceMock - .Setup(x => x.GetStatus( + .Setup(x => x.GetActivity( It.Is(y => y == twitterHandle), It.Is(y => y.Id == tweetId))) - .Returns(note); - - var followersDalMock = new Mock(MockBehavior.Strict); - - foreach (var follower in followers) - { - followersDalMock - .Setup(x => x.UpdateFollowerAsync( - It.Is(y => y.Id == follower.Id && y.FollowingsSyncStatus[twitterUserId] == tweetId))) - .Returns(Task.CompletedTask); - } - - var loggerMock = new Mock>(); - #endregion - - var task = new SendTweetsToSharedInboxTask(activityPubService.Object, statusServiceMock.Object, followersDalMock.Object, settings, loggerMock.Object); - await task.ExecuteAsync(tweets.ToArray(), twitterUser, host, followers.ToArray()); - - #region Validations - activityPubService.VerifyAll(); - statusServiceMock.VerifyAll(); - followersDalMock.VerifyAll(); - #endregion - } - - [TestMethod] - public async Task ExecuteAsync_SingleTweet_Reply_Test() - { - #region Stubs - var tweetId = 10; - var tweets = new List - { - new ExtractedTweet - { - Id = tweetId, - IsReply = true, - IsThread = false - } - }; - - var noteId = "noteId"; - var note = new Note() - { - id = noteId - }; - - var twitterHandle = "Test"; - var twitterUserId = 7; - var twitterUser = new SyncTwitterUser - { - Id = twitterUserId, - Acct = twitterHandle - }; - - var host = "domain.ext"; - var inbox = "/inbox"; - var followers = new List - { - new Follower - { - Id = 1, - Host = host, - SharedInboxRoute = inbox, - FollowingsSyncStatus = new Dictionary { { twitterUserId, 9 } } - }, - new Follower - { - Id = 2, - Host = host, - SharedInboxRoute = inbox, - FollowingsSyncStatus = new Dictionary { { twitterUserId, 8 } } - }, - new Follower - { - Id = 3, - Host = host, - SharedInboxRoute = inbox, - FollowingsSyncStatus = new Dictionary { { twitterUserId, 7 } } - } - }; - - var settings = new InstanceSettings - { - PublishReplies = false - }; - #endregion - - #region Mocks - var activityPubService = new Mock(MockBehavior.Strict); - - var statusServiceMock = new Mock(MockBehavior.Strict); + .Returns(activity); var followersDalMock = new Mock(MockBehavior.Strict); @@ -232,6 +146,10 @@ namespace BirdsiteLive.Pipeline.Tests.Processors.SubTasks { id = noteId }; + var activity = new ActivityCreateNote() + { + apObject = note + }; var twitterHandle = "Test"; var twitterUserId = 7; @@ -278,9 +196,8 @@ namespace BirdsiteLive.Pipeline.Tests.Processors.SubTasks var activityPubService = new Mock(MockBehavior.Strict); activityPubService .Setup(x => x.PostNewActivity( - It.Is(y => y.id == noteId), + It.Is(y => y.apObject.id == noteId), It.Is(y => y == twitterHandle), - "Create", It.Is(y => y == tweetId.ToString()), It.Is(y => y == host), It.Is(y => y == inbox))) @@ -288,10 +205,10 @@ namespace BirdsiteLive.Pipeline.Tests.Processors.SubTasks var statusServiceMock = new Mock(MockBehavior.Strict); statusServiceMock - .Setup(x => x.GetStatus( + .Setup(x => x.GetActivity( It.Is(y => y == twitterHandle), It.Is(y => y.Id == tweetId))) - .Returns(note); + .Returns(activity); var followersDalMock = new Mock(MockBehavior.Strict); @@ -336,6 +253,10 @@ namespace BirdsiteLive.Pipeline.Tests.Processors.SubTasks { id = noteId }; + var activity = new ActivityCreateNote() + { + apObject = note + }; var twitterHandle = "Test"; var twitterUserId = 7; @@ -382,9 +303,8 @@ namespace BirdsiteLive.Pipeline.Tests.Processors.SubTasks var activityPubService = new Mock(MockBehavior.Strict); activityPubService .Setup(x => x.PostNewActivity( - It.Is(y => y.id == noteId), + It.Is(y => y.apObject.id == noteId), It.Is(y => y == twitterHandle), - "Create", It.Is(y => y == tweetId.ToString()), It.Is(y => y == host), It.Is(y => y == inbox))) @@ -392,10 +312,10 @@ namespace BirdsiteLive.Pipeline.Tests.Processors.SubTasks var statusServiceMock = new Mock(MockBehavior.Strict); statusServiceMock - .Setup(x => x.GetStatus( + .Setup(x => x.GetActivity( It.Is(y => y == twitterHandle), It.Is(y => y.Id == tweetId))) - .Returns(note); + .Returns(activity); var followersDalMock = new Mock(MockBehavior.Strict); @@ -483,9 +403,8 @@ namespace BirdsiteLive.Pipeline.Tests.Processors.SubTasks { activityPubService .Setup(x => x.PostNewActivity( - It.Is(y => y.id == tweetId.ToString()), + It.Is(y => y.apObject.id == tweetId.ToString()), It.Is(y => y == twitterHandle), - "Create", It.Is(y => y == tweetId.ToString()), It.Is(y => y == host), It.Is(y => y == inbox))) @@ -496,10 +415,10 @@ namespace BirdsiteLive.Pipeline.Tests.Processors.SubTasks foreach (var tweetId in new[] { tweetId2, tweetId3 }) { statusServiceMock - .Setup(x => x.GetStatus( + .Setup(x => x.GetActivity( It.Is(y => y == twitterHandle), It.Is(y => y.Id == tweetId))) - .Returns(new Note { id = tweetId.ToString() }); + .Returns(new ActivityCreateNote { apObject = new Note { id = tweetId.ToString() }}); } var followersDalMock = new Mock(MockBehavior.Strict); @@ -588,9 +507,8 @@ namespace BirdsiteLive.Pipeline.Tests.Processors.SubTasks activityPubService .Setup(x => x.PostNewActivity( - It.Is(y => y.id == tweetId2.ToString()), + It.Is(y => y.apObject.id == tweetId2.ToString()), It.Is(y => y == twitterHandle), - "Create", It.Is(y => y == tweetId2.ToString()), It.Is(y => y == host), It.Is(y => y == inbox))) @@ -598,9 +516,8 @@ namespace BirdsiteLive.Pipeline.Tests.Processors.SubTasks activityPubService .Setup(x => x.PostNewActivity( - It.Is(y => y.id == tweetId3.ToString()), + It.Is(y => y.apObject.id == tweetId3.ToString()), It.Is(y => y == twitterHandle), - "Create", It.Is(y => y == tweetId3.ToString()), It.Is(y => y == host), It.Is(y => y == inbox))) @@ -610,10 +527,10 @@ namespace BirdsiteLive.Pipeline.Tests.Processors.SubTasks foreach (var tweetId in new[] { tweetId2, tweetId3 }) { statusServiceMock - .Setup(x => x.GetStatus( + .Setup(x => x.GetActivity( It.Is(y => y == twitterHandle), It.Is(y => y.Id == tweetId))) - .Returns(new Note { id = tweetId.ToString() }); + .Returns(new ActivityCreateNote { apObject = new Note { id = tweetId.ToString() }}); } var followersDalMock = new Mock(MockBehavior.Strict); @@ -710,7 +627,7 @@ namespace BirdsiteLive.Pipeline.Tests.Processors.SubTasks var statusServiceMock = new Mock(MockBehavior.Strict); statusServiceMock - .Setup(x => x.GetStatus( + .Setup(x => x.GetActivity( It.Is(y => y == twitterHandle), It.Is(y => y.Id == tweetId))) .Throws(new ArgumentException("Invalid pattern blabla at offset 9")); @@ -798,7 +715,7 @@ namespace BirdsiteLive.Pipeline.Tests.Processors.SubTasks var statusServiceMock = new Mock(MockBehavior.Strict); statusServiceMock - .Setup(x => x.GetStatus( + .Setup(x => x.GetActivity( It.Is(y => y == twitterHandle), It.Is(y => y.Id == tweetId))) .Throws(new ArgumentException());