Add support for the '/p/:post_id' route on the client-side

This commit is contained in:
Régis Hanol 2018-03-24 02:44:39 +01:00
parent 3a58dc0858
commit c5c1d8e180
4 changed files with 34 additions and 16 deletions

View File

@ -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: '/' });

View File

@ -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);
});
}
});

View File

@ -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

View File

@ -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