2013-02-06 03:16:51 +08:00
|
|
|
# Responsible for creating posts and topics
|
|
|
|
#
|
|
|
|
require_dependency 'rate_limiter'
|
|
|
|
|
|
|
|
class PostCreator
|
|
|
|
|
2013-03-19 01:55:34 +08:00
|
|
|
attr_reader :errors, :opts
|
2013-02-06 03:16:51 +08:00
|
|
|
|
2013-05-20 14:44:06 +08:00
|
|
|
def self.create(user,opts)
|
|
|
|
self.new(user,opts).create
|
|
|
|
end
|
|
|
|
|
2013-02-06 03:16:51 +08:00
|
|
|
# Acceptable options:
|
|
|
|
#
|
|
|
|
# raw - raw text of post
|
2013-02-26 00:42:20 +08:00
|
|
|
# image_sizes - We can pass a list of the sizes of images in the post as a shortcut.
|
2013-03-19 01:55:34 +08:00
|
|
|
# invalidate_oneboxes - Whether to force invalidation of oneboxes in this post
|
2013-05-14 02:06:16 +08:00
|
|
|
# acting_user - The user performing the action might be different than the user
|
|
|
|
# who is the post "author." For example when copying posts to a new
|
|
|
|
# topic.
|
2013-05-20 14:44:06 +08:00
|
|
|
# created_at - Post creation time (optional)
|
2013-02-06 03:16:51 +08:00
|
|
|
#
|
|
|
|
# When replying to a topic:
|
|
|
|
# topic_id - topic we're replying to
|
|
|
|
# reply_to_post_number - post number we're replying to
|
|
|
|
#
|
|
|
|
# When creating a topic:
|
|
|
|
# title - New topic title
|
|
|
|
# archetype - Topic archetype
|
|
|
|
# category - Category to assign to topic
|
|
|
|
# target_usernames - comma delimited list of usernames for membership (private message)
|
2013-05-09 15:37:34 +08:00
|
|
|
# target_group_names - comma delimited list of groups for membership (private message)
|
2013-02-06 03:16:51 +08:00
|
|
|
# meta_data - Topic meta data hash
|
|
|
|
def initialize(user, opts)
|
2013-04-05 12:29:46 +08:00
|
|
|
# TODO: we should reload user in case it is tainted, should take in a user_id as opposed to user
|
|
|
|
# If we don't do this we introduce a rather risky dependency
|
2013-02-26 00:42:20 +08:00
|
|
|
@user = user
|
2013-02-06 03:16:51 +08:00
|
|
|
@opts = opts
|
2013-05-11 04:58:23 +08:00
|
|
|
@spam = false
|
|
|
|
|
2013-02-06 03:16:51 +08:00
|
|
|
raise Discourse::InvalidParameters.new(:raw) if @opts[:raw].blank?
|
|
|
|
end
|
|
|
|
|
2013-05-11 04:58:23 +08:00
|
|
|
# True if the post was considered spam
|
|
|
|
def spam?
|
|
|
|
@spam
|
|
|
|
end
|
|
|
|
|
2013-02-06 03:16:51 +08:00
|
|
|
def guardian
|
|
|
|
@guardian ||= Guardian.new(@user)
|
|
|
|
end
|
|
|
|
|
|
|
|
def create
|
|
|
|
topic = nil
|
|
|
|
post = nil
|
2013-05-16 13:03:03 +08:00
|
|
|
new_topic = false
|
2013-02-06 03:16:51 +08:00
|
|
|
|
|
|
|
Post.transaction do
|
|
|
|
if @opts[:topic_id].blank?
|
2013-05-16 13:03:03 +08:00
|
|
|
topic = create_topic
|
|
|
|
new_topic = true
|
2013-02-06 03:16:51 +08:00
|
|
|
else
|
|
|
|
topic = Topic.where(id: @opts[:topic_id]).first
|
|
|
|
guardian.ensure_can_create!(Post, topic)
|
|
|
|
end
|
|
|
|
|
2013-02-26 00:42:20 +08:00
|
|
|
post = topic.posts.new(raw: @opts[:raw],
|
2013-02-06 03:16:51 +08:00
|
|
|
user: @user,
|
|
|
|
reply_to_post_number: @opts[:reply_to_post_number])
|
2013-03-29 04:40:54 +08:00
|
|
|
|
|
|
|
post.post_type = @opts[:post_type] if @opts[:post_type].present?
|
|
|
|
post.no_bump = @opts[:no_bump] if @opts[:no_bump].present?
|
2013-03-19 03:54:08 +08:00
|
|
|
post.extract_quoted_post_numbers
|
2013-05-14 02:06:16 +08:00
|
|
|
post.acting_user = @opts[:acting_user] if @opts[:acting_user].present?
|
2013-05-19 03:24:29 +08:00
|
|
|
post.created_at = Time.zone.parse(@opts[:created_at].to_s) if @opts[:created_at].present?
|
2013-03-19 03:54:08 +08:00
|
|
|
|
2013-02-06 03:16:51 +08:00
|
|
|
post.image_sizes = @opts[:image_sizes] if @opts[:image_sizes].present?
|
2013-03-19 01:55:34 +08:00
|
|
|
post.invalidate_oneboxes = @opts[:invalidate_oneboxes] if @opts[:invalidate_oneboxes].present?
|
2013-05-11 04:58:23 +08:00
|
|
|
|
|
|
|
|
|
|
|
# If the post has host spam, roll it back.
|
|
|
|
if post.has_host_spam?
|
|
|
|
post.errors.add(:base, I18n.t(:spamming_host))
|
|
|
|
@errors = post.errors
|
|
|
|
@spam = true
|
|
|
|
raise ActiveRecord::Rollback.new
|
|
|
|
end
|
|
|
|
|
2013-02-06 03:16:51 +08:00
|
|
|
unless post.save
|
|
|
|
@errors = post.errors
|
2013-02-26 00:42:20 +08:00
|
|
|
raise ActiveRecord::Rollback.new
|
2013-02-06 03:16:51 +08:00
|
|
|
end
|
|
|
|
|
2013-02-26 00:42:20 +08:00
|
|
|
# Extract links
|
2013-02-06 03:16:51 +08:00
|
|
|
TopicLink.extract_from(post)
|
2013-03-19 01:55:34 +08:00
|
|
|
|
|
|
|
# Store unique post key
|
|
|
|
if SiteSetting.unique_posts_mins > 0
|
|
|
|
$redis.setex(post.unique_post_key, SiteSetting.unique_posts_mins.minutes.to_i, "1")
|
|
|
|
end
|
|
|
|
|
|
|
|
# send a mail to notify users in case of a private message
|
|
|
|
if topic.private_message?
|
|
|
|
topic.allowed_users.where(["users.email_private_messages = true and users.id != ?", @user.id]).each do |u|
|
2013-05-16 13:03:03 +08:00
|
|
|
Jobs.enqueue_in(SiteSetting.email_time_window_mins.minutes,
|
|
|
|
:user_email,
|
|
|
|
type: :private_message,
|
|
|
|
user_id: u.id,
|
|
|
|
post_id: post.id
|
|
|
|
)
|
2013-03-19 01:55:34 +08:00
|
|
|
end
|
2013-05-13 09:48:01 +08:00
|
|
|
|
|
|
|
clear_possible_flags(topic) if post.post_number > 1 && topic.user_id != post.user_id
|
2013-03-19 01:55:34 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
# Track the topic
|
|
|
|
TopicUser.auto_track(@user.id, topic.id, TopicUser.notification_reasons[:created_post])
|
|
|
|
|
2013-05-16 03:16:42 +08:00
|
|
|
# We don't count replies to your own topics
|
2013-04-05 12:29:46 +08:00
|
|
|
if @user.id != topic.user_id
|
2013-04-05 13:48:38 +08:00
|
|
|
@user.update_topic_reply_count
|
2013-04-05 12:29:46 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
@user.last_posted_at = post.created_at
|
|
|
|
@user.save!
|
2013-03-19 02:45:05 +08:00
|
|
|
|
2013-05-16 13:03:03 +08:00
|
|
|
if post.post_number > 1
|
|
|
|
MessageBus.publish("/topic/#{post.topic_id}",{
|
|
|
|
id: post.id,
|
|
|
|
created_at: post.created_at,
|
|
|
|
user: BasicUserSerializer.new(post.user).as_json(root: false),
|
|
|
|
post_number: post.post_number
|
|
|
|
},
|
|
|
|
group_ids: secure_group_ids(topic)
|
|
|
|
)
|
|
|
|
end
|
2013-03-19 03:12:31 +08:00
|
|
|
|
|
|
|
# Advance the draft sequence
|
|
|
|
post.advance_draft_sequence
|
2013-03-19 03:54:08 +08:00
|
|
|
|
|
|
|
# Save the quote relationships
|
|
|
|
post.save_reply_relationships
|
2013-02-06 03:16:51 +08:00
|
|
|
end
|
|
|
|
|
2013-04-08 22:51:01 +08:00
|
|
|
# We need to enqueue jobs after the transaction. Otherwise they might begin before the data has
|
|
|
|
# been comitted.
|
2013-04-08 23:23:03 +08:00
|
|
|
topic_id = @opts[:topic_id] || topic.try(:id)
|
|
|
|
Jobs.enqueue(:feature_topic_users, topic_id: topic.id) if topic_id.present?
|
2013-05-16 13:03:03 +08:00
|
|
|
if post
|
|
|
|
post.trigger_post_process
|
|
|
|
after_topic_create(topic) if new_topic
|
|
|
|
end
|
2013-04-08 22:51:01 +08:00
|
|
|
|
2013-02-06 03:16:51 +08:00
|
|
|
post
|
|
|
|
end
|
|
|
|
|
2013-04-08 22:51:01 +08:00
|
|
|
|
2013-02-06 03:16:51 +08:00
|
|
|
# Shortcut
|
|
|
|
def self.create(user, opts)
|
2013-02-26 00:42:20 +08:00
|
|
|
PostCreator.new(user, opts).create
|
2013-02-06 03:16:51 +08:00
|
|
|
end
|
|
|
|
|
2013-05-02 13:15:17 +08:00
|
|
|
protected
|
|
|
|
|
2013-05-16 13:03:03 +08:00
|
|
|
def secure_group_ids(topic)
|
|
|
|
@secure_group_ids ||= if topic.category && topic.category.secure?
|
|
|
|
topic.category.groups.select("groups.id").map{|g| g.id}
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def after_topic_create(topic)
|
|
|
|
|
|
|
|
# Don't publish invisible topics
|
|
|
|
return unless topic.visible?
|
|
|
|
|
|
|
|
return if topic.private_message?
|
|
|
|
|
|
|
|
topic.posters = topic.posters_summary
|
|
|
|
topic.posts_count = 1
|
|
|
|
topic_json = TopicListItemSerializer.new(topic).as_json
|
|
|
|
|
2013-05-28 15:52:52 +08:00
|
|
|
message = {
|
|
|
|
topic_id: topic.id,
|
|
|
|
message_type: "new_topic",
|
|
|
|
payload: {
|
|
|
|
last_read_post_number: nil,
|
|
|
|
topic_id: topic.id
|
|
|
|
}
|
|
|
|
}
|
2013-05-16 13:03:03 +08:00
|
|
|
|
2013-05-28 15:52:52 +08:00
|
|
|
group_ids = secure_group_ids(topic)
|
|
|
|
MessageBus.publish("/new", message.as_json, group_ids: group_ids)
|
2013-05-16 13:03:03 +08:00
|
|
|
|
2013-05-28 15:52:52 +08:00
|
|
|
# TODO post creator should get an unread
|
2013-05-16 13:03:03 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
def create_topic
|
|
|
|
topic_params = {title: @opts[:title], user_id: @user.id, last_post_user_id: @user.id}
|
|
|
|
topic_params[:archetype] = @opts[:archetype] if @opts[:archetype].present?
|
|
|
|
topic_params[:subtype] = @opts[:subtype] if @opts[:subtype].present?
|
|
|
|
|
|
|
|
guardian.ensure_can_create!(Topic)
|
|
|
|
|
|
|
|
category = Category.where(name: @opts[:category]).first
|
|
|
|
topic_params[:category_id] = category.id if category.present?
|
|
|
|
topic_params[:meta_data] = @opts[:meta_data] if @opts[:meta_data].present?
|
2013-05-19 03:24:29 +08:00
|
|
|
topic_params[:created_at] = Time.zone.parse(@opts[:created_at].to_s) if @opts[:created_at].present?
|
2013-05-16 13:03:03 +08:00
|
|
|
|
|
|
|
topic = Topic.new(topic_params)
|
|
|
|
|
|
|
|
if @opts[:auto_close_days]
|
|
|
|
guardian.ensure_can_moderate!(topic)
|
|
|
|
topic.auto_close_days = @opts[:auto_close_days]
|
|
|
|
end
|
|
|
|
|
|
|
|
if @opts[:archetype] == Archetype.private_message
|
|
|
|
|
|
|
|
topic.subtype = TopicSubtype.user_to_user unless topic.subtype
|
|
|
|
|
|
|
|
unless @opts[:target_usernames].present? || @opts[:target_group_names].present?
|
|
|
|
topic.errors.add(:archetype, :cant_send_pm)
|
|
|
|
@errors = topic.errors
|
|
|
|
raise ActiveRecord::Rollback.new
|
|
|
|
end
|
|
|
|
|
|
|
|
add_users(topic,@opts[:target_usernames])
|
|
|
|
add_groups(topic,@opts[:target_group_names])
|
|
|
|
topic.topic_allowed_users.build(user_id: @user.id)
|
|
|
|
end
|
|
|
|
|
|
|
|
unless topic.save
|
|
|
|
@errors = topic.errors
|
|
|
|
raise ActiveRecord::Rollback.new
|
|
|
|
end
|
|
|
|
|
|
|
|
topic
|
|
|
|
end
|
|
|
|
|
2013-05-13 09:48:01 +08:00
|
|
|
def clear_possible_flags(topic)
|
|
|
|
# at this point we know the topic is a PM and has been replied to ... check if we need to clear any flags
|
|
|
|
#
|
|
|
|
first_post = Post.select(:id).where(topic_id: topic.id).where('post_number = 1').first
|
|
|
|
post_action = nil
|
|
|
|
|
|
|
|
if first_post
|
|
|
|
post_action = PostAction.where(
|
|
|
|
related_post_id: first_post.id,
|
|
|
|
deleted_at: nil,
|
|
|
|
post_action_type_id: PostActionType.types[:notify_moderators]
|
|
|
|
).first
|
|
|
|
end
|
|
|
|
|
|
|
|
if post_action
|
|
|
|
post_action.remove_act!(@user)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-05-02 13:15:17 +08:00
|
|
|
def add_users(topic, usernames)
|
|
|
|
return unless usernames
|
2013-05-28 07:13:53 +08:00
|
|
|
User.where(username: usernames.split(',')).each do |user|
|
|
|
|
check_can_send_permission!(topic,user)
|
|
|
|
topic.topic_allowed_users.build(user_id: user.id)
|
2013-05-02 13:15:17 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def add_groups(topic, groups)
|
|
|
|
return unless groups
|
2013-05-28 07:13:53 +08:00
|
|
|
Group.where(name: groups.split(',')).each do |group|
|
|
|
|
check_can_send_permission!(topic,group)
|
|
|
|
topic.topic_allowed_groups.build(group_id: group.id)
|
|
|
|
end
|
|
|
|
end
|
2013-05-02 13:15:17 +08:00
|
|
|
|
2013-05-28 07:13:53 +08:00
|
|
|
def check_can_send_permission!(topic,item)
|
|
|
|
unless guardian.can_send_private_message?(item)
|
|
|
|
topic.errors.add(:archetype, :cant_send_pm)
|
|
|
|
@errors = topic.errors
|
|
|
|
raise ActiveRecord::Rollback.new
|
2013-05-02 13:15:17 +08:00
|
|
|
end
|
|
|
|
end
|
2013-02-06 03:16:51 +08:00
|
|
|
end
|