From 1cc37e32b9594c4ae3f8a19e529159cbbc41c1b0 Mon Sep 17 00:00:00 2001 From: Sam Date: Mon, 27 Oct 2014 09:44:42 +1100 Subject: [PATCH] FEATURE: add max_reply_history to limit number of replies that can be expanded, when clicking "in-reply-to" --- app/assets/javascripts/discourse/models/post_stream.js | 2 +- app/controllers/posts_controller.rb | 2 +- app/models/post.rb | 9 +++++++-- config/locales/server.en.yml | 1 + config/site_settings.yml | 3 +++ spec/models/post_spec.rb | 1 + 6 files changed, 14 insertions(+), 4 deletions(-) diff --git a/app/assets/javascripts/discourse/models/post_stream.js b/app/assets/javascripts/discourse/models/post_stream.js index ee69626a12a..6955fdce12e 100644 --- a/app/assets/javascripts/discourse/models/post_stream.js +++ b/app/assets/javascripts/discourse/models/post_stream.js @@ -649,7 +649,7 @@ Discourse.PostStream = Em.Object.extend({ **/ findReplyHistory: function(post) { var postStream = this, - url = "/posts/" + post.get('id') + "/reply-history.json"; + url = "/posts/" + post.get('id') + "/reply-history.json?max_replies=" + Discourse.SiteSettings.max_reply_history; return Discourse.ajax(url).then(function(result) { return result.map(function (p) { diff --git a/app/controllers/posts_controller.rb b/app/controllers/posts_controller.rb index 14fbc4acbf6..bd840b72cb8 100644 --- a/app/controllers/posts_controller.rb +++ b/app/controllers/posts_controller.rb @@ -158,7 +158,7 @@ class PostsController < ApplicationController def reply_history post = find_post_from_params - render_serialized(post.reply_history, PostSerializer) + render_serialized(post.reply_history(params[:max_replies].to_i), PostSerializer) end def destroy diff --git a/app/models/post.rb b/app/models/post.rb index 91c0de77ac2..da6b98b9b0f 100644 --- a/app/models/post.rb +++ b/app/models/post.rb @@ -476,7 +476,7 @@ class Post < ActiveRecord::Base end - def reply_history + def reply_history(max_replies=100) post_ids = Post.exec_sql("WITH RECURSIVE breadcrumb(id, reply_to_post_number) AS ( SELECT p.id, p.reply_to_post_number FROM posts AS p WHERE p.id = :post_id @@ -486,7 +486,12 @@ class Post < ActiveRecord::Base AND p.topic_id = :topic_id ) SELECT id from breadcrumb ORDER by id", post_id: id, topic_id: topic_id).to_a - post_ids.map! {|r| r['id'].to_i }.reject! {|post_id| post_id == id} + post_ids.map! {|r| r['id'].to_i } + .reject! {|post_id| post_id == id} + + # [1,2,3][-10,-1] => nil + post_ids = (post_ids[(0-max_replies)..-1] || post_ids) + Post.where(id: post_ids).includes(:user, :topic).order(:id).to_a end diff --git a/config/locales/server.en.yml b/config/locales/server.en.yml index ca3dc4625bd..7e1e67d841c 100644 --- a/config/locales/server.en.yml +++ b/config/locales/server.en.yml @@ -741,6 +741,7 @@ en: suppress_reply_directly_below: "Don't show the expandable reply count on a post when there is only a single reply directly below this post." suppress_reply_directly_above: "Don't show the expandable in-reply-to on a post when there is only a single reply directly above this post." suppress_reply_when_quoting: "Don't show the expandable in-reply-to on a post when post quotes reply." + max_reply_history: "Maximum number of replies to expand when expanding in-reply-to" experimental_reply_expansion: "Hide intermediate replies when expanding a reply to (experimental)" diff --git a/config/site_settings.yml b/config/site_settings.yml index d34d8faa20a..e76a72e1e41 100644 --- a/config/site_settings.yml +++ b/config/site_settings.yml @@ -347,6 +347,9 @@ posting: default: true suppress_reply_when_quoting: default: true + max_reply_history: + default: 1 + client: true experimental_reply_expansion: default: false client: true diff --git a/spec/models/post_spec.rb b/spec/models/post_spec.rb index 8a4c0773257..b7d8901688c 100644 --- a/spec/models/post_spec.rb +++ b/spec/models/post_spec.rb @@ -715,6 +715,7 @@ describe Post do it "returns the posts in reply to this post" do p4.reply_history.should == [p1, p2] + p4.reply_history(1).should == [p2] p3.reply_history.should be_blank p2.reply_history.should == [p1] end