More optimizations to only get data once

This commit is contained in:
Sebastiaan Janssen
2017-01-28 20:28:24 +01:00
parent 05d9e27428
commit 8e9bafd0d7
6 changed files with 105 additions and 57 deletions
+58 -4
View File
@@ -1,6 +1,54 @@
@inherits UmbracoTemplatePage
@using OurUmbraco.Forum.Extensions
@using OurUmbraco.Forum.Services
@using OurUmbraco.NotificationsWeb.Library
@inherits OurUmbraco.Our.Models.OurUmbracoTemplatePage
@{
Layout = "~/Views/Master.cshtml";
var topicService = new TopicService(ApplicationContext.DatabaseContext);
var topic = topicService.CurrentTopic(Context, ApplicationContext.ApplicationCache.RequestCache, MemberData);
var subscribed = false;
if (MemberData != null && MemberData.Member != null)
{
subscribed = Utils.IsSubscribedToForumTopic(topic.Id, MemberData.Member.Id);
}
var currentMember = MemberData != null && MemberData.Member != null ? MemberData.Member : null;
var topicAuthor = currentMember != null && topic.MemberId == currentMember.Id ? currentMember : Members.GetById(topic.MemberId);
var topicMembers = new List<IPublishedContent> { topicAuthor };
var memberRoles = currentMember != null && topic.MemberId == currentMember.Id
? new Dictionary<int, List<string>> { { topicAuthor.Id, MemberData.Roles.ToList() } }
: new Dictionary<int, List<string>> { { topicAuthor.Id, topicAuthor.GetRoles() } };
foreach (var comment in topic.Comments)
{
if (topicMembers.Any(x => x.Id == comment.MemberId) == false)
{
IPublishedContent commentAuthor;
if (MemberData != null && MemberData.Member != null && MemberData.Member.Id == comment.MemberId)
{
commentAuthor = currentMember;
if (memberRoles.ContainsKey(comment.MemberId) == false)
{
var roles = MemberData.Roles.ToList();
memberRoles.Add(comment.MemberId, roles);
}
}
else
{
commentAuthor = Members.GetById(comment.MemberId);
if (memberRoles.ContainsKey(comment.MemberId) == false)
{
var roles = commentAuthor.GetRoles();
memberRoles.Add(comment.MemberId, roles);
}
}
topicMembers.Add(commentAuthor);
}
}
}
<!-- FORUM OVERVIEW START -->
<section class="forum-overview">
@@ -8,15 +56,21 @@
<!-- FORUM HEADER START -->
<div class="utilities">
<!-- FORUM BREADCRUMB START -->
@Html.Action("Render", "Breadcrumb", new { linkToCurrent = true })
@Html.Action("Render", "Breadcrumb", new {linkToCurrent = true})
<!-- FORUM BREADCRUMB END -->
<!-- THREAD ACTIONS START -->
@Html.Partial("~/Views/Partials/Forum/TopicActions.cshtml")
@Html.Partial("~/Views/Partials/Forum/TopicActions.cshtml", topic, new ViewDataDictionary { { "subscribed", subscribed }, { "topicMembers", topicMembers } })
<!-- THREAD ACTIONS END -->
<div class="clear"></div>
</div>
<!-- FORUM HEADER END -->
@Html.Partial("~/Views/Partials/Forum/Thread.cshtml")
@Html.Partial("~/Views/Partials/Forum/Thread.cshtml", topic, new ViewDataDictionary
{
{ "subscribed", subscribed },
{ "content", Model.Content },
{ "topicMembers", topicMembers },
{ "memberRoles", memberRoles }
})
</div>
</section>
@Html.Partial("~/Views/Partials/Forum/TopicForm.cshtml")
@@ -5,19 +5,12 @@
@{
var currentMember = Model.MemberData != null && Model.MemberData.Member != null ? Model.MemberData.Member : null;
var answer = (int)ViewData["answer"];
var roles = new List<string>();
var commentAuthor = Members.GetById(Model.MemberId);
// author could have been deleted (spammer) so make sure to check if they exist
if (commentAuthor != null)
{
roles = commentAuthor.GetRoles();
}
var commentAuthor = (IPublishedContent)ViewData["commentAuthor"];
var roles = (List<string>)ViewData["roles"];
var topicParentId = (int)ViewData["topicParentId"];
var forum = Umbraco.TypedContent(topicParentId);
var forumReadOnly = forum.NewTopicsAllowed() == false;
}
@* If author exists and comment is either not spam or the viewer is a the author or an admin admin *@
@if (commentAuthor != null && (Model.IsSpam == false || (currentMember != null && currentMember.Id == commentAuthor.Id) || Model.MemberData != null && Model.MemberData.IsAdmin))
@@ -4,14 +4,13 @@
@{
var currentMember = Model.MemberData != null && Model.MemberData.Member != null ? Model.MemberData.Member : null;
var topicAuthor = Members.GetById(Model.MemberId);
var topicAuthor = (IPublishedContent) ViewData["topicAuthor"];
@* If author exists and question is either not spam or the viewer is a the author or an admin admin *@
if (topicAuthor != null && (Model.IsSpam == false || (currentMember != null && currentMember.Id == topicAuthor.Id) || Model.MemberData != null && Model.MemberData.IsAdmin))
{
var forum = Umbraco.TypedContent(Model.ParentId);
var roles = topicAuthor.GetRoles();
var roles = (List<string>)ViewData["roles"];
<li class="comment question" id="comment-@Model.Id" data-deeplink="@Model.Title" data-version="@Model.Version" data-cat="@Model.ParentId">
<!-- Start of question -->
@@ -1,19 +1,20 @@
@inherits OurUmbraco.Our.Models.OurUmbracoTemplatePage
@model ReadOnlyTopic
@using System.Web.Mvc.Html
@using OurUmbraco.Forum.Extensions
@using OurUmbraco.Forum.Services
@using OurUmbraco.NotificationsWeb.Library
@using OurUmbraco.Forum.Models
@using Umbraco.Core.Logging
@{
var topicService = new TopicService(ApplicationContext.DatabaseContext);
var topic = topicService.CurrentTopic(Context, ApplicationContext.ApplicationCache.RequestCache, MemberData);
var notification = Model.Content.GetPropertyValue<string>("mainNotification");
var topic = Model;
var content = (IPublishedContent)ViewData["content"];
var subscribed = (bool)ViewData["subscribed"];
var memberRoles = (Dictionary<int, List<string>>)ViewData["memberRoles"];
var notification = content.GetPropertyValue<string>("mainNotification");
if (string.IsNullOrWhiteSpace(notification))
{
notification = "This forum is in read only mode, you can no longer reply";
}
var topicMembers = (List<IPublishedContent>)ViewData["topicMembers"];
}
<!-- COPY LINK -->
@@ -32,7 +33,7 @@
<!-- COPY LINK END -->
<!-- DELETE THREAD/COMMENT START -->
@if (MemberData != null)
@if (Model.MemberData != null)
{
<div id="confirm-wrapper" class="confirm-wrapper">
<h4>Are you sure?</h4>
@@ -55,7 +56,7 @@
<!-- FLAG SPAM END -->
@if (topic != null)
{
if (Model.Content.NewTopicsAllowed() == false)
if (content.NewTopicsAllowed() == false)
{
<div class="alertbar__yellow" style="margin-bottom: 10px;">
@Html.Raw(notification)
@@ -67,7 +68,11 @@
@{
try
{
@Html.Partial("forum/question", topic)
var topicAuthor = topicMembers.SingleOrDefault(m => m.Id == topic.MemberId);
var roles = new List<string>();
memberRoles.TryGetValue(topic.MemberId, out roles);
@Html.Partial("forum/question", topic, new ViewDataDictionary { { "topicAuthor", topicAuthor }, { "roles", roles } })
}
catch (Exception ex)
{
@@ -78,9 +83,12 @@
@foreach (var comment in topic.Comments.Where(x => x.ParentCommentId == 0))
{
var comm = comment;
comm.MemberData = MemberData;
comm.MemberData = Model.MemberData;
var commentAuthor = topicMembers.SingleOrDefault(m => m.Id == comm.MemberId);
var roles = new List<string>();
memberRoles.TryGetValue(comm.MemberId, out roles);
Html.RenderPartial("forum/comment", comm, new ViewDataDictionary { { "level", 1 }, { "answer", topic.Answer }, { "topicParentId", topic.ParentId } });
Html.RenderPartial("forum/comment", comm, new ViewDataDictionary { { "level", 1 }, { "answer", topic.Answer }, { "topicParentId", topic.ParentId }, { "commentAuthor", commentAuthor }, { "roles", roles } });
if (comm.HasChildren)
{
@@ -88,15 +96,21 @@
foreach (var child in children.Where(c => c.ParentCommentId == comm.Id))
{
Html.RenderPartial("forum/comment", child, new ViewDataDictionary { { "level", 2 }, { "answer", topic.Answer }, { "topicParentId", topic.ParentId } });
var commChild = child;
child.MemberData = Model.MemberData;
var commentChildAuthor = topicMembers.SingleOrDefault(m => m.Id == commChild.MemberId);
var childRoles = new List<string>();
memberRoles.TryGetValue(commChild.MemberId, out childRoles);
Html.RenderPartial("forum/comment", child, new ViewDataDictionary { { "level", 2 }, { "answer", topic.Answer }, { "topicParentId", topic.ParentId }, { "commentAuthor", commentChildAuthor }, { "roles", childRoles } });
}
}
}
</ul>
if (MemberData != null)
if (Model.MemberData != null)
{
if (Model.Content.NewTopicsAllowed())
if (content.NewTopicsAllowed())
{
<div class="replybutton">
<a href="#" class="button green large reply forum-reply" data-author="@topic.AuthorName" data-topic="@topic.Id" data-controller="comment"><i class="icon-Reply-arrow"></i>&nbsp;Reply to topic</a>
@@ -105,13 +119,7 @@
<div class="utilities">
<div class="utility-actions thread-bottom">
@{
var subscribed = false;
if (MemberData.Member != null)
{
subscribed = Utils.IsSubscribedToForumTopic(topic.Id, MemberData.Member.Id);
}
if (MemberData.IsAdmin)
if (Model.MemberData.IsAdmin)
{
<a href="#" class="delete-thread button" data-id="@topic.Id">
<i class="icon-Delete-key"></i><span>Delete thread</span>
@@ -1,37 +1,29 @@
@using OurUmbraco.Forum.Services
@using OurUmbraco.NotificationsWeb.Library
@inherits OurUmbraco.Our.Models.OurUmbracoTemplatePage
@using OurUmbraco.Forum.Models
@model ReadOnlyTopic
@{
var topicService = new TopicService(ApplicationContext.DatabaseContext);
var topic = topicService.CurrentTopic(Context, ApplicationContext.ApplicationCache.RequestCache, MemberData);
var subscribed = (bool)ViewData["subscribed"];
}
<div class="utility-actions">
@if (topic != null)
@if (Model != null)
{
if (MemberData != null)
if (Model.MemberData != null)
{
var subscribed = false;
if (MemberData != null)
if (Model.MemberData.IsAdmin)
{
subscribed = Utils.IsSubscribedToForumTopic(topic.Id, MemberData.Member.Id);
}
if (MemberData.IsAdmin)
{
<a href="#" class="delete-thread button" data-id="@topic.Id">
<a href="#" class="delete-thread button" data-id="@Model.Id">
<i class="icon-Delete-key"></i><span>Delete thread</span>
</a>
}
<a href="#" class="follow button @(subscribed ? "following" : "transparent")" data-id="@topic.Id" data-controller="topic">
<a href="#" class="follow button @(subscribed ? "following" : "transparent")" data-id="@Model.Id" data-controller="topic">
<i class="icon-Bookmark"></i><span>@(subscribed ? "Following" : "Follow")</span>
</a>
}
if (topic.Answer > 0)
if (Model.Answer > 0)
{
<a href="#comment-@topic.Answer.ToString()" class="go-to-solution button transparent">
<a href="#comment-@Model.Answer.ToString()" class="go-to-solution button transparent">
<i class="icon-Check"></i><span>Go to solution</span>
</a>
}
@@ -158,7 +158,9 @@ namespace OurUmbraco.Forum.Extensions
public static List<string> GetRoles(this IPublishedContent member)
{
var roles = Roles.GetRolesForUser(member.GetPropertyValue<string>("UserName"));
const string sql = @"SELECT [umbracoNode].[text] FROM [cmsMember2MemberGroup] LEFT JOIN [umbracoNode] ON [cmsMember2MemberGroup].[MemberGroup] = [umbracoNode].[id] WHERE [cmsMember2MemberGroup].[Member] = @memberId";
var roles = ApplicationContext.Current.DatabaseContext.Database.Fetch<string>(sql, new { memberId = member.Id });
var memberRoles = new List<string>();
foreach (var role in roles)