Add support for the '/p/:post_id' route on the client-side
This commit is contained in:
parent
3a58dc0858
commit
c5c1d8e180
|
@ -5,6 +5,8 @@ export default function() {
|
||||||
|
|
||||||
this.route('about', { path: '/about', resetNamespace: true });
|
this.route('about', { path: '/about', resetNamespace: true });
|
||||||
|
|
||||||
|
this.route('post', { path: '/p/:id' });
|
||||||
|
|
||||||
// Topic routes
|
// Topic routes
|
||||||
this.route('topic', { path: '/t/:slug/:id', resetNamespace: true }, function() {
|
this.route('topic', { path: '/t/:slug/:id', resetNamespace: true }, function() {
|
||||||
this.route('fromParams', { path: '/' });
|
this.route('fromParams', { path: '/' });
|
||||||
|
|
|
@ -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);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
});
|
|
@ -50,6 +50,7 @@ class TopicViewSerializer < ApplicationSerializer
|
||||||
:unpinned,
|
:unpinned,
|
||||||
:pinned,
|
:pinned,
|
||||||
:details,
|
:details,
|
||||||
|
:current_post_number,
|
||||||
:highest_post_number,
|
:highest_post_number,
|
||||||
:last_read_post_number,
|
:last_read_post_number,
|
||||||
:last_read_post_id,
|
:last_read_post_id,
|
||||||
|
@ -172,6 +173,14 @@ class TopicViewSerializer < ApplicationSerializer
|
||||||
object.topic_user.present?
|
object.topic_user.present?
|
||||||
end
|
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
|
def highest_post_number
|
||||||
object.highest_post_number
|
object.highest_post_number
|
||||||
end
|
end
|
||||||
|
|
|
@ -6,7 +6,7 @@ require_dependency 'gaps'
|
||||||
class TopicView
|
class TopicView
|
||||||
|
|
||||||
attr_reader :topic, :posts, :guardian, :filtered_posts, :chunk_size, :print, :message_bus_last_id
|
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
|
def self.slow_chunk_size
|
||||||
10
|
10
|
||||||
|
@ -43,13 +43,16 @@ class TopicView
|
||||||
@guardian = Guardian.new(@user)
|
@guardian = Guardian.new(@user)
|
||||||
@topic = find_topic(topic_id)
|
@topic = find_topic(topic_id)
|
||||||
@print = options[:print].present?
|
@print = options[:print].present?
|
||||||
|
|
||||||
check_and_raise_exceptions
|
check_and_raise_exceptions
|
||||||
|
|
||||||
options.each do |key, value|
|
options.each do |key, value|
|
||||||
self.instance_variable_set("@#{key}".to_sym, value)
|
self.instance_variable_set("@#{key}".to_sym, value)
|
||||||
end
|
end
|
||||||
|
|
||||||
@page = 1 if (!@page || @page.zero?)
|
@post_number = [@post_number.to_i, 1].max
|
||||||
|
@page = [@page.to_i, 1].max
|
||||||
|
|
||||||
@chunk_size =
|
@chunk_size =
|
||||||
case
|
case
|
||||||
when options[:slow_platform] then TopicView.slow_chunk_size
|
when options[:slow_platform] then TopicView.slow_chunk_size
|
||||||
|
@ -82,13 +85,12 @@ class TopicView
|
||||||
|
|
||||||
def canonical_path
|
def canonical_path
|
||||||
path = relative_url
|
path = relative_url
|
||||||
|
|
||||||
path <<
|
path <<
|
||||||
if @post_number
|
if @page > 1
|
||||||
page = ((@post_number.to_i - 1) / @limit) + 1
|
"?page=#{@page}"
|
||||||
(page > 1) ? "?page=#{page}" : ""
|
|
||||||
else
|
else
|
||||||
(@page && @page.to_i > 1) ? "?page=#{@page}" : ""
|
page = ((@post_number - 1) / @limit) + 1
|
||||||
|
page > 1 ? "?page=#{page}" : ""
|
||||||
end
|
end
|
||||||
|
|
||||||
path
|
path
|
||||||
|
@ -109,11 +111,7 @@ class TopicView
|
||||||
end
|
end
|
||||||
|
|
||||||
def prev_page
|
def prev_page
|
||||||
if @page && @page > 1 && posts.length > 0
|
@page > 1 && posts.size > 0 ? @page - 1 : nil
|
||||||
@page - 1
|
|
||||||
else
|
|
||||||
nil
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
|
|
||||||
def next_page
|
def next_page
|
||||||
|
@ -164,7 +162,7 @@ class TopicView
|
||||||
return @desired_post if @desired_post.present?
|
return @desired_post if @desired_post.present?
|
||||||
return nil if posts.blank?
|
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 ||= posts.first
|
||||||
@desired_post
|
@desired_post
|
||||||
end
|
end
|
||||||
|
@ -177,17 +175,17 @@ class TopicView
|
||||||
end
|
end
|
||||||
|
|
||||||
def read_time
|
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
|
(@topic.word_count / SiteSetting.read_time_word_count).floor if @topic.word_count
|
||||||
end
|
end
|
||||||
|
|
||||||
def like_count
|
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
|
@topic.like_count
|
||||||
end
|
end
|
||||||
|
|
||||||
def image_url
|
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?
|
if @desired_post.image_url.present?
|
||||||
@desired_post.image_url
|
@desired_post.image_url
|
||||||
elsif @desired_post.user
|
elsif @desired_post.user
|
||||||
|
|
Loading…
Reference in New Issue