public ITentRequestPost FromUser(User user) { Ensure.Argument.IsNotNull(user, nameof(user)); return new TentRequestPost(this.userLogic, this.userRepository, this.postRepository, this.uriHelpers) { User = user }; }
public async Task<string> CreateBewitForPostAsync(User user, TentPost post, TimeSpan expiresIn, CancellationToken cancellationToken = default(CancellationToken)) { // Compute the expiration date for this bewit. var expiresAt = DateTime.UtcNow + expiresIn; // Create the bewit object and save it. var bewit = this.bewitFactory.FromExpirationDate(expiresAt); await this.bewitRepository.UpdateAsync(bewit, cancellationToken); // Generate the bewit signature. return this.cryptoHelpers.CreateBewit(expiresAt, this.uriHelpers.GetCamprPostUri(user.Handle, post.Id), null, bewit.Id, bewit.Key); }
public async Task UserPostVersionAndLastVersion() { var ownerId = Guid.NewGuid().ToString("N"); var user = new User { Id = Guid.NewGuid().ToString("N") }; // Create a new post and use it to update the corresponding user post. var newPost = this.postFactory.FromContent(user, new TentContentMeta { Entity = "http://external1.tent.is" }, this.postTypeFactory.FromString("https://test.com/type")).Post(); // Save the user post. await this.userPostRepository.UpdateAsync(ownerId, newPost, false); // Create a new version of the same post. var versionId1 = newPost.Version.Id; var date2 = DateTime.UtcNow.AddSeconds(2).TruncateToMilliseconds(); newPost.Content.Entity = "http://external2.tent.is"; newPost.Version = new TentVersion { UserId = user.Id, Type = newPost.Type, ReceivedAt = date2, PublishedAt = date2 }; newPost.Version.Id = this.modelHelpers.GetVersionIdFromPost(newPost); // Update the user post. await this.userPostRepository.UpdateAsync(ownerId, newPost, false); // Retrieve the last version for this user post. var userPostLastVersion = await this.userPostRepository.GetAsync(ownerId, user.Id, newPost.Id); Assert.NotNull(userPostLastVersion); Assert.Equal(newPost.Version.Id, userPostLastVersion.VersionId); // Retrieve a specific version for this user post. var userPostVersion1 = await this.userPostRepository.GetAsync(ownerId, user.Id, newPost.Id, versionId1); Assert.NotNull(userPostVersion1); Assert.Equal(versionId1, userPostVersion1.VersionId); }
public async Task PostVersionAndLastVersion() { const string entity1 = "http://external1.tent.is"; const string entity2 = "http://external2.tent.is"; var user = new User { Id = Guid.NewGuid().ToString("N") }; // Create a new post and add it to the db. var newPost = this.postFactory.FromContent(user, new TentContentMeta { Entity = entity1 }, this.postTypeFactory.FromString("https://test.com/type")).Post(); // Save the post to the db. await this.postRepository.UpdateAsync(newPost); // Keep the first version around. var version1 = newPost.Version; // Update the post and publish a new version. newPost.Content.Entity = entity2; newPost.Version = new TentVersion { UserId = user.Id, Type = newPost.Type }; // Save the new version. await this.postRepository.UpdateAsync(newPost); // Retrieve the last version for this post. var postLastVersion = await this.postRepository.GetAsync<TentContentMeta>(user.Id, newPost.Id); Assert.NotNull(postLastVersion); Assert.Equal(newPost.Version.Id, postLastVersion.Version.Id); Assert.Equal(entity2, postLastVersion.Content.Entity); // Retrieve the first version. var postVersion1 = await this.postRepository.GetAsync<TentContentMeta>(user.Id, newPost.Id, version1.Id); Assert.NotNull(postVersion1); Assert.Equal(version1.Id, postVersion1.Version.Id); Assert.Equal(entity1, postVersion1.Content.Entity); }
public async Task<TentPost<object>> GetRelationship(User user, User targetUser, bool createIfNotFound, bool propagate, bool alwaysIncludeCredentials, CancellationToken cancellationToken = default(CancellationToken)) { Ensure.Argument.IsNotNull(user, nameof(user)); Ensure.Argument.IsNotNull(targetUser, nameof(targetUser)); // First, try to find an existing relationship between our user and the target user. var existingRelationship = await this.postLogic.GetLastPostOfTypeMentioningAsync<object>(user, user, user, this.tentConstants.RelationshipPostType, this.requestPostFactory.FromUser(targetUser), cancellationToken); if (((existingRelationship != null && existingRelationship.Type.SubType != "initial") || !createIfNotFound) && !alwaysIncludeCredentials) return existingRelationship; // If needed, create the local Relationship post. Don't propagate it just yet. var localRelationshipPost = existingRelationship ?? this.postFactory.Make(user, this.postTypeFactory.FromType(this.tentConstants.RelationshipPostType, "initial")) .WithMentions(new TentMention { User = targetUser }) .WithPublic(false) .Post(); // If we just created a new relationship post, create the corresponding credentials post. TentPost<TentContentCredentials> localCredentialsPost; if (existingRelationship == null) { // Save the relationship post. await this.postLogic.CreatePostAsync(user, localRelationshipPost, cancellationToken); // Create a new credentials post to go along with this new relationship post. localCredentialsPost = await this.postLogic.CreateNewCredentialsPostAsync(user, user, localRelationshipPost); } // Otherwise, retrieve the existing credentials post. else { // Find the associated credentials post. localCredentialsPost = await this.postLogic.GetLastPostOfTypeMentioningAsync<TentContentCredentials>(user, user, user, this.tentConstants.CredentialsPostType, this.requestPostFactory.FromPost(existingRelationship), cancellationToken); // Attach the credentials to the relationship post. existingRelationship.PassengerCredentials = localCredentialsPost; } // If we were instructed not to propagate, this stops here. if (!propagate) return localRelationshipPost; // If no relationship currently exists, establish one. TentPost<object> remoteRelationshipPost; // If the user is internal, just create the necessary posts. if (targetUser.IsInternal()) { // Add the local credentials to the target user's feed. await this.postLogic.CreateUserPostAsync(targetUser, localCredentialsPost, false, cancellationToken); // Create the relationship post for the target user. remoteRelationshipPost = this.postFactory.Make(targetUser, this.postTypeFactory.FromType(this.tentConstants.RelationshipPostType, "subscriber")) .WithMentions(new TentMention { User = user, Post = localRelationshipPost }) .WithPublic(false) .Post(); await this.postLogic.CreatePostAsync(targetUser, remoteRelationshipPost, cancellationToken); // Create the Credentials post for the target user. var remoteCredentialsPost = await this.postLogic.CreateNewCredentialsPostAsync(targetUser, targetUser, remoteRelationshipPost); // Add the remote Credentials to our user's feed. await this.postLogic.CreateUserPostAsync(user, remoteCredentialsPost, false, cancellationToken); } // Otherwise, contact the remote server and negociate a new relationship. else { // Retrieve the Meta Post for the target user. var targetUserMetaPost = await this.postLogic.GetMetaPostAsync(targetUser, cancellationToken); // Send the Relationship Post to the remote server. var tentClient = this.tentClientFactory.Make(targetUserMetaPost); var remoteCredentialsLink = await tentClient.PostRelationshipAsync(user, localRelationshipPost, cancellationToken); if (remoteCredentialsLink == null) return null; // Retrieve the remote Credentials Post, and import it into our feed. var remoteCredentialsPost = await this.postLogic.ImportPostFromLinkAsync<TentContentCredentials>(user, targetUser, remoteCredentialsLink, cancellationToken); if (remoteCredentialsPost?.Mentions == null || !remoteCredentialsPost.Mentions.Any(m => m.FoundPost)) return null; // Retrieve the remote Relationship Post. var remoteRelationshipPostMention = remoteCredentialsPost.Mentions.First(m => m.FoundPost); remoteRelationshipPost = await this.postLogic.GetPostAsync<object>(user, targetUser, targetUser, remoteRelationshipPostMention.PostId, remoteRelationshipPostMention.VersionId, remoteCredentialsPost, cancellationToken); } if (remoteRelationshipPost == null) return null; // Update the Relationship post for our user. localRelationshipPost.Type = this.postTypeFactory.FromType(this.tentConstants.RelationshipPostType, "subscribing"); localRelationshipPost.Permissions = null; // Set the visibility to public. var localMentionToRemoteRelationshipPost = localRelationshipPost.Mentions.First(); localMentionToRemoteRelationshipPost.PostId = remoteRelationshipPost.Id; localMentionToRemoteRelationshipPost.Type = remoteRelationshipPost.Type; // Create the new version, this one will be propagated through mentions. localRelationshipPost = await this.postLogic.CreatePostAsync(user, localRelationshipPost, cancellationToken); // Create a new subscription for meta posts. var subscriptionType = this.postTypeFactory.FromType(this.tentConstants.SubscriptionPostType, this.tentConstants.MetaPostType.Type); var subscriptionContent = new TentContentSubscription { Type = subscriptionType.ToString() }; var subscriptionPost = this.postFactory.FromContent(user, subscriptionContent, subscriptionType) .WithMentions(new TentMention { User = targetUser }) .Post(); await this.postLogic.CreatePostAsync(user, subscriptionPost, cancellationToken); return localRelationshipPost; }
public Task<TentPost<object>> GetRelationship(User user, User targetUser, bool createIfNotFound, bool propagate, CancellationToken cancellationToken = default(CancellationToken)) { return this.GetRelationship(user, targetUser, createIfNotFound, propagate, false, cancellationToken); }
public Task<TentPost<object>> GetRelationship(User user, User targetUser, CancellationToken cancellationToken = default(CancellationToken)) { return this.GetRelationship(user, targetUser, true, true, false, cancellationToken); }
public async Task<ITentHawkSignature> GetCredentialsForUserAsync(User user, User targetUser, bool createIfNotFound, TentPost<TentContentCredentials> credentials, CancellationToken cancellationToken = new CancellationToken()) { Ensure.Argument.IsNotNull(user, nameof(user)); Ensure.Argument.IsNotNull(targetUser, nameof(targetUser)); // If a credentials post was provided, use it to create the signature. if (credentials != null) return this.hawkSignatureFactory.FromCredentials(credentials); // Otherwise, retrieve the relationship between our two users. var localRelationshipPost = await this.GetRelationship(user, targetUser, createIfNotFound, cancellationToken); if (localRelationshipPost?.Mentions == null || !localRelationshipPost.Mentions.Any(m => m.UserId != user.Id && m.FoundPost)) return null; // Extract the mention to the remote relationship post. var remoteRelationshipPostMention = localRelationshipPost.Mentions.First(m => m.UserId != user.Id && m.FoundPost); // Retrieve the credentials post for the remote relationship post. var remoteCredentialsPost = await this.postLogic.GetLastPostOfTypeMentioningAsync<TentContentCredentials>(user, user, targetUser, this.tentConstants.CredentialsPostType, this.requestPostFactory.FromMention(remoteRelationshipPostMention), cancellationToken); return remoteCredentialsPost == null ? null : this.hawkSignatureFactory.FromCredentials(remoteCredentialsPost); }
public async Task PostDeleteVersion() { var type = this.postTypeFactory.FromString("https://test.com/type#type"); var user = new User { Id = Guid.NewGuid().ToString("N") }; // Create a new post and save it. var newPost = this.postFactory.FromContent(user, new TentContentMeta { Entity = "http://external1.tent.is" }, type).Post(); await this.postRepository.UpdateAsync(newPost); // Delete this specific version of this post. await this.postRepository.DeleteAsync(newPost, true); // Make sure this post can't be found anymore. var postLastVersion = await this.postRepository.GetAsync<TentContentMeta>(user.Id, newPost.Id); Assert.Null(postLastVersion); var postExactVersion = await this.postRepository.GetAsync<TentContentMeta>(user.Id, newPost.Id, newPost.Version.Id); Assert.Null(postExactVersion); }
public async Task UserPostDeleteLastVersion() { var ownerId = Guid.NewGuid().ToString("N"); var user = new User { Id = Guid.NewGuid().ToString("N") }; var date1 = DateTime.UtcNow; var date2 = DateTime.UtcNow.AddSeconds(1); // Create a new post and use it to update the corresponding user post. var newPost = this.postFactory.FromContent(user, new TentContentMeta { Entity = "http://external1.tent.is" }, this.postTypeFactory.FromString("https://test.com/type")).Post(); // Compute the VersionId for this post. newPost.Version.Id = this.modelHelpers.GetVersionIdFromPost(newPost); newPost.Version.ReceivedAt = date1; newPost.Version.PublishedAt = date1; newPost.ReceivedAt = date1; newPost.PublishedAt = date1; // Save the user post. await this.userPostRepository.UpdateAsync(ownerId, newPost, false); // Create a new version of the same post. var versionId1 = newPost.Version.Id; newPost.Content.Entity = "http://external2.tent.is"; newPost.Version = new TentVersion { UserId = user.Id, Type = newPost.Type, ReceivedAt = date2, PublishedAt = date2 }; newPost.Version.Id = this.modelHelpers.GetVersionIdFromPost(newPost); // Update the user post. await this.userPostRepository.UpdateAsync(ownerId, newPost, false); // Delete the last version for this user post. await this.userPostRepository.DeleteAsync(ownerId, newPost, true); // Check that the first version still exists. var userPostVersion1 = await this.userPostRepository.GetAsync(ownerId, user.Id, newPost.Id, versionId1); Assert.NotNull(userPostVersion1); // Check that the second version was deleted. var userPostVersion2 = await this.userPostRepository.GetAsync(ownerId, user.Id, newPost.Id, newPost.Version.Id); Assert.Null(userPostVersion2); // Check that the last version is now back to version 1. var userPostLastVersion = await this.userPostRepository.GetAsync(ownerId, user.Id, newPost.Id); Assert.NotNull(userPostLastVersion); Assert.Equal(versionId1, userPostLastVersion.VersionId); }
public Task<string> CreateBewitForPostAsync(User user, TentPost post, CancellationToken cancellationToken = default(CancellationToken)) { return this.CreateBewitForPostAsync(user, post, this.configuration.DefaultBewitExpiration, cancellationToken); }
public async Task PostLastVersionOfType() { const string entity1 = "http://external1.tent.is"; const string entity2 = "http://external2.tent.is"; const string entity3 = "http://external3.tent.is"; const string entity4 = "http://external4.tent.is"; var type1 = this.postTypeFactory.FromString("https://test.com/type#type1"); var type2 = this.postTypeFactory.FromString("https://test.com/type#type2"); var typeWildcard = this.postTypeFactory.FromString("https://test.com/type"); var user = new User { Id = Guid.NewGuid().ToString("N") }; // Create two new posts of the same type. var newPost1 = this.postFactory.FromContent(user, new TentContentMeta { Entity = entity1 }, type1).Post(); var newPost2 = this.postFactory.FromContent(user, new TentContentMeta { Entity = entity2 }, type1).Post(); // Save the posts to the db. await this.postRepository.UpdateAsync(newPost1); await this.postRepository.UpdateAsync(newPost2); // Retrieve the last post version of this type. var lastPostVersionOfType1 = await this.postRepository.GetLastVersionOfTypeAsync<TentContentMeta>(user.Id, type1); Assert.NotNull(lastPostVersionOfType1); Assert.Equal(type1.ToString(), lastPostVersionOfType1.Type.ToString()); Assert.Equal(entity2, lastPostVersionOfType1.Content.Entity); // Update this first post. newPost1.Content.Entity = entity3; newPost1.Version = new TentVersion { UserId = user.Id, Type = type1 }; await this.postRepository.UpdateAsync(newPost1); // Verify that it's now the last post version of this type. var lastPostVersionOfType2 = await this.postRepository.GetLastVersionOfTypeAsync<TentContentMeta>(user.Id, type1); Assert.NotNull(lastPostVersionOfType2); Assert.Equal(type1.ToString(), lastPostVersionOfType2.Type.ToString()); Assert.Equal(entity3, lastPostVersionOfType2.Content.Entity); // Create a new post with a different subtype. var newPost3 = this.postFactory.FromContent(user, new TentContentMeta { Entity = entity4 }, type2).Post(); await this.postRepository.UpdateAsync(newPost3); // Make sure that the previous request still returns the same post. var lastPostVersionOfType3 = await this.postRepository.GetLastVersionOfTypeAsync<TentContentMeta>(user.Id, type1); Assert.NotNull(lastPostVersionOfType3); Assert.Equal(type1.ToString(), lastPostVersionOfType3.Type.ToString()); Assert.Equal(entity3, lastPostVersionOfType3.Content.Entity); // Perform a wildcard request, and make sure we get the new post. var lastPostVersionOfType4 = await this.postRepository.GetLastVersionOfTypeAsync<TentContentMeta>(user.Id, typeWildcard); Assert.NotNull(lastPostVersionOfType4); Assert.Equal(type2.ToString(), lastPostVersionOfType4.Type.ToString()); Assert.Equal(entity4, lastPostVersionOfType4.Content.Entity); }
public async Task PostDeleteLastVersion() { const string entity1 = "http://external1.tent.is"; const string entity2 = "http://external2.tent.is"; var type = this.postTypeFactory.FromString("https://test.com/type#type"); var user = new User { Id = Guid.NewGuid().ToString("N") }; // Create a new post and save it. var newPost = this.postFactory.FromContent(user, new TentContentMeta { Entity = entity1 }, type).Post(); await this.postRepository.UpdateAsync(newPost); // Update the post and publish a new version. var versionId1 = newPost.Version.Id; newPost.Content.Entity = entity2; newPost.Version = new TentVersion { UserId = user.Id, Type = newPost.Type }; // Save the new version. await this.postRepository.UpdateAsync(newPost); // Delete the last version of this post. await this.postRepository.DeleteAsync(newPost, true); // Check that the first version still exists. var postVersion1 = await this.postRepository.GetAsync<TentContentMeta>(user.Id, newPost.Id, versionId1); Assert.NotNull(postVersion1); // Check that this second version doesn't exist anymore. var postVersion2 = await this.postRepository.GetAsync<TentContentMeta>(user.Id, newPost.Id, newPost.Version.Id); Assert.Null(postVersion2); // Check that the last version is now back to version 1. var postLastVersion = await this.postRepository.GetAsync<TentContentMeta>(user.Id, newPost.Id); Assert.NotNull(postLastVersion); Assert.Equal(entity1, postLastVersion.Content.Entity); }
public async Task UserPostFeedRequestCount() { var ownerId = Guid.NewGuid().ToString("N"); var user1 = new User { Id = Guid.NewGuid().ToString("N") }; var user2 = new User { Id = Guid.NewGuid().ToString("N") }; await this.userRepository.UpdateAsync(user1); await this.userRepository.UpdateAsync(user2); var type1 = this.postTypeFactory.FromString("https://test.com/type#type1"); var type2 = this.postTypeFactory.FromString("https://test.com/type2#type2"); // Create three new posts and use them to update the corresponding user posts. var newPost1 = this.postFactory.FromContent(user1, new TentContentMeta(), type1) .WithMentions(new TentMention { UserId = user2.Id }) .Post(); var newPost2 = this.postFactory.FromContent(user2, new TentContentMeta(), type1).Post(); newPost2.ReceivedAt = newPost2.PublishedAt.GetValueOrDefault().AddSeconds(2); var newPost3 = this.postFactory.FromContent(user2, new TentContentMeta(), type2) .WithMentions(new TentMention { UserId = newPost1.UserId, PostId = newPost1.Id, VersionId = newPost1.Version.Id }) .Post(); newPost3.ReceivedAt = newPost3.PublishedAt.GetValueOrDefault().AddSeconds(4); // Save the corresponding User Posts. await this.userPostRepository.UpdateAsync(ownerId, newPost1, false); await this.userPostRepository.UpdateAsync(ownerId, newPost2, true); await this.userPostRepository.UpdateAsync(ownerId, newPost3, false); // Create a feed request to count all the posts. var feedRequestAll = this.feedRequestFactory.Make(); var feedCountAll = await this.userPostRepository.CountAsync(ownerId, ownerId, feedRequestAll); Assert.Equal(3, feedCountAll); // Create a feed request to count all the posts by User2. var feedRequestFromUser2 = this.feedRequestFactory.Make() .AddUsers(user2); var feedCountFromUser2 = await this.userPostRepository.CountAsync(ownerId, ownerId, feedRequestFromUser2); Assert.Equal(2, feedCountFromUser2); // Make sure the count isn't affected by skipping posts. var feedRequestAllSkip = this.feedRequestFactory.Make() .AddSkip(2); var feedCountAllSkip = await this.userPostRepository.CountAsync(ownerId, ownerId, feedRequestAllSkip); Assert.Equal(3, feedCountAllSkip); // Make sure the count isn't affected by limiting posts. var feedRequestAllLimit = this.feedRequestFactory.Make() .AddLimit(2); var feedCountAllLimit = await this.userPostRepository.CountAsync(ownerId, ownerId, feedRequestAllLimit); Assert.Equal(3, feedCountAllLimit); }
public async Task<TentPost<object>> AcceptRelationship(User user, User targetUser, Uri credentialsLinkUri, CancellationToken cancellationToken = default(CancellationToken)) { // Retrieve any local existing relationship post. var localRelationshipPost = await this.GetRelationship(user, targetUser, true, false, true, cancellationToken); var localCredentialsPost = localRelationshipPost.PassengerCredentials; // Retrieve the meta post for the remote user. var metaPost = await this.postLogic.GetMetaPostAsync(targetUser, cancellationToken); // Check the meta post's server to see if we have a match with the provided credentials link. if (!metaPost.Content.IsUrlServerMatch(credentialsLinkUri)) return null; // Retrieve and import the credentials post into our user's feed. var remoteCredentialsPost = await this.postLogic.ImportPostFromLinkAsync<TentContentCredentials>(user, targetUser, credentialsLinkUri, cancellationToken); if (remoteCredentialsPost?.Mentions == null || remoteCredentialsPost.Mentions.All(m => m.Post == null)) return null; // Extract the remote relationship post. var remoteRelationshipPost = remoteCredentialsPost.Mentions.First(m => m.Post != null).Post; // Update the Relationship post for our user. localRelationshipPost.Type = this.tentConstants.RelationshipPostType; localRelationshipPost.Permissions = null; // Set the visibility to public. var localMentionToRemoteRelationshipPost = localRelationshipPost.Mentions.First(); localMentionToRemoteRelationshipPost.PostId = remoteRelationshipPost.Id; localMentionToRemoteRelationshipPost.Type = remoteRelationshipPost.Type; // Create the new version, this one will be propagated through mentions. localRelationshipPost = await this.postLogic.CreatePostAsync(user, localRelationshipPost, false, cancellationToken); // Send a link to the local credentials back to the remote server. remoteRelationshipPost.PassengerCredentials = localCredentialsPost; //// Queue the creation of a meta post subscription for that user. //await this.tentQueues.MetaSubscriptions.AddMessageAsync(new QueueMetaSubscriptionMessage //{ // UserId = user.Id, // TargetUserId = targetUser.Id //}); return remoteRelationshipPost; }
public async Task OtherUserPostFeedRequest() { var requesterId = Guid.NewGuid().ToString("N"); var ownerId = Guid.NewGuid().ToString("N"); var user1 = new User { Id = requesterId }; var user2 = new User { Id = ownerId }; var user3 = new User { Id = Guid.NewGuid().ToString("N") }; await this.userRepository.UpdateAsync(user1); await this.userRepository.UpdateAsync(user2); await this.userRepository.UpdateAsync(user3); var type = this.postTypeFactory.FromString("https://test.com/type#type"); // Public post from feedowner. var newPost1 = this.postFactory.FromContent(user2, new TentContentMeta(), type).Post(); // Private post from feedowner that mentions the requester. var newPost2 = this.postFactory.FromContent(user2, new TentContentMeta(), type) .WithMentions(new TentMention { User = user1 }) .WithPublic(false) .Post(); newPost2.ReceivedAt = newPost2.PublishedAt.GetValueOrDefault().AddSeconds(2); // Private post from the feedowner without mentions. var newPost3 = this.postFactory.FromContent(user2, new TentContentMeta(), type) .WithPublic(false) .Post(); newPost3.ReceivedAt = newPost3.PublishedAt.GetValueOrDefault().AddSeconds(4); // Public post from different user. var newPost4 = this.postFactory.FromContent(user3, new TentContentMeta(), type).Post(); newPost4.ReceivedAt = newPost4.PublishedAt.GetValueOrDefault().AddSeconds(6); // Save the corresponding User Posts. await this.userPostRepository.UpdateAsync(ownerId, newPost1, false); await this.userPostRepository.UpdateAsync(ownerId, newPost2, false); await this.userPostRepository.UpdateAsync(ownerId, newPost3, false); await this.userPostRepository.UpdateAsync(ownerId, newPost4, false); // Create a feed request to retrieve all the posts that the requester can access. var feedRequestAllRequester = this.feedRequestFactory.Make(); var feedResultAllRequester = await this.userPostRepository.GetAsync(requesterId, ownerId, feedRequestAllRequester); Assert.Equal(2, feedResultAllRequester.Count); Assert.Equal(newPost2.Id, feedResultAllRequester[0].PostId); Assert.Equal(newPost1.Id, feedResultAllRequester[1].PostId); // The owners themselves should have access to all the posts. var feedRequestAllOwner = this.feedRequestFactory.Make(); var feedResultAllOwner = await this.userPostRepository.GetAsync(ownerId, ownerId, feedRequestAllOwner); Assert.Equal(4, feedResultAllOwner.Count); Assert.Equal(newPost4.Id, feedResultAllOwner[0].PostId); Assert.Equal(newPost1.Id, feedResultAllOwner[3].PostId); }
public Task<ITentHawkSignature> GetCredentialsForUserAsync(User user, User targetUser, CancellationToken cancellationToken = new CancellationToken()) { return this.GetCredentialsForUserAsync(user, targetUser, false, null, cancellationToken); }
public async Task UserPostDeleteVersion() { var ownerId = Guid.NewGuid().ToString("N"); var user = new User { Id = Guid.NewGuid().ToString("N") }; var date = DateTime.UtcNow; // Create a new post and use it to update the corresponding user post. var newPost = this.postFactory.FromContent(user, new TentContentMeta { Entity = "http://external1.tent.is" }, this.postTypeFactory.FromString("https://test.com/type")).Post(); // Compute the VersionId for this post. newPost.Version.Id = this.modelHelpers.GetVersionIdFromPost(newPost); newPost.Version.ReceivedAt = date; newPost.Version.PublishedAt = date; newPost.ReceivedAt = date; newPost.PublishedAt = date; // Save the user post. await this.userPostRepository.UpdateAsync(ownerId, newPost, false); // Delete this user post and all its versions. await this.userPostRepository.DeleteAsync(ownerId, newPost, true); // Make sure this post can't be found anymore. var userPostLastVersion = await this.userPostRepository.GetAsync(ownerId, user.Id, newPost.Id); Assert.Null(userPostLastVersion); var userPostVersion = await this.userPostRepository.GetAsync(ownerId, user.Id, newPost.Id, newPost.Version.Id); Assert.Null(userPostVersion); }
public Task<ITentHawkSignature> GetCredentialsForUserAsync(User user, User targetUser, bool createIfNotFound, CancellationToken cancellationToken = new CancellationToken()) { return this.GetCredentialsForUserAsync(user, targetUser, createIfNotFound, null, cancellationToken); }
public async Task UserPostFeedRequest() { var ownerId = Guid.NewGuid().ToString("N"); var user1 = new User { Id = Guid.NewGuid().ToString("N") }; var user2 = new User { Id = Guid.NewGuid().ToString("N") }; await this.userRepository.UpdateAsync(user1); await this.userRepository.UpdateAsync(user2); var type1 = this.postTypeFactory.FromString("https://test.com/type#type1"); var type2 = this.postTypeFactory.FromString("https://test.com/type2#type2"); // Create three new posts and use them to update the corresponding user posts. var newPost1 = this.postFactory.FromContent(user1, new TentContentMeta(), type1) .WithMentions(new TentMention { UserId = user2.Id }) .Post(); var newPost2 = this.postFactory.FromContent(user2, new TentContentMeta(), type1).Post(); newPost2.ReceivedAt = newPost2.PublishedAt.GetValueOrDefault().AddSeconds(2); var newPost3 = this.postFactory.FromContent(user2, new TentContentMeta(), type2) .WithMentions(new TentMention { UserId = newPost1.UserId, PostId = newPost1.Id, VersionId = newPost1.Version.Id }) .Post(); newPost3.ReceivedAt = newPost3.PublishedAt.GetValueOrDefault().AddSeconds(4); // Save the corresponding User Posts. await this.userPostRepository.UpdateAsync(ownerId, newPost1, false); await this.userPostRepository.UpdateAsync(ownerId, newPost2, true); await this.userPostRepository.UpdateAsync(ownerId, newPost3, false); // Create a feed request to retrieve all the posts. var feedRequestAll = this.feedRequestFactory.Make(); var feedResultAll = await this.userPostRepository.GetAsync(ownerId, ownerId, feedRequestAll); Assert.Equal(3, feedResultAll.Count); Assert.Equal(newPost3.Id, feedResultAll[0].PostId); Assert.Equal(newPost1.Id, feedResultAll[2].PostId); // Create a feed request to retrieve all the posts by User2. var feedRequestFromUser2 = this.feedRequestFactory.Make() .AddUsers(user2); var feedResultFromUser2 = await this.userPostRepository.GetAsync(ownerId, ownerId, feedRequestFromUser2); Assert.Equal(2, feedResultFromUser2.Count); Assert.Equal(newPost3.Id, feedResultFromUser2[0].PostId); Assert.Equal(newPost2.Id, feedResultFromUser2[1].PostId); // Create a feed request to retrieve all the posts from followings. var feedRequestFromFollowings = this.feedRequestFactory.Make() .AddSpecialEntities(TentFeedRequestSpecialEntities.Followings); var feedResultFromFollowings = await this.userPostRepository.GetAsync(ownerId, ownerId, feedRequestFromFollowings); Assert.Equal(1, feedResultFromFollowings.Count); Assert.Equal(newPost2.Id, feedResultFromFollowings[0].PostId); // Create a feed request to retrieve all the posts of Type1. var feedRequestOfType1 = this.feedRequestFactory.Make() .AddTypes(type1); var feedResultOfType1 = await this.userPostRepository.GetAsync(ownerId, ownerId, feedRequestOfType1); Assert.Equal(2, feedResultOfType1.Count); Assert.Equal(newPost2.Id, feedResultOfType1[0].PostId); Assert.Equal(newPost1.Id, feedResultOfType1[1].PostId); // Create a feed request to retrieve all the posts of a wildcard type. var feedRequestOfWildcardType1 = this.feedRequestFactory.Make() .AddTypes(this.postTypeFactory.FromString(type1.Type)); var feedResultOfWildcardType1 = await this.userPostRepository.GetAsync(ownerId, ownerId, feedRequestOfWildcardType1); Assert.Equal(2, feedResultOfWildcardType1.Count); Assert.Equal(newPost2.Id, feedResultOfWildcardType1[0].PostId); Assert.Equal(newPost1.Id, feedResultOfWildcardType1[1].PostId); // Create a feed request that combines conditions (User + Type). var feedRequestUserAndType = this.feedRequestFactory.Make() .AddUsers(user2) .AddTypes(type1); var feedResultUserAndType = await this.userPostRepository.GetAsync(ownerId, ownerId, feedRequestUserAndType); Assert.Equal(1, feedResultUserAndType.Count); Assert.Equal(newPost2.Id, feedResultUserAndType[0].PostId); // Create a feed request that skips the first item. var feedRequestSkip = this.feedRequestFactory.Make() .AddSkip(1); var feedResultSkip = await this.userPostRepository.GetAsync(ownerId, ownerId, feedRequestSkip); Assert.Equal(2, feedResultSkip.Count); Assert.Equal(newPost2.Id, feedResultSkip[0].PostId); Assert.Equal(newPost1.Id, feedResultSkip[1].PostId); // Create a feed request that limits the results to 2. var feedRequestLimit = this.feedRequestFactory.Make() .AddLimit(2); var feedResultLimit = await this.userPostRepository.GetAsync(ownerId, ownerId, feedRequestLimit); Assert.Equal(2, feedResultLimit.Count); Assert.Equal(newPost3.Id, feedResultLimit[0].PostId); Assert.Equal(newPost2.Id, feedResultLimit[1].PostId); // Create a feed request that combines Skip + Limit. var feedRequestSkipAndLimit = this.feedRequestFactory.Make() .AddSkip(1) .AddLimit(1); var feedResultSkipAndLimit = await this.userPostRepository.GetAsync(ownerId, ownerId, feedRequestSkipAndLimit); Assert.Equal(1, feedResultSkipAndLimit.Count); Assert.Equal(newPost2.Id, feedResultSkipAndLimit[0].PostId); // Create a feed request to find all the posts until Post1. var feedRequestUntilPost1 = this.feedRequestFactory.Make() .AddPostBoundary(this.requestPostFactory.FromPost(newPost1), TentFeedRequestBoundaryType.Until); var feedResultUntilPost1 = await this.userPostRepository.GetAsync(ownerId, ownerId, feedRequestUntilPost1); Assert.Equal(2, feedResultUntilPost1.Count); Assert.Equal(newPost3.Id, feedResultUntilPost1[0].PostId); Assert.Equal(newPost2.Id, feedResultUntilPost1[1].PostId); // Create a feed request to find all posts since Post1. var feedRequestSincePost1 = this.feedRequestFactory.Make() .AddPostBoundary(this.requestPostFactory.FromPost(newPost1), TentFeedRequestBoundaryType.Since); var feedResultSincePost1 = await this.userPostRepository.GetAsync(ownerId, ownerId, feedRequestSincePost1); Assert.Equal(2, feedResultSincePost1.Count); Assert.Equal(newPost2.Id, feedResultSincePost1[0].PostId); // Notice the inverted order here. Assert.Equal(newPost3.Id, feedResultSincePost1[1].PostId); // Create a feed request to find the first post since Post1. var feedRequestFirstSincePost1 = this.feedRequestFactory.Make() .AddPostBoundary(this.requestPostFactory.FromPost(newPost1), TentFeedRequestBoundaryType.Since) .AddLimit(1); var feedResultFirstSincePost1 = await this.userPostRepository.GetAsync(ownerId, ownerId, feedRequestFirstSincePost1); Assert.Equal(1, feedResultFirstSincePost1.Count); Assert.Equal(newPost2.Id, feedResultFirstSincePost1[0].PostId); // Create a feed request to find all posts before Post3. var feedRequestBeforePost3 = this.feedRequestFactory.Make() .AddPostBoundary(this.requestPostFactory.FromPost(newPost3), TentFeedRequestBoundaryType.Before); var feedResultBeforePost3 = await this.userPostRepository.GetAsync(ownerId, ownerId, feedRequestBeforePost3); Assert.Equal(2, feedResultBeforePost3.Count); Assert.Equal(newPost2.Id, feedResultBeforePost3[0].PostId); Assert.Equal(newPost1.Id, feedResultBeforePost3[1].PostId); // Create a feed request to find posts that mention User2. var feedRequestMentionsUser2 = this.feedRequestFactory.Make() .AddMentions(this.requestPostFactory.FromUser(user2)); var feedResultMentionsUser2 = await this.userPostRepository.GetAsync(ownerId, ownerId, feedRequestMentionsUser2); Assert.Equal(1, feedResultMentionsUser2.Count); Assert.Equal(newPost1.Id, feedResultMentionsUser2[0].PostId); // Create a feed request to find posts that don't mention User2. var feedRequestNotMentionsUser2 = this.feedRequestFactory.Make() .AddNotMentions(this.requestPostFactory.FromUser(user2)); var feedResultNotMentionsUser2 = await this.userPostRepository.GetAsync(ownerId, ownerId, feedRequestNotMentionsUser2); Assert.Equal(2, feedResultNotMentionsUser2.Count); Assert.Equal(newPost3.Id, feedResultNotMentionsUser2[0].PostId); Assert.Equal(newPost2.Id, feedResultNotMentionsUser2[1].PostId); // Create a feed request to find posts that mention NewPost1. var feedRequestMentionsPost1 = this.feedRequestFactory.Make() .AddMentions(this.requestPostFactory.FromPost(newPost1)); var feedResultMentionsPost1 = await this.userPostRepository.GetAsync(ownerId, ownerId, feedRequestMentionsPost1); Assert.Equal(1, feedResultMentionsPost1.Count); Assert.Equal(newPost3.Id, feedResultMentionsPost1[0].PostId); // Create a feed request to find posts that mention NewPost2 (none). var feedRequestMentionsPost2 = this.feedRequestFactory.Make() .AddMentions(this.requestPostFactory.FromPost(newPost2)); var feedResultMentionsPost2 = await this.userPostRepository.GetAsync(ownerId, ownerId, feedRequestMentionsPost2); Assert.Equal(0, feedResultMentionsPost2.Count); // Create a feed request to find posts that don't mention NewPost3 (all). var feedRequestNotMentionsPost3 = this.feedRequestFactory.Make() .AddNotMentions(this.requestPostFactory.FromPost(newPost3)); var feedResultNotMentionsPost3 = await this.userPostRepository.GetAsync(ownerId, ownerId, feedRequestNotMentionsPost3); Assert.Equal(3, feedResultNotMentionsPost3.Count); Assert.Equal(newPost3.Id, feedResultNotMentionsPost3[0].PostId); Assert.Equal(newPost1.Id, feedResultNotMentionsPost3[2].PostId); }
public async Task PostBulkRequest() { const string entity1 = "http://external1.tent.is"; const string entity2 = "http://external2.tent.is"; var type = this.postTypeFactory.FromString("https://test.com/type#type"); var user = new User { Id = Guid.NewGuid().ToString("N") }; // Create two new posts of the same type. var newPost1 = this.postFactory.FromContent(user, new TentContentMeta { Entity = entity1 }, type).Post(); var newPost2 = this.postFactory.FromContent(user, new TentContentMeta { Entity = entity2 }, type).Post(); // Save the posts to the db. await this.postRepository.UpdateAsync(newPost1); await this.postRepository.UpdateAsync(newPost2); // Retrieve these two posts in bulk. var references = new List<ITentPostIdentifier> { new TentPostIdentifier(newPost1.UserId, newPost1.Id, newPost1.Version.Id), new TentPostIdentifier(newPost2.UserId, newPost2.Id, newPost2.Version.Id) }; var posts = await this.postRepository.GetAsync<TentContentMeta>(references); // Check that the right posts were returned, in the correct order. Assert.NotNull(posts); Assert.Equal(2, posts.Count); Assert.True(posts.Any(p => p.Id == newPost1.Id)); Assert.True(posts.Any(p => p.Id == newPost2.Id)); }