Ejemplo n.º 1
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);
        }
Ejemplo n.º 2
0
        public string Upsert(string mainSubPostId, string mainSubPostPermalink, string mainSubPostTitle, string opUsername)
        {
            // Get all of the delta comments for this post
            var deltaComments = _repository.GetDeltaCommentsForPost(mainSubPostId);

            // Build post title and text
            var postTitleAndText = _postBuilder.BuildDeltaLogPost(mainSubPostTitle, mainSubPostPermalink, opUsername, deltaComments);

            // Determine if a post already exists in DeltaLog
            var existingPostMapping = _repository.GetDeltaLogPostMapping(mainSubPostId);

            if (existingPostMapping == null)
            {
                return(createNewPost(postTitleAndText.Item1, postTitleAndText.Item2, mainSubPostId, mainSubPostPermalink));
            }

            try
            {
                // There is an existing mapping - edit post
                _redditService.EditPost(existingPostMapping.DeltaLogPostUrl, postTitleAndText.Item2);
            }
            catch (Exception ex)
            {
                // This will fail with a forbidden if the post was manually deleted.
                // It should never really happen, but this is handy for my testing
                // and won't really hurt in production
                if (ex.ToString().Contains("403 (Forbidden)"))
                {
                    return(createNewPost(postTitleAndText.Item1, postTitleAndText.Item2, mainSubPostId, mainSubPostPermalink));
                }

                throw;
            }

            return(existingPostMapping.DeltaLogPostUrl);
        }