public static string Url(Post post) { var slug = SlugConverter.TitleToSlug(post.Title); var id = RavenIdResolver.Resolve(post.Id); var blogUrl = ConfigurationHelper.MainBlogUrl.TrimEnd('/'); return $"{blogUrl}/{id}/{slug}"; }
public static PostReference GetNextPrevPost(this IDocumentSession session, Post compareTo, bool isNext) { var queryable = session.Query<Post>() .WhereIsPublicPost(); if (isNext) { queryable = queryable .Where(post => post.PublishAt >= compareTo.PublishAt && post.Id != compareTo.Id) .OrderBy(post => post.PublishAt); } else { queryable = queryable .Where(post => post.PublishAt <= compareTo.PublishAt && post.Id != compareTo.Id) .OrderByDescending(post => post.PublishAt); } var postReference = queryable .Select(p => new Post {Id = p.Id, Title = p.Title}) .FirstOrDefault(); if (postReference == null) return null; return postReference.MapTo<PostReference>(); }
private void HandleManualSubmission(Subreddit subreddit, Post post) { _log.Info($"Post was manually submitted \"{post.Title}\" to /r/{subreddit.Name}. Verifying."); var postUrl = PostHelper.Url(post); var posts = subreddit.Search($"url:{postUrl}"); var postSubmission = post.Integration.Reddit.GetPostSubmissionForSubreddit(subreddit.Name); if (posts.Any()) { _log.Info($"Post \"{post.Title}\" found in /r/{subreddit.Name}."); postSubmission.Status = SubmissionStatus.Submitted; return; } postSubmission.Attempts++; _log.Warn($"Post \"{post.Title}\" not found in /r/{subreddit.Name}."); if (postSubmission.Attempts >= 3) { postSubmission.Status = SubmissionStatus.ManualSubmissionFailure; postSubmission.Attempts = 0; } }
public bool AreCommentsClosed(Post post, int numberOfDayToCloseComments) { if (numberOfDayToCloseComments < 1) return false; DateTimeOffset lastCommentDate = Comments.Count == 0 ? post.CreatedAt : Comments.Max(x => x.CreatedAt); return DateTimeOffset.Now - lastCommentDate > TimeSpan.FromDays(numberOfDayToCloseComments); }
private void SubmitPostToSubreddit(Post post, Subreddit subreddit) { var redditIntegration = post.Integration.Reddit; var postSubmission = redditIntegration.GetPostSubmissionForSubreddit(subreddit.Name) ?? new PostSubmission() { Subreddit = subreddit.Name }; if (postSubmission.IsManualSubmissionPending) { HandleManualSubmission(subreddit, post); return; } if (postSubmission.ShouldTrySubmit == false) { return; } _log.Info($"Submitting \"{post.Title}\" to /r/{subreddit.Name}."); try { var redditPost = subreddit.SubmitPost(HttpUtility.HtmlDecode(post.Title), PostHelper.Url(post)); if (redditPost == null) { throw new Exception($"Got null from reddit submit for {post.Id}"); } postSubmission.Status = SubmissionStatus.Submitted; _log.Info( $"Post \"{post.Title}\" with ID {post.Id} has been successfully submitted to /r/{subreddit.Name} with permalink {redditPost.Permalink}."); } catch (DuplicateLinkException duplicateLinkException) { _log.InfoException($"Link to post \"{post.Title}\" with ID {post.Id} already submitted to Reddit.", duplicateLinkException); postSubmission.Status = SubmissionStatus.Submitted; } catch (CaptchaFailedException) { postSubmission.Status = SubmissionStatus.CaptchaFailure; } catch (Exception exception) { _log.ErrorException($"Error submitting post \"{post.Title}\" with ID {post.Id} to /r/{subreddit.Name}", exception); postSubmission.Status = SubmissionStatus.UnknownFailure; } redditIntegration.RegisterPostSubmission(postSubmission); NotifyOnFailure(post, postSubmission); }
public static string SubmitUrl(string subredditName, Post post) { var queryStringData = new NameValueCollection() { { "url", PostHelper.Url(post) }, { "title", post.Title }, { "resubmit", false.ToString() } }; var queryString = ToQueryString(queryStringData); return $"https://www.reddit.com/r/{subredditName}/submit{queryString}"; }
private void SendNewCommentEmail(Post post, PostComments.Comment comment) { var viewModel = comment.MapTo<NewCommentEmailViewModel>(); viewModel.PostId = RavenIdResolver.Resolve(post.Id); viewModel.PostTitle = post.Title; viewModel.BlogName = Session.Load<BlogConfig>("Blog/Config").Title; var subject = string.Format("Comment on: {0} from {1}", viewModel.PostTitle, viewModel.BlogName); if(comment.IsSpam) subject = "Spam " + subject; CommandExecutor.ExcuteLater(new SendEmailCommand(viewModel.Email,subject, "NewComment", viewModel)); }
public ActionResult Add(PostInput input) { if (!ModelState.IsValid) return View("Edit", input); // Be able to record the user making the actual post var user = RavenSession.GetCurrentUser(); // Create the post comments object and link between it and the post var comments = new PostComments { Comments = new List<PostComments.Comment>(), Spam = new List<PostComments.Comment>() }; RavenSession.Store(comments); // Create new post object var post = new Post { Tags = TagsResolver.ResolveTagsInput(input.Tags), PublishAt = input.PublishAt, AllowComments = input.AllowComments, AuthorId = user.Id, LastEditedByUserId = user.Id, LastEditedAt = DateTimeOffset.Now, CommentsId = comments.Id, ContentType = input.ContentType, Body = input.Body, CreatedAt = DateTimeOffset.Now, Title = input.Title, }; if (post.PublishAt == DateTimeOffset.MinValue) { var postScheduleringStrategy = new PostSchedulingStrategy(RavenSession, DateTimeOffset.Now); post.PublishAt = postScheduleringStrategy.Schedule(); } // Actually save the post now RavenSession.Store(post); comments.Post = new PostComments.PostReference { Id = post.Id, PublishAt = post.PublishAt, }; return RedirectToAction("Details", new { id = post.Id.ToIntId() }); }
private void SendNewCommentEmail(Post post, PostComments.Comment comment, User postAuthor) { if (_requestValues.IsAuthenticated) return; // we don't send email for authenticated users var viewModel = comment.MapTo<NewCommentEmailViewModel>(); viewModel.PostId = RavenIdResolver.Resolve(post.Id); viewModel.PostTitle = HttpUtility.HtmlDecode(post.Title); viewModel.PostSlug = SlugConverter.TitleToSlug(post.Title); viewModel.BlogName = DocumentSession.Load<BlogConfig>("Blog/Config").Title; viewModel.Key = post.ShowPostEvenIfPrivate.MapTo<string>(); var subject = string.Format("{2}Comment on: {0} from {1}", viewModel.PostTitle, viewModel.BlogName, comment.IsSpam ? "[Spam] " : string.Empty); TaskExecutor.ExcuteLater(new SendEmailTask(viewModel.Email, subject, "NewComment", postAuthor.Email, viewModel)); }
private static void InitRedditIntegration(Post post) { if (post.Integration == null) { post.Integration = new SocialNetworkIntegration(); } if (post.Integration.Reddit == null) { post.Integration.Reddit = new Models.SocialNetwork.Reddit(); } if (post.Integration.Reddit.PostSubmissions == null) { post.Integration.Reddit.PostSubmissions = new HashSet <PostSubmission>(); } }
private void SendNewCommentEmail(Post post, PostComments.Comment comment) { if (_requestValues.IsAuthenticated) return; // we don't send email for authenticated users var viewModel = comment.MapTo<NewCommentEmailViewModel>(); viewModel.PostId = RavenIdResolver.Resolve(post.Id); viewModel.PostTitle = HttpUtility.HtmlDecode(post.Title); viewModel.BlogName = Session.Load<BlogConfig>("Blog/Config").Title; var subject = string.Format("Comment on: {0} from {1}", viewModel.PostTitle, viewModel.BlogName); if(comment.IsSpam) subject = "Spam " + subject; CommandExecutor.ExcuteLater(new SendEmailCommand(viewModel.Email,subject, "NewComment", viewModel)); }
private void NotifyOnFailure(Post post, PostSubmission postSubmission) { if (postSubmission.IsFailure == false) { return; } try { var subject = $"Failed submitting post to /r/{postSubmission.Subreddit}"; var author = _session.Load <User>(post.AuthorId); var model = new RedditSubmissionFailedViewModel() { Submission = postSubmission, Post = post }; TaskExecutor.ExcuteLater(new SendEmailTask(null, subject, "RedditSubmissionFailed", author.Email, model)); } catch (Exception e) { _log.ErrorException("Error notifying on Reddit submission failures.", e); } }
private void importBlogPosts(IDocumentStore store, BlogMLBlog blog, Dictionary<string, User> usersList) { foreach (var post in blog.Posts) { var authorId = getAuthorId(usersList, post); var ravenPost = new Post { AuthorId = authorId, CreatedAt = new DateTimeOffset(post.DateCreated), PublishAt = new DateTimeOffset(post.DateCreated), Body = post.Content.Text, LegacySlug = SlugConverter.TitleToSlug(post.PostName ?? post.Title), Title = HttpUtility.HtmlDecode(post.Title), Tags = post.Categories.Cast<BlogMLCategoryReference>() .Select(x => blog.Categories.First(c => c.ID == x.Ref).Title) .ToArray(), AllowComments = true }; var commentsCollection = new PostComments(); commentsCollection.Spam = new List<PostComments.Comment>(); commentsCollection.Comments = post.Comments.Cast<BlogMLComment>() .Where(comment => comment.Approved) .OrderBy(comment => comment.DateCreated) .Select( comment => new PostComments.Comment { Id = commentsCollection.GenerateNewCommentId(), Author = comment.UserName, Body = ConvertCommentToMarkdown(comment.Content.Text), CreatedAt = comment.DateCreated, Email = comment.UserEMail, Url = comment.UserUrl, Important = usersList.Any( u => u.Value.FullName == comment.UserName), //UserAgent = comment., //UserHostAddress = comment.IpAddress, IsSpam = false, CommenterId = null, } ).ToList(); commentsCollection.Spam = post.Comments.Cast<BlogMLComment>() .Where(comment => !comment.Approved) .OrderBy(comment => comment.DateCreated) .Select( comment => new PostComments.Comment { Id = commentsCollection.GenerateNewCommentId(), Author = comment.UserName, Body = ConvertCommentToMarkdown(comment.Content.Text), CreatedAt = comment.DateCreated, Email = comment.UserEMail, Url = comment.UserUrl, Important = usersList.Any(u => u.Value.FullName == comment.UserName), //UserAgent = comment.UserAgent, //UserHostAddress = comment.IpAddress, IsSpam = true, CommenterId = null, } ).Where(c => c.Body != null).ToList(); ravenPost.CommentsCount = commentsCollection.Comments.Count; using (var s = store.OpenSession()) { s.Store(commentsCollection); ravenPost.CommentsId = commentsCollection.Id; s.Store(ravenPost); commentsCollection.Post = new PostComments.PostReference { Id = ravenPost.Id, PublishAt = ravenPost.PublishAt }; s.SaveChanges(); } } }
private static void InitRedditIntegration(Post post) { if (post.Integration == null) post.Integration = new SocialNetworkIntegration(); if (post.Integration.Reddit == null) post.Integration.Reddit = new Models.SocialNetwork.Reddit(); if (post.Integration.Reddit.PostSubmissions == null) post.Integration.Reddit.PostSubmissions = new HashSet<PostSubmission>(); }
private ActionResult PostingCommentFailed(Post post, CommentInput input, Guid key) { if (Request.IsAjaxRequest()) return Json(new {Success = false, message = ModelState.FirstErrorMessage()}); var postReference = post.MapTo<PostReference>(); var result = Details(postReference.DomainId, postReference.Slug, key); var model = result as ViewResult; if (model != null) { var viewModel = model.Model as PostViewModel; if (viewModel != null) viewModel.Input = input; } return result; }
private void ValidateCommentsAllowed(Post post, PostComments comments) { if (comments.AreCommentsClosed(post, BlogConfig.NumberOfDayToCloseComments)) ModelState.AddModelError("CommentsClosed", "This post is closed for new comments."); if (post.AllowComments == false) ModelState.AddModelError("CommentsClosed", "This post does not allow comments."); }
private ActionResult PostingCommentSucceeded(Post post) { const string successMessage = "Your comment will be posted soon. Thanks!"; if (Request.IsAjaxRequest()) return Json(new {Success = true, message = successMessage}); TempData["message"] = successMessage; var postReference = post.MapTo<PostReference>(); return RedirectToAction("Details", new {Id = postReference.DomainId, postReference.Slug}); }
private void NotifyOnFailure(Post post, PostSubmission postSubmission) { if (postSubmission.IsFailure == false) return; try { var subject = $"Failed submitting post to /r/{postSubmission.Subreddit}"; var author = _session.Load<User>(post.AuthorId); var model = new RedditSubmissionFailedViewModel() { Submission = postSubmission, Post = post }; TaskExecutor.ExcuteLater(new SendEmailTask(null, subject, "RedditSubmissionFailed", author.Email, model)); } catch (Exception e) { _log.ErrorException("Error notifying on Reddit submission failures.", e); } }
public bool AreCommentsClosed(Post post) { DateTimeOffset lastCommentDate = Comments.Count == 0 ? post.CreatedAt : Comments.Max(x => x.CreatedAt); return DateTimeOffset.Now - lastCommentDate > TimeSpan.FromDays(30); }
private string GetPostLink(Post post) { var postReference = post.MapTo<PostReference>(); return Url.RelativeToAbsolute(Url.Action("Details", "PostDetails", new { Id = postReference.DomainId, postReference.Slug })); }
private ActionResult PostingCommentSucceeded(Post post, CommentInput input) { const string successMessage = "Your comment will be posted soon. Thanks!"; if (Request.IsAjaxRequest()) return Json(new {Success = true, message = successMessage}); TempData["new-comment"] = input; var postReference = post.MapTo<PostReference>(); return Redirect(Url.Action("Details", new { Id = postReference.DomainId, postReference.Slug, key = post.ShowPostEvenIfPrivate }) + "#comments-form-location"); }
private string GetPostLink(Post post) { if (post.Id == null) // invalid feed return Url.AbsoluteAction("Index", "Posts"); return Url.AbsoluteAction("Details", "PostDetails", new { Id = RavenIdResolver.Resolve(post.Id), Slug = SlugConverter.TitleToSlug(post.Title), Key = post.ShowPostEvenIfPrivate }); }
private void SubmitPostToSubreddit(Post post, Subreddit subreddit) { var redditIntegration = post.Integration.Reddit; var postSubmission = redditIntegration.GetPostSubmissionForSubreddit(subreddit.Name) ?? new PostSubmission() { Subreddit = subreddit.Name }; if (postSubmission.IsManualSubmissionPending) { HandleManualSubmission(subreddit, post); return; } if (postSubmission.ShouldTrySubmit == false) return; _log.Info($"Submitting \"{post.Title}\" to /r/{subreddit.Name}."); try { var redditPost = subreddit.SubmitPost(HttpUtility.HtmlDecode(post.Title), PostHelper.Url(post)); if (redditPost == null) { throw new Exception($"Got null from reddit submit for {post.Id}"); } postSubmission.Status = SubmissionStatus.Submitted; _log.Info( $"Post \"{post.Title}\" with ID {post.Id} has been successfully submitted to /r/{subreddit.Name} with permalink {redditPost.Permalink}."); } catch (DuplicateLinkException duplicateLinkException) { _log.InfoException($"Link to post \"{post.Title}\" with ID {post.Id} already submitted to Reddit.", duplicateLinkException); postSubmission.Status = SubmissionStatus.Submitted; } catch (CaptchaFailedException) { postSubmission.Status = SubmissionStatus.CaptchaFailure; } catch (Exception exception) { _log.ErrorException($"Error submitting post \"{post.Title}\" with ID {post.Id} to /r/{subreddit.Name}", exception); postSubmission.Status = SubmissionStatus.UnknownFailure; } redditIntegration.RegisterPostSubmission(postSubmission); NotifyOnFailure(post, postSubmission); }