Beispiel #1
0
        public ExpandoObject Comment(CommentSaveModel model)
        {
            dynamic o = new ExpandoObject();
            var currentMemberId = Members.GetCurrentMemberId();

            var c = new Comment();
            c.Body = model.Body;
            c.MemberId = currentMemberId;
            c.Created = DateTime.Now;
            c.ParentCommentId = model.Parent;
            c.TopicId = model.Topic;
            c.IsSpam = Members.GetCurrentMember().GetPropertyValue<bool>("blocked") || c.DetectSpam();
            CommentService.Save(c);
            if (c.IsSpam)
                SpamChecker.SendSlackSpamReport(c.Body, c.TopicId, "comment", c.MemberId);

            o.id = c.Id;
            o.body = c.Body.Sanitize().ToString();
            o.topicId = c.TopicId;
            o.authorId = c.MemberId;
            o.created = c.Created.ConvertToRelativeTime();
            var author = Members.GetById(currentMemberId);
            o.authorKarma = author.Karma();
            o.authorName = author.Name;
            o.roles = author.GetRoles();
            o.cssClass = model.Parent > 0 ? "level-2" : string.Empty;
            o.parent = model.Parent;
            o.isSpam = c.IsSpam;

            return o;
        }
        public void SendNotification(Comment comment, string memberName, string url)
        {
            var topicService = new TopicService(ApplicationContext.Current.DatabaseContext);
            var topic = topicService.GetById(comment.TopicId);

            var db = ApplicationContext.Current.DatabaseContext.Database;
            var sql = new Sql().Select("memberId")
                .From("forumTopicSubscribers")
                .Where("topicId = @topicId", new { topicId = topic.Id });

            var results = db.Query<int>(sql).ToList();

            using (ContextHelper.EnsureHttpContext())
            {
                var memberShipHelper = new MembershipHelper(UmbracoContext.Current);

                foreach (var memberId in results.Where(memberId => memberId != comment.MemberId))
                {
                    try
                    {
                        var member = memberShipHelper.GetById(memberId);
                        if (member.GetPropertyValue<bool>("bugMeNot"))
                            continue;

                        var from = new MailAddress(_details.SelectSingleNode("//from/email").InnerText,
                            _details.SelectSingleNode("//from/name").InnerText);

                        var subject = string.Format(_details.SelectSingleNode("//subject").InnerText, topic.Title);

                        var domain = _details.SelectSingleNode("//domain").InnerText;
                        var body = _details.SelectSingleNode("//body").InnerText;
                        body = string.Format(body, topic.Title, "https://" + domain + url + "#comment-" + comment.Id, memberName,
                            HttpUtility.HtmlDecode(umbraco.library.StripHtml(comment.Body)));

                        var mailMessage = new MailMessage
                        {
                            Subject = subject,
                            Body = body
                        };

                        mailMessage.To.Add(member.GetPropertyValue<string>("Email"));
                        mailMessage.From = @from;

                        using (var smtpClient = new SmtpClient())
                        {
                            smtpClient.Send(mailMessage);
                        }
                    }
                    catch (Exception exception)
                    {
                        LogHelper.Error<NewForumComment>(
                            string.Format("Error sending mail to member id {0}", memberId), exception);
                    }
                }
            }
        }
 public void Delete(Comment comment)
 {
     var eventArgs = new CommentEventArgs() { Comment = comment };
     if (Deleting.RaiseAndContinue(this, eventArgs))
     {
         UpdateTopicPostsCount(comment, false);
         _databaseContext.Database.Delete(comment);
         Deleted.Raise(this, eventArgs);
     }
     else
         CancelledByEvent.Raise(this, eventArgs);
 }
Beispiel #4
0
        /* Crud */
        public Comment Save(Comment comment, bool updateTopicPostCount = true)
        {
            var newComment = comment.Id <= 0;
            var eventArgs = new CommentEventArgs() { Comment = comment };

            if (newComment)
                Creating.Raise(this, eventArgs);
            else
                Updating.Raise(this, eventArgs);

            if (!eventArgs.Cancel)
            {
                //save comment
                _databaseContext.Database.Save(comment);


                //topic post count
                if(updateTopicPostCount)
                    UpdateTopicPostsCount(comment);

                //parent comment state
                if (comment.ParentCommentId > 0)
                {
                    var p = GetById(comment.ParentCommentId);
                    if (p != null)
                        p.HasChildren = true;
                    Save(p, false);
                }

                if (newComment)
                    Created.Raise(this, eventArgs);
                else
                    Updated.Raise(this, eventArgs);
            }
            else
            {
                CancelledByEvent.Raise(this, eventArgs);
            }

            return comment;
        }
        private void UpdateTopicPostsCount(Comment c, bool adding = true)
        {
            var ts = _topicService;
            var t = ts.GetById(c.TopicId);
            t.Replies = adding ? t.Replies + 1 : t.Replies - 1;
            t.Updated = DateTime.Now;

            if (adding)
                t.LatestReplyAuthor = c.MemberId;

            ts.Save(t);
        }
 public void SendNotifications(Comment comment, string memberName, string commentUrl)
 {
     var newForumCommentNotification = new NotificationsCore.Notifications.NewForumComment();
     newForumCommentNotification.SendNotification(comment, memberName, commentUrl);
 }