2014-03-07 15:59:47 +08:00
|
|
|
require_dependency "discourse_diff"
|
|
|
|
|
2013-12-12 10:41:34 +08:00
|
|
|
class PostRevision < ActiveRecord::Base
|
|
|
|
belongs_to :post
|
|
|
|
belongs_to :user
|
|
|
|
|
|
|
|
serialize :modifications, Hash
|
2014-03-07 15:59:47 +08:00
|
|
|
|
2014-10-16 03:09:08 +08:00
|
|
|
def self.ensure_consistency!
|
|
|
|
# 1 - fix the numbers
|
|
|
|
sql = <<-SQL
|
|
|
|
UPDATE post_revisions
|
|
|
|
SET number = pr.rank
|
|
|
|
FROM (SELECT id, ROW_NUMBER() OVER (PARTITION BY post_id ORDER BY number, created_at, updated_at) AS rank FROM post_revisions) AS pr
|
|
|
|
WHERE post_revisions.id = pr.id
|
|
|
|
AND post_revisions.number <> pr.rank
|
|
|
|
SQL
|
|
|
|
|
|
|
|
PostRevision.exec_sql(sql)
|
|
|
|
|
|
|
|
# 2 - fix the versions on the posts
|
|
|
|
sql = <<-SQL
|
|
|
|
UPDATE posts
|
|
|
|
SET version = pv.version
|
|
|
|
FROM (SELECT post_id, MAX(number) AS version FROM post_revisions GROUP BY post_id) AS pv
|
|
|
|
WHERE posts.id = pv.post_id
|
|
|
|
AND posts.version <> pv.version
|
|
|
|
SQL
|
|
|
|
|
|
|
|
PostRevision.exec_sql(sql)
|
|
|
|
end
|
|
|
|
|
2014-03-07 15:59:47 +08:00
|
|
|
def body_changes
|
2014-03-12 01:51:26 +08: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 15:59:47 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
def category_changes
|
2014-03-12 01:51:26 +08:00
|
|
|
prev = previous("category_id")
|
|
|
|
cur = current("category_id")
|
|
|
|
return if prev == cur
|
|
|
|
|
2014-03-07 15:59:47 +08:00
|
|
|
{
|
2014-03-12 01:51:26 +08:00
|
|
|
previous_category_id: prev,
|
|
|
|
current_category_id: cur,
|
2014-03-07 15:59:47 +08:00
|
|
|
}
|
|
|
|
end
|
|
|
|
|
2014-05-13 20:53:11 +08:00
|
|
|
def wiki_changes
|
2014-10-13 16:18:49 +08:00
|
|
|
prev = previous("wiki")
|
|
|
|
cur = current("wiki")
|
2014-05-13 20:53:11 +08:00
|
|
|
return if prev == cur
|
|
|
|
|
|
|
|
{
|
|
|
|
previous_wiki: prev,
|
|
|
|
current_wiki: cur,
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
2014-09-11 05:08:33 +08:00
|
|
|
def post_type_changes
|
2014-10-13 16:18:49 +08:00
|
|
|
prev = previous("post_type")
|
|
|
|
cur = current("post_type")
|
2014-09-11 05:08:33 +08:00
|
|
|
return if prev == cur
|
|
|
|
|
|
|
|
{
|
|
|
|
previous_post_type: prev,
|
|
|
|
current_post_type: cur,
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
2014-03-07 15:59:47 +08:00
|
|
|
def title_changes
|
2014-03-12 01:51:26 +08:00
|
|
|
prev = "<div>#{CGI::escapeHTML(previous("title"))}</div>"
|
|
|
|
cur = "<div>#{CGI::escapeHTML(current("title"))}</div>"
|
|
|
|
return if prev == cur
|
2014-03-07 15:59:47 +08:00
|
|
|
|
|
|
|
diff = DiscourseDiff.new(prev, cur)
|
|
|
|
|
2014-03-12 01:51:26 +08:00
|
|
|
{
|
2014-03-07 15:59:47 +08:00
|
|
|
inline: diff.inline_html,
|
|
|
|
side_by_side: diff.side_by_side_html
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
2014-03-28 09:28:14 +08:00
|
|
|
def user_changes
|
|
|
|
prev = previous("user_id")
|
|
|
|
cur = current("user_id")
|
|
|
|
return if prev == cur
|
|
|
|
|
|
|
|
{
|
2014-05-06 21:41:59 +08:00
|
|
|
previous_user: User.find_by(id: prev),
|
|
|
|
current_user: User.find_by(id: cur)
|
2014-03-28 09:28:14 +08:00
|
|
|
}
|
|
|
|
end
|
|
|
|
|
2014-03-07 15:59:47 +08:00
|
|
|
def previous(field)
|
2014-10-13 16:18:49 +08:00
|
|
|
val = lookup(field)
|
|
|
|
if val.nil?
|
|
|
|
val = lookup_in_previous_revisions(field)
|
|
|
|
end
|
|
|
|
|
|
|
|
if val.nil?
|
|
|
|
val = lookup_in_post(field)
|
|
|
|
end
|
|
|
|
|
|
|
|
val
|
2014-03-07 15:59:47 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
def current(field)
|
2014-10-13 16:18:49 +08:00
|
|
|
val = lookup_in_next_revision(field)
|
|
|
|
if val.nil?
|
|
|
|
val = lookup_in_post(field)
|
|
|
|
end
|
|
|
|
|
|
|
|
if val.nil?
|
|
|
|
val = lookup(field)
|
|
|
|
end
|
|
|
|
|
|
|
|
if val.nil?
|
|
|
|
val = lookup_in_previous_revisions(field)
|
|
|
|
end
|
|
|
|
|
|
|
|
return val
|
2014-03-07 15:59:47 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
def previous_revisions
|
2014-10-13 16:18:49 +08:00
|
|
|
@previous_revs ||= PostRevision.where("post_id = ? AND number < ? AND hidden = ?", post_id, number, false)
|
2014-03-12 01:51:26 +08:00
|
|
|
.order("number desc")
|
|
|
|
.to_a
|
2014-03-07 15:59:47 +08:00
|
|
|
end
|
|
|
|
|
2014-10-13 16:18:49 +08:00
|
|
|
def next_revision
|
|
|
|
@next_revision ||= PostRevision.where("post_id = ? AND number > ? AND hidden = ?", post_id, number, false)
|
|
|
|
.order("number asc")
|
|
|
|
.to_a.first
|
|
|
|
end
|
|
|
|
|
2014-03-07 15:59:47 +08:00
|
|
|
def has_topic_data?
|
|
|
|
post && post.post_number == 1
|
|
|
|
end
|
|
|
|
|
2014-10-13 16:18:49 +08:00
|
|
|
def lookup_in_previous_revisions(field)
|
|
|
|
previous_revisions.each do |v|
|
|
|
|
val = v.lookup(field)
|
|
|
|
return val unless val.nil?
|
|
|
|
end
|
|
|
|
|
|
|
|
nil
|
|
|
|
end
|
2014-03-07 15:59:47 +08:00
|
|
|
|
2014-10-13 16:18:49 +08:00
|
|
|
def lookup_in_next_revision(field)
|
|
|
|
if next_revision
|
|
|
|
return next_revision.lookup(field)
|
2014-03-07 15:59:47 +08:00
|
|
|
end
|
2014-10-13 16:18:49 +08:00
|
|
|
end
|
2014-03-07 15:59:47 +08:00
|
|
|
|
2014-10-13 16:18:49 +08:00
|
|
|
def lookup_in_post(field)
|
|
|
|
if !post
|
|
|
|
return
|
|
|
|
elsif ["cooked", "raw"].include?(field)
|
|
|
|
val = post.send(field)
|
|
|
|
elsif ["title", "category_id"].include?(field)
|
|
|
|
val = post.topic.send(field)
|
2014-03-07 15:59:47 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
val
|
|
|
|
end
|
|
|
|
|
2014-10-13 16:18:49 +08:00
|
|
|
def lookup(field)
|
|
|
|
return nil if hidden
|
|
|
|
mod = modifications[field]
|
|
|
|
unless mod.nil?
|
|
|
|
mod[0]
|
2014-03-07 15:59:47 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2014-10-13 16:18:49 +08:00
|
|
|
def hide!
|
|
|
|
self.hidden = true
|
|
|
|
self.save!
|
|
|
|
end
|
|
|
|
|
|
|
|
def show!
|
|
|
|
self.hidden = false
|
|
|
|
self.save!
|
|
|
|
end
|
|
|
|
|
2013-12-12 10:41:34 +08:00
|
|
|
end
|
2014-02-07 08:07:36 +08:00
|
|
|
|
|
|
|
# == Schema Information
|
|
|
|
#
|
|
|
|
# Table name: post_revisions
|
|
|
|
#
|
|
|
|
# id :integer not null, primary key
|
|
|
|
# user_id :integer
|
|
|
|
# post_id :integer
|
|
|
|
# modifications :text
|
|
|
|
# number :integer
|
2014-08-27 13:19:25 +08:00
|
|
|
# created_at :datetime not null
|
|
|
|
# updated_at :datetime not null
|
2014-02-07 08:07:36 +08:00
|
|
|
#
|
|
|
|
# Indexes
|
|
|
|
#
|
|
|
|
# index_post_revisions_on_post_id (post_id)
|
|
|
|
# index_post_revisions_on_post_id_and_number (post_id,number)
|
|
|
|
#
|