2014-03-07 02:59:47 -05:00
|
|
|
require_dependency "discourse_diff"
|
|
|
|
|
2013-12-11 21:41:34 -05:00
|
|
|
class PostRevision < ActiveRecord::Base
|
|
|
|
belongs_to :post
|
|
|
|
belongs_to :user
|
|
|
|
|
|
|
|
serialize :modifications, Hash
|
2014-03-07 02:59:47 -05:00
|
|
|
|
|
|
|
def body_changes
|
2014-03-11 13:51:26 -04:00
|
|
|
cooked_diff = DiscourseDiff.new(previous("cooked"), current("cooked"))
|
|
|
|
raw_diff = DiscourseDiff.new(previous("raw"), current("raw"))
|
|
|
|
|
|
|
|
{
|
|
|
|
inline: cooked_diff.inline_html,
|
|
|
|
side_by_side: cooked_diff.side_by_side_html,
|
|
|
|
side_by_side_markdown: raw_diff.side_by_side_markdown
|
|
|
|
}
|
2014-03-07 02:59:47 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def category_changes
|
2014-03-11 13:51:26 -04:00
|
|
|
prev = previous("category_id")
|
|
|
|
cur = current("category_id")
|
|
|
|
return if prev == cur
|
|
|
|
|
2014-03-07 02:59:47 -05:00
|
|
|
{
|
2014-03-11 13:51:26 -04:00
|
|
|
previous_category_id: prev,
|
|
|
|
current_category_id: cur,
|
2014-03-07 02:59:47 -05:00
|
|
|
}
|
|
|
|
end
|
|
|
|
|
2014-05-13 08:53:11 -04:00
|
|
|
def wiki_changes
|
|
|
|
prev = lookup("wiki", 0)
|
|
|
|
cur = lookup("wiki", 1)
|
|
|
|
return if prev == cur
|
|
|
|
|
|
|
|
{
|
|
|
|
previous_wiki: prev,
|
|
|
|
current_wiki: cur,
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
2014-03-07 02:59:47 -05:00
|
|
|
def title_changes
|
2014-03-11 13:51:26 -04:00
|
|
|
prev = "<div>#{CGI::escapeHTML(previous("title"))}</div>"
|
|
|
|
cur = "<div>#{CGI::escapeHTML(current("title"))}</div>"
|
|
|
|
return if prev == cur
|
2014-03-07 02:59:47 -05:00
|
|
|
|
|
|
|
diff = DiscourseDiff.new(prev, cur)
|
|
|
|
|
2014-03-11 13:51:26 -04:00
|
|
|
{
|
2014-03-07 02:59:47 -05:00
|
|
|
inline: diff.inline_html,
|
|
|
|
side_by_side: diff.side_by_side_html
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
2014-03-27 21:28:14 -04:00
|
|
|
def user_changes
|
|
|
|
prev = previous("user_id")
|
|
|
|
cur = current("user_id")
|
|
|
|
return if prev == cur
|
|
|
|
|
|
|
|
{
|
2014-05-06 09:41:59 -04:00
|
|
|
previous_user: User.find_by(id: prev),
|
|
|
|
current_user: User.find_by(id: cur)
|
2014-03-27 21:28:14 -04:00
|
|
|
}
|
|
|
|
end
|
|
|
|
|
2014-03-07 02:59:47 -05:00
|
|
|
def previous(field)
|
|
|
|
lookup_with_fallback(field, 0)
|
|
|
|
end
|
|
|
|
|
|
|
|
def current(field)
|
|
|
|
lookup_with_fallback(field, 1)
|
|
|
|
end
|
|
|
|
|
|
|
|
def previous_revisions
|
2014-03-11 13:51:26 -04:00
|
|
|
@previous_revs ||= PostRevision.where("post_id = ? AND number < ?", post_id, number)
|
|
|
|
.order("number desc")
|
|
|
|
.to_a
|
2014-03-07 02:59:47 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def has_topic_data?
|
|
|
|
post && post.post_number == 1
|
|
|
|
end
|
|
|
|
|
|
|
|
def lookup_with_fallback(field, index)
|
|
|
|
|
|
|
|
unless val = lookup(field, index)
|
|
|
|
previous_revisions.each do |v|
|
|
|
|
break if val = v.lookup(field, 1)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
unless val
|
2014-05-13 08:53:11 -04:00
|
|
|
if ["cooked", "raw"].include?(field)
|
2014-03-07 02:59:47 -05:00
|
|
|
val = post.send(field)
|
|
|
|
else
|
|
|
|
val = post.topic.send(field)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
val
|
|
|
|
end
|
|
|
|
|
|
|
|
def lookup(field, index)
|
|
|
|
if mod = modifications[field]
|
|
|
|
mod[index]
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-12-11 21:41:34 -05:00
|
|
|
end
|
2014-02-06 19:07:36 -05:00
|
|
|
|
|
|
|
# == Schema Information
|
|
|
|
#
|
|
|
|
# Table name: post_revisions
|
|
|
|
#
|
|
|
|
# id :integer not null, primary key
|
|
|
|
# user_id :integer
|
|
|
|
# post_id :integer
|
|
|
|
# modifications :text
|
|
|
|
# number :integer
|
|
|
|
# created_at :datetime
|
|
|
|
# updated_at :datetime
|
|
|
|
#
|
|
|
|
# Indexes
|
|
|
|
#
|
|
|
|
# index_post_revisions_on_post_id (post_id)
|
|
|
|
# index_post_revisions_on_post_id_and_number (post_id,number)
|
|
|
|
#
|