From 32e2eecdd4904383df6c377cb3ea64f8f0a8f1e4 Mon Sep 17 00:00:00 2001 From: Michael Date: Mon, 27 Mar 2017 16:11:39 -0400 Subject: [PATCH] Finish unread posts --- Project-Unite/ACL.cs | 29 +++++++++++++++++ Project-Unite/Controllers/ForumController.cs | 16 +++++++++ Project-Unite/Models/IdentityModels.cs | 4 ++- Project-Unite/Project-Unite.csproj | 1 + Project-Unite/Views/Forum/ViewTopic.cshtml | 8 +++++ Project-Unite/Views/Forum/ViewUnread.cshtml | 34 ++++++++++++++++++++ 6 files changed, 91 insertions(+), 1 deletion(-) create mode 100644 Project-Unite/Views/Forum/ViewUnread.cshtml diff --git a/Project-Unite/ACL.cs b/Project-Unite/ACL.cs index ca356f9..36eba6e 100644 --- a/Project-Unite/ACL.cs +++ b/Project-Unite/ACL.cs @@ -62,6 +62,35 @@ namespace Project_Unite return hpr.Raw(builder.ToString()); } + public static bool IsUnlisted(string topicId) + { + return new ApplicationDbContext().ForumTopics.FirstOrDefault(x => x.Id == topicId).IsUnlisted; + } + + public static bool IsUnread(string user, string postId) + { + var db = new ApplicationDbContext(); + var u = db.Users.FirstOrDefault(x => x.Id == user); + return u.UnreadPosts.FirstOrDefault(x => x.Id == postId) != null; + } + + public static void MarkRead(string userId, string postId) + { + var mark = new ReadPost(); + mark.Id = Guid.NewGuid().ToString(); + mark.PostId = postId; + mark.UserId = userId; + var db = new ApplicationDbContext(); + db.ReadPosts.Add(mark); + db.SaveChanges(); + } + + public static IHtmlString TopicLinkFor(this HtmlHelper hpr, string topicId) + { + var topic = new ApplicationDbContext().ForumTopics.FirstOrDefault(x => x.Id == topicId); + return hpr.ActionLink(topic.Subject, "ViewTopic", "Forum", new { id = topic.Discriminator }, null); + } + public static IHtmlString Markdown(this HtmlHelper hpr, string md) { return hpr.Raw(CommonMark.CommonMarkConverter.Convert(hpr.Encode(md))); diff --git a/Project-Unite/Controllers/ForumController.cs b/Project-Unite/Controllers/ForumController.cs index f64bcfb..b65ed3e 100644 --- a/Project-Unite/Controllers/ForumController.cs +++ b/Project-Unite/Controllers/ForumController.cs @@ -302,6 +302,22 @@ namespace Project_Unite.Controllers const string AllowedChars = "ABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890abcdefghijklmnopqrstuvwxyz"; + [Authorize] + public ActionResult ViewUnread() + { + var db = new ApplicationDbContext(); + var posts = db.Users.FirstOrDefault(x => x.UserName == User.Identity.Name).UnreadPosts; + + return View(posts); + } + + [Authorize] + public ActionResult MarkRead(string id) + { + ACL.MarkRead(User.Identity.GetUserId(), id); + return RedirectToAction("ViewUnread"); + } + [Authorize] public ActionResult ViewTopic(string id, bool triedtolikeowntopic = false) { diff --git a/Project-Unite/Models/IdentityModels.cs b/Project-Unite/Models/IdentityModels.cs index 6cfb821..bf66238 100644 --- a/Project-Unite/Models/IdentityModels.cs +++ b/Project-Unite/Models/IdentityModels.cs @@ -44,7 +44,9 @@ namespace Project_Unite.Models get { var db = new ApplicationDbContext(); - var posts = db.ForumPosts.Where(x => db.ReadPosts.FirstOrDefault(y => y.UserId == this.Id && y.PostId == x.Id) == null); + + + var posts = db.ForumPosts.Where(x => ACL.IsUnlisted(x.Parent) && db.ReadPosts.FirstOrDefault(y => y.UserId == this.Id && y.PostId == x.Id) == null); return posts.ToArray(); } } diff --git a/Project-Unite/Project-Unite.csproj b/Project-Unite/Project-Unite.csproj index 3e3bd11..74f2ef4 100644 --- a/Project-Unite/Project-Unite.csproj +++ b/Project-Unite/Project-Unite.csproj @@ -533,6 +533,7 @@ + diff --git a/Project-Unite/Views/Forum/ViewTopic.cshtml b/Project-Unite/Views/Forum/ViewTopic.cshtml index 4348518..ceeaa97 100644 --- a/Project-Unite/Views/Forum/ViewTopic.cshtml +++ b/Project-Unite/Views/Forum/ViewTopic.cshtml @@ -32,6 +32,14 @@ @foreach (var post in Model.Posts.OrderBy(x => x.PostedAt)) { + if (Request.IsAuthenticated) + { + if (ACL.IsUnread(User.Identity.GetUserId(), post.Id)) + { + ACL.MarkRead(User.Identity.GetUserId(), post.Id); + } + } +
diff --git a/Project-Unite/Views/Forum/ViewUnread.cshtml b/Project-Unite/Views/Forum/ViewUnread.cshtml new file mode 100644 index 0000000..260999e --- /dev/null +++ b/Project-Unite/Views/Forum/ViewUnread.cshtml @@ -0,0 +1,34 @@ +@model Project_Unite.Models.ForumPost[] +@{ + ViewBag.Title = "Unread posts"; +} + +

Unread Forum Posts

+ +

Here is a summary of what's been said since you last visited the forums:

+ + + + + + + @if(Model.Length == 0) + { + + + + + } + @foreach(var post in Model) + { + + + + + + } +
PostActions
No unread posts! You're all caught up :)
re: @Html.TopicLinkFor(post.Parent)
+

By @Html.UserLink(post.AuthorId) on @post.PostedAt

+
+ @Html.ActionLink("Mark as read", "MarkRead", "Forum", new { id = post.Id }, new { @class = "btn btn-default" }); +
\ No newline at end of file