Finish unread posts

This commit is contained in:
Michael 2017-03-27 16:11:39 -04:00
parent 93734dd552
commit 32e2eecdd4
6 changed files with 91 additions and 1 deletions

View file

@ -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)));

View file

@ -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)
{

View file

@ -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();
}
}

View file

@ -533,6 +533,7 @@
<Content Include="Views\Manage\UploadAvatar.cshtml" />
<Content Include="Views\Admin\Backups.cshtml" />
<Content Include="Views\Download\Index.cshtml" />
<Content Include="Views\Forum\ViewUnread.cshtml" />
</ItemGroup>
<ItemGroup>
<Folder Include="App_Data\" />

View file

@ -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);
}
}
<div class="row panel panel-default">
<div class="panel-body">
<div class="col-xs-4"> <!--Mini profile data-->

View file

@ -0,0 +1,34 @@
@model Project_Unite.Models.ForumPost[]
@{
ViewBag.Title = "Unread posts";
}
<h2>Unread Forum Posts</h2>
<p>Here is a summary of what's been said since you last visited the forums:</p>
<table class="table">
<tr>
<th style="width:75%">Post</th>
<th>Actions</th>
</tr>
@if(Model.Length == 0)
{
<tr>
<td><em>No unread posts! You're all caught up :)</em></td>
<td></td>
</tr>
}
@foreach(var post in Model)
{
<tr>
<td>re: @Html.TopicLinkFor(post.Parent) <br/>
<p>By <strong>@Html.UserLink(post.AuthorId)</strong> on @post.PostedAt</p>
</td>
<td>
@Html.ActionLink("Mark as read", "MarkRead", "Forum", new { id = post.Id }, new { @class = "btn btn-default" });
</td>
</tr>
}
</table>