public async Task <IActionResult> Post(string url) { var service = new PostService(); var post = await service.GetPost(url); UserCookieData data = Deserialize(_crypto.Decrypt(HttpContext.Request.Cookies[userCookieId])); if ((DateTime.UtcNow - data.LastAccessed).TotalHours > 12) { data = new UserCookieData(); } if (!data.ViewedPostIds.Contains(post.Id)) { data.ViewedPostIds.Add(post.Id); string encryptedCookie = _crypto.Encrypt(GetJson(data.ViewedPostIds)); HttpContext.Response.Cookies.Delete(userCookieId); HttpContext.Response.Cookies.Append(userCookieId, encryptedCookie, new CookieOptions() { Expires = DateTimeOffset.Now.AddHours(24) }); new IncrementPostViewCount().HandleAsync(post.Id); } ViewBag.Title = post.Title + " - YouIT"; if (post.Text.Length > 135) { ViewBag.MetaDescription = post.Text.Substring(0, 135).Replace("<p>", "").Replace("</p>", "") + "..."; } //TODO: introduce UI services layer and move it to there and refactor //if (HttpContext.User.Identity.IsAuthenticated) //{ // string userId = UserId; // foreach (var comment in post.Comments) // { // if (comment.WhoLiked.Contains(userId)) // comment.UserReaction = UserReaction.Liked; // else if (comment.WhoDisliked.Contains(userId)) // comment.UserReaction = UserReaction.Disliked; // else comment.UserReaction = UserReaction.None; // foreach (var answer in comment.Answers) // { // if (answer.WhoLiked.Contains(userId)) // answer.UserReaction = UserReaction.Liked; // else if (answer.WhoDisliked.Contains(userId)) // answer.UserReaction = UserReaction.Disliked; // else answer.UserReaction = UserReaction.None; // } // } //} return(View(post)); }
private string GetJson(List <string> postIds) { var data = new UserCookieData { ViewedPostIds = postIds, LastAccessed = DateTime.UtcNow }; return(JsonConvert.SerializeObject(data)); }