2019-05-02 18:17:27 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2013-12-11 21:41:34 -05:00
|
|
|
class PostRevisionSerializer < ApplicationSerializer
|
2014-10-27 17:06:43 -04:00
|
|
|
attributes :created_at,
|
|
|
|
:post_id,
|
|
|
|
# which revision is hidden
|
|
|
|
:previous_hidden,
|
|
|
|
:current_hidden,
|
|
|
|
# dynamic & based on the current scope
|
|
|
|
:first_revision,
|
|
|
|
:previous_revision,
|
|
|
|
:current_revision,
|
|
|
|
:next_revision,
|
|
|
|
:last_revision,
|
|
|
|
# used for display
|
|
|
|
:current_version,
|
|
|
|
:version_count,
|
|
|
|
# from the user
|
2013-12-11 21:41:34 -05:00
|
|
|
:username,
|
|
|
|
:display_username,
|
|
|
|
:avatar_template,
|
2014-10-27 17:06:43 -04:00
|
|
|
# all the changes
|
2013-12-11 21:41:34 -05:00
|
|
|
:edit_reason,
|
2014-03-07 02:59:47 -05:00
|
|
|
:body_changes,
|
|
|
|
:title_changes,
|
2016-06-15 14:15:30 -04:00
|
|
|
:user_changes,
|
2017-01-25 01:33:16 -05:00
|
|
|
:tags_changes,
|
2024-05-22 04:09:20 -04:00
|
|
|
:category_id_changes,
|
2017-01-26 21:49:17 -05:00
|
|
|
:can_edit
|
2015-01-27 17:12:43 -05:00
|
|
|
|
|
|
|
# Creates a field called field_name_changes with previous and
|
|
|
|
# current members if a field has changed in this revision
|
|
|
|
def self.add_compared_field(field)
|
|
|
|
changes_name = "#{field}_changes".to_sym
|
|
|
|
|
|
|
|
self.attributes changes_name
|
|
|
|
define_method(changes_name) { { previous: previous[field], current: current[field] } }
|
|
|
|
|
|
|
|
define_method("include_#{changes_name}?") { previous[field] != current[field] }
|
|
|
|
end
|
|
|
|
|
|
|
|
add_compared_field :wiki
|
2024-05-22 04:09:20 -04:00
|
|
|
add_compared_field :post_type
|
2014-10-27 17:06:43 -04:00
|
|
|
|
|
|
|
def previous_hidden
|
|
|
|
previous["hidden"]
|
|
|
|
end
|
2014-03-07 02:59:47 -05:00
|
|
|
|
2014-10-27 17:06:43 -04:00
|
|
|
def current_hidden
|
|
|
|
current["hidden"]
|
2014-03-07 02:59:47 -05:00
|
|
|
end
|
|
|
|
|
2014-10-27 17:06:43 -04:00
|
|
|
def first_revision
|
|
|
|
revisions.first["revision"]
|
2014-03-07 02:59:47 -05:00
|
|
|
end
|
2013-12-11 21:41:34 -05:00
|
|
|
|
2014-10-27 17:06:43 -04:00
|
|
|
def previous_revision
|
|
|
|
@previous_revision ||=
|
|
|
|
revisions
|
|
|
|
.select { |r| r["revision"] >= first_revision }
|
|
|
|
.select { |r| r["revision"] < current_revision }
|
|
|
|
.last
|
|
|
|
.try(:[], "revision")
|
2014-10-13 04:18:49 -04:00
|
|
|
end
|
|
|
|
|
2014-10-27 17:06:43 -04:00
|
|
|
def current_revision
|
2013-12-11 21:41:34 -05:00
|
|
|
object.number
|
|
|
|
end
|
|
|
|
|
2014-10-27 17:06:43 -04:00
|
|
|
def next_revision
|
|
|
|
@next_revision ||=
|
|
|
|
revisions
|
|
|
|
.select { |r| r["revision"] <= last_revision }
|
|
|
|
.select { |r| r["revision"] > current_revision }
|
|
|
|
.first
|
|
|
|
.try(:[], "revision")
|
|
|
|
end
|
|
|
|
|
|
|
|
def last_revision
|
|
|
|
@last_revision ||= revisions.select { |r| r["revision"] <= post.version }.last["revision"]
|
|
|
|
end
|
|
|
|
|
|
|
|
def current_version
|
|
|
|
@current_version ||= revisions.select { |r| r["revision"] <= current_revision }.count + 1
|
|
|
|
end
|
|
|
|
|
|
|
|
def version_count
|
|
|
|
revisions.count
|
2013-12-11 21:41:34 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def username
|
2014-03-07 02:59:47 -05:00
|
|
|
user.username_lower
|
2013-12-11 21:41:34 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def display_username
|
2014-03-07 02:59:47 -05:00
|
|
|
user.username
|
2013-12-11 21:41:34 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def avatar_template
|
2014-03-07 02:59:47 -05:00
|
|
|
user.avatar_template
|
2013-12-11 21:41:34 -05:00
|
|
|
end
|
|
|
|
|
2017-01-26 21:49:17 -05:00
|
|
|
def can_edit
|
|
|
|
scope.can_edit?(object.post)
|
|
|
|
end
|
|
|
|
|
2013-12-11 21:41:34 -05:00
|
|
|
def edit_reason
|
2024-05-22 04:09:20 -04:00
|
|
|
current["edit_reason"]
|
|
|
|
end
|
|
|
|
|
|
|
|
def include_edit_reason?
|
|
|
|
scope.can_view_hidden_post_revisions? || current["revision"] == previous["revision"] + 1
|
2014-10-27 17:06:43 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def body_changes
|
|
|
|
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,
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
def title_changes
|
2015-02-26 21:22:52 -05:00
|
|
|
prev = "<div>#{previous["title"] && CGI.escapeHTML(previous["title"])}</div>"
|
|
|
|
cur = "<div>#{current["title"] && CGI.escapeHTML(current["title"])}</div>"
|
|
|
|
|
2015-03-02 12:29:59 -05:00
|
|
|
# always show the title for post_number == 1
|
|
|
|
return if object.post.post_number > 1 && prev == cur
|
2014-10-27 17:06:43 -04:00
|
|
|
|
|
|
|
diff = DiscourseDiff.new(prev, cur)
|
|
|
|
|
|
|
|
{ inline: diff.inline_html, side_by_side: diff.side_by_side_html }
|
|
|
|
end
|
|
|
|
|
2024-05-22 04:09:20 -04:00
|
|
|
def include_title_changes?
|
|
|
|
object.post.post_number == 1
|
|
|
|
end
|
|
|
|
|
2014-03-27 21:28:14 -04:00
|
|
|
def user_changes
|
2014-10-27 17:06:43 -04:00
|
|
|
prev = previous["user_id"]
|
|
|
|
cur = current["user_id"]
|
|
|
|
|
|
|
|
# if stuff is messed up, default to system
|
|
|
|
previous = User.find_by(id: prev) || Discourse.system_user
|
|
|
|
current = User.find_by(id: cur) || Discourse.system_user
|
|
|
|
|
2014-03-27 21:28:14 -04:00
|
|
|
{
|
|
|
|
previous: {
|
2014-10-27 17:06:43 -04:00
|
|
|
username: previous.username_lower,
|
|
|
|
display_username: previous.username,
|
|
|
|
avatar_template: previous.avatar_template,
|
2014-03-27 21:28:14 -04:00
|
|
|
},
|
|
|
|
current: {
|
2014-10-27 17:06:43 -04:00
|
|
|
username: current.username_lower,
|
|
|
|
display_username: current.username,
|
|
|
|
avatar_template: current.avatar_template,
|
2014-03-27 21:28:14 -04:00
|
|
|
},
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
2024-05-22 04:09:20 -04:00
|
|
|
def include_user_changes?
|
|
|
|
previous["user_id"] != current["user_id"]
|
|
|
|
end
|
|
|
|
|
2016-06-15 14:15:30 -04:00
|
|
|
def tags_changes
|
2024-05-22 04:09:20 -04:00
|
|
|
pre = filter_tags previous["tags"]
|
|
|
|
cur = filter_tags current["tags"]
|
|
|
|
|
|
|
|
pre == cur ? nil : { previous: pre, current: cur }
|
2016-06-15 14:15:30 -04:00
|
|
|
end
|
|
|
|
|
2016-05-04 14:02:47 -04:00
|
|
|
def include_tags_changes?
|
2024-05-22 04:09:20 -04:00
|
|
|
previous["tags"] != current["tags"] && scope.can_see_tags?(topic)
|
|
|
|
end
|
|
|
|
|
|
|
|
def category_id_changes
|
|
|
|
pre = filter_category_id previous["category_id"]
|
|
|
|
cur = filter_category_id current["category_id"]
|
|
|
|
|
|
|
|
pre == cur ? nil : { previous: pre, current: cur }
|
|
|
|
end
|
|
|
|
|
|
|
|
def include_category_id_changes?
|
|
|
|
previous["category_id"] != current["category_id"]
|
2016-05-04 14:02:47 -04:00
|
|
|
end
|
|
|
|
|
2014-10-27 17:06:43 -04:00
|
|
|
protected
|
|
|
|
|
|
|
|
def post
|
|
|
|
@post ||= object.post
|
|
|
|
end
|
|
|
|
|
|
|
|
def topic
|
|
|
|
@topic ||= object.post.topic
|
|
|
|
end
|
|
|
|
|
|
|
|
def revisions
|
|
|
|
@revisions ||=
|
|
|
|
all_revisions.select { |r| scope.can_view_hidden_post_revisions? || !r["hidden"] }
|
2018-06-07 01:28:18 -04:00
|
|
|
end
|
2014-10-27 17:06:43 -04:00
|
|
|
|
|
|
|
def all_revisions
|
|
|
|
return @all_revisions if @all_revisions
|
2015-01-28 11:01:01 -05:00
|
|
|
|
2021-05-05 06:18:25 -04:00
|
|
|
post_revisions =
|
|
|
|
PostRevision.where(post_id: object.post_id).order(number: :desc).limit(99).to_a.reverse
|
2015-01-28 11:01:01 -05:00
|
|
|
|
2018-02-22 09:57:02 -05:00
|
|
|
latest_modifications = {
|
|
|
|
"raw" => [post.raw],
|
2015-01-28 11:01:01 -05:00
|
|
|
"cooked" => [post.cooked],
|
2018-02-22 09:57:02 -05:00
|
|
|
"edit_reason" => [post.edit_reason],
|
2015-01-28 11:01:01 -05:00
|
|
|
"wiki" => [post.wiki],
|
2018-02-22 09:57:02 -05:00
|
|
|
"post_type" => [post.post_type],
|
|
|
|
"user_id" => [post.user_id],
|
2018-06-07 01:28:18 -04:00
|
|
|
}
|
2015-01-28 11:01:01 -05:00
|
|
|
|
2018-02-22 09:57:02 -05:00
|
|
|
# Retrieve any `tracked_topic_fields`
|
|
|
|
PostRevisor.tracked_topic_fields.each_key do |field|
|
2024-10-22 09:29:07 -04:00
|
|
|
next unless topic.respond_to?(field)
|
|
|
|
topic
|
|
|
|
.public_send(field)
|
|
|
|
.then do |value|
|
|
|
|
next if value.try(:proxy_association)
|
|
|
|
latest_modifications[field.to_s] = [value]
|
|
|
|
end
|
2018-06-07 01:28:18 -04:00
|
|
|
end
|
2016-05-04 14:02:47 -04:00
|
|
|
|
2014-10-27 17:06:43 -04:00
|
|
|
latest_modifications["featured_link"] = [
|
2024-05-22 04:09:20 -04:00
|
|
|
topic.featured_link,
|
2014-10-27 17:06:43 -04:00
|
|
|
] if SiteSetting.topic_featured_link_enabled
|
2024-05-22 04:09:20 -04:00
|
|
|
|
|
|
|
latest_modifications["tags"] = [topic.tags.map(&:name).sort]
|
2014-10-27 17:06:43 -04:00
|
|
|
|
|
|
|
post_revisions << PostRevision.new(
|
|
|
|
number: post_revisions.last.number + 1,
|
|
|
|
hidden: post.hidden,
|
2018-02-22 09:57:02 -05:00
|
|
|
modifications: latest_modifications,
|
2018-06-07 01:28:18 -04:00
|
|
|
)
|
2014-10-27 17:06:43 -04:00
|
|
|
|
|
|
|
@all_revisions = []
|
|
|
|
|
|
|
|
# backtrack
|
2015-04-18 07:53:53 -04:00
|
|
|
post_revisions.each do |pr|
|
2014-10-27 17:06:43 -04:00
|
|
|
revision = HashWithIndifferentAccess.new
|
|
|
|
revision[:revision] = pr.number
|
|
|
|
revision[:hidden] = pr.hidden
|
|
|
|
|
2024-05-22 04:09:20 -04:00
|
|
|
pr.modifications.each { |field, (value, _)| revision[field] = value }
|
2014-10-27 17:06:43 -04:00
|
|
|
|
|
|
|
@all_revisions << revision
|
2018-06-07 01:28:18 -04:00
|
|
|
end
|
2014-10-27 17:06:43 -04:00
|
|
|
|
|
|
|
# waterfall
|
2015-04-18 07:53:53 -04:00
|
|
|
(@all_revisions.count - 1)
|
|
|
|
.downto(1)
|
|
|
|
.each do |r|
|
2014-10-27 17:06:43 -04:00
|
|
|
cur = @all_revisions[r]
|
|
|
|
prev = @all_revisions[r - 1]
|
|
|
|
|
|
|
|
cur.each_key { |field| prev[field] = prev.has_key?(field) ? prev[field] : cur[field] }
|
2018-06-07 01:28:18 -04:00
|
|
|
end
|
2014-10-27 17:06:43 -04:00
|
|
|
|
|
|
|
@all_revisions
|
|
|
|
end
|
|
|
|
|
|
|
|
def previous
|
|
|
|
@previous ||= revisions.select { |r| r["revision"] <= current_revision }.last
|
|
|
|
end
|
|
|
|
|
|
|
|
def current
|
|
|
|
@current ||= revisions.select { |r| r["revision"] > current_revision }.first
|
|
|
|
end
|
|
|
|
|
2018-06-07 01:28:18 -04:00
|
|
|
def user
|
2014-10-27 17:06:43 -04:00
|
|
|
# if stuff goes pear shape attribute to system
|
|
|
|
object.user || Discourse.system_user
|
2018-06-07 01:28:18 -04:00
|
|
|
end
|
|
|
|
|
2024-05-22 04:09:20 -04:00
|
|
|
def hidden_tags
|
|
|
|
@hidden_tags ||= DiscourseTagging.hidden_tag_names(scope)
|
|
|
|
end
|
|
|
|
|
|
|
|
def filter_tags(tags)
|
2024-06-20 18:09:21 -04:00
|
|
|
tags.is_a?(Array) && tags.any? ? tags - hidden_tags : tags
|
2024-05-22 04:09:20 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def filter_category_id(category_id)
|
|
|
|
return if category_id.blank?
|
|
|
|
Category.secured(scope).find_by(id: category_id)&.id
|
2018-06-07 01:28:18 -04:00
|
|
|
end
|
2013-12-11 21:41:34 -05:00
|
|
|
end
|