public void UpsertWATTArticle(WATTArticle article)
        {
            var wattArticlesCollection = _liteDatabase.GetCollection <WATTArticle>(WATTArticlesCollectionName);

            wattArticlesCollection.EnsureIndex(d => d.Id, true);
            wattArticlesCollection.Upsert(article);
        }
示例#2
0
        public DB4Comment BuildSticky(DB4Thing post, int deltaCount, WATTArticle article, string deltaLogPostUrl)
        {
            // Can only call BuildSticky on posts
            Assert.That(post.Type == DB4ThingType.Post);

            string body = string.Empty;

            if (deltaCount > 0)
            {
                body += _appConfiguration.Comments.PostStickyDeltas
                        .Replace(_appConfiguration.ReplaceTokens.UsernameToken, post.AuthorName)
                        .Replace(_appConfiguration.ReplaceTokens.CountToken, deltaCount.ToString())
                        .Replace(_appConfiguration.ReplaceTokens.PostLink, deltaLogPostUrl)
                        .Replace(_appConfiguration.ReplaceTokens.DeltaLogSubredditToken,
                                 _appConfiguration.DeltaLogSubredditName);
            }

            if (article != null)
            {
                if (!string.IsNullOrEmpty(body))
                {
                    body += "\n\n";
                }

                body += _appConfiguration.Comments.PostStickyWATT
                        .Replace(_appConfiguration.ReplaceTokens.WATTLinkToken, article.Url);
            }

            return(new DB4Comment
            {
                CommentType = DB4CommentType.PostSticky,
                CommentBody = body
            });
        }
        public void UpsertOrRemove(DB4Thing post, int?deltaCount, WATTArticle article, string deltaLogPostUrl)
        {
            // This method needs to do some extra work to handle WATT stuff
            // I'm not loving how this particular implementation came out...

            // Anyhow, we're expecting either a delta count from award / unaward OR a WATTArticle from a private message
            // Not both.
            if (deltaCount.HasValue && article != null)
            {
                throw new InvalidOperationException("Both a delta count and WATT article were provided when only one or the other was expected.");
            }

            // If we didn't get a delta count, look it up
            int finalDeltaCount;

            if (deltaCount.HasValue)
            {
                finalDeltaCount = deltaCount.Value;
            }
            else
            {
                finalDeltaCount = _repository.GetDeltaCommentsForPost(post.Id, post.AuthorName).Count;
            }

            // If we didn't get a WATT article, look it up
            // It's optional, so it could still be null after the lookup
            if (article == null)
            {
                article = _repository.GetWattArticleForPost(post.Id);
            }

            // Find out if the sticky comment has been made yet
            var result = _commentDetector.DidDB4MakeStickyComment(post);

            // During an Unaward, we can go down to zero.
            // If there are no deltas and no WATT article, delete the sticky
            if (deltaCount == 0 && article == null)
            {
                _commentReplier.DeleteReply(result.Comment);
                return;
            }

            // We'll need the updated sticky whether it has been made or not
            var db4Comment = _commentBuilder.BuildSticky(post, finalDeltaCount, article, deltaLogPostUrl);

            if (result.HasDB4Replied)
            {
                // Sticky exists, edit
                _commentReplier.EditReply(result.Comment, db4Comment);
            }
            else
            {
                // No sticky, create it
                _commentReplier.Reply(post, db4Comment, true);
            }
        }
示例#4
0
        public void Handle(DB4Thing privateMessage)
        {
            // First split the PM up on newlines
            var privateMessageLines = privateMessage.Body.Split(
                new[] { "\r\n", "\r", "\n" },
                StringSplitOptions.RemoveEmptyEntries);

            // We are expecting four lines
            // postid, postfullname, title, url
            // ex:
            // 8hr3tt
            // t3_8hr3tt
            // This is a WATT article
            // https://www.url.com
            if (privateMessageLines.Length != 4)
            {
                _redditService.ReplyToPrivateMessage(privateMessage.Id, CreateFailedInvalidFormatMessage);
                return;
            }

            // Create article
            string postId       = privateMessageLines[0];
            string postFullname = privateMessageLines[1];
            string title        = privateMessageLines[2];
            string url          = privateMessageLines[3];

            var article = new WATTArticle
            {
                Id           = Guid.NewGuid(),
                RedditPostId = postId,
                Title        = title,
                Url          = url
            };

            // Save article
            _db4Repository.UpsertWATTArticle(article);

            // Need to retrieve the full post to make life easy
            var post = _redditService.GetThingByFullname(postFullname);

            _redditService.PopulateChildren(post);

            // Get DeltaLog mapping for building out sticky
            // The mapping can be null
            var    deltaLogMapping = _db4Repository.GetDeltaLogPostMapping(postId);
            string deltaLogPostUrl = string.Empty;

            if (deltaLogMapping != null)
            {
                deltaLogPostUrl = deltaLogMapping.DeltaLogPostUrl;
            }

            // Update sticky for the post in question
            _stickyCommentEditor.UpsertOrRemove(post, null, article, deltaLogPostUrl);
        }