Skip to content

Commit 0e223b6

Browse files
committed
Reply on comments from admin comment list
1 parent 82cd9f6 commit 0e223b6

10 files changed

Lines changed: 122 additions & 25 deletions

File tree

BlogEngine/BlogEngine.Core/Data/CommentsRepository.cs

Lines changed: 44 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
using System.Collections.Generic;
66
using System.Linq;
77
using System.Linq.Dynamic;
8+
using System.Web;
9+
using System.Web.Security;
810

911
namespace BlogEngine.Core.Data
1012
{
@@ -104,7 +106,45 @@ public CommentItem Add(CommentItem item)
104106
if (!Security.IsAuthorizedTo(Rights.CreateComments))
105107
throw new UnauthorizedAccessException();
106108

107-
return null;
109+
var c = new Comment();
110+
try
111+
{
112+
var post = Post.Posts.Where(p => p.Id == item.PostId).FirstOrDefault();
113+
114+
c.Id = Guid.NewGuid();
115+
c.ParentId = item.ParentId;
116+
c.IsApproved = item.IsApproved;
117+
c.Content = HttpUtility.HtmlAttributeEncode(item.Content);
118+
119+
if (string.IsNullOrEmpty(item.Author))
120+
{
121+
c.Author = Security.CurrentUser.Identity.Name;
122+
var profile = AuthorProfile.GetProfile(c.Author);
123+
if(profile != null && !string.IsNullOrEmpty(profile.DisplayName))
124+
{
125+
c.Author = profile.DisplayName;
126+
}
127+
}
128+
129+
if (string.IsNullOrEmpty(item.Email))
130+
c.Email = Membership.Provider.GetUser(Security.CurrentUser.Identity.Name, true).Email;
131+
132+
c.IP = Utils.GetClientIP();
133+
c.DateCreated = DateTime.Now;
134+
c.Parent = post;
135+
136+
post.AddComment(c);
137+
post.Save();
138+
139+
var newComm = post.Comments.Where(cm => cm.Content == c.Content).FirstOrDefault();
140+
141+
return Json.GetComment(newComm, post.Comments);
142+
}
143+
catch (Exception ex)
144+
{
145+
Utils.Log("Core.Data.CommentsRepository.Add", ex);
146+
return null;
147+
}
108148
}
109149

110150
/// <summary>
@@ -140,6 +180,7 @@ public bool Update(CommentItem item, string action)
140180
return true;
141181
}
142182

183+
c.Content = item.Content;
143184
c.Author = item.Author;
144185
c.Email = item.Email;
145186
c.Website = string.IsNullOrEmpty(item.Website) ? null : new Uri(item.Website);
@@ -176,11 +217,11 @@ public bool Update(CommentItem item, string action)
176217
public bool Remove(Guid id)
177218
{
178219
if (!Security.IsAuthorizedTo(Rights.ModerateComments))
179-
throw new System.UnauthorizedAccessException();
220+
throw new UnauthorizedAccessException();
180221

181222
foreach (var p in Post.Posts.ToArray())
182223
{
183-
BlogEngine.Core.Comment item = (from cmn in p.AllComments
224+
Comment item = (from cmn in p.AllComments
184225
where cmn.Id == id select cmn).FirstOrDefault();
185226

186227
if (item != null)

BlogEngine/BlogEngine.Core/Data/Contracts/ICommentsRepository.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ public interface ICommentsRepository
1818
/// <param name="filter">Filter expression</param>
1919
/// <param name="order">Sort order</param>
2020
/// <returns>List of comments</returns>
21-
IEnumerable<BlogEngine.Core.Data.Models.CommentItem> GetComments(CommentType commentType = CommentType.All, int take = 10, int skip = 0, string filter = "", string order = "");
21+
IEnumerable<CommentItem> GetComments(CommentType commentType = CommentType.All, int take = 10, int skip = 0, string filter = "", string order = "");
2222
/// <summary>
2323
/// Single commnet by ID
2424
/// </summary>
@@ -28,20 +28,20 @@ public interface ICommentsRepository
2828
/// <returns>
2929
/// A JSON Comment
3030
/// </returns>
31-
BlogEngine.Core.Data.Models.CommentItem FindById(Guid id);
31+
CommentItem FindById(Guid id);
3232
/// <summary>
3333
/// Add item
3434
/// </summary>
3535
/// <param name="item">Comment</param>
3636
/// <returns>Comment object</returns>
37-
Data.Models.CommentItem Add(Data.Models.CommentItem item);
37+
CommentItem Add(CommentItem item);
3838
/// <summary>
3939
/// Update item
4040
/// </summary>
4141
/// <param name="item">Item to update</param>
4242
/// <param name="action">Action (approve/unapprove)</param>
4343
/// <returns>True on success</returns>
44-
bool Update(Data.Models.CommentItem item, string action);
44+
bool Update(CommentItem item, string action);
4545
/// <summary>
4646
/// Delete item
4747
/// </summary>

BlogEngine/BlogEngine.Core/Data/Models/CommentItem.cs

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
11
using System;
2-
using System.Globalization;
3-
using System.Linq;
42

53
namespace BlogEngine.Core.Data.Models
64
{
@@ -19,6 +17,16 @@ public class CommentItem
1917
/// </summary>
2018
public Guid Id { get; set; }
2119

20+
/// <summary>
21+
/// Parent comment Id
22+
/// </summary>
23+
public Guid ParentId { get; set; }
24+
25+
/// <summary>
26+
/// Comment post ID
27+
/// </summary>
28+
public Guid PostId { get; set; }
29+
2230
/// <summary>
2331
/// Is approved
2432
/// </summary>
@@ -64,6 +72,11 @@ public class CommentItem
6472
/// </summary>
6573
public string Title { get; set; }
6674

75+
/// <summary>
76+
/// Comment content
77+
/// </summary>
78+
public string Content { get; set; }
79+
6780
/// <summary>
6881
/// Gets or sets the author's website
6982
/// </summary>

BlogEngine/BlogEngine.Core/Data/Services/Json.cs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -132,12 +132,15 @@ public static CommentItem GetComment(Comment c, List<Comment> postComments)
132132
{
133133
var jc = new CommentItem();
134134
jc.Id = c.Id;
135+
jc.ParentId = c.ParentId;
136+
jc.PostId = c.Parent.Id;
135137
jc.IsApproved = c.IsApproved;
136138
jc.IsSpam = c.IsSpam;
137139
jc.IsPending = !c.IsApproved && !c.IsSpam;
138140
jc.Email = c.Email == "trackback" ? "pingback" : c.Email;
139141
jc.Author = c.Author;
140-
jc.Title = c.Teaser;
142+
jc.Title = c.Teaser.Length < 80 ? c.Teaser : c.Teaser.Substring(0, 80) + "...";
143+
jc.Content = c.Content;
141144
jc.Website = c.Website == null ? "" : c.Website.ToString();
142145
jc.AuthorAvatar = c.Avatar;
143146
jc.Ip = c.IP;
@@ -175,6 +178,8 @@ from cmn in p.AllComments
175178
item.IsSpam = true;
176179
}
177180

181+
item.ParentId = c.ParentId;
182+
item.Content = c.Content;
178183
item.Email = c.Email;
179184
item.Author = c.Author;
180185
item.Website = string.IsNullOrEmpty(c.Website) ? null : new Uri(c.Website);

BlogEngine/BlogEngine.Core/Properties/AssemblyInfo.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,5 +19,5 @@
1919
[assembly: CLSCompliant(false)]
2020
[assembly: ComVisible(false)]
2121
[assembly: AllowPartiallyTrustedCallers]
22-
[assembly: AssemblyVersion("3.1.3.0")]
22+
[assembly: AssemblyVersion("3.1.3.2")]
2323
[assembly: SecurityRules(SecurityRuleSet.Level1)]

BlogEngine/BlogEngine.NET/AppCode/Api/CommentsController.cs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,15 @@ public HttpResponseMessage Get(string id)
2929
return Request.CreateResponse(HttpStatusCode.OK, result);
3030
}
3131

32+
public HttpResponseMessage Post([FromBody]CommentItem item)
33+
{
34+
var result = repository.Add(item);
35+
if (result == null)
36+
return Request.CreateResponse(HttpStatusCode.NotModified);
37+
38+
return Request.CreateResponse(HttpStatusCode.Created, result);
39+
}
40+
3241
public HttpResponseMessage Put([FromBody]CommentItem item)
3342
{
3443
repository.Update(item, "update");

BlogEngine/BlogEngine.NET/Custom/Controls/CommentList.ascx.cs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -320,10 +320,8 @@ public void RaiseCallbackEvent(string eventArgument)
320320
{
321321
Id = Guid.NewGuid(),
322322
ParentId = replyToCommentId,
323-
// Author = Server.HtmlEncode(author),
324323
Author = HttpUtility.HtmlAttributeEncode(author),
325324
Email = Server.HtmlEncode(email),
326-
// Content = Server.HtmlEncode(content),
327325
Content = HttpUtility.HtmlAttributeEncode(content),
328326
IP = Utils.GetClientIP(),
329327
Country = Server.HtmlEncode(country),

BlogEngine/BlogEngine.NET/admin/app/controllers/commentList.js

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,17 +6,34 @@
66
$scope.sortingOrder = 'DateCreated';
77
$scope.reverse = true;
88
$scope.commentsPage = true;
9-
10-
if ($scope.id) {
11-
$("#modal-add-item").modal();
12-
}
9+
$scope.commentReply = {};
1310

1411
$scope.showEditForm = function (id) {
15-
$("#modal-comment-edit").modal();
1612
$scope.id = id;
13+
$scope.item = findInArray($scope.items, 'Id', id);
14+
$("#modal-comment-edit").modal();
1715
$scope.focusInput = true;
1816
}
1917

18+
$scope.reply = function () {
19+
var comment = {
20+
"ParentId": $scope.item.Id,
21+
"PostId": $scope.item.PostId,
22+
"IsApproved": true,
23+
"Content": $scope.commentReply.text
24+
}
25+
dataService.addItem("/api/comments", comment)
26+
.success(function (data) {
27+
toastr.success($rootScope.lbl.commentUpdated);
28+
$scope.load();
29+
$("#modal-comment-edit").modal('hide');
30+
})
31+
.error(function () {
32+
toastr.error($rootScope.lbl.updateFailed);
33+
$("#modal-comment-edit").modal('hide');
34+
});
35+
}
36+
2037
$scope.load = function () {
2138
var p = { type: 5, take: 0, skip: 0, filter: "", order: "" };
2239
dataService.getItems('/api/comments', p)

BlogEngine/BlogEngine.NET/admin/views/about/about.cshtml

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@
5050
<div class="panel-title">Introduction</div>
5151
</div>
5252
<div class="panel-body text-justify">
53-
<p class="text-center" style="padding:30px 0"><a href="http://dotnetblogengine.net/"><img src="/Content/images/blog/logo.png" /></a></p>
53+
<p class="text-center" style="padding:30px 0"><a href="http://dotnetblogengine.net/"><img src="~/Content/images/blog/logo.png" /></a></p>
5454
<p>
5555
BlogEngine.NET is an open source ASP.NET project that was born out of desire for a better blogging platform. We focused on simplicity, ease of use, extendibility and innovative design while taking advantage of the latest .NET features.
5656
</p>
@@ -97,6 +97,9 @@
9797
<li class="list-group-item">
9898
Trust level: @trust
9999
</li>
100+
<li class="list-group-item">
101+
Identity: @System.Security.Principal.WindowsIdentity.GetCurrent().Name
102+
</li>
100103
<li class="list-group-item">
101104
Blog provider: @blogProviderSection.DefaultProvider
102105
</li>

BlogEngine/BlogEngine.NET/admin/views/content/comments.html

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,36 @@
11
<div data-ng-controller="CommentListController">
2-
2+
<style>
3+
pre {
4+
display: block;
5+
font-family: monospace;
6+
white-space: pre-wrap;
7+
height: 200px;
8+
}
9+
</style>
310
<form id="cat-form" action="">
411
<div id="modal-comment-edit" class="modal fade">
512
<div class="modal-dialog">
613
<div class="modal-content">
714
<div class="modal-header">
815
<button type="button" class="close" data-dismiss="modal" aria-hidden="true" ng-click="focusInput=false"><i class="fa fa-times"></i></button>
9-
<h4 class="modal-title">{{id}}</h4>
16+
<h4 class="modal-title">{{item.Title}}</h4>
1017
</div>
1118
<div class="modal-body">
1219
<div class="form-horizontal clearfix">
1320
<div class="form-group">
14-
<label class="control-label col-md-4" for="txtCatTitle">{{lbl.title}}</label>
15-
<div class="col col-md-8">
16-
<input type="text" class="form-control" id="txtCatTitle" name="txtCatTitle" data-ng-model="category.Title" focus-me="focusInput" />
21+
<div class="col col-md-12">
22+
<pre>{{item.Content}}</pre>
23+
</div>
24+
</div>
25+
<div class="form-group">
26+
<div class="col col-md-12">
27+
<textarea ng-model="commentReply.text" placeholder="Reply..." style="margin-top: 5px" class="form-control ltr-dir" rows="2" id="txtDraftText" name="txtDraftText"></textarea>
1728
</div>
1829
</div>
1930
</div>
2031
</div>
2132
<div class="modal-footer">
22-
<button type="button" ng-click="saveCategory()" class="btn btn-success pull-right btn-tabkey"><i class="fa fa-save"></i>{{lbl.save}}</button>
33+
<button type="button" ng-click="reply()" ng-disabled="commentReply.text.length < 1" class="btn btn-success pull-right btn-tabkey"><i class="fa fa-save"></i>{{lbl.save}}</button>
2334
<button type="button" class="btn btn-default" data-dismiss="modal" ng-click="focusInput=false"><i class="fa fa-ban"></i>{{lbl.cancel}}</button>
2435
</div>
2536
</div>

0 commit comments

Comments
 (0)