Skip to content

Instantly share code, notes, and snippets.

@gammons
Created February 10, 2013 15:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gammons/4749984 to your computer and use it in GitHub Desktop.
Save gammons/4749984 to your computer and use it in GitHub Desktop.
require_dependency 'topic_view'
require_dependency 'promotion'
class TopicsController < ApplicationController
# Avatar is an image request, not XHR
before_filter :ensure_logged_in, only: [:timings,
:destroy_timings,
:update,
:star,
:destroy,
:status,
:invite,
:mute,
:unmute,
:set_notifications,
:move_posts]
before_filter :consider_user_for_promotion, only: :show
skip_before_filter :check_xhr, only: [:avatar, :show]
caches_action :avatar, :cache_path => Proc.new {|c| "#{c.params[:post_number]}-#{c.params[:topic_id]}" }
def show
@topic_view = TopicView.new(params[:id] || params[:topic_id], current_user, params)
anonymous_etag(@topic_view.topic) do
redirect_to_correct_topic and return if slugs_do_not_match
View.create_for(@topic_view.topic, request.remote_ip, current_user)
track_visit_to_topic if should_track_visit_to_topic?
topic_view_serializer = TopicViewSerializer.new(@topic_view, scope: guardian, root: false)
respond_to do |format|
format.html do
@canonical = "#{request.protocol}#{request.host_with_port}" + @topic_view.topic.relative_url
if params[:post_number]
@post = @topic_view.posts.select{|p| p.post_number == params[:post_number].to_i}.first
page = ((params[:post_number].to_i - 1) / SiteSetting.posts_per_page) + 1
@canonical << "?page=#{page}" if page > 1
else
@canonical << "?page=#{params[:page]}" if params[:page] && params[:page].to_i > 1
end
last_post = @topic_view.posts[-1]
if last_post.present? and (@topic_view.topic.highest_post_number > last_post.post_number)
@next_page = (@topic_view.posts[0].post_number / SiteSetting.posts_per_page) + 2
end
store_preloaded("topic_#{@topic_view.topic.id}", MultiJson.dump(topic_view_serializer))
end
format.json do
render_json_dump(topic_view_serializer)
end
end
end
end
private
def consider_user_for_promotion
Promotion.new(current_user).review unless current_user.present?
end
def slugs_do_not_match
params[:slug] && @topic_view.topic.slug != params[:slug]
end
def redirect_to_correct_topic
fullpath = request.fullpath
split = fullpath.split('/')
split[2] = @topic_view.topic.slug
redirect_to split.join('/'), status: 301
end
def track_visit_to_topic
TopicUser.track_visit! @topic_view.topic, current_user
@topic_view.draft = Draft.get(current_user, @topic_view.draft_key, @topic_view.draft_sequence)
end
def should_track_visit_to_topic?
(!request.xhr? || params[:track_visit]) && current_user
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment