2010-01-23 23:55:19 +00:00
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
2009-11-19 21:58:48 +00:00
|
|
|
|
using System.Linq;
|
2009-12-21 08:24:39 +00:00
|
|
|
|
using JetBrains.Annotations;
|
2009-11-19 21:58:48 +00:00
|
|
|
|
using Orchard.Comments.Models;
|
2010-01-19 06:29:58 +00:00
|
|
|
|
using Orchard.ContentManagement.Aspects;
|
2009-11-19 21:58:48 +00:00
|
|
|
|
using Orchard.Data;
|
|
|
|
|
using Orchard.Logging;
|
2009-12-21 20:29:53 +00:00
|
|
|
|
using Orchard.ContentManagement;
|
2009-11-19 23:59:24 +00:00
|
|
|
|
using Orchard.Security;
|
2010-01-23 23:55:19 +00:00
|
|
|
|
using Orchard.Services;
|
2009-11-19 23:59:24 +00:00
|
|
|
|
using Orchard.Settings;
|
|
|
|
|
using Orchard.UI.Notify;
|
2009-11-19 21:58:48 +00:00
|
|
|
|
|
|
|
|
|
namespace Orchard.Comments.Services {
|
|
|
|
|
public interface ICommentService : IDependency {
|
|
|
|
|
IEnumerable<Comment> GetComments();
|
|
|
|
|
IEnumerable<Comment> GetComments(CommentStatus status);
|
2009-11-21 23:55:11 +00:00
|
|
|
|
IEnumerable<Comment> GetCommentsForCommentedContent(int id);
|
|
|
|
|
IEnumerable<Comment> GetCommentsForCommentedContent(int id, CommentStatus status);
|
2009-11-19 21:58:48 +00:00
|
|
|
|
Comment GetComment(int id);
|
2009-12-02 02:24:39 +00:00
|
|
|
|
ContentItemMetadata GetDisplayForCommentedContent(int id);
|
2010-01-23 23:55:19 +00:00
|
|
|
|
Comment CreateComment(CreateCommentContext commentRecord);
|
2009-11-25 22:05:44 +00:00
|
|
|
|
void UpdateComment(int id, string name, string email, string siteName, string commentText, CommentStatus status);
|
2010-01-18 20:55:58 +00:00
|
|
|
|
void ApproveComment(int commentId);
|
|
|
|
|
void PendComment(int commentId);
|
2009-11-19 21:58:48 +00:00
|
|
|
|
void MarkCommentAsSpam(int commentId);
|
|
|
|
|
void DeleteComment(int commentId);
|
2009-11-23 21:08:47 +00:00
|
|
|
|
bool CommentsClosedForCommentedContent(int id);
|
2009-11-21 23:55:11 +00:00
|
|
|
|
void CloseCommentsForCommentedContent(int id);
|
2009-11-23 21:08:47 +00:00
|
|
|
|
void EnableCommentsForCommentedContent(int id);
|
2009-11-19 21:58:48 +00:00
|
|
|
|
}
|
|
|
|
|
|
2010-01-23 23:55:19 +00:00
|
|
|
|
public class CreateCommentContext {
|
|
|
|
|
public virtual string Author { get; set; }
|
|
|
|
|
public virtual string SiteName { get; set; }
|
|
|
|
|
public virtual string Email { get; set; }
|
|
|
|
|
public virtual string CommentText { get; set; }
|
|
|
|
|
public virtual int CommentedOn { get; set; }
|
|
|
|
|
}
|
|
|
|
|
|
2010-01-23 23:21:08 +00:00
|
|
|
|
[UsedImplicitly]
|
2009-11-19 21:58:48 +00:00
|
|
|
|
public class CommentService : ICommentService {
|
2010-01-23 23:21:08 +00:00
|
|
|
|
private readonly IRepository<ClosedCommentsRecord> _closedCommentsRepository;
|
2009-11-19 23:59:24 +00:00
|
|
|
|
private readonly ICommentValidator _commentValidator;
|
2009-11-21 03:53:55 +00:00
|
|
|
|
private readonly IContentManager _contentManager;
|
2010-01-23 23:55:19 +00:00
|
|
|
|
private readonly IClock _clock;
|
2009-11-19 21:58:48 +00:00
|
|
|
|
|
2010-01-23 23:21:08 +00:00
|
|
|
|
public CommentService(IRepository<CommentRecord> commentRepository,
|
|
|
|
|
IRepository<ClosedCommentsRecord> closedCommentsRepository,
|
2009-12-24 04:49:43 +00:00
|
|
|
|
IRepository<HasCommentsRecord> hasCommentsRepository,
|
2009-11-21 03:53:55 +00:00
|
|
|
|
ICommentValidator commentValidator,
|
|
|
|
|
IContentManager contentManager,
|
2010-01-23 23:55:19 +00:00
|
|
|
|
IClock clock,
|
2009-12-24 04:49:43 +00:00
|
|
|
|
IAuthorizer authorizer,
|
2009-11-21 03:53:55 +00:00
|
|
|
|
INotifier notifier) {
|
2009-11-21 23:55:11 +00:00
|
|
|
|
_closedCommentsRepository = closedCommentsRepository;
|
2009-11-19 23:59:24 +00:00
|
|
|
|
_commentValidator = commentValidator;
|
2009-11-21 03:53:55 +00:00
|
|
|
|
_contentManager = contentManager;
|
2010-01-23 23:55:19 +00:00
|
|
|
|
_clock = clock;
|
2009-11-19 21:58:48 +00:00
|
|
|
|
Logger = NullLogger.Instance;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public ILogger Logger { get; set; }
|
2009-12-21 08:24:39 +00:00
|
|
|
|
protected virtual ISite CurrentSite { get; [UsedImplicitly] private set; }
|
2010-01-23 23:55:19 +00:00
|
|
|
|
protected virtual IUser CurrentUser { get; [UsedImplicitly] private set; }
|
2009-12-24 04:49:43 +00:00
|
|
|
|
|
2009-11-19 21:58:48 +00:00
|
|
|
|
|
|
|
|
|
#region Implementation of ICommentService
|
|
|
|
|
|
|
|
|
|
public IEnumerable<Comment> GetComments() {
|
2010-01-23 23:21:08 +00:00
|
|
|
|
return _contentManager
|
|
|
|
|
.Query<Comment, CommentRecord>()
|
|
|
|
|
.List();
|
2009-11-19 21:58:48 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public IEnumerable<Comment> GetComments(CommentStatus status) {
|
2010-01-23 23:21:08 +00:00
|
|
|
|
return _contentManager
|
|
|
|
|
.Query<Comment, CommentRecord>()
|
|
|
|
|
.Where(c => c.Status == status)
|
|
|
|
|
.List();
|
2009-11-19 21:58:48 +00:00
|
|
|
|
}
|
|
|
|
|
|
2009-11-21 23:55:11 +00:00
|
|
|
|
public IEnumerable<Comment> GetCommentsForCommentedContent(int id) {
|
2010-01-23 23:21:08 +00:00
|
|
|
|
return _contentManager
|
|
|
|
|
.Query<Comment, CommentRecord>()
|
|
|
|
|
.Where(c => c.CommentedOn == id || c.CommentedOnContainer == id)
|
|
|
|
|
.List();
|
2009-11-21 23:55:11 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public IEnumerable<Comment> GetCommentsForCommentedContent(int id, CommentStatus status) {
|
2010-01-23 23:21:08 +00:00
|
|
|
|
return _contentManager
|
|
|
|
|
.Query<Comment, CommentRecord>()
|
|
|
|
|
.Where(c => c.CommentedOn == id || c.CommentedOnContainer == id)
|
|
|
|
|
.Where(ctx => ctx.Status == status)
|
|
|
|
|
.List();
|
2009-11-21 23:55:11 +00:00
|
|
|
|
}
|
|
|
|
|
|
2009-11-19 21:58:48 +00:00
|
|
|
|
public Comment GetComment(int id) {
|
2010-01-23 23:21:08 +00:00
|
|
|
|
return _contentManager.Get<Comment>(id);
|
2009-11-19 21:58:48 +00:00
|
|
|
|
}
|
|
|
|
|
|
2009-12-02 02:24:39 +00:00
|
|
|
|
public ContentItemMetadata GetDisplayForCommentedContent(int id) {
|
|
|
|
|
var content = _contentManager.Get(id);
|
|
|
|
|
if (content == null)
|
|
|
|
|
return null;
|
|
|
|
|
return _contentManager.GetItemMetadata(content);
|
2009-11-21 03:53:55 +00:00
|
|
|
|
}
|
|
|
|
|
|
2010-01-23 23:55:19 +00:00
|
|
|
|
public Comment CreateComment(CreateCommentContext context) {
|
2010-01-23 23:21:08 +00:00
|
|
|
|
var comment = _contentManager.Create<Comment>("comment");
|
|
|
|
|
|
2010-01-23 23:55:19 +00:00
|
|
|
|
comment.Record.Author = context.Author;
|
|
|
|
|
comment.Record.CommentDateUtc = _clock.UtcNow;
|
|
|
|
|
comment.Record.CommentText = context.CommentText;
|
|
|
|
|
comment.Record.Email = context.Email;
|
|
|
|
|
comment.Record.SiteName = context.SiteName;
|
|
|
|
|
comment.Record.UserName = (CurrentUser == null ? "Anonymous" : CurrentUser.UserName);
|
|
|
|
|
comment.Record.CommentedOn = context.CommentedOn;
|
2010-01-23 23:21:08 +00:00
|
|
|
|
|
2010-01-23 23:55:19 +00:00
|
|
|
|
comment.Record.Status = _commentValidator.ValidateComment(comment.Record) ? CommentStatus.Pending : CommentStatus.Spam;
|
2010-01-19 06:29:58 +00:00
|
|
|
|
|
|
|
|
|
// store id of the next layer for large-grained operations, e.g. rss on blog
|
2010-01-23 23:21:08 +00:00
|
|
|
|
//TODO:(rpaquay) Get rid of this (comment aspect takes care of container)
|
|
|
|
|
var commentedOn = _contentManager.Get<ICommonAspect>(comment.Record.CommentedOn);
|
2010-01-19 06:29:58 +00:00
|
|
|
|
if (commentedOn != null && commentedOn.Container != null) {
|
2010-01-23 23:21:08 +00:00
|
|
|
|
comment.Record.CommentedOnContainer = commentedOn.Container.ContentItem.Id;
|
2010-01-19 06:29:58 +00:00
|
|
|
|
}
|
|
|
|
|
|
2010-01-23 23:21:08 +00:00
|
|
|
|
return comment;
|
2009-11-19 23:59:24 +00:00
|
|
|
|
}
|
|
|
|
|
|
2009-11-25 22:05:44 +00:00
|
|
|
|
public void UpdateComment(int id, string name, string email, string siteName, string commentText, CommentStatus status) {
|
2009-11-21 23:55:11 +00:00
|
|
|
|
Comment comment = GetComment(id);
|
2010-01-23 23:21:08 +00:00
|
|
|
|
comment.Record.Author = name;
|
|
|
|
|
comment.Record.Email = email;
|
|
|
|
|
comment.Record.SiteName = siteName;
|
|
|
|
|
comment.Record.CommentText = commentText;
|
|
|
|
|
comment.Record.Status = status;
|
2009-11-21 23:55:11 +00:00
|
|
|
|
}
|
|
|
|
|
|
2010-01-18 20:55:58 +00:00
|
|
|
|
public void ApproveComment(int commentId) {
|
|
|
|
|
Comment comment = GetComment(commentId);
|
2010-01-23 23:21:08 +00:00
|
|
|
|
comment.Record.Status = CommentStatus.Approved;
|
2010-01-18 20:55:58 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void PendComment(int commentId) {
|
|
|
|
|
Comment comment = GetComment(commentId);
|
2010-01-23 23:21:08 +00:00
|
|
|
|
comment.Record.Status = CommentStatus.Pending;
|
2010-01-18 20:55:58 +00:00
|
|
|
|
}
|
|
|
|
|
|
2009-11-19 21:58:48 +00:00
|
|
|
|
public void MarkCommentAsSpam(int commentId) {
|
|
|
|
|
Comment comment = GetComment(commentId);
|
2010-01-23 23:21:08 +00:00
|
|
|
|
comment.Record.Status = CommentStatus.Spam;
|
2009-11-19 21:58:48 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void DeleteComment(int commentId) {
|
2010-01-23 23:21:08 +00:00
|
|
|
|
_contentManager.Remove(_contentManager.Get(commentId));
|
2009-11-19 21:58:48 +00:00
|
|
|
|
}
|
|
|
|
|
|
2009-11-23 21:08:47 +00:00
|
|
|
|
public bool CommentsClosedForCommentedContent(int id) {
|
2010-01-23 23:21:08 +00:00
|
|
|
|
return _closedCommentsRepository.Fetch(x => x.ContentItemId == id).Count() >= 1;
|
2009-11-23 21:08:47 +00:00
|
|
|
|
}
|
|
|
|
|
|
2009-11-21 23:55:11 +00:00
|
|
|
|
public void CloseCommentsForCommentedContent(int id) {
|
2010-01-23 23:21:08 +00:00
|
|
|
|
if (CommentsClosedForCommentedContent(id))
|
|
|
|
|
return;
|
|
|
|
|
_closedCommentsRepository.Create(new ClosedCommentsRecord { ContentItemId = id });
|
2009-11-21 23:55:11 +00:00
|
|
|
|
}
|
|
|
|
|
|
2009-11-23 21:08:47 +00:00
|
|
|
|
public void EnableCommentsForCommentedContent(int id) {
|
2010-01-23 23:21:08 +00:00
|
|
|
|
var closedComments = _closedCommentsRepository.Fetch(x => x.ContentItemId == id);
|
2010-01-23 23:55:19 +00:00
|
|
|
|
foreach (var c in closedComments) {
|
|
|
|
|
_closedCommentsRepository.Delete(c);
|
|
|
|
|
}
|
2009-11-23 21:08:47 +00:00
|
|
|
|
}
|
|
|
|
|
|
2009-11-19 21:58:48 +00:00
|
|
|
|
#endregion
|
|
|
|
|
}
|
|
|
|
|
}
|