public void Handle(CastVoteForPost command) { var user = _membershipService.GetUserById(command.UserId); if (user == null) { return; } var post = _postService.GetPostById(command.PostId); if (post == null) { return; } if (post.Deleted) { // if the post is deleted, any user other than the author can't cast a vote. if (user.Id != post.UserId) { return; } // also, that vote that the author may cast can only be a unvote (remove vote). if (command.VoteType != null) { return; } } if (!user.IsAdmin) { if (_subUserBanService.IsUserBannedFromSub(post.SubId, user.Id)) { return; } } var currentVote = _voteService.GetVoteForUserOnPost(user.Id, post.Id); if (currentVote == command.VoteType) { return; // already voted with that type! } if (command.VoteType.HasValue) { _voteService.VoteForPost(post.Id, user.Id, command.IpAddress, command.VoteType.Value, command.DateCasted); } else { _voteService.UnVotePost(post.Id, user.Id); } _eventBus.Publish(new VoteForPostCasted { PostId = post.Id, UserId = user.Id, PreviousVote = currentVote, VoteType = command.VoteType }); }
public void Handle(ReportComment command) { if (string.IsNullOrEmpty(command.Reason)) { return; } if (command.Reason.Length > 200) { command.Reason = command.Reason.Substring(0, 200); } var user = _membershipService.GetUserById(command.ReportBy); if (user == null) { return; } var comment = _commentService.GetCommentById(command.CommentId); if (comment == null) { return; } // did a mod/admin configure this comment to ignore any reports? if (comment.IgnoreReports) { return; } // the user can't report things in a sub they are banned from if (_subUserBanService.IsUserBannedFromSub(comment.Id, user.Id)) { return; } // make sure the user hasn't already report the comment var currentReports = _reportService.GetReportsForComment(comment.Id); if (currentReports.Any(x => x.ReportedBy == user.Id)) { return; } _reportService.ReportComment(comment.Id, user.Id, command.Reason); _commentService.UpdateNumberOfReportsForComment(comment.Id, currentReports.Count + 1); }
public CreatePostResponse Handle(CreatePost command) { var response = new CreatePostResponse(); try { var user = _membershipService.GetUserById(command.CreatedByUserId); if (user == null) { response.Error = "Invalid user."; return(response); } if (string.IsNullOrEmpty(command.SubName)) { response.Error = "The sub name is required."; return(response); } var sub = _subService.GetSubByName(command.SubName); if (sub == null) { response.Error = "That sub doesn't exist."; return(response); } if (!user.IsAdmin) { // make sure the user isn't banned from this sub if (_subUserBanService.IsUserBannedFromSub(sub.Id, user.Id)) { response.Error = "You are currently banned from this sub."; return(response); } } // TODO: does user look like spam? // todo: make sure the post type is allowed if (string.IsNullOrEmpty(command.Title)) { response.Error = "The title is required."; return(response); } // remove extrenous white space, and trim whitespace from the edges command.Title = Regex.Replace(command.Title, @"\s+", " ").Trim(); if (command.Title.Length > 300) { response.Error = "The title is too long."; return(response); } string domain = null; if (command.PostType == PostType.Link) { if (string.IsNullOrEmpty(command.Url)) { response.Error = "You must provide a url."; return(response); } // check https://github.com/skimur/skimur/issues/83 command.Url = command.Url.RemoveBOM(); // todo: improve url validation string scheme; string formattedUrl; if (!UrlParser.TryParseUrl(command.Url, out formattedUrl, out domain, out scheme)) { response.Error = "The url appears to be invalid."; return(response); } command.Url = formattedUrl; switch (scheme) { case "http": case "https": break; default: response.Error = "The scheme is invalid for the url."; return(response); } // todo: make sure the domain isn't banned // todo: make sure the url wasn't already submitted } else if (command.PostType == PostType.Text) { if (!user.IsAdmin) { if (!string.IsNullOrEmpty(command.Content) && command.Content.Length > 40000) { response.Error = "The post content is too long (maximum 40000 characters)."; return(response); } } } else { throw new Exception("unknown post type " + command.PostType); } bool isNsfw; if (sub.Nsfw) { // NSFW by default isNsfw = true; } else { // Let's see if the user marked this as NSFW. isNsfw = Common.IsNsfw(command.Title); } var post = new Post { Id = GuidUtil.NewSequentialId(), DateCreated = command.OverrideDateCreated.HasValue ? command.OverrideDateCreated.Value : Common.CurrentTime(), LastEditDate = null, SubId = sub.Id, UserId = user.Id, UserIp = command.IpAddress, PostType = command.PostType, Title = command.Title, SendReplies = command.NotifyReplies, Mirrored = command.Mirror, Nsfw = isNsfw, InAll = sub.InAll }; List <string> mentions = null; if (post.PostType == PostType.Link) { post.Url = command.Url; post.Domain = domain; } else { post.Content = command.Content; post.ContentFormatted = _markdownCompiler.Compile(post.Content, out mentions); } _postService.InsertPost(post); _commandBus.Send(new CastVoteForPost { DateCasted = post.DateCreated, IpAddress = command.IpAddress, PostId = post.Id, UserId = user.Id, VoteType = VoteType.Up }); if (mentions != null && mentions.Count > 0) { _eventBus.Publish(new UsersMentioned { PostId = post.Id, Users = mentions }); } _commandBus.Send(new GenerateThumbnailForPost { PostId = post.Id }); if (_embeddedProvider.IsEnabled) { _commandBus.Send(new GenerateEmbeddedMediaObject { PostId = post.Id }); } response.Title = command.Title; response.PostId = post.Id; } catch (Exception ex) { // todo: log response.Error = ex.Message; } return(response); }