Пример #1
0
        /// <summary>
        /// Tries to get a subreddit from the web by the display name.
        /// </summary>
        /// <returns>Returns null if the subreddit get fails.</returns>
        public async Task <Subreddit> GetSubredditFromWebByDisplayName(string displayName)
        {
            Subreddit foundSubreddit = null;

            try
            {
                // Make the call
                string jsonResponse = await m_baconMan.NetworkMan.MakeRedditGetRequestAsString($"/r/{displayName}/about/.json");

                // Parse the new subreddit
                foundSubreddit = await MiscellaneousHelper.ParseOutRedditDataElement <Subreddit>(m_baconMan, jsonResponse);
            }
            catch (Exception e)
            {
                m_baconMan.TelemetryMan.ReportUnexpectedEvent(this, "failed to get subreddit", e);
                m_baconMan.MessageMan.DebugDia("failed to get subreddit", e);
            }

            // If we found it add it to the cache.
            if (foundSubreddit != null)
            {
                lock (m_tempSubredditCache)
                {
                    m_tempSubredditCache.Add(foundSubreddit);
                }
            }

            return(foundSubreddit);
        }
Пример #2
0
        /// <summary>
        ///     Called when the user added a comment or edit an existing comment.
        /// </summary>
        /// <returns></returns>
        public bool CommentAddedOrEdited(string parentOrOrgionalId, string serverResponse, bool isEdit)
        {
            // Assume if we can find author we are successful. Not sure if that is safe or not... :)
            if (!string.IsNullOrWhiteSpace(serverResponse) && serverResponse.Contains("\"author\""))
            {
                // Do the next part in a try catch so if we fail we will still report success since the
                // message was sent to reddit.
                try
                {
                    // Parse the new comment
                    var newComment = MiscellaneousHelper.ParseOutRedditDataElement <Comment>(_baconMan, serverResponse)
                                     .Result;

                    if (isEdit)
                    {
                        UpdateComment(newComment);
                    }
                    else
                    {
                        // Inject the new comment
                        InjectComment(parentOrOrgionalId, newComment);
                    }
                }
                catch (Exception e)
                {
                    // We f****d up adding the comment to the UI.
                    _baconMan.MessageMan.DebugDia("Failed injecting comment", e);
                    TelemetryManager.ReportUnexpectedEvent(this, "AddCommentSuccessButAddUiFailed");
                }

                // If we get to adding to the UI return true because reddit has the comment.
                return(true);
            }

            // Reddit returned something wrong
            _baconMan.MessageMan.ShowMessageSimple("That's not right",
                                                   "Sorry we can't post your comment right now, reddit returned and unexpected message.");
            TelemetryManager.ReportUnexpectedEvent(this, "CommentPostReturnedUnexpectedMessage");
            return(false);
        }
Пример #3
0
        /// <summary>
        /// Tries to get a subreddit from the web by the display name.
        /// </summary>
        /// <returns>Returns null if the subreddit get fails.</returns>
        public async Task <Subreddit> GetSubredditFromWebByDisplayName(string displayName)
        {
            Subreddit foundSubreddit = null;

            try
            {
                // Make the call
                string jsonResponse = await m_baconMan.NetworkMan.MakeRedditGetRequestAsString($"/r/{displayName}/about/.json");

                // Try to parse out the subreddit
                string subredditData = MiscellaneousHelper.ParseOutRedditDataElement(jsonResponse);
                if (subredditData == null)
                {
                    throw new Exception("Failed to parse out data object");
                }

                // Parse the new subreddit
                foundSubreddit = await Task.Run(() => JsonConvert.DeserializeObject <Subreddit>(subredditData));
            }
            catch (Exception e)
            {
                m_baconMan.TelemetryMan.ReportUnExpectedEvent(this, "failed to get subreddit", e);
                m_baconMan.MessageMan.DebugDia("failed to get subreddit", e);
            }

            // If we found it add it to the cache.
            if (foundSubreddit != null)
            {
                lock (m_tempSubredditCache)
                {
                    m_tempSubredditCache.Add(foundSubreddit);
                }

                // Format the subreddit
                foundSubreddit.Description = WebUtility.HtmlDecode(foundSubreddit.Description);
            }

            return(foundSubreddit);
        }