//public BasicJsonMessage<Topic> PostNewTopic(Forum forum, User user, ForumPermissionContext permissionContext, NewPost newPost, string ip, string userUrl, Func<Topic, string> topicLinkGenerator)
        //{

        //}

        public Topic PostNewTopic(Forum forum, User user, ForumPermissionContext permissionContext, NewPost newPost, string ip, string userUrl, Func <Topic, string> topicLinkGenerator)
        {
            if (!permissionContext.UserCanPost || !permissionContext.UserCanView)
            {
                throw new Exception($"User {user.Name} can't post to forum {forum.Title}.");
            }
            newPost.Title = _textParsingService.Censor(newPost.Title);
            // TODO: text parsing is controller, see issue #121 https://github.com/POPWorldMedia/POPForums/issues/121
            var urlName   = newPost.Title.ToUniqueUrlName(_topicRepository.GetUrlNamesThatStartWith(newPost.Title.ToUrlName()));
            var timeStamp = DateTime.UtcNow;
            var topicID   = _topicRepository.Create(forum.ForumID, newPost.Title, 0, 0, user.UserID, user.Name, user.UserID, user.Name, timeStamp, false, false, false, urlName);
            var postID    = _postRepository.Create(topicID, 0, ip, true, newPost.IncludeSignature, user.UserID, user.Name, newPost.Title, newPost.FullText, timeStamp, false, user.Name, null, false, 0);

            _forumRepository.UpdateLastTimeAndUser(forum.ForumID, timeStamp, user.Name);
            _forumRepository.IncrementPostAndTopicCount(forum.ForumID);
            _profileRepository.SetLastPostID(user.UserID, postID);
            var topic = new Topic {
                TopicID = topicID, ForumID = forum.ForumID, IsClosed = false, IsDeleted = false, IsPinned = false, LastPostName = user.Name, LastPostTime = timeStamp, LastPostUserID = user.UserID, ReplyCount = 0, StartedByName = user.Name, StartedByUserID = user.UserID, Title = newPost.Title, UrlName = urlName, ViewCount = 0
            };
            // <a href="{0}">{1}</a> started a new topic: <a href="{2}">{3}</a>
            var topicLink = topicLinkGenerator(topic);
            var message   = String.Format(Resources.NewPostPublishMessage, userUrl, user.Name, topicLink, topic.Title);
            var forumHasViewRestrictions = _forumRepository.GetForumViewRoles(forum.ForumID).Count > 0;

            _eventPublisher.ProcessEvent(message, user, EventDefinitionService.StaticEventIDs.NewTopic, forumHasViewRestrictions);
            _eventPublisher.ProcessEvent(String.Empty, user, EventDefinitionService.StaticEventIDs.NewPost, true);
            forum = _forumRepository.Get(forum.ForumID);
            _broker.NotifyForumUpdate(forum);
            _broker.NotifyTopicUpdate(topic, forum, topicLink);
            _searchIndexQueueRepository.Enqueue(new SearchIndexPayload {
                TenantID = _tenantService.GetTenant(), TopicID = topic.TopicID
            });
            return(topic);
        }
Exemple #2
0
        public void EditPost(Post post, PostEdit postEdit, User editingUser)
        {
            // TODO: text parsing is controller for new topic and replies, see issue #121 https://github.com/POPWorldMedia/POPForums/issues/121
            // TODO: also not checking for empty posts
            var oldText = post.FullText;

            post.Title = _textParsingService.Censor(postEdit.Title);
            if (postEdit.IsPlainText)
            {
                post.FullText = _textParsingService.ForumCodeToHtml(postEdit.FullText);
            }
            else
            {
                post.FullText = _textParsingService.ClientHtmlToHtml(postEdit.FullText);
            }
            post.ShowSig      = postEdit.ShowSig;
            post.LastEditTime = DateTime.UtcNow;
            post.LastEditName = editingUser.Name;
            post.IsEdited     = true;
            _postRepository.Update(post);
            _moderationLogService.LogPost(editingUser, ModerationType.PostEdit, post, postEdit.Comment, oldText);
            _searchIndexQueueRepository.Enqueue(new SearchIndexPayload {
                TenantID = _tenantService.GetTenant(), TopicID = post.TopicID
            });
        }
Exemple #3
0
        public Post PostReply(Topic topic, User user, int parentPostID, string ip, bool isFirstInTopic, NewPost newPost, DateTime postTime, string topicLink, Func <User, string> unsubscribeLinkGenerator, string userUrl, Func <Post, string> postLinkGenerator)
        {
            newPost.Title = _textParsingService.Censor(newPost.Title);
            if (newPost.IsPlainText)
            {
                newPost.FullText = _textParsingService.ForumCodeToHtml(newPost.FullText);
            }
            else
            {
                newPost.FullText = _textParsingService.ClientHtmlToHtml(newPost.FullText);
            }
            var postID = _postRepository.Create(topic.TopicID, parentPostID, ip, isFirstInTopic, newPost.IncludeSignature, user.UserID, user.Name, newPost.Title, newPost.FullText, postTime, false, user.Name, null, false, 0);
            var post   = new Post
            {
                PostID         = postID,
                FullText       = newPost.FullText,
                IP             = ip,
                IsDeleted      = false,
                IsEdited       = false,
                IsFirstInTopic = isFirstInTopic,
                LastEditName   = user.Name,
                LastEditTime   = null,
                Name           = user.Name,
                ParentPostID   = parentPostID,
                PostTime       = postTime,
                ShowSig        = newPost.IncludeSignature,
                Title          = newPost.Title,
                TopicID        = topic.TopicID,
                UserID         = user.UserID
            };

            _topicRepository.IncrementReplyCount(topic.TopicID);
            _topicRepository.UpdateLastTimeAndUser(topic.TopicID, user.UserID, user.Name, postTime);
            _forumRepository.UpdateLastTimeAndUser(topic.ForumID, postTime, user.Name);
            _forumRepository.IncrementPostCount(topic.ForumID);
            _topicRepository.MarkTopicForIndexing(topic.TopicID);
            _profileRepository.SetLastPostID(user.UserID, postID);
            if (unsubscribeLinkGenerator != null)
            {
                _subscribedTopicService.NotifySubscribers(topic, user, topicLink, unsubscribeLinkGenerator);
            }
            // <a href="{0}">{1}</a> made a post in the topic: <a href="{2}">{3}</a>
            var message = String.Format(Resources.NewReplyPublishMessage, userUrl, user.Name, postLinkGenerator(post), topic.Title);
            var forumHasViewRestrictions = _forumRepository.GetForumViewRoles(topic.ForumID).Count > 0;

            _eventPublisher.ProcessEvent(message, user, EventDefinitionService.StaticEventIDs.NewPost, forumHasViewRestrictions);
            _broker.NotifyNewPosts(topic, post.PostID);
            _broker.NotifyNewPost(topic, post.PostID);
            var forum = _forumRepository.Get(topic.ForumID);

            _broker.NotifyForumUpdate(forum);
            topic = _topicRepository.Get(topic.TopicID);
            _broker.NotifyTopicUpdate(topic, forum, topicLink);
            return(post);
        }
Exemple #4
0
        public Post PostReply(Topic topic, User user, int parentPostID, string ip, bool isFirstInTopic, NewPost newPost, DateTime postTime, string topicLink, Func <User, string> unsubscribeLinkGenerator, string userUrl, Func <Post, string> postLinkGenerator)
        {
            newPost.Title = _textParsingService.Censor(newPost.Title);
            // TODO: text parsing is controller, see issue #121 https://github.com/POPWorldMedia/POPForums/issues/121
            var postID = _postRepository.Create(topic.TopicID, parentPostID, ip, isFirstInTopic, newPost.IncludeSignature, user.UserID, user.Name, newPost.Title, newPost.FullText, postTime, false, user.Name, null, false, 0);
            var post   = new Post
            {
                PostID         = postID,
                FullText       = newPost.FullText,
                IP             = ip,
                IsDeleted      = false,
                IsEdited       = false,
                IsFirstInTopic = isFirstInTopic,
                LastEditName   = user.Name,
                LastEditTime   = null,
                Name           = user.Name,
                ParentPostID   = parentPostID,
                PostTime       = postTime,
                ShowSig        = newPost.IncludeSignature,
                Title          = newPost.Title,
                TopicID        = topic.TopicID,
                UserID         = user.UserID
            };

            _topicRepository.IncrementReplyCount(topic.TopicID);
            _topicRepository.UpdateLastTimeAndUser(topic.TopicID, user.UserID, user.Name, postTime);
            _forumRepository.UpdateLastTimeAndUser(topic.ForumID, postTime, user.Name);
            _forumRepository.IncrementPostCount(topic.ForumID);
            _searchIndexQueueRepository.Enqueue(new SearchIndexPayload {
                TenantID = _tenantService.GetTenant(), TopicID = topic.TopicID
            });
            _profileRepository.SetLastPostID(user.UserID, postID);
            if (unsubscribeLinkGenerator != null)
            {
                _subscribedTopicService.NotifySubscribers(topic, user, topicLink, unsubscribeLinkGenerator);
            }
            // <a href="{0}">{1}</a> made a post in the topic: <a href="{2}">{3}</a>
            var message = String.Format(Resources.NewReplyPublishMessage, userUrl, user.Name, postLinkGenerator(post), topic.Title);
            var forumHasViewRestrictions = _forumRepository.GetForumViewRoles(topic.ForumID).Count > 0;

            _eventPublisher.ProcessEvent(message, user, EventDefinitionService.StaticEventIDs.NewPost, forumHasViewRestrictions);
            _broker.NotifyNewPosts(topic, post.PostID);
            _broker.NotifyNewPost(topic, post.PostID);
            var forum = _forumRepository.Get(topic.ForumID);

            _broker.NotifyForumUpdate(forum);
            topic = _topicRepository.Get(topic.TopicID);
            _broker.NotifyTopicUpdate(topic, forum, topicLink);
            return(post);
        }
Exemple #5
0
        public void EditPost(Post post, PostEdit postEdit, User editingUser)
        {
            var oldText = post.FullText;

            post.Title = _textParsingService.Censor(postEdit.Title);
            if (postEdit.IsPlainText)
            {
                post.FullText = _textParsingService.ForumCodeToHtml(postEdit.FullText);
            }
            else
            {
                post.FullText = _textParsingService.ClientHtmlToHtml(postEdit.FullText);
            }
            post.ShowSig      = postEdit.ShowSig;
            post.LastEditTime = DateTime.UtcNow;
            post.LastEditName = editingUser.Name;
            post.IsEdited     = true;
            _postRepository.Update(post);
            _moderationLogService.LogPost(editingUser, ModerationType.PostEdit, post, postEdit.Comment, oldText);
            _topicRepository.MarkTopicForIndexing(post.TopicID);
        }
Exemple #6
0
        public Topic PostNewTopic(Forum forum, User user, ForumPermissionContext permissionContext, NewPost newPost, string ip, string userUrl, Func <Topic, string> topicLinkGenerator)
        {
            if (!permissionContext.UserCanPost || !permissionContext.UserCanView)
            {
                throw new Exception(String.Format("User {0} can't post to forum {1}.", user.Name, forum.Title));
            }
            newPost.Title = _textParsingService.Censor(newPost.Title);
            if (newPost.IsPlainText)
            {
                newPost.FullText = _textParsingService.ForumCodeToHtml(newPost.FullText);
            }
            else
            {
                newPost.FullText = _textParsingService.ClientHtmlToHtml(newPost.FullText);
            }
            var urlName   = newPost.Title.ToUniqueUrlName(_topicRepository.GetUrlNamesThatStartWith(newPost.Title.ToUrlName()));
            var timeStamp = DateTime.UtcNow;
            var topicID   = _topicRepository.Create(forum.ForumID, newPost.Title, 0, 0, user.UserID, user.Name, user.UserID, user.Name, timeStamp, false, false, false, false, urlName);
            var postID    = _postRepository.Create(topicID, 0, ip, true, newPost.IncludeSignature, user.UserID, user.Name, newPost.Title, newPost.FullText, timeStamp, false, user.Name, null, false, 0);

            _forumRepository.UpdateLastTimeAndUser(forum.ForumID, timeStamp, user.Name);
            _forumRepository.IncrementPostAndTopicCount(forum.ForumID);
            _profileRepository.SetLastPostID(user.UserID, postID);
            var topic = new Topic(topicID)
            {
                ForumID = forum.ForumID, IsClosed = false, IsDeleted = false, IsIndexed = false, IsPinned = false, LastPostName = user.Name, LastPostTime = timeStamp, LastPostUserID = user.UserID, ReplyCount = 0, StartedByName = user.Name, StartedByUserID = user.UserID, Title = newPost.Title, UrlName = urlName, ViewCount = 0
            };
            // <a href="{0}">{1}</a> started a new topic: <a href="{2}">{3}</a>
            var topicLink = topicLinkGenerator(topic);
            var message   = String.Format(Resources.NewPostPublishMessage, userUrl, user.Name, topicLink, topic.Title);
            var forumHasViewRestrictions = _forumRepository.GetForumViewRoles(forum.ForumID).Count > 0;

            _eventPublisher.ProcessEvent(message, user, EventDefinitionService.StaticEventIDs.NewTopic, forumHasViewRestrictions);
            _eventPublisher.ProcessEvent(String.Empty, user, EventDefinitionService.StaticEventIDs.NewPost, true);
            forum = _forumRepository.Get(forum.ForumID);
            _broker.NotifyForumUpdate(forum);
            _broker.NotifyTopicUpdate(topic, forum, topicLink);
            _topicRepository.MarkTopicForIndexing(topic.TopicID);
            return(topic);
        }
Exemple #7
0
        public async Task <User> CreateUser(string name, string email, string password, bool isApproved, string ip)
        {
            name = _textParsingService.Censor(name);
            if (!email.IsEmailAddress())
            {
                throw new Exception("E-mail address invalid.");
            }
            if (string.IsNullOrEmpty(name))
            {
                throw new Exception("Name must not be empty or null.");
            }
            if (await IsNameInUse(name))
            {
                throw new Exception($"The name \"{name}\" is already in use.");
            }
            if (await IsEmailInUse(email))
            {
                throw new Exception($"The e-mail \"{email}\" is already in use.");
            }
            if (await IsIPBanned(ip))
            {
                throw new Exception($"The IP {ip} is banned.");
            }
            if (await IsEmailBanned(email))
            {
                throw new Exception($"The e-mail {email} is banned.");
            }
            var creationDate     = DateTime.UtcNow;
            var authorizationKey = Guid.NewGuid();
            var salt             = Guid.NewGuid();
            var hashedPassword   = password.GetSHA256Hash(salt);
            var user             = await _userRepository.CreateUser(name, email, creationDate, isApproved, hashedPassword, authorizationKey, salt);

            await _securityLogService.CreateLogEntry(null, user, ip, string.Empty, SecurityLogType.UserCreated);

            return(user);
        }
Exemple #8
0
        public User CreateUser(string name, string email, string password, bool isApproved, string ip, int userType, int departmentId)
        {
            name = _textParsingService.Censor(name);
            if (!email.IsEmailAddress())
            {
                throw new Exception("E-mail address invalid.");
            }
            if (String.IsNullOrEmpty(name))
            {
                throw new Exception("Name must not be empty or null.");
            }
            if (IsNameInUse(name))
            {
                throw new Exception(String.Format("The name \"{0}\" is already in use.", name));
            }
            if (IsEmailInUse(email))
            {
                throw new Exception(String.Format("The e-mail \"{0}\" is already in use.", email));
            }
            if (IsIPBanned(ip))
            {
                throw new Exception(String.Format("The IP {0} is banned.", ip));
            }
            if (IsEmailBanned(email))
            {
                throw new Exception(String.Format("The e-mail {0} is banned.", email));
            }
            var creationDate     = DateTime.UtcNow;
            var authorizationKey = Guid.NewGuid();
            var salt             = Guid.NewGuid();
            var hashedPassword   = password.GetMD5Hash(salt);
            var user             = _userRepository.CreateUser(name, email, creationDate, isApproved, hashedPassword, authorizationKey, salt, userType, departmentId);

            _securityLogService.CreateLogEntry(null, user, ip, String.Empty, SecurityLogType.UserCreated);
            return(user);
        }
        public async Task <BasicServiceResponse <Topic> > PostNewTopic(User user, NewPost newPost, string ip, string userUrl, Func <Topic, string> topicLinkGenerator, Func <Topic, string> redirectLinkGenerator)
        {
            if (user == null)
            {
                return(GetPostFailMessage(Resources.LoginToPost));
            }
            var forum = await _forumRepository.Get(newPost.ItemID);

            if (forum == null)
            {
                throw new Exception($"Forum {newPost.ItemID} not found");
            }
            var permissionContext = await _forumPermissionService.GetPermissionContext(forum, user);

            if (!permissionContext.UserCanView)
            {
                return(GetPostFailMessage(Resources.ForumNoView));
            }
            if (!permissionContext.UserCanPost)
            {
                return(GetPostFailMessage(Resources.ForumNoPost));
            }
            newPost.FullText = newPost.IsPlainText ? _textParsingService.ForumCodeToHtml(newPost.FullText) : _textParsingService.ClientHtmlToHtml(newPost.FullText);
            if (await IsNewPostDupeOrInTimeLimit(newPost.FullText, user))
            {
                return(GetPostFailMessage(string.Format(Resources.PostWait, _settingsManager.Current.MinimumSecondsBetweenPosts)));
            }
            if (string.IsNullOrWhiteSpace(newPost.FullText) || string.IsNullOrWhiteSpace(newPost.Title))
            {
                return(GetPostFailMessage(Resources.PostEmpty));
            }
            newPost.Title = _textParsingService.Censor(newPost.Title);
            var urlName   = newPost.Title.ToUniqueUrlName(await _topicRepository.GetUrlNamesThatStartWith(newPost.Title.ToUrlName()));
            var timeStamp = DateTime.UtcNow;
            var topicID   = await _topicRepository.Create(forum.ForumID, newPost.Title, 0, 0, user.UserID, user.Name, user.UserID, user.Name, timeStamp, false, false, false, urlName);

            var postID = await _postRepository.Create(topicID, 0, ip, true, newPost.IncludeSignature, user.UserID, user.Name, newPost.Title, newPost.FullText, timeStamp, false, user.Name, null, false, 0);

            await _forumRepository.UpdateLastTimeAndUser(forum.ForumID, timeStamp, user.Name);

            await _forumRepository.IncrementPostAndTopicCount(forum.ForumID);

            await _profileRepository.SetLastPostID(user.UserID, postID);

            var topic = new Topic {
                TopicID = topicID, ForumID = forum.ForumID, IsClosed = false, IsDeleted = false, IsPinned = false, LastPostName = user.Name, LastPostTime = timeStamp, LastPostUserID = user.UserID, ReplyCount = 0, StartedByName = user.Name, StartedByUserID = user.UserID, Title = newPost.Title, UrlName = urlName, ViewCount = 0
            };
            // <a href="{0}">{1}</a> started a new topic: <a href="{2}">{3}</a>
            var topicLink = topicLinkGenerator(topic);
            var message   = string.Format(Resources.NewPostPublishMessage, userUrl, user.Name, topicLink, topic.Title);
            var forumHasViewRestrictions = _forumRepository.GetForumViewRoles(forum.ForumID).Result.Count > 0;
            await _eventPublisher.ProcessEvent(message, user, EventDefinitionService.StaticEventIDs.NewTopic, forumHasViewRestrictions);

            await _eventPublisher.ProcessEvent(string.Empty, user, EventDefinitionService.StaticEventIDs.NewPost, true);

            forum = await _forumRepository.Get(forum.ForumID);

            _broker.NotifyForumUpdate(forum);
            _broker.NotifyTopicUpdate(topic, forum, topicLink);
            await _searchIndexQueueRepository.Enqueue(new SearchIndexPayload { TenantID = _tenantService.GetTenant(), TopicID = topic.TopicID });

            _topicViewCountService.SetViewedTopic(topic);

            var redirectLink = redirectLinkGenerator(topic);

            return(new BasicServiceResponse <Topic> {
                Data = topic, Message = null, Redirect = redirectLink, IsSuccessful = true
            });
        }