Implementing threaded comments

--HG--
branch : 1.x
extra : rebase_source : a4e1ddb2327e65cfef1f2bae64cd45f7386c0102
This commit is contained in:
Sebastien Ros
2012-11-02 12:12:16 -07:00
parent b9b030fff1
commit 3217151eb3
23 changed files with 251 additions and 39 deletions

View File

@@ -38,6 +38,58 @@ namespace Orchard.Comments.Controllers {
if (comment.Has<CommentPart>()) {
var commentPart = comment.As<CommentPart>();
// ensure the comments are not closed on the container, as the html could have been tampered manually
var container = Services.ContentManager.Get(commentPart.CommentedOnContainer);
CommentsPart commentsPart = null;
if(container != null) {
commentsPart = container.As<CommentsPart>();
if(commentsPart != null && !commentsPart.CommentsActive) {
Services.TransactionManager.Cancel();
return this.RedirectLocal(returnUrl, "~/");
}
}
// is it a response to another comment ?
if(commentPart.RepliedOn.HasValue && (commentsPart == null || !commentsPart.ThreadedComments)) {
var replied = Services.ContentManager.Get(commentPart.RepliedOn.Value);
if(replied != null) {
var repliedPart = replied.As<CommentPart>();
// what is the next position after the anwered comment
if(repliedPart != null) {
// the next comment is the one right after the RepliedOn one, at the same level
var nextComment = _commentService.GetCommentsForCommentedContent(commentPart.CommentedOnContainer)
.Where(x => x.RepliedOn == repliedPart.RepliedOn && x.CommentDateUtc > repliedPart.CommentDateUtc)
.OrderBy(x => x.CommentDateUtc)
.Slice(0, 1)
.FirstOrDefault();
// the previous comment is the last one under the RepliedOn
var previousComment = _commentService.GetCommentsForCommentedContent(commentPart.CommentedOnContainer)
.Where(x => x.RepliedOn == commentPart.RepliedOn)
.OrderByDescending(x => x.CommentDateUtc)
.Slice(0, 1)
.FirstOrDefault();
if(nextComment == null) {
commentPart.Position = repliedPart.Position + 1;
}
else {
if (previousComment == null) {
commentPart.Position = (repliedPart.Position + nextComment.Position) / 2;
}
else {
commentPart.Position = (previousComment.Position + nextComment.Position) / 2;
}
}
}
}
}
else {
commentPart.RepliedOn = null;
}
if (commentPart.Status == CommentStatus.Pending) {
// if the user who submitted the comment has the right to moderate, don't make this comment moderated
if (Services.Authorizer.Authorize(Permissions.ManageComments)) {

View File

@@ -1,4 +1,5 @@
using System;
using System.Globalization;
using System.Xml;
using JetBrains.Annotations;
using Orchard.Comments.Models;
@@ -108,6 +109,11 @@ namespace Orchard.Comments.Drivers {
part.Record.Email = email;
}
var position = context.Attribute(part.PartDefinition.Name, "Position");
if (position != null) {
part.Record.Position = decimal.Parse(position, CultureInfo.InvariantCulture);
}
var status = context.Attribute(part.PartDefinition.Name, "Status");
if (status != null) {
part.Record.Status = (CommentStatus)Enum.Parse(typeof(CommentStatus), status);
@@ -133,6 +139,16 @@ namespace Orchard.Comments.Drivers {
contentItem.As<CommentsPart>().Record.CommentPartRecords.Add(part.Record);
}
var repliedOn = context.Attribute(part.PartDefinition.Name, "RepliedOn");
if (repliedOn != null) {
var contentItem = context.GetItemFromSession(repliedOn);
if (contentItem != null) {
part.Record.RepliedOn = contentItem.Id;
}
contentItem.As<CommentsPart>().Record.CommentPartRecords.Add(part.Record);
}
var commentedOnContainer = context.Attribute(part.PartDefinition.Name, "CommentedOnContainer");
if (commentedOnContainer != null) {
var container = context.GetItemFromSession(commentedOnContainer);
@@ -147,6 +163,7 @@ namespace Orchard.Comments.Drivers {
context.Element(part.PartDefinition.Name).SetAttributeValue("SiteName", part.Record.SiteName);
context.Element(part.PartDefinition.Name).SetAttributeValue("UserName", part.Record.UserName);
context.Element(part.PartDefinition.Name).SetAttributeValue("Email", part.Record.Email);
context.Element(part.PartDefinition.Name).SetAttributeValue("Position", part.Record.Position.ToString(CultureInfo.InvariantCulture));
context.Element(part.PartDefinition.Name).SetAttributeValue("Status", part.Record.Status.ToString());
if (part.Record.CommentDateUtc != null) {
@@ -166,6 +183,14 @@ namespace Orchard.Comments.Drivers {
var commentedOnContainerIdentity = _contentManager.GetItemMetadata(commentedOnContainer).Identity;
context.Element(part.PartDefinition.Name).SetAttributeValue("CommentedOnContainer", commentedOnContainerIdentity.ToString());
}
if (part.Record.RepliedOn.HasValue) {
var repliedOn = _contentManager.Get(part.Record.RepliedOn.Value);
if (repliedOn != null) {
var repliedOnIdentity = _contentManager.GetItemMetadata(repliedOn).Identity;
context.Element(part.PartDefinition.Name).SetAttributeValue("RepliedOn", repliedOnIdentity.ToString());
}
}
}
}
}

View File

@@ -30,12 +30,30 @@ namespace Orchard.Comments.Drivers {
return Combined(
ContentShape("Parts_ListOfComments",
() => {
// create a hierarchy of shapes
var commentShapes = new List<dynamic>();
var index = new Dictionary<int, dynamic>();
foreach (var item in part.Comments) {
commentShapes.Add(_contentManager.BuildDisplay(item.ContentItem, "Summary"));
var shape = _contentManager.BuildDisplay(item.ContentItem, "Summary");
index.Add(item.Id, shape);
if (!item.RepliedOn.HasValue) {
commentShapes.Add(shape);
}
else {
if (index.ContainsKey(item.RepliedOn.Value)) {
var parent = index[item.RepliedOn.Value];
if (parent.CommentShapes == null) {
parent.CommentShapes = new List<dynamic>();
}
parent.CommentShapes.Add(shape);
}
}
}
return shapeHelper.Parts_ListOfComments(CommentShapes: commentShapes);
return shapeHelper.Parts_ListOfComments(CommentShapes: commentShapes, CommentCount: approvedCount());
}),
ContentShape("Parts_CommentForm",
() => {
@@ -72,11 +90,17 @@ namespace Orchard.Comments.Drivers {
if (commentsActive != null) {
part.CommentsActive = Convert.ToBoolean(commentsActive);
}
var threadedComments = context.Attribute(part.PartDefinition.Name, "ThreadedComments");
if (threadedComments != null) {
part.ThreadedComments = Convert.ToBoolean(threadedComments);
}
}
protected override void Exporting(CommentsPart part, ContentManagement.Handlers.ExportContentContext context) {
context.Element(part.PartDefinition.Name).SetAttributeValue("CommentsShown", part.CommentsShown);
context.Element(part.PartDefinition.Name).SetAttributeValue("CommentsActive", part.CommentsActive);
context.Element(part.PartDefinition.Name).SetAttributeValue("ThreadedComments", part.ThreadedComments);
}
}
}

View File

@@ -23,6 +23,13 @@ namespace Orchard.Comments.Handlers {
);
});
OnRemoving<CommentPart>((context, comment) => {
foreach(var response in contentManager.Query<CommentPart, CommentPartRecord>().Where(x => x.RepliedOn == comment.Id).List()) {
contentManager.Remove(response.ContentItem);
}
});
OnIndexing<CommentPart>((context, commentPart) => context.DocumentIndex
.Add("commentText", commentPart.Record.CommentText));
}

View File

@@ -20,6 +20,7 @@ namespace Orchard.Comments.Handlers {
OnInitializing<CommentsPart>((ctx, part) => {
part.CommentsActive = true;
part.CommentsShown = true;
part.ThreadedComments = true;
part.Comments = new List<CommentPart>();
});
@@ -27,6 +28,7 @@ namespace Orchard.Comments.Handlers {
comments.CommentsField.Loader(list => contentManager
.Query<CommentPart, CommentPartRecord>()
.Where(x => x.CommentsPartRecord == context.ContentItem.As<CommentsPart>().Record && x.Status == CommentStatus.Approved)
.OrderBy(x => x.Position)
.List().ToList());
comments.PendingCommentsField.Loader(list => contentManager

View File

@@ -26,6 +26,8 @@ namespace Orchard.Comments {
.Column<string>("CommentText", column => column.Unlimited())
.Column<int>("CommentedOn")
.Column<int>("CommentedOnContainer")
.Column<int>("RepliedOn", c => c.WithDefault(null))
.Column<decimal>("Position")
.Column<int>("CommentsPartRecord_id")
);
@@ -39,6 +41,7 @@ namespace Orchard.Comments {
.ContentPartRecord()
.Column<bool>("CommentsShown")
.Column<bool>("CommentsActive")
.Column<bool>("ThreadedComments")
);
ContentDefinitionManager.AlterTypeDefinition("Comment",
@@ -58,7 +61,7 @@ namespace Orchard.Comments {
ContentDefinitionManager.AlterPartDefinition("CommentsPart", builder => builder.Attachable());
return 3;
return 4;
}
public int UpdateFrom1() {
@@ -103,6 +106,23 @@ namespace Orchard.Comments {
.DropColumn("AkismetUrl")
);
SchemaBuilder.AlterTable("CommentPartRecord", table => table
.AddColumn<int>("RepliedOn", c => c.WithDefault(null))
);
SchemaBuilder.AlterTable("CommentPartRecord", table => table
.AddColumn<decimal>("Position")
);
SchemaBuilder.AlterTable("CommentsPartRecord", table => table
.AddColumn<bool>("ThreadedComments")
);
// define the default value for positions
foreach (var comment in _commentService.GetComments().List()) {
comment.Position = comment.Id;
}
return 4;
}
}

View File

@@ -57,7 +57,17 @@ namespace Orchard.Comments.Models {
get { return Record.CommentedOn; }
set { Record.CommentedOn = value; }
}
public int? RepliedOn {
get { return Record.RepliedOn; }
set { Record.RepliedOn = value; }
}
public decimal Position {
get { return Record.Position; }
set { Record.Position = value; }
}
public ContentItem CommentedOnContentItem {
get { return _commentedOnContentItem.Value; }
set { _commentedOnContentItem.Value = value; }

View File

@@ -14,6 +14,8 @@ namespace Orchard.Comments.Models {
public virtual string CommentText { get; set; }
public virtual int CommentedOn { get; set; }
public virtual int CommentedOnContainer { get; set; }
public virtual int? RepliedOn { get; set; }
public virtual decimal Position { get; set; }
public virtual CommentsPartRecord CommentsPartRecord { get; set; }
}

View File

@@ -29,5 +29,10 @@ namespace Orchard.Comments.Models {
get { return Record.CommentsActive; }
set { Record.CommentsActive = value; }
}
public bool ThreadedComments {
get { return Record.ThreadedComments; }
set { Record.ThreadedComments = value; }
}
}
}

View File

@@ -10,6 +10,7 @@ namespace Orchard.Comments.Models {
public virtual bool CommentsShown { get; set; }
public virtual bool CommentsActive { get; set; }
public virtual bool ThreadedComments { get; set; }
[CascadeAllDeleteOrphan]
public virtual IList<CommentPartRecord> CommentPartRecords { get; set; }

View File

@@ -158,6 +158,8 @@
<ItemGroup>
<Content Include="Views\EditorTemplates\Parts.Comment.AdminEdit.cshtml" />
<Content Include="Views\Content-Comment.SummaryAdmin.cshtml" />
<Content Include="Views\ListOfComments.cshtml" />
<Content Include="Views\CommentReplyButton.cshtml" />
<None Include="Views\Parts.Comment.SummaryAdmin.cshtml" />
</ItemGroup>
<PropertyGroup>

View File

@@ -16,14 +16,22 @@
<Match ContentType="Comment">
<Place Content_SaveButton="-"/><!--Show default save button only on admin-->
</Match>
<Match DisplayType="Detail">
<Place Parts_ListOfComments="Content:10" />
<Place Parts_CommentForm="Content:10" />
<Place Parts_ListOfComments="Content:10.2" />
<Place Parts_CommentForm="Content:10.3" />
</Match>
<Match ContentType="Comment" DisplayType="Summary">
<Place Parts_Common_Metadata_Summary="-"
Parts_Comment="Content:10"
/>
</Match>
<Match DisplayType="Summary">
<Place Parts_Comments_Count="Meta:5"
Parts_Comment="Content:10" />
<Place Parts_Comments_Count="Meta:5" />
</Match>
<Match DisplayType="SummaryAdmin">
<Place Parts_Comment_SummaryAdmin="Content:10"
Parts_Comments_Count_SummaryAdmin="Meta:4" />

View File

@@ -10,8 +10,6 @@ namespace Orchard.Comments.Services {
}
public bool ValidateComment(CommentPart commentPart) {
// true == spam
var text = commentPart.Author + System.Environment.NewLine
+ commentPart.CommentText + System.Environment.NewLine
+ commentPart.Email + System.Environment.NewLine
@@ -24,7 +22,7 @@ namespace Orchard.Comments.Services {
_spamEventHandler.CheckSpam(context);
return context.IsSpam;
return !context.IsSpam;
}
}
}

View File

@@ -1,5 +1,6 @@
@{
var comment = (Orchard.Comments.Models.CommentPart)Model.ContentPart;
@using Orchard.Comments.Models
@{
CommentPart comment = Model.ContentPart;
}
@Html.LinkOrDefault(comment.Author, comment.SiteName, new { rel = "nofollow" })

View File

@@ -1,5 +1,6 @@
@{
var comment = (Orchard.Comments.Models.CommentPart)Model.ContentPart;
@using Orchard.Comments.Models
@{
CommentPart comment = Model.ContentPart;
}
@T("said") <time datetime="@comment.CommentDateUtc.GetValueOrDefault()">@Html.Link((string)Display.DateTimeRelative(dateTimeUtc: comment.CommentDateUtc.GetValueOrDefault()).ToString(), @Url.RouteUrl(comment.CommentedOnContentItemMetadata.DisplayRouteValues) + "#comment-" + comment.Id)</time>

View File

@@ -0,0 +1,6 @@
@using Orchard.Comments.Models
@{
CommentPart comment = Model.ContentPart;
}
<a class="comment-reply-button" href="#" data-id="@comment.Id">@T("Reply")</a>

View File

@@ -22,6 +22,7 @@
<div>
@Html.LabelFor(m => m.CommentText, T("Comment"))
@Html.TextAreaFor(m => m.CommentText, new { rows = 10, cols = 30, @class = "comment-text" })
@Html.HiddenFor(m => m.RepliedOn)
@Html.HiddenFor(m => m.Id)
</div>
</fieldset>

View File

@@ -34,7 +34,9 @@ else {
</li>
<li>
<button class="primaryAction" type="submit">@T("Submit Comment")</button>
@Html.Hidden("CommentedOn", Model.CommentedOn)
@Html.HiddenFor(m => m.CommentedOn)
@Html.HiddenFor(m => m.RepliedOn)
</li>
</ol>
</fieldset>

View File

@@ -1,5 +1,4 @@
@model Orchard.Comments.Models.CommentsPart
@using Orchard.Localization;
<fieldset>
<legend>@T("Comments")
@@ -7,8 +6,13 @@
<span>&#150; @Display.CommentSummaryLinks(item: Model.ContentItem, count: Model.Comments.Count, pendingCount: Model.PendingComments.Count)</span>
}
</legend>
@Html.EditorFor(m => m.CommentsShown)
<label class="forcheckbox" for="@Html.FieldIdFor(m => m.CommentsShown)">@T("Show comments")</label>
<span class="hint forcheckbox">@T("Enable to show the existing comments.")</span>
@Html.EditorFor(m => m.CommentsActive)
<label class="forcheckbox" for="CommentsActive">
@T("Allow new comments")</label> <span class="hint forcheckbox">
@T("Enable to show the comment form. Disabling still allows the existing comments to be shown but does not allow the conversation to continue.")</span>
<label class="forcheckbox" for="@Html.FieldIdFor(m => m.CommentsActive)">@T("Allow new comments")</label>
<span class="hint forcheckbox">@T("Enable to show the comment form. Disabling still allows the existing comments to be shown but does not allow the conversation to continue.")</span>
@Html.EditorFor(m => m.ThreadedComments)
<label class="forcheckbox" for="@Html.FieldIdFor(m => m.ThreadedComments)">@T("Allow threaded comments")</label>
<span class="hint forcheckbox">@T("Enable to allow users to answer directly to other comments.")</span>
</fieldset>

View File

@@ -0,0 +1,19 @@
<ul class="comments">
@foreach (var comment in Model.CommentShapes) {
<li>
@{
comment.Id = "comment-" + comment.ContentPart.Id;
var tag = Tag(comment, "article"); // using Tag so the comment can be altered by custom modules
tag.AddCssClass("comment");
}
@tag.StartElement
@Display(comment)
@tag.EndElement
@if(comment.CommentShapes != null) {
@Display.ListOfComments(CommentShapes: comment.CommentShapes)
}
</li>
}
</ul>

View File

@@ -1,13 +1,18 @@
@{
var comment = (Orchard.Comments.Models.CommentPart)Model.ContentPart;
}
@using Orchard.Comments.Models
@using Orchard.ContentManagement
@using Orchard.Utility.Extensions;
@{
var comment = (CommentPart) Model.ContentPart;
}
<header>
<h4>
<span class="who">@Display.CommentAuthor(ContentPart: comment)</span>
<span class="when">@Display.CommentMetadata(ContentPart: comment)</span>
@if (comment.CommentedOnContentItem.As<CommentsPart>().ThreadedComments) {
<span class="reply">@Display.CommentReplyButton(ContentPart: comment)</span>
}
</h4>
</header>
<p class="text">@(new MvcHtmlString(Html.Encode(comment.Record.CommentText).Replace("\r\n", "<br />\r\n")))</p>
<p class="text">@(new MvcHtmlString(Html.Encode(comment.Record.CommentText).Replace("\r\n", "<br />\r\n")))</p>

View File

@@ -1,14 +1,29 @@
@if (Model.ContentPart.Comments.Count > 0) {
<div id="comments">
<h2 class="comment-count">@T.Plural("1 Comment", "{0} Comments", (int)Model.ContentPart.Comments.Count)</h2>
<ul class="comments">
@foreach (var comment in Model.CommentShapes) {
<li>
<article class="comment" id="comment-@comment.ContentItem.Id">
@Display(comment)
</article>
</li>
}
</ul>
</div>
@if (Model.CommentCount > 0) {
<div id="comments">
<h2 class="comment-count">@T.Plural("1 Comment", "{0} Comments", (int)Model.CommentCount)</h2>
@Display.ListOfComments(CommentShapes: Model.CommentShapes)
</div>
}
@using(Script.Foot()) {
Script.Require("jQuery");
<script type="text/javascript">
//<![CDATA[
$(function () {
$('.comment-reply-button').click(function () {
var self = $(this);
var id = self.data("id");
var reply = $('#Comments_RepliedOn');
reply.val(id);
// inject the form in the replied zone
$('.comment-form').appendTo(self.parents('article').first());
// don't execute the link action
return false;
});
});
//]]>
</script>
}

View File

@@ -480,10 +480,12 @@ nav ul.breadcrumb
.comment h4 { font-size: 1.077em; }
.comment .who {}
.comment .what {}
.comment .reply { float: right;}
.comment #add-comment {}
.comment-form #comment-by { font-size: 1.231em; font-weight: normal; margin: 0 0 1.2em 0; border:none; }
.comment-form #comment-text { width: 33.2em; }
.comment-form .comment-text { width: 33.2em; }
.comment-form input[type="text"] { width: 32em; }
.comment-form li { border: inherit; margin: inherit; padding: inherit}
.comment-disabled {}
.comment-count { font-size: 1.231em; }