diaspora/app/models/report.rb
Lukas Matt 3d9fceb479 DB fix to work with existing entries
* added temp. default values for user_id and item_type
* changed model validation for item_type
2014-05-15 07:23:44 -04:00

60 lines
1.5 KiB
Ruby

class Report < ActiveRecord::Base
validates :user_id, presence: true
validates :item_id, presence: true
validates :item_type, presence: true, :inclusion => { :in => %w(post comment),
:message => 'Type should match `post` or `comment`!'}
validates :text, presence: true
validate :entry_does_not_exist, :on => :create
belongs_to :user
belongs_to :post
belongs_to :comment
after_commit :send_report_notification, :on => :create
def entry_does_not_exist
if Report.where(item_id: item_id, item_type: item_type).exists?(user_id: user_id)
errors[:base] << 'You cannot report the same post twice.'
end
end
def destroy_reported_item
if item_type == 'post'
delete_post
elsif item_type == 'comment'
delete_comment
end
mark_as_reviewed
end
def delete_post
if post = Post.where(id: item_id).first
if post.author.local?
post.author.owner.retract(post)
else
post.destroy
end
end
end
def delete_comment
if comment = Comment.where(id: item_id).first
if comment.author.local?
comment.author.owner.retract(comment)
elsif comment.parent.author.local?
comment.parent.author.owner.retract(comment)
else
comment.destroy
end
end
end
def mark_as_reviewed
Report.where(item_id: item_id, item_type: item_type).update_all(reviewed: true)
end
def send_report_notification
Workers::Mail::ReportWorker.perform_async(self.item_type, self.item_id)
end
end