discourse/app/controllers/topics_controller.rb

365 lines
11 KiB
Ruby
Raw Normal View History

2013-02-06 03:16:51 +08:00
require_dependency 'topic_view'
require_dependency 'promotion'
require_dependency 'url_helper'
2013-02-06 03:16:51 +08:00
class TopicsController < ApplicationController
include UrlHelper
2013-02-06 03:16:51 +08:00
2013-02-07 23:45:24 +08:00
before_filter :ensure_logged_in, only: [:timings,
:destroy_timings,
:update,
:star,
:destroy,
:recover,
2013-02-07 23:45:24 +08:00
:status,
:invite,
:mute,
:unmute,
2013-02-06 03:16:51 +08:00
:set_notifications,
:move_posts,
:merge_topic,
:clear_pin,
:autoclose]
before_filter :consider_user_for_promotion, only: :show
2013-02-06 03:16:51 +08:00
2013-08-14 04:08:29 +08:00
skip_before_filter :check_xhr, only: [:show, :feed]
2013-02-06 03:16:51 +08:00
def show
# We'd like to migrate the wordpress feed to another url. This keeps up backwards compatibility with
# existing installs.
return wordpress if params[:best].present?
opts = params.slice(:username_filters, :filter, :page, :post_number)
username_filters = opts[:username_filters]
2013-09-17 02:08:55 +08:00
opts[:username_filters] = [username_filters] if username_filters.is_a?(String)
begin
@topic_view = TopicView.new(params[:id] || params[:topic_id], current_user, opts)
rescue Discourse::NotFound
topic = Topic.where(slug: params[:id]).first if params[:id]
raise Discourse::NotFound unless topic
return redirect_to(topic.relative_url)
end
discourse_expires_in 1.minute
redirect_to_correct_topic && return if slugs_do_not_match
# render workaround pseudo-static HTML page for old crawlers which ignores <noscript>
# (see http://meta.discourse.org/t/noscript-tag-and-some-search-engines/8078)
return render 'topics/plain', layout: false if (SiteSetting.enable_escaped_fragments && params.key?('_escaped_fragment_'))
track_visit_to_topic
if should_track_visit_to_topic?
@topic_view.draft = Draft.get(current_user, @topic_view.draft_key, @topic_view.draft_sequence)
2013-02-06 03:16:51 +08:00
end
perform_show_response
canonical_url absolute_without_cdn(@topic_view.canonical_path)
2013-02-06 03:16:51 +08:00
end
def wordpress
params.require(:best)
params.require(:topic_id)
params.permit(:min_trust_level, :min_score, :min_replies, :bypass_trust_level_score, :only_moderator_liked)
opts = { best: params[:best].to_i,
min_trust_level: params[:min_trust_level] ? 1 : params[:min_trust_level].to_i,
min_score: params[:min_score].to_i,
min_replies: params[:min_replies].to_i,
bypass_trust_level_score: params[:bypass_trust_level_score].to_i, # safe cause 0 means ignore
only_moderator_liked: params[:only_moderator_liked].to_s == "true"
}
@topic_view = TopicView.new(params[:topic_id], current_user, opts)
discourse_expires_in 1.minute
wordpress_serializer = TopicViewWordpressSerializer.new(@topic_view, scope: guardian, root: false)
render_json_dump(wordpress_serializer)
end
def posts
params.require(:topic_id)
params.require(:post_ids)
@topic_view = TopicView.new(params[:topic_id], current_user, post_ids: params[:post_ids])
render_json_dump(TopicViewPostsSerializer.new(@topic_view, scope: guardian, root: false))
end
2013-02-06 03:16:51 +08:00
def destroy_timings
PostTiming.destroy_for(current_user.id, params[:topic_id].to_i)
render nothing: true
end
def update
topic = Topic.where(id: params[:topic_id]).first
title, archetype = params[:title], params[:archetype]
2013-02-07 23:45:24 +08:00
guardian.ensure_can_edit!(topic)
2013-02-06 03:16:51 +08:00
topic.title = params[:title] if title.present?
2013-02-06 03:16:51 +08:00
# TODO: we may need smarter rules about converting archetypes
topic.archetype = "regular" if current_user.admin? && archetype == 'regular'
2013-02-06 03:16:51 +08:00
2013-12-12 10:41:34 +08:00
topic.acting_user = current_user
success = false
2013-02-06 03:16:51 +08:00
Topic.transaction do
2013-12-12 10:41:34 +08:00
success = topic.save && topic.change_category(params[:category])
2013-02-06 03:16:51 +08:00
end
2013-12-12 10:41:34 +08:00
# this is used to return the title to the client as it may have been changed by "TextCleaner"
success ? render_serialized(topic, BasicTopicSerializer) : render_json_error(topic)
2013-02-06 03:16:51 +08:00
end
def similar_to
params.require(:title)
params.require(:raw)
title, raw = params[:title], params[:raw]
[:title, :raw].each { |key| check_length_of(key, params[key]) }
2013-12-21 15:19:22 +08:00
# Only suggest similar topics if the site has a minimum amount of topics present.
topics = Topic.similar_to(title, raw, current_user).to_a if Topic.count_exceeds_minimum?
render_serialized(topics, BasicTopicSerializer)
end
2013-02-06 03:16:51 +08:00
def status
params.require(:status)
params.require(:enabled)
status, topic_id = params[:status], params[:topic_id].to_i
enabled = (params[:enabled] == 'true')
2013-02-06 03:16:51 +08:00
check_for_status_presence(:status, status)
@topic = Topic.where(id: topic_id).first
2013-02-06 03:16:51 +08:00
guardian.ensure_can_moderate!(@topic)
@topic.update_status(status, enabled, current_user)
2013-02-06 03:16:51 +08:00
render nothing: true
end
def star
@topic = Topic.where(id: params[:topic_id].to_i).first
guardian.ensure_can_see!(@topic)
@topic.toggle_star(current_user, params[:starred] == 'true')
render nothing: true
end
2013-02-07 23:45:24 +08:00
def mute
2013-06-14 13:38:59 +08:00
toggle_mute
2013-02-06 03:16:51 +08:00
end
def unmute
2013-06-14 13:38:59 +08:00
toggle_mute
2013-02-06 03:16:51 +08:00
end
def autoclose
raise Discourse::InvalidParameters.new(:auto_close_time) unless params.has_key?(:auto_close_time)
topic = Topic.where(id: params[:topic_id].to_i).first
guardian.ensure_can_moderate!(topic)
topic.set_auto_close(params[:auto_close_time], current_user)
if topic.save
render json: success_json.merge!(auto_close_at: topic.auto_close_at)
else
render_json_error(topic)
end
end
2013-02-06 03:16:51 +08:00
def destroy
topic = Topic.where(id: params[:id]).first
guardian.ensure_can_delete!(topic)
2013-07-10 03:20:18 +08:00
topic.trash!(current_user)
2013-02-06 03:16:51 +08:00
render nothing: true
end
2013-02-07 23:45:24 +08:00
def recover
topic = Topic.where(id: params[:topic_id]).with_deleted.first
guardian.ensure_can_recover_topic!(topic)
topic.recover!
render nothing: true
end
2013-02-06 03:16:51 +08:00
def excerpt
2013-02-07 23:45:24 +08:00
render nothing: true
2013-02-06 03:16:51 +08:00
end
def remove_allowed_user
params.require(:username)
topic = Topic.where(id: params[:topic_id]).first
guardian.ensure_can_remove_allowed_users!(topic)
if topic.remove_allowed_user(params[:username])
render json: success_json
else
render json: failed_json, status: 422
end
end
2013-02-06 03:16:51 +08:00
def invite
username_or_email = params[:user] ? fetch_username : fetch_email
2013-02-06 03:16:51 +08:00
topic = Topic.where(id: params[:topic_id]).first
guardian.ensure_can_invite_to!(topic)
if topic.invite(current_user, username_or_email)
user = User.find_by_username_or_email(username_or_email)
if user
render_json_dump BasicUserSerializer.new(user, scope: guardian, root: 'user')
else
render json: success_json
end
2013-02-06 03:16:51 +08:00
else
render json: failed_json, status: 422
end
end
def set_notifications
topic = Topic.find(params[:topic_id].to_i)
TopicUser.change(current_user, topic.id, notification_level: params[:notification_level].to_i)
render json: success_json
end
def merge_topic
params.require(:destination_topic_id)
topic = Topic.where(id: params[:topic_id]).first
guardian.ensure_can_move_posts!(topic)
dest_topic = topic.move_posts(current_user, topic.posts.pluck(:id), destination_topic_id: params[:destination_topic_id].to_i)
render_topic_changes(dest_topic)
end
2013-02-06 03:16:51 +08:00
def move_posts
params.require(:post_ids)
params.require(:topic_id)
params.permit(:category_id)
2013-02-06 03:16:51 +08:00
topic = Topic.where(id: params[:topic_id]).first
guardian.ensure_can_move_posts!(topic)
2013-09-04 23:53:00 +08:00
dest_topic = move_posts_to_destination(topic)
render_topic_changes(dest_topic)
2013-02-06 03:16:51 +08:00
end
def clear_pin
topic = Topic.where(id: params[:topic_id].to_i).first
guardian.ensure_can_see!(topic)
topic.clear_pin_for(current_user)
render nothing: true
end
def timings
PostTiming.process_timings(
current_user,
params[:topic_id].to_i,
params[:topic_time].to_i,
(params[:timings] || []).map{|post_number, t| [post_number.to_i, t.to_i]}
)
2013-02-06 03:16:51 +08:00
render nothing: true
end
def feed
@topic_view = TopicView.new(params[:topic_id])
discourse_expires_in 1.minute
render 'topics/show', formats: [:rss]
end
2013-02-06 03:16:51 +08:00
private
2013-06-14 13:38:59 +08:00
def toggle_mute
@topic = Topic.where(id: params[:topic_id].to_i).first
guardian.ensure_can_see!(@topic)
2013-05-24 14:06:38 +08:00
@topic.toggle_mute(current_user)
render nothing: true
end
def consider_user_for_promotion
Promotion.new(current_user).review if 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
2013-02-06 03:16:51 +08:00
split = fullpath.split('/')
split[2] = @topic_view.topic.slug
redirect_to split.join('/'), status: 301
end
def track_visit_to_topic
Jobs.enqueue(:view_tracker,
topic_id: @topic_view.topic.id,
ip: request.remote_ip,
user_id: (current_user.id if current_user),
track_visit: should_track_visit_to_topic?
)
end
def should_track_visit_to_topic?
!!((!request.xhr? || params[:track_visit]) && current_user)
end
def perform_show_response
topic_view_serializer = TopicViewSerializer.new(@topic_view, scope: guardian, root: false)
respond_to do |format|
format.html do
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
def render_topic_changes(dest_topic)
if dest_topic.present?
render json: {success: true, url: dest_topic.relative_url}
else
render json: {success: false}
end
end
2013-09-04 23:53:00 +08:00
def move_posts_to_destination(topic)
args = {}
args[:title] = params[:title] if params[:title].present?
args[:destination_topic_id] = params[:destination_topic_id].to_i if params[:destination_topic_id].present?
args[:category_id] = params[:category_id].to_i if params[:category_id].present?
2013-09-04 23:53:00 +08:00
topic.move_posts(current_user, post_ids_including_replies, args)
end
def check_length_of(key, attr)
str = (key == :raw) ? "body" : key.to_s
invalid_param(key) if attr.length < SiteSetting.send("min_#{str}_similar_length")
end
def check_for_status_presence(key, attr)
invalid_param(key) unless %w(visible closed pinned archived).include?(attr)
end
def invalid_param(key)
raise Discourse::InvalidParameters.new(key.to_sym)
end
def fetch_username
params.require(:user)
params[:user]
end
def fetch_email
params.require(:email)
params[:email]
end
2013-02-06 03:16:51 +08:00
end