2009-11-23 23:43:04 +00:00
|
|
|
|
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;
|
|
|
|
|
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;
|
|
|
|
|
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);
|
2009-11-19 23:59:24 +00:00
|
|
|
|
void CreateComment(Comment comment);
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public class CommentService : ICommentService {
|
|
|
|
|
private readonly IRepository<Comment> _commentRepository;
|
2009-11-21 23:55:11 +00:00
|
|
|
|
private readonly IRepository<ClosedComments> _closedCommentsRepository;
|
2009-12-24 04:49:43 +00:00
|
|
|
|
private readonly IRepository<HasCommentsRecord> _hasCommentsRepository;
|
2009-11-19 23:59:24 +00:00
|
|
|
|
private readonly ICommentValidator _commentValidator;
|
2009-11-21 03:53:55 +00:00
|
|
|
|
private readonly IContentManager _contentManager;
|
2009-11-19 23:59:24 +00:00
|
|
|
|
private readonly IAuthorizer _authorizer;
|
|
|
|
|
private readonly INotifier _notifier;
|
2009-11-19 21:58:48 +00:00
|
|
|
|
|
2009-12-24 04:49:43 +00:00
|
|
|
|
public CommentService(IRepository<Comment> commentRepository,
|
2009-11-21 23:55:11 +00:00
|
|
|
|
IRepository<ClosedComments> closedCommentsRepository,
|
2009-12-24 04:49:43 +00:00
|
|
|
|
IRepository<HasCommentsRecord> hasCommentsRepository,
|
2009-11-21 03:53:55 +00:00
|
|
|
|
ICommentValidator commentValidator,
|
|
|
|
|
IContentManager contentManager,
|
2009-12-24 04:49:43 +00:00
|
|
|
|
IAuthorizer authorizer,
|
2009-11-21 03:53:55 +00:00
|
|
|
|
INotifier notifier) {
|
2009-11-19 21:58:48 +00:00
|
|
|
|
_commentRepository = commentRepository;
|
2009-11-21 23:55:11 +00:00
|
|
|
|
_closedCommentsRepository = closedCommentsRepository;
|
2009-12-24 04:49:43 +00:00
|
|
|
|
_hasCommentsRepository = hasCommentsRepository;
|
2009-11-19 23:59:24 +00:00
|
|
|
|
_commentValidator = commentValidator;
|
2009-11-21 03:53:55 +00:00
|
|
|
|
_contentManager = contentManager;
|
2009-11-19 23:59:24 +00:00
|
|
|
|
_authorizer = authorizer;
|
|
|
|
|
_notifier = notifier;
|
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; }
|
2009-12-24 04:49:43 +00:00
|
|
|
|
|
2009-11-19 21:58:48 +00:00
|
|
|
|
|
|
|
|
|
#region Implementation of ICommentService
|
|
|
|
|
|
|
|
|
|
public IEnumerable<Comment> GetComments() {
|
|
|
|
|
return from comment in _commentRepository.Table.ToList() select comment;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public IEnumerable<Comment> GetComments(CommentStatus status) {
|
2009-11-19 23:59:24 +00:00
|
|
|
|
return from comment in _commentRepository.Table.ToList() where comment.Status == status select comment;
|
2009-11-19 21:58:48 +00:00
|
|
|
|
}
|
|
|
|
|
|
2009-11-21 23:55:11 +00:00
|
|
|
|
public IEnumerable<Comment> GetCommentsForCommentedContent(int id) {
|
|
|
|
|
return from comment in _commentRepository.Table.ToList() where comment.CommentedOn == id select comment;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public IEnumerable<Comment> GetCommentsForCommentedContent(int id, CommentStatus status) {
|
|
|
|
|
return from comment in _commentRepository.Table.ToList() where comment.CommentedOn == id && comment.Status == status select comment;
|
|
|
|
|
}
|
|
|
|
|
|
2009-11-19 21:58:48 +00:00
|
|
|
|
public Comment GetComment(int id) {
|
|
|
|
|
return _commentRepository.Get(id);
|
|
|
|
|
}
|
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
2009-11-19 23:59:24 +00:00
|
|
|
|
public void CreateComment(Comment comment) {
|
2010-01-18 20:55:58 +00:00
|
|
|
|
comment.Status = _commentValidator.ValidateComment(comment) ? CommentStatus.Pending : CommentStatus.Spam;
|
2009-11-19 23:59:24 +00:00
|
|
|
|
_commentRepository.Create(comment);
|
|
|
|
|
}
|
|
|
|
|
|
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);
|
|
|
|
|
comment.Author = name;
|
|
|
|
|
comment.Email = email;
|
|
|
|
|
comment.SiteName = siteName;
|
|
|
|
|
comment.CommentText = commentText;
|
2009-11-25 22:05:44 +00:00
|
|
|
|
comment.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);
|
|
|
|
|
comment.Status = CommentStatus.Approved;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void PendComment(int commentId) {
|
|
|
|
|
Comment comment = GetComment(commentId);
|
|
|
|
|
comment.Status = CommentStatus.Pending;
|
|
|
|
|
}
|
|
|
|
|
|
2009-11-19 21:58:48 +00:00
|
|
|
|
public void MarkCommentAsSpam(int commentId) {
|
|
|
|
|
Comment comment = GetComment(commentId);
|
|
|
|
|
comment.Status = CommentStatus.Spam;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void DeleteComment(int commentId) {
|
|
|
|
|
_commentRepository.Delete(GetComment(commentId));
|
|
|
|
|
}
|
|
|
|
|
|
2009-11-23 21:08:47 +00:00
|
|
|
|
public bool CommentsClosedForCommentedContent(int id) {
|
|
|
|
|
return _closedCommentsRepository.Get(x => x.ContentItemId == id) == null ? false : true;
|
|
|
|
|
}
|
|
|
|
|
|
2009-11-21 23:55:11 +00:00
|
|
|
|
public void CloseCommentsForCommentedContent(int id) {
|
|
|
|
|
_closedCommentsRepository.Create(new ClosedComments { ContentItemId = id });
|
|
|
|
|
}
|
|
|
|
|
|
2009-11-23 21:08:47 +00:00
|
|
|
|
public void EnableCommentsForCommentedContent(int id) {
|
|
|
|
|
ClosedComments closedComments = _closedCommentsRepository.Get(x => x.ContentItemId == id);
|
|
|
|
|
if (closedComments != null) {
|
|
|
|
|
_closedCommentsRepository.Delete(closedComments);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2009-11-19 21:58:48 +00:00
|
|
|
|
#endregion
|
|
|
|
|
}
|
|
|
|
|
}
|