2013-02-05 13:16:51 -06:00
|
|
|
require_dependency 'topic_view'
|
|
|
|
require_dependency 'promotion'
|
|
|
|
|
|
|
|
class TopicsController < ApplicationController
|
|
|
|
|
2013-02-07 09:45:24 -06:00
|
|
|
before_filter :ensure_logged_in, only: [:timings,
|
|
|
|
:destroy_timings,
|
|
|
|
:update,
|
|
|
|
:star,
|
|
|
|
:destroy,
|
2013-07-12 11:08:23 -05:00
|
|
|
:recover,
|
2013-02-07 09:45:24 -06:00
|
|
|
:status,
|
|
|
|
:invite,
|
|
|
|
:mute,
|
|
|
|
:unmute,
|
2013-02-05 13:16:51 -06:00
|
|
|
:set_notifications,
|
2013-03-06 14:17:07 -06:00
|
|
|
:move_posts,
|
2013-05-16 14:55:14 -05:00
|
|
|
:merge_topic,
|
2013-05-07 13:25:41 -05:00
|
|
|
:clear_pin,
|
|
|
|
:autoclose]
|
2013-03-06 14:17:07 -06:00
|
|
|
|
2013-02-10 12:50:26 -06:00
|
|
|
before_filter :consider_user_for_promotion, only: :show
|
2013-02-05 13:16:51 -06:00
|
|
|
|
2013-08-13 15:08:29 -05:00
|
|
|
skip_before_filter :check_xhr, only: [:show, :feed]
|
2013-02-05 13:16:51 -06:00
|
|
|
|
2013-02-10 12:50:26 -06:00
|
|
|
def show
|
2013-06-28 12:55:34 -05:00
|
|
|
# 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?
|
|
|
|
|
2013-06-20 16:20:08 -05:00
|
|
|
opts = params.slice(:username_filters, :filter, :page, :post_number)
|
2013-09-16 13:08:55 -05:00
|
|
|
|
2013-10-03 11:51:30 -05:00
|
|
|
opts[:username_filters] = [opts[:username_filters]] if opts[:username_filters].is_a?(String)
|
|
|
|
|
2013-06-06 13:41:27 -05:00
|
|
|
begin
|
|
|
|
@topic_view = TopicView.new(params[:id] || params[:topic_id], current_user, opts)
|
|
|
|
rescue Discourse::NotFound
|
2013-06-07 13:17:12 -05:00
|
|
|
topic = Topic.where(slug: params[:id]).first if params[:id]
|
2013-06-06 13:41:27 -05:00
|
|
|
raise Discourse::NotFound unless topic
|
|
|
|
return redirect_to(topic.relative_url)
|
|
|
|
end
|
2013-05-19 19:29:49 -05:00
|
|
|
|
2013-02-05 13:16:51 -06:00
|
|
|
anonymous_etag(@topic_view.topic) do
|
2013-03-04 18:42:44 -06:00
|
|
|
redirect_to_correct_topic && return if slugs_do_not_match
|
2013-07-04 13:08:23 -05:00
|
|
|
|
2013-07-05 06:59:39 -05:00
|
|
|
# render workaround pseudo-static HTML page for old crawlers which ignores <noscript>
|
2013-07-04 13:08:23 -05:00
|
|
|
# (see http://meta.discourse.org/t/noscript-tag-and-some-search-engines/8078)
|
2013-07-05 06:59:39 -05:00
|
|
|
return render 'topics/plain', layout: false if (SiteSetting.enable_escaped_fragments && params.has_key?('_escaped_fragment_'))
|
2013-07-04 13:08:23 -05:00
|
|
|
|
2013-02-10 12:50:26 -06:00
|
|
|
track_visit_to_topic
|
2013-10-04 02:00:23 -05:00
|
|
|
|
|
|
|
if should_track_visit_to_topic?
|
|
|
|
@topic_view.draft = Draft.get(current_user, @topic_view.draft_key, @topic_view.draft_sequence)
|
|
|
|
end
|
|
|
|
|
2013-02-10 12:50:26 -06:00
|
|
|
perform_show_response
|
2013-02-05 13:16:51 -06:00
|
|
|
end
|
2013-02-13 05:04:43 -06:00
|
|
|
|
|
|
|
canonical_url @topic_view.canonical_path
|
2013-02-05 13:16:51 -06:00
|
|
|
end
|
|
|
|
|
2013-06-28 12:55:34 -05:00
|
|
|
def wordpress
|
|
|
|
params.require(:best)
|
|
|
|
params.require(:topic_id)
|
2013-07-05 15:07:24 -05:00
|
|
|
params.permit(:min_trust_level, :min_score, :min_replies, :bypass_trust_level_score, :only_moderator_liked)
|
2013-07-01 06:29:45 -05:00
|
|
|
|
|
|
|
@topic_view = TopicView.new(
|
|
|
|
params[:topic_id],
|
|
|
|
current_user,
|
|
|
|
best: params[:best].to_i,
|
|
|
|
min_trust_level: params[:min_trust_level].nil? ? 1 : params[:min_trust_level].to_i,
|
|
|
|
min_score: params[:min_score].to_i,
|
2013-07-02 19:21:10 -05:00
|
|
|
min_replies: params[:min_replies].to_i,
|
2013-07-05 15:07:24 -05:00
|
|
|
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"
|
2013-07-01 06:29:45 -05:00
|
|
|
)
|
2013-06-28 12:55:34 -05:00
|
|
|
|
|
|
|
anonymous_etag(@topic_view.topic) do
|
|
|
|
wordpress_serializer = TopicViewWordpressSerializer.new(@topic_view, scope: guardian, root: false)
|
|
|
|
render_json_dump(wordpress_serializer)
|
|
|
|
end
|
2013-06-20 16:20:08 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
def posts
|
|
|
|
params.require(:topic_id)
|
|
|
|
params.require(:post_ids)
|
2013-06-28 12:55:34 -05:00
|
|
|
|
2013-06-20 16:20:08 -05:00
|
|
|
@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))
|
2013-06-28 12:55:34 -05:00
|
|
|
end
|
|
|
|
|
2013-02-05 13:16:51 -06: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
|
2013-02-07 09:45:24 -06:00
|
|
|
guardian.ensure_can_edit!(topic)
|
2013-02-05 13:16:51 -06:00
|
|
|
topic.title = params[:title] if params[:title].present?
|
|
|
|
|
|
|
|
# TODO: we may need smarter rules about converting archetypes
|
2013-02-07 09:45:24 -06:00
|
|
|
if current_user.admin?
|
2013-02-05 13:16:51 -06:00
|
|
|
topic.archetype = "regular" if params[:archetype] == 'regular'
|
|
|
|
end
|
|
|
|
|
2013-05-31 14:22:34 -05:00
|
|
|
success = false
|
2013-02-05 13:16:51 -06:00
|
|
|
Topic.transaction do
|
2013-05-31 14:22:34 -05:00
|
|
|
success = topic.save
|
|
|
|
topic.change_category(params[:category]) if success
|
2013-02-05 13:16:51 -06:00
|
|
|
end
|
|
|
|
|
2013-04-10 04:00:50 -05:00
|
|
|
# this is used to return the title to the client as it may have been
|
|
|
|
# changed by "TextCleaner"
|
2013-05-31 14:22:34 -05:00
|
|
|
if success
|
|
|
|
render_serialized(topic, BasicTopicSerializer)
|
|
|
|
else
|
|
|
|
render_json_error(topic)
|
|
|
|
end
|
2013-02-05 13:16:51 -06:00
|
|
|
end
|
|
|
|
|
2013-03-14 13:45:29 -05:00
|
|
|
def similar_to
|
2013-06-06 02:14:32 -05:00
|
|
|
params.require(:title)
|
|
|
|
params.require(:raw)
|
2013-03-14 13:45:29 -05:00
|
|
|
title, raw = params[:title], params[:raw]
|
|
|
|
|
|
|
|
raise Discourse::InvalidParameters.new(:title) if title.length < SiteSetting.min_title_similar_length
|
|
|
|
raise Discourse::InvalidParameters.new(:raw) if raw.length < SiteSetting.min_body_similar_length
|
|
|
|
|
2013-06-19 12:13:12 -05:00
|
|
|
# Only suggest similar topics if the site has a minimmum amount of topics present.
|
|
|
|
if Topic.count > SiteSetting.minimum_topics_similar
|
2013-07-23 09:01:40 -05:00
|
|
|
topics = Topic.similar_to(title, raw, current_user).to_a
|
2013-06-19 12:13:12 -05:00
|
|
|
end
|
|
|
|
|
2013-03-14 13:45:29 -05:00
|
|
|
render_serialized(topics, BasicTopicSerializer)
|
|
|
|
end
|
|
|
|
|
2013-02-05 13:16:51 -06:00
|
|
|
def status
|
2013-06-06 02:14:32 -05:00
|
|
|
params.require(:status)
|
|
|
|
params.require(:enabled)
|
2013-02-05 13:16:51 -06:00
|
|
|
|
|
|
|
raise Discourse::InvalidParameters.new(:status) unless %w(visible closed pinned archived).include?(params[:status])
|
|
|
|
@topic = Topic.where(id: params[:topic_id].to_i).first
|
|
|
|
guardian.ensure_can_moderate!(@topic)
|
|
|
|
@topic.update_status(params[:status], (params[:enabled] == 'true'), current_user)
|
|
|
|
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 09:45:24 -06:00
|
|
|
def mute
|
2013-06-14 00:38:59 -05:00
|
|
|
toggle_mute
|
2013-02-05 13:16:51 -06:00
|
|
|
end
|
|
|
|
|
|
|
|
def unmute
|
2013-06-14 00:38:59 -05:00
|
|
|
toggle_mute
|
2013-02-05 13:16:51 -06:00
|
|
|
end
|
|
|
|
|
2013-05-07 13:25:41 -05:00
|
|
|
def autoclose
|
2013-06-06 02:14:32 -05:00
|
|
|
raise Discourse::InvalidParameters.new(:auto_close_days) unless params.has_key?(:auto_close_days)
|
2013-05-07 13:25:41 -05:00
|
|
|
@topic = Topic.where(id: params[:topic_id].to_i).first
|
|
|
|
guardian.ensure_can_moderate!(@topic)
|
2013-06-06 16:04:10 -05:00
|
|
|
@topic.set_auto_close(params[:auto_close_days], current_user)
|
2013-05-07 13:25:41 -05:00
|
|
|
@topic.save
|
|
|
|
render nothing: true
|
|
|
|
end
|
|
|
|
|
2013-02-05 13:16:51 -06:00
|
|
|
def destroy
|
|
|
|
topic = Topic.where(id: params[:id]).first
|
|
|
|
guardian.ensure_can_delete!(topic)
|
2013-07-09 14:20:18 -05:00
|
|
|
topic.trash!(current_user)
|
2013-02-05 13:16:51 -06:00
|
|
|
render nothing: true
|
|
|
|
end
|
2013-02-07 09:45:24 -06:00
|
|
|
|
2013-07-12 11:08:23 -05: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-05 13:16:51 -06:00
|
|
|
def excerpt
|
2013-02-07 09:45:24 -06:00
|
|
|
render nothing: true
|
2013-02-05 13:16:51 -06:00
|
|
|
end
|
|
|
|
|
2013-06-18 02:17:01 -05:00
|
|
|
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-05 13:16:51 -06:00
|
|
|
def invite
|
2013-07-10 20:21:39 -05:00
|
|
|
username_or_email = params[:user]
|
|
|
|
if username_or_email
|
|
|
|
# provides a level of protection for hashes
|
|
|
|
params.require(:user)
|
|
|
|
else
|
|
|
|
params.require(:email)
|
|
|
|
username_or_email = params[:email]
|
|
|
|
end
|
|
|
|
|
2013-02-05 13:16:51 -06:00
|
|
|
topic = Topic.where(id: params[:topic_id]).first
|
|
|
|
guardian.ensure_can_invite_to!(topic)
|
|
|
|
|
2013-07-10 20:21:39 -05:00
|
|
|
if topic.invite(current_user, username_or_email)
|
|
|
|
user = User.find_by_username_or_email(username_or_email)
|
2013-06-18 02:17:01 -05:00
|
|
|
if user
|
|
|
|
render_json_dump BasicUserSerializer.new(user, scope: guardian, root: 'user')
|
|
|
|
else
|
|
|
|
render json: success_json
|
|
|
|
end
|
2013-02-05 13:16:51 -06: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
|
|
|
|
|
2013-05-16 14:55:14 -05:00
|
|
|
def merge_topic
|
2013-06-06 02:14:32 -05:00
|
|
|
params.require(:destination_topic_id)
|
2013-05-16 14:55:14 -05:00
|
|
|
|
|
|
|
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)
|
2013-06-18 00:52:09 -05:00
|
|
|
render_topic_changes(dest_topic)
|
2013-05-16 14:55:14 -05:00
|
|
|
end
|
|
|
|
|
2013-02-05 13:16:51 -06:00
|
|
|
def move_posts
|
2013-06-06 02:14:32 -05:00
|
|
|
params.require(:post_ids)
|
2013-05-08 12:33:58 -05:00
|
|
|
|
2013-02-05 13:16:51 -06:00
|
|
|
topic = Topic.where(id: params[:topic_id]).first
|
|
|
|
guardian.ensure_can_move_posts!(topic)
|
|
|
|
|
2013-09-04 10:53:00 -05:00
|
|
|
dest_topic = move_posts_to_destination(topic)
|
2013-06-18 00:52:09 -05:00
|
|
|
render_topic_changes(dest_topic)
|
2013-02-05 13:16:51 -06:00
|
|
|
end
|
|
|
|
|
2013-03-06 14:17:07 -06:00
|
|
|
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
|
2013-02-25 17:38:11 -06:00
|
|
|
|
2013-03-06 14:17:07 -06:00
|
|
|
def timings
|
2013-02-25 01:42:42 -06:00
|
|
|
PostTiming.process_timings(
|
2013-03-06 14:17:07 -06:00
|
|
|
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-25 01:42:42 -06:00
|
|
|
)
|
2013-02-05 13:16:51 -06:00
|
|
|
render nothing: true
|
|
|
|
end
|
|
|
|
|
2013-02-21 12:20:00 -06:00
|
|
|
def feed
|
|
|
|
@topic_view = TopicView.new(params[:topic_id])
|
2013-03-01 15:56:14 -06:00
|
|
|
anonymous_etag(@topic_view.topic) do
|
|
|
|
render 'topics/show', formats: [:rss]
|
|
|
|
end
|
2013-02-21 12:20:00 -06:00
|
|
|
end
|
|
|
|
|
2013-02-05 13:16:51 -06:00
|
|
|
private
|
|
|
|
|
2013-06-14 00:38:59 -05:00
|
|
|
def toggle_mute
|
2013-02-10 12:50:26 -06:00
|
|
|
@topic = Topic.where(id: params[:topic_id].to_i).first
|
|
|
|
guardian.ensure_can_see!(@topic)
|
|
|
|
|
2013-05-24 01:06:38 -05:00
|
|
|
@topic.toggle_mute(current_user)
|
2013-02-10 12:50:26 -06:00
|
|
|
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-05 13:16:51 -06:00
|
|
|
|
2013-02-10 12:50:26 -06:00
|
|
|
split = fullpath.split('/')
|
|
|
|
split[2] = @topic_view.topic.slug
|
|
|
|
|
|
|
|
redirect_to split.join('/'), status: 301
|
|
|
|
end
|
|
|
|
|
|
|
|
def track_visit_to_topic
|
2013-10-04 02:00:23 -05:00
|
|
|
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?
|
|
|
|
)
|
|
|
|
|
2013-02-10 12:50:26 -06:00
|
|
|
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)
|
2013-06-28 12:55:34 -05:00
|
|
|
|
2013-02-10 12:50:26 -06:00
|
|
|
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
|
2013-06-18 00:52:09 -05:00
|
|
|
|
|
|
|
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 10:53:00 -05:00
|
|
|
def move_posts_to_destination(topic)
|
2013-06-18 00:52:09 -05:00
|
|
|
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?
|
|
|
|
|
2013-09-04 10:53:00 -05:00
|
|
|
topic.move_posts(current_user, post_ids_including_replies, args)
|
2013-06-18 00:52:09 -05:00
|
|
|
end
|
|
|
|
|
2013-02-05 13:16:51 -06:00
|
|
|
end
|