Ejemplo n.º 1
0
		public static bool IsPostEditable(this IPrincipal user, Post post)
		{
			if (user == null)
				return false;
			var castUser = user as User;
			if (castUser == null)
				return false;
			return user.IsInRole(PermanentRoles.Moderator) || castUser.UserID == post.UserID;
		}
Ejemplo n.º 2
0
		public bool Update(Post post)
		{
			var result = false;
			_sqlObjectFactory.GetConnection().Using(c => result = c.Command("UPDATE pf_Post SET TopicID = @TopicID, ParentPostID = @ParentPostID, IP = @IP, IsFirstInTopic = @IsFirstInTopic, ShowSig = @ShowSig, UserID = @UserID, Name = @Name, Title = @Title, FullText = @FullText, PostTime = @PostTime, IsEdited = @IsEdited, LastEditName = @LastEditName, LastEditTime = @LastEditTime, IsDeleted = @IsDeleted, Votes = @Votes WHERE PostID = @PostID")
				.AddParameter("@TopicID", post.TopicID)
				.AddParameter("@ParentPostID", post.ParentPostID)
				.AddParameter("@IP", post.IP)
				.AddParameter("@IsFirstInTopic", post.IsFirstInTopic)
				.AddParameter("@ShowSig", post.ShowSig)
				.AddParameter("@UserID", post.UserID)
				.AddParameter("@Name", post.Name)
				.AddParameter("@Title", post.Title)
				.AddParameter("@FullText", post.FullText)
				.AddParameter("@PostTime", post.PostTime)
				.AddParameter("@IsEdited", post.IsEdited)
				.AddParameter("@LastEditTime", post.LastEditTime.GetObjectOrDbNull())
				.AddParameter("@LastEditName", post.LastEditName)
				.AddParameter("@IsDeleted", post.IsDeleted)
				.AddParameter("@PostID", post.PostID)
				.AddParameter("@Votes", post.Votes)
				.ExecuteNonQuery() == 1);
			return result;
		}
Ejemplo n.º 3
0
		public void GetCallsRepoAndReturns()
		{
			var postService = GetService();
			var postID = 123;
			var post = new Post(postID);
			_postRepo.Setup(p => p.Get(postID)).Returns(post);
			var postResult = postService.Get(postID);
			_postRepo.Verify(p => p.Get(postID), Times.Once());
			Assert.AreSame(postResult, post);
		}
Ejemplo n.º 4
0
		public void DeleteCallFiresRecalcs()
		{
			var forum = new Forum(5);
			var topic = new Topic(4) { ForumID = forum.ForumID };
			var service = GetService();
			var user = new User(123, DateTime.MinValue);
			var post = new Post(67) { UserID = user.UserID, IsFirstInTopic = false, TopicID = topic.TopicID };
			_topicService.Setup(t => t.Get(topic.TopicID)).Returns(topic);
			_forumService.Setup(f => f.Get(forum.ForumID)).Returns(forum);
			service.Delete(post, user);
			_topicService.Verify(t => t.RecalculateReplyCount(topic), Times.Exactly(1));
			_topicService.Verify(t => t.UpdateLast(topic), Times.Once());
			_forumService.Verify(f => f.UpdateCounts(forum), Times.Exactly(1));
			_forumService.Verify(f => f.UpdateLast(forum), Times.Exactly(1));
		}
Ejemplo n.º 5
0
		public void GetVotersFiltersNullNames()
		{
			var service = GetService();
			var post = new Post(1);
			var voters = new Dictionary<int, string> { { 1, "Foo" }, { 2, "Dude" }, { 3, null }, { 4, "Chica" } };
			_postRepo.Setup(x => x.GetVotes(post.PostID)).Returns(voters);
			var result = service.GetVoters(post);
			Assert.AreEqual(3, result.Voters.Count);
			Assert.False(result.Voters.ContainsValue(null));
		}
Ejemplo n.º 6
0
		public void VotePost(Post post, User user, string userUrl, string topicUrl, string topicTitle)
		{
			if (post.UserID == user.UserID)
				return;
			var voters = _postRepository.GetVotes(post.PostID);
			if (voters.ContainsKey(user.UserID))
				return;
			_postRepository.VotePost(post.PostID, user.UserID);
			var votes = _postRepository.CalculateVoteCount(post.PostID);
			_postRepository.SetVoteCount(post.PostID, votes);
			var votedUpUser = _userService.GetUser(post.UserID);
			if (votedUpUser != null)
			{
				// <a href="{0}">{1}</a> voted for a post in the topic: <a href="{2}">{3}</a>
				var message = String.Format(Resources.VoteUpPublishMessage, userUrl, user.Name, topicUrl, topicTitle);
				_eventPublisher.ProcessEvent(message, votedUpUser, EventDefinitionService.StaticEventIDs.PostVote, false);
			}
		}
Ejemplo n.º 7
0
		public int GetVoteCount(Post post)
		{
			return _postRepository.GetVoteCount(post.PostID);
		}
Ejemplo n.º 8
0
		public PostEdit GetPostForEdit(Post post, User user, bool isMobile)
		{
			if (post == null)
				throw new ArgumentNullException("post");
			if (user == null)
				throw new ArgumentNullException("user");
			var profile = _profileRepository.GetProfile(user.UserID);
			var postEdit = new PostEdit(post) { IsPlainText = profile.IsPlainText };
			if (profile.IsPlainText || isMobile)
			{
				postEdit.FullText = _textParsingService.HtmlToForumCode(post.FullText);
				postEdit.IsPlainText = true;
			}
			else
				postEdit.FullText = _textParsingService.HtmlToClientHtml(post.FullText);
			return postEdit;
		}
Ejemplo n.º 9
0
		public void EditPost(Post post, PostEdit postEdit, User editingUser)
		{
			var oldText = post.FullText;
			post.Title = _textParsingService.EscapeHtmlAndCensor(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);
		}
Ejemplo n.º 10
0
		public void GetFirstInTopicReturnsFromRepo()
		{
			var service = GetService();
			var post = new Post(123);
			_postRepo.Setup(p => p.GetFirstInTopic(It.IsAny<int>())).Returns(post);
			var result = service.GetFirstInTopic(new Topic(2));
			Assert.AreSame(post, result);
			_postRepo.Verify(p => p.GetFirstInTopic(It.IsAny<int>()), Times.Exactly(1));
		}
Ejemplo n.º 11
0
		public void VotePostCallsRepoWithPostIDAndUserID()
		{
			var service = GetService();
			var post = new Post(1);
			var user = new User(2, new DateTime());
			_postRepo.Setup(x => x.GetVotes(post.PostID)).Returns(new Dictionary<int, string>());
			service.VotePost(post, user, "abc", "def", "ghi");
			_postRepo.Verify(x => x.VotePost(post.PostID, user.UserID), Times.Once());
		}
Ejemplo n.º 12
0
		public void UndeleteCallSetsIsDeletedAndUpdates()
		{
			var forum = new Forum(5);
			var topic = new Topic(4) { ForumID = forum.ForumID };
			var service = GetService();
			var user = new User(123, DateTime.MinValue) { Roles = new List<string> { PermanentRoles.Moderator } };
			var post = new Post(67) { TopicID = topic.TopicID, IsDeleted = true };
			_topicService.Setup(t => t.Get(topic.TopicID)).Returns(topic);
			_forumService.Setup(f => f.Get(forum.ForumID)).Returns(forum);
			var persistedPost = new Post(0);
			_postRepo.Setup(p => p.Update(It.IsAny<Post>())).Callback<Post>(p => persistedPost = p);
			service.Undelete(post, user);
			Assert.AreEqual(post.PostID, persistedPost.PostID);
			Assert.False(persistedPost.IsDeleted);
		}
Ejemplo n.º 13
0
		public void UndeleteCallFiresRecalcs()
		{
			var forum = new Forum(5);
			var topic = new Topic(4) { ForumID = forum.ForumID };
			var service = GetService();
			var user = new User(123, DateTime.MinValue) { Roles = new List<string> { PermanentRoles.Moderator } };
			var post = new Post(67) { TopicID = topic.TopicID };
			_topicService.Setup(t => t.Get(topic.TopicID)).Returns(topic);
			_forumService.Setup(f => f.Get(forum.ForumID)).Returns(forum);
			service.Undelete(post, user);
			_topicService.Verify(t => t.RecalculateReplyCount(topic), Times.Exactly(1));
			_topicService.Verify(t => t.UpdateLast(topic), Times.Once());
			_forumService.Verify(f => f.UpdateCounts(forum), Times.Exactly(1));
			_forumService.Verify(f => f.UpdateLast(forum), Times.Exactly(1));
		}
Ejemplo n.º 14
0
		public void UndeleteSetsEditFields()
		{
			var forum = new Forum(5);
			var topic = new Topic(4) { ForumID = forum.ForumID };
			var service = GetService();
			var user = new User(123, DateTime.MinValue) { Roles = new List<string> { PermanentRoles.Moderator } };
			var post = new Post(67) { TopicID = topic.TopicID, IsEdited = false, UserID = user.UserID };
			_topicService.Setup(t => t.Get(topic.TopicID)).Returns(topic);
			_forumService.Setup(f => f.Get(forum.ForumID)).Returns(forum);
			var editedPost = new Post(0);
			_postRepo.Setup(p => p.Update(It.IsAny<Post>())).Callback<Post>(x => editedPost = x);
			service.Undelete(post, user);
			Assert.IsTrue(editedPost.IsEdited);
			Assert.AreEqual(user.Name, editedPost.LastEditName);
			Assert.IsTrue(editedPost.LastEditTime.HasValue);
		}
Ejemplo n.º 15
0
		public void UndeleteCallLogs()
		{
			var forum = new Forum(5);
			var topic = new Topic(4) { ForumID = forum.ForumID };
			var service = GetService();
			var user = new User(123, DateTime.MinValue) { Roles = new List<string> { PermanentRoles.Moderator }};
			var post = new Post(67) { TopicID = topic.TopicID };
			_topicService.Setup(t => t.Get(topic.TopicID)).Returns(topic);
			_forumService.Setup(f => f.Get(forum.ForumID)).Returns(forum);
			service.Undelete(post, user);
			_modLogService.Verify(m => m.LogPost(user, ModerationType.PostUndelete, post, String.Empty, String.Empty), Times.Exactly(1));
		}
Ejemplo n.º 16
0
		public void UndeleteThrowsForNonMod()
		{
			var service = GetService();
			var user = new User(123, DateTime.MinValue);
			var post = new Post(67) { UserID = user.UserID };
			Assert.Throws<Exception>(() => service.Undelete(post, user));
		}
Ejemplo n.º 17
0
		public static string PostDeleteLinkFormatter(this HtmlHelper helper, Post post)
		{
			if (post.IsDeleted)
				return Resources.Undelete;
			if (post.IsFirstInTopic)
				return Resources.DeleteTopic;
			return Resources.Delete;
		}
Ejemplo n.º 18
0
		public void VotePostCalcsAndSetsCount()
		{
			var service = GetService();
			var post = new Post(1);
			var user = new User(2, new DateTime());
			const int votes = 32;
			_postRepo.Setup(x => x.GetVotes(post.PostID)).Returns(new Dictionary<int, string>());
			_postRepo.Setup(x => x.CalculateVoteCount(post.PostID)).Returns(votes);
			service.VotePost(post, user, "abc", "def", "ghi");
			_postRepo.Verify(x => x.SetVoteCount(post.PostID, votes), Times.Once());
		}
Ejemplo n.º 19
0
		public int GetTopicPageForPost(Post post, bool includeDeleted, out Topic topic)
		{
			topic = _topicService.Get(post.TopicID);
			var postIDs = _postRepository.GetPostIDsWithTimes(post.TopicID, includeDeleted).Select(p => p.Key).ToList();
			var index = postIDs.IndexOf(post.PostID);
			var pageSize = _settingsManager.Current.PostsPerPage;
			var page = Convert.ToInt32(Math.Floor((double)index/pageSize)) + 1;
			return page;
		}
Ejemplo n.º 20
0
		public void VotePostDoesntAllowDupeVote()
		{
			var service = GetService();
			var post = new Post(1);
			var user = new User(2, new DateTime());
			var voters = new Dictionary<int, string> { { 1, "Foo" }, { user.UserID, "Dude" }, { 3, null }, { 4, "Chica" } };
			_postRepo.Setup(x => x.GetVotes(post.PostID)).Returns(voters);
			service.VotePost(post, user, "abc", "def", "ghi");
			_postRepo.Verify(x => x.VotePost(post.PostID, user.UserID), Times.Never());
		}
Ejemplo n.º 21
0
		public string GetPostForQuote(Post post, User user, bool forcePlainText)
		{
			if (post == null)
				throw new ArgumentNullException("post");
			if (post.IsDeleted)
				return "Post not found";
			if (user == null)
				throw new ArgumentNullException("user");
			var profile = _profileRepository.GetProfile(user.UserID);
			string quote;
			if (profile.IsPlainText || forcePlainText)
				quote = String.Format("[quote]\r\n[i]{0} said:[/i]\r\n{1}[/quote]", post.Name, _textParsingService.HtmlToForumCode(post.FullText));
			else
				quote = String.Format("[quote]<i>{0} said:</i><br />{1}[/quote]", post.Name, _textParsingService.HtmlToClientHtml(post.FullText));
			return quote;
		}
Ejemplo n.º 22
0
		public void GetVoteCountCallsRepoAndReturns()
		{
			var service = GetService();
			var post = new Post(1);
			const int votes = 32;
			_postRepo.Setup(x => x.GetVoteCount(post.PostID)).Returns(votes);
			var result = service.GetVoteCount(post);
			Assert.AreEqual(votes, result);
		}
Ejemplo n.º 23
0
		public void Undelete(Post post, User user)
		{
			if (user.IsInRole(PermanentRoles.Moderator))
			{
				_moderationLogService.LogPost(user, ModerationType.PostUndelete, post, String.Empty, String.Empty);
				post.IsDeleted = false;
				post.LastEditTime = DateTime.UtcNow;
				post.LastEditName = user.Name;
				post.IsEdited = true;
				_postRepository.Update(post);
				var topic = _topicService.Get(post.TopicID);
				_topicService.RecalculateReplyCount(topic);
				_topicService.UpdateLast(topic);
				var forum = _forumService.Get(topic.ForumID);
				_forumService.UpdateCounts(forum);
				_forumService.UpdateLast(forum);
			}
			else
				throw new InvalidOperationException("User must be Moderator to undelete post.");
		}
Ejemplo n.º 24
0
		public void SetAnswer(User user, Topic topic, Post post, string userUrl, string topicUrl)
		{
			if (user.UserID != topic.StartedByUserID)
				throw new SecurityException("Only the user that started a topic may set its answer.");
			if (post == null || post.TopicID != topic.TopicID)
				throw new InvalidOperationException("You can't use a post as an answer unless it's a child of the topic.");
			var answerUser = _userRepository.GetUser(post.UserID);
			if (answerUser != null // answer user is still valid
				&& !topic.AnswerPostID.HasValue && // an answer wasn't already chosen
				topic.StartedByUserID != post.UserID) // the answer isn't coming from the question asker
			{
				// <a href="{0}">{1}</a> chose an answer for the question: <a href="{2}">{3}</a>
				var message = String.Format(Resources.QuestionAnswered, userUrl, user.Name, topicUrl, topic.Title);
				_eventPublisher.ProcessEvent(message, answerUser, EventDefinitionService.StaticEventIDs.QuestionAnswered, false);
			}
			_topicRepository.UpdateAnswerPostID(topic.TopicID, post.PostID);
		}
Ejemplo n.º 25
0
		public VotePostContainer GetVoters(Post post)
		{
			var results = _postRepository.GetVotes(post.PostID);
			var filtered = results.Where(x => x.Value != null).ToDictionary(x => x.Key, x => x.Value);
			var container = new VotePostContainer
			                	{
			                		PostID = post.PostID,
									Votes = results.Count,
									Voters = filtered
			                	};
			return container;
		}
Ejemplo n.º 26
0
		public void DeleteCallLogs()
		{
			var forum = new Forum(5);
			var topic = new Topic(4) { ForumID = forum.ForumID };
			var service = GetService();
			var user = new User(123, DateTime.MinValue);
			var post = new Post(67) { UserID = user.UserID, IsFirstInTopic = false, TopicID = topic.TopicID };
			_topicService.Setup(t => t.Get(topic.TopicID)).Returns(topic);
			_forumService.Setup(f => f.Get(forum.ForumID)).Returns(forum);
			service.Delete(post, user);
			_modLogService.Verify(m => m.LogPost(user, ModerationType.PostDelete, post, String.Empty, String.Empty), Times.Exactly(1));
		}
Ejemplo n.º 27
0
		public void GetVotersReturnsContainerWithPostID()
		{
			var service = GetService();
			var post = new Post(1);
			_postRepo.Setup(x => x.GetVotes(post.PostID)).Returns(new Dictionary<int, string>());
			var result = service.GetVoters(post);
			Assert.AreEqual(post.PostID, result.PostID);
		}
Ejemplo n.º 28
0
		public void DeleteCallSetsIsDeletedAndUpdates()
		{
			var forum = new Forum(5);
			var topic = new Topic(4) { ForumID = forum.ForumID };
			var service = GetService();
			var user = new User(123, DateTime.MinValue);
			var post = new Post(67) { UserID = user.UserID, IsFirstInTopic = false, TopicID = topic.TopicID, IsDeleted = false };
			_topicService.Setup(t => t.Get(topic.TopicID)).Returns(topic);
			_forumService.Setup(f => f.Get(forum.ForumID)).Returns(forum);
			var persistedPost = new Post(0);
			_postRepo.Setup(p => p.Update(It.IsAny<Post>())).Callback<Post>(p => persistedPost = p);
			service.Delete(post, user);
			Assert.AreEqual(post.PostID, persistedPost.PostID);
			Assert.True(persistedPost.IsDeleted);
		}
Ejemplo n.º 29
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.EscapeHtmlAndCensor(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)
			{
				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;
		}
Ejemplo n.º 30
0
		public void GetVotersReturnsContainerWithTotalVotes()
		{
			var service = GetService();
			var post = new Post(1);
			var voters = new Dictionary<int, string> {{1, "Foo"}, {2, "Dude"}, {3, null}, {4, "Chica"}};
			_postRepo.Setup(x => x.GetVotes(post.PostID)).Returns(voters);
			var result = service.GetVoters(post);
			Assert.AreEqual(4, result.Votes);
		}