From c5c1d8e1805f92743403e243644f1a8298f6877d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=A9gis=20Hanol?= Date: Sat, 24 Mar 2018 02:44:39 +0100 Subject: [PATCH] Add support for the '/p/:post_id' route on the client-side --- .../discourse/routes/app-route-map.js.es6 | 2 ++ .../javascripts/discourse/routes/post.js.es6 | 9 ++++++ app/serializers/topic_view_serializer.rb | 9 ++++++ lib/topic_view.rb | 30 +++++++++---------- 4 files changed, 34 insertions(+), 16 deletions(-) create mode 100644 app/assets/javascripts/discourse/routes/post.js.es6 diff --git a/app/assets/javascripts/discourse/routes/app-route-map.js.es6 b/app/assets/javascripts/discourse/routes/app-route-map.js.es6 index 740952f0f94..4ed43977d72 100644 --- a/app/assets/javascripts/discourse/routes/app-route-map.js.es6 +++ b/app/assets/javascripts/discourse/routes/app-route-map.js.es6 @@ -5,6 +5,8 @@ export default function() { this.route('about', { path: '/about', resetNamespace: true }); + this.route('post', { path: '/p/:id' }); + // Topic routes this.route('topic', { path: '/t/:slug/:id', resetNamespace: true }, function() { this.route('fromParams', { path: '/' }); diff --git a/app/assets/javascripts/discourse/routes/post.js.es6 b/app/assets/javascripts/discourse/routes/post.js.es6 new file mode 100644 index 00000000000..f8f06e46c43 --- /dev/null +++ b/app/assets/javascripts/discourse/routes/post.js.es6 @@ -0,0 +1,9 @@ +import { ajax } from "discourse/lib/ajax"; + +export default Discourse.Route.extend({ + beforeModel({ params }) { + return ajax(`/p/${params.post.id}`).then(t => { + this.transitionTo("topic.fromParamsNear", t.slug, t.id, t.current_post_number); + }); + } +}); diff --git a/app/serializers/topic_view_serializer.rb b/app/serializers/topic_view_serializer.rb index 7253032ab55..7ca94fd87a6 100644 --- a/app/serializers/topic_view_serializer.rb +++ b/app/serializers/topic_view_serializer.rb @@ -50,6 +50,7 @@ class TopicViewSerializer < ApplicationSerializer :unpinned, :pinned, :details, + :current_post_number, :highest_post_number, :last_read_post_number, :last_read_post_id, @@ -172,6 +173,14 @@ class TopicViewSerializer < ApplicationSerializer object.topic_user.present? end + def current_post_number + [object.post_number, object.highest_post_number].min + end + + def include_current_post_number? + object.highest_post_number.present? + end + def highest_post_number object.highest_post_number end diff --git a/lib/topic_view.rb b/lib/topic_view.rb index 8217aaa9304..3c3dbd4e84b 100644 --- a/lib/topic_view.rb +++ b/lib/topic_view.rb @@ -6,7 +6,7 @@ require_dependency 'gaps' class TopicView attr_reader :topic, :posts, :guardian, :filtered_posts, :chunk_size, :print, :message_bus_last_id - attr_accessor :draft, :draft_key, :draft_sequence, :user_custom_fields, :post_custom_fields + attr_accessor :draft, :draft_key, :draft_sequence, :user_custom_fields, :post_custom_fields, :post_number def self.slow_chunk_size 10 @@ -43,13 +43,16 @@ class TopicView @guardian = Guardian.new(@user) @topic = find_topic(topic_id) @print = options[:print].present? + check_and_raise_exceptions options.each do |key, value| self.instance_variable_set("@#{key}".to_sym, value) end - @page = 1 if (!@page || @page.zero?) + @post_number = [@post_number.to_i, 1].max + @page = [@page.to_i, 1].max + @chunk_size = case when options[:slow_platform] then TopicView.slow_chunk_size @@ -82,13 +85,12 @@ class TopicView def canonical_path path = relative_url - path << - if @post_number - page = ((@post_number.to_i - 1) / @limit) + 1 - (page > 1) ? "?page=#{page}" : "" + if @page > 1 + "?page=#{@page}" else - (@page && @page.to_i > 1) ? "?page=#{@page}" : "" + page = ((@post_number - 1) / @limit) + 1 + page > 1 ? "?page=#{page}" : "" end path @@ -109,11 +111,7 @@ class TopicView end def prev_page - if @page && @page > 1 && posts.length > 0 - @page - 1 - else - nil - end + @page > 1 && posts.size > 0 ? @page - 1 : nil end def next_page @@ -164,7 +162,7 @@ class TopicView return @desired_post if @desired_post.present? return nil if posts.blank? - @desired_post = posts.detect { |p| p.post_number == @post_number.to_i } + @desired_post = posts.detect { |p| p.post_number == @post_number } @desired_post ||= posts.first @desired_post end @@ -177,17 +175,17 @@ class TopicView end def read_time - return nil if @post_number.present? && @post_number.to_i != 1 # only show for topic URLs + return nil if @post_number > 1 # only show for topic URLs (@topic.word_count / SiteSetting.read_time_word_count).floor if @topic.word_count end def like_count - return nil if @post_number.present? && @post_number.to_i != 1 # only show for topic URLs + return nil if @post_number > 1 # only show for topic URLs @topic.like_count end def image_url - if @post_number.present? && @post_number.to_i != 1 && @desired_post.present? + if @post_number > 1 && @desired_post.present? if @desired_post.image_url.present? @desired_post.image_url elsif @desired_post.user