コード例 #1
0
        public static void OpenCommentDiscussion(int articleId, int topicId)
        {
            try
            {
                var api = DiscourseHelper.CreateApi();

                api.SetVisibility(topicId, true);

                // delete the worthless auto-generated "this topic is now invisible/visible" rubbish
                var topic = api.GetTopic(topicId);
                foreach (var post in topic.Posts.Where(p => p.Type == Post.PostType.ModeratorAction))
                {
                    api.DeletePost(post.Id);
                }

                StoredProcs
                .Articles_CreateOrUpdateArticle(articleId, Discourse_Topic_Opened: YNIndicator.Yes)
                .Execute();
            }
            catch (Exception ex)
            {
                throw new InvalidOperationException(
                          string.Format("An unknown error occurred when attempting to open the Discourse discussion. "
                                        + "Verify that the article #{0} is assigned to a valid correct topic ID (currently #{1})", articleId, topicId), ex);
            }
        }
コード例 #2
0
ファイル: DiscourseHelper.cs プロジェクト: hifi/WtfWebApp
        public static int CreateCommentDiscussion(ArticleModel article)
        {
            Logger.Information("Creating comment discussion for article \"{0}\" (ID={1}).", article.Title, article.Id);

            var api = DiscourseHelper.CreateApi();

            // create topic and embed <!--ARTICLEID:...--> in the body so the hacky JavaScript
            // for the "Feature" button can append the article ID to each of its query strings
            var topic = api.CreateTopic(
                new Category(Config.Discourse.CommentCategory),
                article.Title,
                string.Format(
                    "Discussion for the article: {0}\r\n\r\n<!--ARTICLEID:{1}-->",
                    article.Url,
                    article.Id
                    )
                );

            api.SetVisibility(topic.Id, false);

            StoredProcs
            .Articles_CreateOrUpdateArticle(article.Id, Discourse_Topic_Id: topic.Id)
            .Execute();

            return(topic.Id);
        }
コード例 #3
0
ファイル: DiscourseHelper.cs プロジェクト: hifi/WtfWebApp
        public static IList <Post> GetFeaturedCommentsForArticle(int articleId)
        {
            try
            {
                return(DiscourseCache.GetOrAdd(
                           "FeaturedCommentsForArticle_" + articleId,
                           () =>
                {
                    Logger.Debug("Getting and caching featured comments for article (ID={0}).", articleId);

                    var api = DiscourseHelper.CreateApi();

                    return StoredProcs.Articles_GetFeaturedComments(articleId)
                    .Execute()
                    .Select(c => api.GetReplyPost(c.Discourse_Post_Id))
                    .Where(p => !p.Hidden)
                    .ToList()
                    .AsReadOnly();
                }
                           ));
            }
            catch (Exception ex)
            {
                Logger.Error("Error getting featured comments for article (ID={0}), error: {1}", articleId, ex);
                return(new Post[0]);
            }
        }
コード例 #4
0
ファイル: DiscourseHelper.cs プロジェクト: hifi/WtfWebApp
        public static IList <Topic> GetSideBarWtfs()
        {
            try
            {
                return(DiscourseCache.GetOrAdd(
                           "SideBarWtfs",
                           () =>
                {
                    Logger.Debug("Getting Side Bar WTFs.");

                    var api = DiscourseHelper.CreateApi();
                    return api.GetTopicsByCategory(new Category(Config.Discourse.SideBarWtfCategory))
                    .Where(topic => !topic.Pinned && topic.Visible)
                    .Take(5)
                    .ToList()
                    .AsReadOnly();
                }
                           ));
            }
            catch (Exception ex)
            {
                Logger.Error("Error getting Side Bar WTFs, error: {0}", ex);
                return(new Topic[0]);
            }
        }
コード例 #5
0
        private string GetOrDeleteRequest(string method, string urlFormat, params object[] args)
        {
            lock (requestLock)
            {
                string relativeUrl = string.Format(urlFormat, args);

                string requestUrl = this.GetRequestUrl(relativeUrl);

                var request = WebRequest.Create(requestUrl);
                request.Method  = method;
                request.Timeout = Config.Discourse.ApiRequestTimeout;
                try
                {
                    using (var response = request.GetResponse())
                        using (var stream = response.GetResponseStream())
                        {
                            return(new StreamReader(stream).ReadToEnd());
                        }
                }
                catch (TimeoutException tex)
                {
                    DiscourseHelper.PauseDiscourseConnections(tex, 10);
                    throw;
                }
                catch (WebException wex)
                {
                    DiscourseHelper.PauseDiscourseConnections(wex, 10);
                    throw ParseFirstError(wex);
                }
            }
        }
コード例 #6
0
ファイル: DiscourseApi.cs プロジェクト: hifi/WtfWebApp
        private static Exception ParseFirstError(WebException wex)
        {
            Logger.Debug("Attempting to get and parse the first error returned from Discourse JSON result...");
            try
            {
                string jsonErrors = new StreamReader(wex.Response.GetResponseStream()).ReadToEnd();
                Logger.Debug("JSON result was: " + jsonErrors);
                wex.Response.Dispose();
                dynamic json = JsonConvert.DeserializeObject(jsonErrors);
                if (json == null)
                {
                    Logger.Debug("JSON error was null.");
                    return(wex);
                }

                return(new InvalidOperationException(json.errors[0].ToString(), wex));
            }
            catch
            {
                Logger.Debug("There was an error attempting to parse the first JSON error.");
                var ex = new InvalidOperationException("Unknown error connecting to the forum API. Ensure the host name and API key settings are correct in web.config. The comment and sidebar categories must also exist on Discourse.", wex);
                DiscourseHelper.PauseDiscourseConnections(ex, 10);
                return(ex);
            }
        }
コード例 #7
0
ファイル: DiscourseApi.cs プロジェクト: hifi/WtfWebApp
        private string PutOrPostRequest(string relativeUrl, IEnumerable <KeyValuePair <string, string> > postData, string method)
        {
            lock (requestLock)
            {
                string requestUrl = this.GetRequestUrl(relativeUrl);

                var request = WebRequest.Create(requestUrl);
                request.Method      = method;
                request.Timeout     = Config.Discourse.ApiRequestTimeout;
                request.ContentType = "application/x-www-form-urlencoded";

                Logger.Debug("Sending Discourse {0} request to URL: {1}", method, request.RequestUri);

                try
                {
                    using (var body = request.GetRequestStream())
                        using (var writer = new StreamWriter(body))
                        {
                            foreach (var pair in postData)
                            {
                                writer.Write("&{0}={1}", HttpUtility.UrlEncode(pair.Key), HttpUtility.UrlEncode(pair.Value));
                            }
                        }

                    using (var response = request.GetResponse())
                        using (var stream = response.GetResponseStream())
                        {
                            Logger.Debug("Response received, response code was: {0}", GetResponseCode(response));
                            return(new StreamReader(stream).ReadToEnd());
                        }
                }
                catch (TimeoutException tex)
                {
                    Logger.Debug("Timeout exception for {0} request to URL: {1}", method, request.RequestUri);
                    DiscourseHelper.PauseDiscourseConnections(tex, 10);
                    throw;
                }
                catch (WebException wex)
                {
                    Logger.Debug("Web exception for {0} request to URL: {1}", method, request.RequestUri);
                    DiscourseHelper.PauseDiscourseConnections(wex, 10);
                    throw ParseFirstError(wex);
                }
            }
        }
コード例 #8
0
 public static Topic GetDiscussionTopic(int topicId)
 {
     try
     {
         return(DiscourseCache.GetOrAdd(
                    "Topic_" + topicId,
                    () =>
         {
             var api = DiscourseHelper.CreateApi();
             return api.GetTopic(topicId);
         }
                    ));
     }
     catch (Exception ex)
     {
         throw new InvalidOperationException(
                   string.Format("An unknown error occurred when attempting to get the Discourse topic #{0}. "
                                 + "Verify that this Discourse topic ID actually exists (e.g. /t/{0} relative to forum)", topicId), ex);
     }
 }
コード例 #9
0
        private static Exception ParseFirstError(WebException wex)
        {
            try
            {
                string jsonErrors = new StreamReader(wex.Response.GetResponseStream()).ReadToEnd();
                wex.Response.Dispose();
                dynamic json = JsonConvert.DeserializeObject(jsonErrors);
                if (json == null)
                {
                    return(wex);
                }

                return(new InvalidOperationException(json.errors[0].ToString(), wex));
            }
            catch
            {
                var ex = new InvalidOperationException("Unknown error connecting to the forum API. Ensure the host name and API key settings are correct in web.config. The comment and sidebar categories must also exist on Discourse.", wex);
                DiscourseHelper.PauseDiscourseConnections(ex, 10);
                return(ex);
            }
        }
コード例 #10
0
 public static IList <Topic> GetSideBarWtfs()
 {
     try
     {
         return(DiscourseCache.GetOrAdd(
                    "SideBarWtfs",
                    () =>
         {
             var api = DiscourseHelper.CreateApi();
             return api.GetTopicsByCategory(new Category(Config.Discourse.SideBarWtfCategory))
             .Where(topic => !topic.Pinned && topic.Visible)
             .Take(5)
             .ToList()
             .AsReadOnly();
         }
                    ));
     }
     catch
     {
         return(new Topic[0]);
     }
 }
コード例 #11
0
ファイル: DiscourseApi.cs プロジェクト: hifi/WtfWebApp
        private string GetOrDeleteRequest(string method, string urlFormat, params object[] args)
        {
            lock (requestLock)
            {
                string relativeUrl = string.Format(urlFormat, args);

                string requestUrl = this.GetRequestUrl(relativeUrl);

                var request = WebRequest.Create(requestUrl);
                request.Method  = method;
                request.Timeout = Config.Discourse.ApiRequestTimeout;

                Logger.Debug("Sending Discourse {0} request to URL: {1}", method, request.RequestUri);

                try
                {
                    using (var response = request.GetResponse())
                        using (var stream = response.GetResponseStream())
                        {
                            Logger.Debug("Response received, response code was: {0}", GetResponseCode(response));
                            return(new StreamReader(stream).ReadToEnd());
                        }
                }
                catch (TimeoutException tex)
                {
                    Logger.Debug("Timeout exception for {0} request to URL: {1}", method, request.RequestUri);
                    DiscourseHelper.PauseDiscourseConnections(tex, 10);
                    throw;
                }
                catch (WebException wex)
                {
                    Logger.Debug("Web exception for {0} request to URL: {1}", method, request.RequestUri);
                    DiscourseHelper.PauseDiscourseConnections(wex, 10);
                    throw ParseFirstError(wex);
                }
            }
        }
コード例 #12
0
        public static IList <Post> GetFeaturedCommentsForArticle(int articleId)
        {
            try
            {
                return(DiscourseCache.GetOrAdd(
                           "FeaturedCommentsForArticle_" + articleId,
                           () =>
                {
                    var api = DiscourseHelper.CreateApi();

                    return StoredProcs.Articles_GetFeaturedComments(articleId)
                    .Execute()
                    .Select(c => api.GetReplyPost(c.Discourse_Post_Id))
                    .Where(p => !p.Hidden)
                    .ToList()
                    .AsReadOnly();
                }
                           ));
            }
            catch
            {
                return(new Post[0]);
            }
        }