public async Task <IActionResult> PostComment(int id, PostCommentRequest request) { // Returns error if comment is null if (request.Content == null) { return(BadRequest("Comment cannot be empty")); } // Retrieves user and returns error if muted User user = await _repository.GetUserAsync(User); if (user.Muted) { return(Unauthorized("Your account is muted, you cannot post comments or create threads")); } // Retrieves thread and returns error if locked or not found / deleted Thread thread = await _repository.GetThreadAsync(id); if (thread == null || thread.Deleted) { return(NotFound("Thread not found")); } if (thread.Locked) { return(Forbid("Cannot reply, thread is locked")); } // Creates and adds comment to database Comment comment = new Comment() { Content = request.Content, DatePosted = DateTime.Now, ThreadID = id }; await _repository.PostCommentAsync(comment); await _repository.SaveChangesAsync(); // Returns JSON response return(Json(new ApiComment(comment))); }
public async Task <IActionResult> PostComment(int id, PostCommentRequest request) { // Returns error if comment is empty if (request.Content == null) { return(BadRequest("Comment cannot be null")); } // Retrieves user and returns error if user is not found, deleted or locked User currentUser = await _repository.GetUserAsync(User); User profileUser = await _repository.GetUserAsync(id); if (profileUser == null || profileUser.Deleted) { return(NotFound("Requested user not found")); } if (profileUser.CommentsLocked) { return(Forbid("The user's comments are locked")); } // Creates user comment and adds it to database UserComment userComment = new UserComment() { Content = request.Content, DatePosted = DateTime.Now, UserID = currentUser.UserID, UserPageID = profileUser.UserID }; Result <UserComment> result = await _repository.PostUserCommentAsync(userComment); if (result.Failure) { return(StatusCode(result.Code, result.Error)); } await _repository.SaveChangesAsync(); // Returns JSON response return(Json(new ApiComment(userComment))); }
/// <summary> /// Helper routine for posting a generic comment /// </summary> /// <param name="client">Client object</param> /// <param name="auth">authorization header value</param> /// <param name="topicHandle">The topic handle for the comment.</param> /// <returns>A post comment response</returns> public static async Task <PostCommentResponse> PostGenericComment(SocialPlusClient client, string auth, string topicHandle) { if (string.IsNullOrEmpty(topicHandle)) { throw new ArgumentNullException("PostGenericComment called with empty topicHandle"); } string uniqueSuffix = TestUtilities.CreateUniqueDigits(); string text = "The text of a comment" + uniqueSuffix; string language = "en-US"; PostCommentRequest postCommentRequest = new PostCommentRequest(text: text, language: language); var httpResponse = await client.TopicComments.PostCommentWithHttpMessagesAsync(topicHandle : topicHandle, request : postCommentRequest, authorization : auth); if (httpResponse == null || httpResponse.Response == null || !httpResponse.Response.IsSuccessStatusCode) { throw new InvalidOperationException(string.Format("PostGenericComment failed for auth {0}, topicHandle {1}", auth, topicHandle)); } return(httpResponse.Body); }
/// <summary> /// Create a new comment /// </summary> /// <param name='topicHandle'> /// Topic handle /// </param> /// <param name='request'> /// Post comment request /// </param> /// <param name='authorization'> /// Format is: "Scheme CredentialsList". Possible values are: /// /// - Anon AK=AppKey /// /// - SocialPlus TK=SessionToken /// /// - Facebook AK=AppKey|TK=AccessToken /// /// - Google AK=AppKey|TK=AccessToken /// /// - Twitter AK=AppKey|RT=RequestToken|TK=AccessToken /// /// - Microsoft AK=AppKey|TK=AccessToken /// /// - AADS2S AK=AppKey|[UH=UserHandle]|TK=AADToken /// </param> /// <param name='customHeaders'> /// Headers that will be added to request. /// </param> /// <param name='cancellationToken'> /// The cancellation token. /// </param> /// <return> /// A response object containing the response body and response headers. /// </return> public async Task <HttpOperationResponse <PostCommentResponse> > PostCommentWithHttpMessagesAsync(string topicHandle, PostCommentRequest request, string authorization, Dictionary <string, List <string> > customHeaders = null, CancellationToken cancellationToken = default(CancellationToken)) { if (topicHandle == null) { throw new ValidationException(ValidationRules.CannotBeNull, "topicHandle"); } if (request == null) { throw new ValidationException(ValidationRules.CannotBeNull, "request"); } if (request != null) { request.Validate(); } if (authorization == null) { throw new ValidationException(ValidationRules.CannotBeNull, "authorization"); } // Tracing bool _shouldTrace = ServiceClientTracing.IsEnabled; string _invocationId = null; if (_shouldTrace) { _invocationId = ServiceClientTracing.NextInvocationId.ToString(); Dictionary <string, object> tracingParameters = new Dictionary <string, object>(); tracingParameters.Add("topicHandle", topicHandle); tracingParameters.Add("request", request); tracingParameters.Add("authorization", authorization); tracingParameters.Add("cancellationToken", cancellationToken); ServiceClientTracing.Enter(_invocationId, this, "PostComment", tracingParameters); } // Construct URL var _baseUrl = this.Client.BaseUri.AbsoluteUri; var _url = new Uri(new Uri(_baseUrl + (_baseUrl.EndsWith("/") ? "" : "/")), "v0.7/topics/{topicHandle}/comments").ToString(); _url = _url.Replace("{topicHandle}", Uri.EscapeDataString(topicHandle)); // Create HTTP transport objects HttpRequestMessage _httpRequest = new HttpRequestMessage(); HttpResponseMessage _httpResponse = null; _httpRequest.Method = new HttpMethod("POST"); _httpRequest.RequestUri = new Uri(_url); // Set Headers if (authorization != null) { if (_httpRequest.Headers.Contains("Authorization")) { _httpRequest.Headers.Remove("Authorization"); } _httpRequest.Headers.TryAddWithoutValidation("Authorization", authorization); } if (customHeaders != null) { foreach (var _header in customHeaders) { if (_httpRequest.Headers.Contains(_header.Key)) { _httpRequest.Headers.Remove(_header.Key); } _httpRequest.Headers.TryAddWithoutValidation(_header.Key, _header.Value); } } // Serialize Request string _requestContent = null; if (request != null) { _requestContent = SafeJsonConvert.SerializeObject(request, this.Client.SerializationSettings); _httpRequest.Content = new StringContent(_requestContent, Encoding.UTF8); _httpRequest.Content.Headers.ContentType = MediaTypeHeaderValue.Parse("application/json; charset=utf-8"); } // Send Request if (_shouldTrace) { ServiceClientTracing.SendRequest(_invocationId, _httpRequest); } cancellationToken.ThrowIfCancellationRequested(); _httpResponse = await this.Client.HttpClient.SendAsync(_httpRequest, cancellationToken).ConfigureAwait(false); if (_shouldTrace) { ServiceClientTracing.ReceiveResponse(_invocationId, _httpResponse); } HttpStatusCode _statusCode = _httpResponse.StatusCode; cancellationToken.ThrowIfCancellationRequested(); string _responseContent = null; if ((int)_statusCode != 201 && (int)_statusCode != 400 && (int)_statusCode != 401 && (int)_statusCode != 404 && (int)_statusCode != 500) { var ex = new HttpOperationException(string.Format("Operation returned an invalid status code '{0}'", _statusCode)); _responseContent = await _httpResponse.Content.ReadAsStringAsync().ConfigureAwait(false); ex.Request = new HttpRequestMessageWrapper(_httpRequest, _requestContent); ex.Response = new HttpResponseMessageWrapper(_httpResponse, _responseContent); if (_shouldTrace) { ServiceClientTracing.Error(_invocationId, ex); } _httpRequest.Dispose(); if (_httpResponse != null) { _httpResponse.Dispose(); } throw ex; } // Create Result var _result = new HttpOperationResponse <PostCommentResponse>(); _result.Request = _httpRequest; _result.Response = _httpResponse; // Deserialize Response if ((int)_statusCode == 201) { _responseContent = await _httpResponse.Content.ReadAsStringAsync().ConfigureAwait(false); try { _result.Body = SafeJsonConvert.DeserializeObject <PostCommentResponse>(_responseContent, this.Client.DeserializationSettings); } catch (JsonException ex) { _httpRequest.Dispose(); if (_httpResponse != null) { _httpResponse.Dispose(); } throw new SerializationException("Unable to deserialize the response.", _responseContent, ex); } } if (_shouldTrace) { ServiceClientTracing.Exit(_invocationId, _result); } return(_result); }
/// <summary> /// Create a new comment /// </summary> /// <param name='operations'> /// The operations group for this extension method. /// </param> /// <param name='topicHandle'> /// Topic handle /// </param> /// <param name='request'> /// Post comment request /// </param> /// <param name='authorization'> /// Format is: "Scheme CredentialsList". Possible values are: /// /// - Anon AK=AppKey /// /// - SocialPlus TK=SessionToken /// /// - Facebook AK=AppKey|TK=AccessToken /// /// - Google AK=AppKey|TK=AccessToken /// /// - Twitter AK=AppKey|RT=RequestToken|TK=AccessToken /// /// - Microsoft AK=AppKey|TK=AccessToken /// /// - AADS2S AK=AppKey|[UH=UserHandle]|TK=AADToken /// </param> /// <param name='cancellationToken'> /// The cancellation token. /// </param> public static async Task <PostCommentResponse> PostCommentAsync(this ITopicComments operations, string topicHandle, PostCommentRequest request, string authorization, CancellationToken cancellationToken = default(CancellationToken)) { using (var _result = await operations.PostCommentWithHttpMessagesAsync(topicHandle, request, authorization, null, cancellationToken).ConfigureAwait(false)) { return(_result.Body); } }
/// <summary> /// Create a new comment /// </summary> /// <param name='operations'> /// The operations group for this extension method. /// </param> /// <param name='topicHandle'> /// Topic handle /// </param> /// <param name='request'> /// Post comment request /// </param> /// <param name='authorization'> /// Format is: "Scheme CredentialsList". Possible values are: /// /// - Anon AK=AppKey /// /// - SocialPlus TK=SessionToken /// /// - Facebook AK=AppKey|TK=AccessToken /// /// - Google AK=AppKey|TK=AccessToken /// /// - Twitter AK=AppKey|RT=RequestToken|TK=AccessToken /// /// - Microsoft AK=AppKey|TK=AccessToken /// /// - AADS2S AK=AppKey|[UH=UserHandle]|TK=AADToken /// </param> public static PostCommentResponse PostComment(this ITopicComments operations, string topicHandle, PostCommentRequest request, string authorization) { return(Task.Factory.StartNew(s => ((ITopicComments)s).PostCommentAsync(topicHandle, request, authorization), operations, CancellationToken.None, TaskCreationOptions.None, TaskScheduler.Default).Unwrap().GetAwaiter().GetResult()); }
public async Task PostGetLikeDeleteCommentTest() { // create 2 users SocialPlusClient client = new SocialPlusClient(TestConstants.ServerApiBaseUrl); string firstName = "Stan"; string lastName = "TopicMan"; string bio = string.Empty; PostUserResponse postUserResponse1 = await TestUtilities.DoLogin(client, firstName, lastName, bio); string auth1 = AuthHelper.CreateSocialPlusAuth(postUserResponse1.SessionToken); string firstName2 = "NotPopularNelly"; string lastName2 = "Von Jerk"; string bio2 = "Nelly is not very popular. :("; PostUserResponse postUserResponse2 = await TestUtilities.DoLogin(client, firstName2, lastName2, bio2); string auth2 = AuthHelper.CreateSocialPlusAuth(postUserResponse2.SessionToken); // create a topic from user 1 string topicTitle = "My Favorite Topic"; string topicText = "It is all about sports!"; BlobType blobType = BlobType.Custom; string blobHandle = "http://myBlobHandle/"; string language = string.Empty; string deepLink = string.Empty; string categories = string.Empty; string friendlyName = string.Empty; string group = string.Empty; PostTopicRequest postTopicRequest = new PostTopicRequest(publisherType: PublisherType.User, text: topicText, title: topicTitle, blobType: blobType, blobHandle: blobHandle, categories: categories, language: language, deepLink: deepLink, friendlyName: friendlyName, group: group); HttpOperationResponse <PostTopicResponse> postTopicOperationResponse = await client.Topics.PostTopicWithHttpMessagesAsync(request : postTopicRequest, authorization : auth1); string topicHandle = null; if (postTopicOperationResponse != null && postTopicOperationResponse.Response.IsSuccessStatusCode) { topicHandle = postTopicOperationResponse.Body.TopicHandle; } // post comment 1 with an image from user 2 HttpOperationResponse <PostImageResponse> postImageOperationResponse = await ImageTests.AddImage(new System.Uri("http://research.microsoft.com/a/i/c/ms-logo.png"), ImageType.ContentBlob, client, auth2); string imageHandle = string.Empty; PostCommentRequest postCommentRequest1 = null; HttpOperationResponse <PostCommentResponse> postCommentOperationResponse1 = null; if (!string.IsNullOrWhiteSpace(topicHandle) && postImageOperationResponse != null && postImageOperationResponse.Response.IsSuccessStatusCode) { // get the image handle imageHandle = postImageOperationResponse.Body.BlobHandle; // create a comment from user 2 postCommentRequest1 = new PostCommentRequest(text: "my comment", blobType: BlobType.Image, blobHandle: imageHandle, language: "gibberish"); postCommentOperationResponse1 = await client.TopicComments.PostCommentWithHttpMessagesAsync(topicHandle : topicHandle, request : postCommentRequest1, authorization : auth2); } // post comment 2 from user 1 PostCommentRequest postCommentRequest2 = null; HttpOperationResponse <PostCommentResponse> postCommentOperationResponse2 = null; if (!string.IsNullOrWhiteSpace(topicHandle)) { // create a comment from user 1 postCommentRequest2 = new PostCommentRequest(text: "another comment"); postCommentOperationResponse2 = await client.TopicComments.PostCommentWithHttpMessagesAsync(topicHandle : topicHandle, request : postCommentRequest2, authorization : auth1); } // get comment handles string commentHandle1 = null; string commentHandle2 = null; if (postCommentOperationResponse1 != null && postCommentOperationResponse1.Response.IsSuccessStatusCode) { commentHandle1 = postCommentOperationResponse1.Body.CommentHandle; } if (postCommentOperationResponse2 != null && postCommentOperationResponse2.Response.IsSuccessStatusCode) { commentHandle2 = postCommentOperationResponse2.Body.CommentHandle; } // like comment 2 by user 2 HttpOperationResponse <object> postLikeOperationResponse = null; if (!string.IsNullOrWhiteSpace(commentHandle2)) { postLikeOperationResponse = await client.CommentLikes.PostLikeWithHttpMessagesAsync(commentHandle : commentHandle2, authorization : auth2); } // get topic & comment feed from the server as user 2 HttpOperationResponse <TopicView> getTopicOperationResponse = null; HttpOperationResponse <FeedResponseCommentView> getCommentsOperationResponse1 = null; if (!string.IsNullOrWhiteSpace(commentHandle1) && !string.IsNullOrWhiteSpace(commentHandle2)) { getTopicOperationResponse = await client.Topics.GetTopicWithHttpMessagesAsync(topicHandle : topicHandle, authorization : auth1); getCommentsOperationResponse1 = await client.TopicComments.GetTopicCommentsWithHttpMessagesAsync(topicHandle : topicHandle, authorization : auth2); } // try to delete comment 1 with wrong bearer token HttpOperationResponse <object> deleteCommentOperationResponse1 = null; if (postCommentOperationResponse1 != null && postCommentOperationResponse1.Response.IsSuccessStatusCode) { deleteCommentOperationResponse1 = await client.Comments.DeleteCommentWithHttpMessagesAsync(commentHandle : commentHandle1, authorization : auth1); } // delete comment 1 with good bearer token HttpOperationResponse <object> deleteCommentOperationResponse2 = null; if (postCommentOperationResponse1 != null && postCommentOperationResponse1.Response.IsSuccessStatusCode) { deleteCommentOperationResponse2 = await client.Comments.DeleteCommentWithHttpMessagesAsync(commentHandle : commentHandle1, authorization : auth2); } // get comment feed from the server as user 1 HttpOperationResponse <FeedResponseCommentView> getCommentsOperationResponse2 = null; if (!string.IsNullOrWhiteSpace(commentHandle2)) { getCommentsOperationResponse2 = await client.TopicComments.GetTopicCommentsWithHttpMessagesAsync(topicHandle : topicHandle, authorization : auth1); } // delete comment 1 again with good bearer token HttpOperationResponse <object> deleteCommentOperationResponse3 = null; if (postCommentOperationResponse1 != null && postCommentOperationResponse1.Response.IsSuccessStatusCode) { deleteCommentOperationResponse3 = await client.Comments.DeleteCommentWithHttpMessagesAsync(commentHandle : commentHandle1, authorization : auth2); } // delete comment 2 HttpOperationResponse <object> deleteCommentOperationResponse4 = null; if (postCommentOperationResponse2 != null && postCommentOperationResponse2.Response.IsSuccessStatusCode) { deleteCommentOperationResponse4 = await client.Comments.DeleteCommentWithHttpMessagesAsync(commentHandle : commentHandle2, authorization : auth1); } // no need to delete the image because there is no image delete API // delete topic HttpOperationResponse <object> deleteTopicOperationResponse = null; if (!string.IsNullOrWhiteSpace(topicHandle)) { deleteTopicOperationResponse = await client.Topics.DeleteTopicWithHttpMessagesAsync(topicHandle : topicHandle, authorization : auth1); } // delete users HttpOperationResponse <object> deleteUserOperationResponse1 = await client.Users.DeleteUserWithHttpMessagesAsync(authorization : auth1); HttpOperationResponse <object> deleteUserOperationResponse2 = await client.Users.DeleteUserWithHttpMessagesAsync(authorization : auth2); // check failure conditions Assert.IsTrue(postTopicOperationResponse.Response.IsSuccessStatusCode); Assert.IsTrue(deleteTopicOperationResponse.Response.IsSuccessStatusCode); Assert.IsTrue(postImageOperationResponse.Response.IsSuccessStatusCode); Assert.IsTrue(postCommentOperationResponse1.Response.IsSuccessStatusCode); Assert.IsTrue(postCommentOperationResponse2.Response.IsSuccessStatusCode); Assert.IsTrue(deleteUserOperationResponse1.Response.IsSuccessStatusCode); Assert.IsTrue(deleteUserOperationResponse2.Response.IsSuccessStatusCode); Assert.IsTrue(getTopicOperationResponse.Response.IsSuccessStatusCode); Assert.IsTrue(getCommentsOperationResponse1.Response.IsSuccessStatusCode); Assert.IsTrue(getCommentsOperationResponse2.Response.IsSuccessStatusCode); Assert.IsTrue(postLikeOperationResponse.Response.IsSuccessStatusCode); Assert.IsFalse(deleteCommentOperationResponse1.Response.IsSuccessStatusCode); Assert.IsTrue(deleteCommentOperationResponse2.Response.IsSuccessStatusCode); Assert.IsFalse(deleteCommentOperationResponse3.Response.IsSuccessStatusCode); Assert.IsTrue(deleteCommentOperationResponse4.Response.IsSuccessStatusCode); // check response values Assert.IsFalse(string.IsNullOrWhiteSpace(postUserResponse1.SessionToken)); Assert.IsFalse(string.IsNullOrWhiteSpace(postUserResponse2.SessionToken)); Assert.IsFalse(string.IsNullOrWhiteSpace(topicHandle)); Assert.IsFalse(string.IsNullOrWhiteSpace(imageHandle)); Assert.IsFalse(string.IsNullOrWhiteSpace(commentHandle1)); Assert.IsFalse(string.IsNullOrWhiteSpace(commentHandle2)); Assert.AreEqual(getTopicOperationResponse.Body.TotalComments, 2); Assert.AreEqual(getCommentsOperationResponse1.Body.Data.Count, 2); Assert.AreEqual(getCommentsOperationResponse2.Body.Data.Count, 1); // check comment 1 in comment feed 1 (oldest in the feed) Assert.AreEqual(getCommentsOperationResponse1.Body.Data[1].BlobHandle, postCommentRequest1.BlobHandle); Assert.AreEqual(getCommentsOperationResponse1.Body.Data[1].BlobType, postCommentRequest1.BlobType); Assert.IsTrue(getCommentsOperationResponse1.Body.Data[1].BlobUrl.Contains(imageHandle)); Assert.AreEqual(getCommentsOperationResponse1.Body.Data[1].CommentHandle, commentHandle1); Assert.IsTrue(getCommentsOperationResponse1.Body.Data[1].ContentStatus == ContentStatus.Active || getCommentsOperationResponse1.Body.Data[1].ContentStatus == ContentStatus.Clean); Assert.AreEqual(getCommentsOperationResponse1.Body.Data[1].Language, postCommentRequest1.Language); Assert.IsFalse(getCommentsOperationResponse1.Body.Data[1].Liked); Assert.AreEqual(getCommentsOperationResponse1.Body.Data[1].Text, postCommentRequest1.Text); Assert.AreEqual(getCommentsOperationResponse1.Body.Data[1].TopicHandle, topicHandle); Assert.AreEqual(getCommentsOperationResponse1.Body.Data[1].TotalLikes, 0); Assert.AreEqual(getCommentsOperationResponse1.Body.Data[1].TotalReplies, 0); Assert.AreEqual(getCommentsOperationResponse1.Body.Data[1].User.UserHandle, postUserResponse2.UserHandle); // check comment 2 in comment feed 1 (earliest in the feed) Assert.AreEqual(getCommentsOperationResponse1.Body.Data[0].BlobHandle, postCommentRequest2.BlobHandle); Assert.AreEqual(getCommentsOperationResponse1.Body.Data[0].BlobType, BlobType.Unknown); Assert.IsTrue(string.IsNullOrEmpty(getCommentsOperationResponse1.Body.Data[0].BlobUrl)); Assert.AreEqual(getCommentsOperationResponse1.Body.Data[0].CommentHandle, commentHandle2); Assert.IsTrue(getCommentsOperationResponse1.Body.Data[0].ContentStatus == ContentStatus.Active || getCommentsOperationResponse1.Body.Data[0].ContentStatus == ContentStatus.Clean); Assert.IsTrue(string.IsNullOrEmpty(getCommentsOperationResponse1.Body.Data[0].Language)); Assert.IsTrue(getCommentsOperationResponse1.Body.Data[0].Liked); Assert.AreEqual(getCommentsOperationResponse1.Body.Data[0].Text, postCommentRequest2.Text); Assert.AreEqual(getCommentsOperationResponse1.Body.Data[0].TopicHandle, topicHandle); Assert.AreEqual(getCommentsOperationResponse1.Body.Data[0].TotalLikes, 1); Assert.AreEqual(getCommentsOperationResponse1.Body.Data[0].TotalReplies, 0); Assert.AreEqual(getCommentsOperationResponse1.Body.Data[0].User.UserHandle, postUserResponse1.UserHandle); // check comment 2 in comment feed 2 (only one in the feed) Assert.AreEqual(getCommentsOperationResponse2.Body.Data[0].BlobHandle, postCommentRequest2.BlobHandle); Assert.AreEqual(getCommentsOperationResponse2.Body.Data[0].BlobType, BlobType.Unknown); Assert.IsTrue(string.IsNullOrEmpty(getCommentsOperationResponse2.Body.Data[0].BlobUrl)); Assert.AreEqual(getCommentsOperationResponse2.Body.Data[0].CommentHandle, commentHandle2); Assert.IsTrue(getCommentsOperationResponse2.Body.Data[0].ContentStatus == ContentStatus.Active || getCommentsOperationResponse2.Body.Data[0].ContentStatus == ContentStatus.Clean); Assert.IsTrue(string.IsNullOrEmpty(getCommentsOperationResponse2.Body.Data[0].Language)); Assert.IsFalse(getCommentsOperationResponse2.Body.Data[0].Liked); Assert.AreEqual(getCommentsOperationResponse2.Body.Data[0].Text, postCommentRequest2.Text); Assert.AreEqual(getCommentsOperationResponse2.Body.Data[0].TopicHandle, topicHandle); Assert.AreEqual(getCommentsOperationResponse2.Body.Data[0].TotalLikes, 1); Assert.AreEqual(getCommentsOperationResponse2.Body.Data[0].TotalReplies, 0); Assert.AreEqual(getCommentsOperationResponse2.Body.Data[0].User.UserHandle, postUserResponse1.UserHandle); }
public Task StartAsync(CancellationToken cancellationToken) { var postCommentRequest = new PostCommentRequest(_options); return(_requestHandler.HandleAsync(postCommentRequest)); }
public async Task <IHttpActionResult> PostComment(string topicHandle, [FromBody] PostCommentRequest request) { string className = "TopicCommentsController"; string methodName = "PostComment"; string logEntry = $"TopicHandle = {topicHandle}, BlobHandle = {request?.BlobHandle}, BlobType = {request?.BlobType}"; this.LogControllerStart(this.log, className, methodName, logEntry); var topicEntity = await this.topicsManager.ReadTopic(topicHandle); if (topicEntity == null) { return(this.NotFound(ResponseStrings.TopicNotFound)); } if (topicEntity.AppHandle != this.AppHandle) { return(this.Unauthorized(ResponseStrings.NotAllowed)); } string commentHandle = this.handleGenerator.GenerateShortHandle(); await this.commentsManager.CreateComment( ProcessType.Frontend, commentHandle, request.Text, request.BlobType, request.BlobHandle, request.Language, this.UserHandle, topicHandle, topicEntity.PublisherType, topicEntity.UserHandle, DateTime.UtcNow, ReviewStatus.Active, this.AppHandle, null); // Log new comment to app metrics this.applicationMetrics.Comment( ProcessType.Frontend.ToString(), commentHandle, request.Text, request.BlobType.ToString(), request.BlobHandle, request.Language, this.UserHandle, topicHandle, topicEntity.PublisherType.ToString(), topicEntity.UserHandle, DateTime.UtcNow, ReviewStatus.Active.ToString(), this.AppHandle); var response = new PostCommentResponse() { CommentHandle = commentHandle }; logEntry += $", CommentHandle = {commentHandle}"; this.LogControllerEnd(this.log, className, methodName, logEntry); return(this.Created <PostCommentResponse>(new Uri(this.Request.RequestUri.AbsoluteUri + "/" + commentHandle), response)); }
/// <summary> /// Helper routine to perform the main actions of the test /// Create a topic, comment and reply. Like the comment and the reply. Then delete /// both the comment like and the reply like. /// </summary> /// <param name="appPublished">flag to indicate if topic is app published</param> /// <param name="appHandle">app handle</param> /// <returns>Fail if an exception is hit</returns> public async Task LikeCommentReplyDeleteTestHelper(bool appPublished, string appHandle) { // Set up initial login etc SocialPlusClient client = new SocialPlusClient(TestConstants.ServerApiBaseUrl); PostUserResponse postUserResponse; string firstName = "R2D2"; string lastName = "Robot"; string bio = string.Empty; postUserResponse = await TestUtilities.DoLogin(client, firstName, lastName, bio); string auth = AuthHelper.CreateSocialPlusAuth(postUserResponse.SessionToken); string userHandle = postUserResponse.UserHandle; if (appPublished) { // add user as admin bool added = ManageAppsUtils.AddAdmin(TestConstants.EnvironmentName, appHandle, userHandle); if (!added) { // delete the user and fail the test await client.Users.DeleteUserWithHttpMessagesAsync(authorization : auth); Assert.Fail("Failed to set user as administrator"); } } string topicTitle = "Topic for Like Reply Test"; string topicText = "Verify the Like Reply!"; BlobType blobType = BlobType.Unknown; string blobHandle = "http://myBlobHandle/"; string language = "en-US"; string deepLink = "Like It!"; string categories = "#likes; #reply"; string friendlyName = "LT"; string group = "mygroup"; string topicHandle = string.Empty; var postTopicRequest = new PostTopicRequest() { Title = topicTitle, Text = topicText, BlobType = blobType, BlobHandle = blobHandle, Language = language, DeepLink = deepLink, Categories = categories, FriendlyName = friendlyName, Group = group }; if (appPublished) { postTopicRequest.PublisherType = PublisherType.App; } else { postTopicRequest.PublisherType = PublisherType.User; } var postTopicOperationResponse = await client.Topics.PostTopicWithHttpMessagesAsync(request : postTopicRequest, authorization : auth); if (postTopicOperationResponse.Response.IsSuccessStatusCode) { // extract topic handle from the response topicHandle = postTopicOperationResponse.Body.TopicHandle; } else { if (appPublished) { ManageAppsUtils.DeleteAdmin(TestConstants.EnvironmentName, appHandle, userHandle); } await client.Users.DeleteUserWithHttpMessagesAsync(authorization : auth); Assert.Fail("Post topic failed."); } // Post a comment to topic string commentHandle = string.Empty; var postCommentRequest = new PostCommentRequest() { Text = "My First Comment!", Language = "en-US" }; var postCommentOperationResponse = await client.TopicComments.PostCommentWithHttpMessagesAsync(topicHandle : topicHandle, request : postCommentRequest, authorization : auth); if (postCommentOperationResponse.Response.IsSuccessStatusCode) { commentHandle = postCommentOperationResponse.Body.CommentHandle; } else { await client.Topics.DeleteTopicWithHttpMessagesAsync(topicHandle : topicHandle, authorization : auth); if (appPublished) { ManageAppsUtils.DeleteAdmin(TestConstants.EnvironmentName, appHandle, userHandle); } await client.Users.DeleteUserWithHttpMessagesAsync(authorization : auth); Assert.Fail("Post comment failed."); } // Post a reply to the comment to topic string replyHandle = string.Empty; var postReplyRequest = new PostReplyRequest() { Text = "My First Reply", Language = "en-US" }; var postReplyOperationResponse = await client.CommentReplies.PostReplyWithHttpMessagesAsync(commentHandle, request : postReplyRequest, authorization : auth); if (postReplyOperationResponse.Response.IsSuccessStatusCode) { replyHandle = postReplyOperationResponse.Body.ReplyHandle; } else { await client.Comments.DeleteCommentWithHttpMessagesAsync(commentHandle : commentHandle, authorization : auth); await client.Topics.DeleteTopicWithHttpMessagesAsync(topicHandle : topicHandle, authorization : auth); if (appPublished) { ManageAppsUtils.DeleteAdmin(TestConstants.EnvironmentName, appHandle, userHandle); } await client.Users.DeleteUserWithHttpMessagesAsync(authorization : auth); Assert.Fail("Post reply failed."); } // Post a like on the Comment var postLikeOperationResponse = await client.CommentLikes.PostLikeWithHttpMessagesAsync(commentHandle : commentHandle, authorization : auth); if (!postLikeOperationResponse.Response.IsSuccessStatusCode) { await client.Replies.DeleteReplyWithHttpMessagesAsync(replyHandle : replyHandle, authorization : auth); await client.Comments.DeleteCommentWithHttpMessagesAsync(commentHandle : commentHandle, authorization : auth); if (appPublished) { ManageAppsUtils.DeleteAdmin(TestConstants.EnvironmentName, appHandle, userHandle); } await client.Topics.DeleteTopicWithHttpMessagesAsync(topicHandle : topicHandle, authorization : auth); await client.Users.DeleteUserWithHttpMessagesAsync(authorization : auth); Assert.Fail("Post like on comment failed."); } // Post a like on the Reply var postLikeOperationResponse2 = await client.ReplyLikes.PostLikeWithHttpMessagesAsync(replyHandle : replyHandle, authorization : auth); if (!postLikeOperationResponse2.Response.IsSuccessStatusCode) { await client.CommentLikes.DeleteLikeWithHttpMessagesAsync(commentHandle : commentHandle, authorization : auth); await client.Replies.DeleteReplyWithHttpMessagesAsync(replyHandle : replyHandle, authorization : auth); await client.Comments.DeleteCommentWithHttpMessagesAsync(commentHandle : commentHandle, authorization : auth); await client.Topics.DeleteTopicWithHttpMessagesAsync(topicHandle : topicHandle, authorization : auth); if (appPublished) { ManageAppsUtils.DeleteAdmin(TestConstants.EnvironmentName, appHandle, userHandle); } await client.Users.DeleteUserWithHttpMessagesAsync(authorization : auth); Assert.Fail("Post like on reply failed."); } // Get topic to later verify var getTopicOperationResponse = await client.Topics.GetTopicWithHttpMessagesAsync(topicHandle : topicHandle, authorization : auth); if (!getTopicOperationResponse.Response.IsSuccessStatusCode) { await client.ReplyLikes.DeleteLikeWithHttpMessagesAsync(replyHandle : replyHandle, authorization : auth); await client.CommentLikes.DeleteLikeWithHttpMessagesAsync(commentHandle : commentHandle, authorization : auth); await client.Replies.DeleteReplyWithHttpMessagesAsync(replyHandle : replyHandle, authorization : auth); await client.Comments.DeleteCommentWithHttpMessagesAsync(commentHandle : commentHandle, authorization : auth); await client.Topics.DeleteTopicWithHttpMessagesAsync(topicHandle : topicHandle, authorization : auth); if (appPublished) { ManageAppsUtils.DeleteAdmin(TestConstants.EnvironmentName, appHandle, userHandle); } await client.Users.DeleteUserWithHttpMessagesAsync(authorization : auth); Assert.Fail("Get topic failed."); } // Get comment to later verify the like is there var getCommentOperationResponse = await client.Comments.GetCommentWithHttpMessagesAsync(commentHandle : commentHandle, authorization : auth); if (!getCommentOperationResponse.Response.IsSuccessStatusCode) { await client.ReplyLikes.DeleteLikeWithHttpMessagesAsync(replyHandle : replyHandle, authorization : auth); await client.CommentLikes.DeleteLikeWithHttpMessagesAsync(commentHandle : commentHandle, authorization : auth); await client.Replies.DeleteReplyWithHttpMessagesAsync(replyHandle : replyHandle, authorization : auth); await client.Comments.DeleteCommentWithHttpMessagesAsync(commentHandle : commentHandle, authorization : auth); await client.Topics.DeleteTopicWithHttpMessagesAsync(topicHandle : topicHandle, authorization : auth); if (appPublished) { ManageAppsUtils.DeleteAdmin(TestConstants.EnvironmentName, appHandle, userHandle); } await client.Users.DeleteUserWithHttpMessagesAsync(authorization : auth); Assert.Fail("Get comment failed."); } // Get reply to later verify the like is there var getReplyOperationResponse = await client.Replies.GetReplyWithHttpMessagesAsync(replyHandle : replyHandle, authorization : auth); if (!getReplyOperationResponse.Response.IsSuccessStatusCode) { await client.ReplyLikes.DeleteLikeWithHttpMessagesAsync(replyHandle : replyHandle, authorization : auth); await client.CommentLikes.DeleteLikeWithHttpMessagesAsync(commentHandle : commentHandle, authorization : auth); await client.Replies.DeleteReplyWithHttpMessagesAsync(replyHandle : replyHandle, authorization : auth); await client.Comments.DeleteCommentWithHttpMessagesAsync(commentHandle : commentHandle, authorization : auth); await client.Topics.DeleteTopicWithHttpMessagesAsync(topicHandle : topicHandle, authorization : auth); if (appPublished) { ManageAppsUtils.DeleteAdmin(TestConstants.EnvironmentName, appHandle, userHandle); } await client.Users.DeleteUserWithHttpMessagesAsync(authorization : auth); Assert.Fail("Get reply failed."); } var deleteReplyLikeOperationResult = await client.ReplyLikes.DeleteLikeWithHttpMessagesAsync(replyHandle : replyHandle, authorization : auth); if (!deleteReplyLikeOperationResult.Response.IsSuccessStatusCode) { await client.CommentLikes.DeleteLikeWithHttpMessagesAsync(commentHandle : commentHandle, authorization : auth); await client.Replies.DeleteReplyWithHttpMessagesAsync(replyHandle : replyHandle, authorization : auth); await client.Comments.DeleteCommentWithHttpMessagesAsync(commentHandle : commentHandle, authorization : auth); await client.Topics.DeleteTopicWithHttpMessagesAsync(topicHandle : topicHandle, authorization : auth); if (appPublished) { ManageAppsUtils.DeleteAdmin(TestConstants.EnvironmentName, appHandle, userHandle); } await client.Users.DeleteUserWithHttpMessagesAsync(authorization : auth); Assert.Fail("Delete reply like failed."); } var deleteCommentLikeOperationResult = await client.CommentLikes.DeleteLikeWithHttpMessagesAsync(commentHandle : commentHandle, authorization : auth); if (!deleteCommentLikeOperationResult.Response.IsSuccessStatusCode) { await client.Replies.DeleteReplyWithHttpMessagesAsync(replyHandle : replyHandle, authorization : auth); await client.Comments.DeleteCommentWithHttpMessagesAsync(commentHandle : commentHandle, authorization : auth); await client.Topics.DeleteTopicWithHttpMessagesAsync(topicHandle : topicHandle, authorization : auth); if (appPublished) { ManageAppsUtils.DeleteAdmin(TestConstants.EnvironmentName, appHandle, userHandle); } await client.Users.DeleteUserWithHttpMessagesAsync(authorization : auth); Assert.Fail("Delete comment like failed."); } // Get comment a second time to verify the like is gone var getCommentOperationResponse2 = await client.Comments.GetCommentWithHttpMessagesAsync(commentHandle : commentHandle, authorization : auth); if (!getCommentOperationResponse2.Response.IsSuccessStatusCode) { await client.Replies.DeleteReplyWithHttpMessagesAsync(replyHandle : replyHandle, authorization : auth); await client.Comments.DeleteCommentWithHttpMessagesAsync(commentHandle : commentHandle, authorization : auth); await client.Topics.DeleteTopicWithHttpMessagesAsync(topicHandle : topicHandle, authorization : auth); if (appPublished) { ManageAppsUtils.DeleteAdmin(TestConstants.EnvironmentName, appHandle, userHandle); } await client.Users.DeleteUserWithHttpMessagesAsync(authorization : auth); Assert.Fail("Get comment failed."); } // Get reply a second time to verify the like is gone var getReplyOperationResponse2 = await client.Replies.GetReplyWithHttpMessagesAsync(replyHandle : replyHandle, authorization : auth); if (!getReplyOperationResponse2.Response.IsSuccessStatusCode) { await client.Replies.DeleteReplyWithHttpMessagesAsync(replyHandle : replyHandle, authorization : auth); await client.Comments.DeleteCommentWithHttpMessagesAsync(commentHandle : commentHandle, authorization : auth); await client.Topics.DeleteTopicWithHttpMessagesAsync(topicHandle : topicHandle, authorization : auth); if (appPublished) { ManageAppsUtils.DeleteAdmin(TestConstants.EnvironmentName, appHandle, userHandle); } await client.Users.DeleteUserWithHttpMessagesAsync(authorization : auth); Assert.Fail("Get reply failed."); } // perform standard cleanup of the operations before checking assertions await client.Replies.DeleteReplyWithHttpMessagesAsync(replyHandle : replyHandle, authorization : auth); await client.Comments.DeleteCommentWithHttpMessagesAsync(commentHandle : commentHandle, authorization : auth); await client.Topics.DeleteTopicWithHttpMessagesAsync(topicHandle : topicHandle, authorization : auth); if (appPublished) { ManageAppsUtils.DeleteAdmin(TestConstants.EnvironmentName, appHandle, userHandle); } await client.Users.DeleteUserWithHttpMessagesAsync(authorization : auth); var getTopicResponse = getTopicOperationResponse.Body; var getCommentResponse = getCommentOperationResponse.Body; var getReplyResponse = getReplyOperationResponse.Body; var getCommentResponse2 = getCommentOperationResponse2.Body; var getReplyResponse2 = getReplyOperationResponse2.Body; // Verify topic info Assert.AreEqual(getTopicResponse.Liked, false); Assert.AreEqual(getTopicResponse.Text, topicText); Assert.AreEqual(getTopicResponse.BlobType, BlobType.Unknown); Assert.AreEqual(getTopicResponse.BlobHandle, blobHandle); Assert.AreEqual(getTopicResponse.Language, language); Assert.AreEqual(getTopicResponse.DeepLink, deepLink); Assert.AreEqual(getTopicResponse.Categories, categories); Assert.AreEqual(getTopicResponse.FriendlyName, friendlyName); Assert.AreEqual(getTopicResponse.Group, group); Assert.AreEqual(getTopicResponse.TotalComments, 1); Assert.AreEqual(getTopicResponse.TotalLikes, 0); // Verify comment info Assert.AreEqual(getCommentResponse.Liked, true); Assert.AreEqual(getCommentResponse.Text, "My First Comment!"); Assert.AreEqual(getCommentResponse.Language, "en-US"); Assert.AreEqual(getCommentResponse.TotalReplies, 1); Assert.AreEqual(getCommentResponse.TotalLikes, 1); // Verify reply info Assert.AreEqual(getReplyResponse.Liked, true); Assert.AreEqual(getReplyResponse.Text, "My First Reply"); Assert.AreEqual(getReplyResponse.Language, "en-US"); Assert.AreEqual(getReplyResponse.TotalLikes, 1); // Verify comment info Assert.AreEqual(getCommentResponse2.Liked, false); Assert.AreEqual(getCommentResponse2.Text, "My First Comment!"); Assert.AreEqual(getCommentResponse2.Language, "en-US"); Assert.AreEqual(getCommentResponse2.TotalReplies, 1); Assert.AreEqual(getCommentResponse2.TotalLikes, 0); // Verify reply info Assert.AreEqual(getReplyResponse2.Liked, false); Assert.AreEqual(getReplyResponse2.Text, "My First Reply"); Assert.AreEqual(getReplyResponse2.Language, "en-US"); Assert.AreEqual(getReplyResponse2.TotalLikes, 0); }