2013-02-06 03:16:51 +08:00
|
|
|
require_dependency 'post_creator'
|
2013-03-19 05:52:29 +08:00
|
|
|
require_dependency 'post_destroyer'
|
2013-02-06 03:16:51 +08:00
|
|
|
|
|
|
|
class PostsController < ApplicationController
|
|
|
|
|
|
|
|
# Need to be logged in for all actions here
|
2013-04-24 16:05:35 +08:00
|
|
|
before_filter :ensure_logged_in, except: [:show, :replies, :by_number, :short_link]
|
2013-02-06 03:16:51 +08:00
|
|
|
|
2013-04-24 16:05:35 +08:00
|
|
|
skip_before_filter :store_incoming_links, only: [:short_link]
|
2013-04-30 14:29:57 +08:00
|
|
|
skip_before_filter :check_xhr, only: [:markdown,:short_link]
|
|
|
|
|
|
|
|
def markdown
|
|
|
|
post = Post.where(topic_id: params[:topic_id].to_i, post_number: (params[:post_number] || 1).to_i).first
|
|
|
|
if post && guardian.can_see?(post)
|
|
|
|
render text: post.raw, content_type: 'text/plain'
|
|
|
|
else
|
|
|
|
raise Discourse::NotFound
|
|
|
|
end
|
|
|
|
end
|
2013-04-24 16:05:35 +08:00
|
|
|
|
|
|
|
def short_link
|
|
|
|
post = Post.find(params[:post_id].to_i)
|
2013-04-26 14:18:41 +08:00
|
|
|
IncomingLink.add(request,current_user)
|
2013-04-24 16:05:35 +08:00
|
|
|
redirect_to post.url
|
|
|
|
end
|
2013-02-06 03:16:51 +08:00
|
|
|
|
|
|
|
def create
|
|
|
|
requires_parameter(:post)
|
|
|
|
|
|
|
|
post_creator = PostCreator.new(current_user,
|
|
|
|
raw: params[:post][:raw],
|
|
|
|
topic_id: params[:post][:topic_id],
|
|
|
|
title: params[:title],
|
|
|
|
archetype: params[:archetype],
|
|
|
|
category: params[:post][:category],
|
|
|
|
target_usernames: params[:target_usernames],
|
|
|
|
reply_to_post_number: params[:post][:reply_to_post_number],
|
|
|
|
image_sizes: params[:image_sizes],
|
|
|
|
meta_data: params[:meta_data])
|
|
|
|
post = post_creator.create
|
|
|
|
|
|
|
|
if post_creator.errors.present?
|
|
|
|
render_json_error(post_creator)
|
|
|
|
else
|
|
|
|
post_serializer = PostSerializer.new(post, scope: guardian, root: false)
|
|
|
|
post_serializer.topic_slug = post.topic.slug if post.topic.present?
|
|
|
|
post_serializer.draft_sequence = DraftSequence.current(current_user, post.topic.draft_key)
|
|
|
|
render_json_dump(post_serializer)
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
def update
|
|
|
|
requires_parameter(:post)
|
2013-02-07 23:45:24 +08:00
|
|
|
|
2013-02-22 07:09:56 +08:00
|
|
|
post = Post.where(id: params[:id]).first
|
|
|
|
post.image_sizes = params[:image_sizes] if params[:image_sizes].present?
|
|
|
|
guardian.ensure_can_edit!(post)
|
|
|
|
|
2013-04-10 17:00:50 +08:00
|
|
|
# to stay consistent with the create api,
|
2013-03-27 14:49:23 +08:00
|
|
|
# we should allow for title changes and category changes here
|
|
|
|
# we should also move all of this to a post updater.
|
2013-04-10 17:00:50 +08:00
|
|
|
if post.post_number == 1 && (params[:title] || params[:post][:category])
|
2013-03-27 14:49:23 +08:00
|
|
|
post.topic.title = params[:title] if params[:title]
|
2013-04-10 17:00:50 +08:00
|
|
|
Topic.transaction do
|
2013-03-27 14:49:23 +08:00
|
|
|
post.topic.change_category(params[:post][:category])
|
|
|
|
post.topic.save
|
|
|
|
end
|
|
|
|
|
|
|
|
if post.topic.errors.present?
|
|
|
|
render_json_error(post.topic)
|
|
|
|
return
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-02-22 07:09:56 +08:00
|
|
|
revisor = PostRevisor.new(post)
|
|
|
|
if revisor.revise!(current_user, params[:post][:raw])
|
|
|
|
TopicLink.extract_from(post)
|
2013-02-06 03:16:51 +08:00
|
|
|
end
|
|
|
|
|
2013-03-27 14:49:23 +08:00
|
|
|
|
2013-02-22 07:09:56 +08:00
|
|
|
if post.errors.present?
|
|
|
|
render_json_error(post)
|
2013-02-06 03:16:51 +08:00
|
|
|
return
|
|
|
|
end
|
|
|
|
|
2013-02-22 07:09:56 +08:00
|
|
|
post_serializer = PostSerializer.new(post, scope: guardian, root: false)
|
|
|
|
post_serializer.draft_sequence = DraftSequence.current(current_user, post.topic.draft_key)
|
|
|
|
link_counts = TopicLinkClick.counts_for(post.topic, [post])
|
|
|
|
post_serializer.single_post_link_counts = link_counts[post.id] if link_counts.present?
|
2013-03-28 13:53:11 +08:00
|
|
|
post_serializer.topic_slug = post.topic.slug if post.topic.present?
|
2013-02-22 07:09:56 +08:00
|
|
|
|
|
|
|
result = {post: post_serializer.as_json}
|
|
|
|
if revisor.category_changed.present?
|
2013-02-26 00:42:20 +08:00
|
|
|
result[:category] = CategorySerializer.new(revisor.category_changed, scope: guardian, root: false).as_json
|
2013-02-22 07:09:56 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
render_json_dump(result)
|
2013-02-06 03:16:51 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
def by_number
|
|
|
|
@post = Post.where(topic_id: params[:topic_id], post_number: params[:post_number]).first
|
2013-02-07 23:45:24 +08:00
|
|
|
guardian.ensure_can_see!(@post)
|
2013-02-06 03:16:51 +08:00
|
|
|
@post.revert_to(params[:version].to_i) if params[:version].present?
|
|
|
|
post_serializer = PostSerializer.new(@post, scope: guardian, root: false)
|
|
|
|
post_serializer.add_raw = true
|
|
|
|
render_json_dump(post_serializer)
|
|
|
|
end
|
|
|
|
|
|
|
|
def show
|
2013-02-09 06:49:15 +08:00
|
|
|
@post = find_post_from_params
|
2013-02-06 03:16:51 +08:00
|
|
|
@post.revert_to(params[:version].to_i) if params[:version].present?
|
|
|
|
post_serializer = PostSerializer.new(@post, scope: guardian, root: false)
|
|
|
|
post_serializer.add_raw = true
|
|
|
|
render_json_dump(post_serializer)
|
|
|
|
end
|
|
|
|
|
|
|
|
def destroy
|
2013-02-09 06:49:15 +08:00
|
|
|
post = find_post_from_params
|
2013-02-08 04:12:55 +08:00
|
|
|
guardian.ensure_can_delete!(post)
|
2013-03-19 05:52:29 +08:00
|
|
|
|
|
|
|
destroyer = PostDestroyer.new(current_user, post)
|
|
|
|
destroyer.destroy
|
|
|
|
|
2013-02-08 04:12:55 +08:00
|
|
|
render nothing: true
|
|
|
|
end
|
|
|
|
|
|
|
|
def recover
|
2013-02-09 06:49:15 +08:00
|
|
|
post = find_post_from_params
|
2013-02-08 04:12:55 +08:00
|
|
|
guardian.ensure_can_recover_post!(post)
|
|
|
|
post.recover
|
2013-02-06 03:16:51 +08:00
|
|
|
render nothing: true
|
|
|
|
end
|
|
|
|
|
|
|
|
def destroy_many
|
|
|
|
|
|
|
|
requires_parameters(:post_ids)
|
|
|
|
|
|
|
|
posts = Post.where(id: params[:post_ids])
|
|
|
|
raise Discourse::InvalidParameters.new(:post_ids) if posts.blank?
|
|
|
|
|
|
|
|
# Make sure we can delete the posts
|
|
|
|
posts.each {|p| guardian.ensure_can_delete!(p) }
|
|
|
|
|
|
|
|
Post.transaction do
|
|
|
|
topic_id = posts.first.topic_id
|
|
|
|
posts.each {|p| p.destroy }
|
|
|
|
Topic.reset_highest(topic_id)
|
|
|
|
end
|
|
|
|
|
|
|
|
render nothing: true
|
|
|
|
end
|
|
|
|
|
|
|
|
# Retrieves a list of versions and who made them for a post
|
|
|
|
def versions
|
2013-02-09 06:49:15 +08:00
|
|
|
post = find_post_from_params
|
2013-02-06 03:16:51 +08:00
|
|
|
render_serialized(post.all_versions, VersionSerializer)
|
|
|
|
end
|
|
|
|
|
|
|
|
# Direct replies to this post
|
|
|
|
def replies
|
2013-02-09 06:49:15 +08:00
|
|
|
post = find_post_from_params
|
2013-02-06 03:16:51 +08:00
|
|
|
render_serialized(post.replies, PostSerializer)
|
|
|
|
end
|
|
|
|
|
2013-02-15 01:10:53 +08:00
|
|
|
# Returns the "you're creating a post education"
|
|
|
|
def education_text
|
2013-02-26 00:42:20 +08:00
|
|
|
|
2013-02-15 01:10:53 +08:00
|
|
|
end
|
2013-02-06 03:16:51 +08:00
|
|
|
|
|
|
|
def bookmark
|
2013-02-09 06:49:15 +08:00
|
|
|
post = find_post_from_params
|
2013-02-07 23:45:24 +08:00
|
|
|
if current_user
|
2013-02-06 03:16:51 +08:00
|
|
|
if params[:bookmarked] == "true"
|
2013-03-01 20:07:44 +08:00
|
|
|
PostAction.act(current_user, post, PostActionType.types[:bookmark])
|
2013-02-06 03:16:51 +08:00
|
|
|
else
|
2013-03-01 20:07:44 +08:00
|
|
|
PostAction.remove_act(current_user, post, PostActionType.types[:bookmark])
|
2013-02-06 03:16:51 +08:00
|
|
|
end
|
|
|
|
end
|
2013-03-23 02:08:11 +08:00
|
|
|
render nothing: true
|
2013-02-06 03:16:51 +08:00
|
|
|
end
|
|
|
|
|
2013-02-09 06:49:15 +08:00
|
|
|
|
|
|
|
protected
|
|
|
|
|
|
|
|
def find_post_from_params
|
|
|
|
finder = Post.where(id: params[:id] || params[:post_id])
|
|
|
|
|
|
|
|
# Include deleted posts if the user is a moderator
|
2013-03-20 12:05:19 +08:00
|
|
|
finder = finder.with_deleted if current_user.try(:moderator?)
|
2013-02-09 08:04:14 +08:00
|
|
|
|
|
|
|
post = finder.first
|
|
|
|
guardian.ensure_can_see!(post)
|
|
|
|
post
|
2013-02-26 00:42:20 +08:00
|
|
|
end
|
2013-02-06 03:16:51 +08:00
|
|
|
end
|