mirror of
https://github.com/discourse/discourse.git
synced 2025-05-31 02:58:48 +08:00
DEV: Apply syntax_tree formatting to app/*
This commit is contained in:
@ -1,7 +1,6 @@
|
||||
# frozen_string_literal: true
|
||||
|
||||
class PostRevisionSerializer < ApplicationSerializer
|
||||
|
||||
attributes :created_at,
|
||||
:post_id,
|
||||
# which revision is hidden
|
||||
@ -35,13 +34,9 @@ class PostRevisionSerializer < ApplicationSerializer
|
||||
changes_name = "#{field}_changes".to_sym
|
||||
|
||||
self.attributes changes_name
|
||||
define_method(changes_name) do
|
||||
{ previous: previous[field], current: current[field] }
|
||||
end
|
||||
define_method(changes_name) { { previous: previous[field], current: current[field] } }
|
||||
|
||||
define_method("include_#{changes_name}?") do
|
||||
previous[field] != current[field]
|
||||
end
|
||||
define_method("include_#{changes_name}?") { previous[field] != current[field] }
|
||||
end
|
||||
|
||||
add_compared_field :wiki
|
||||
@ -59,9 +54,12 @@ class PostRevisionSerializer < ApplicationSerializer
|
||||
end
|
||||
|
||||
def previous_revision
|
||||
@previous_revision ||= revisions.select { |r| r["revision"] >= first_revision }
|
||||
.select { |r| r["revision"] < current_revision }
|
||||
.last.try(:[], "revision")
|
||||
@previous_revision ||=
|
||||
revisions
|
||||
.select { |r| r["revision"] >= first_revision }
|
||||
.select { |r| r["revision"] < current_revision }
|
||||
.last
|
||||
.try(:[], "revision")
|
||||
end
|
||||
|
||||
def current_revision
|
||||
@ -69,9 +67,12 @@ class PostRevisionSerializer < ApplicationSerializer
|
||||
end
|
||||
|
||||
def next_revision
|
||||
@next_revision ||= revisions.select { |r| r["revision"] <= last_revision }
|
||||
.select { |r| r["revision"] > current_revision }
|
||||
.first.try(:[], "revision")
|
||||
@next_revision ||=
|
||||
revisions
|
||||
.select { |r| r["revision"] <= last_revision }
|
||||
.select { |r| r["revision"] > current_revision }
|
||||
.first
|
||||
.try(:[], "revision")
|
||||
end
|
||||
|
||||
def last_revision
|
||||
@ -108,8 +109,9 @@ class PostRevisionSerializer < ApplicationSerializer
|
||||
|
||||
def edit_reason
|
||||
# only show 'edit_reason' when revisions are consecutive
|
||||
current["edit_reason"] if scope.can_view_hidden_post_revisions? ||
|
||||
current["revision"] == previous["revision"] + 1
|
||||
if scope.can_view_hidden_post_revisions? || current["revision"] == previous["revision"] + 1
|
||||
current["edit_reason"]
|
||||
end
|
||||
end
|
||||
|
||||
def body_changes
|
||||
@ -119,23 +121,20 @@ class PostRevisionSerializer < ApplicationSerializer
|
||||
{
|
||||
inline: cooked_diff.inline_html,
|
||||
side_by_side: cooked_diff.side_by_side_html,
|
||||
side_by_side_markdown: raw_diff.side_by_side_markdown
|
||||
side_by_side_markdown: raw_diff.side_by_side_markdown,
|
||||
}
|
||||
end
|
||||
|
||||
def title_changes
|
||||
prev = "<div>#{previous["title"] && CGI::escapeHTML(previous["title"])}</div>"
|
||||
cur = "<div>#{current["title"] && CGI::escapeHTML(current["title"])}</div>"
|
||||
prev = "<div>#{previous["title"] && CGI.escapeHTML(previous["title"])}</div>"
|
||||
cur = "<div>#{current["title"] && CGI.escapeHTML(current["title"])}</div>"
|
||||
|
||||
# always show the title for post_number == 1
|
||||
return if object.post.post_number > 1 && prev == cur
|
||||
|
||||
diff = DiscourseDiff.new(prev, cur)
|
||||
|
||||
{
|
||||
inline: diff.inline_html,
|
||||
side_by_side: diff.side_by_side_html
|
||||
}
|
||||
{ inline: diff.inline_html, side_by_side: diff.side_by_side_html }
|
||||
end
|
||||
|
||||
def user_changes
|
||||
@ -148,23 +147,23 @@ class PostRevisionSerializer < ApplicationSerializer
|
||||
current = User.find_by(id: cur) || Discourse.system_user
|
||||
|
||||
{
|
||||
previous: {
|
||||
username: previous.username_lower,
|
||||
display_username: previous.username,
|
||||
avatar_template: previous.avatar_template
|
||||
},
|
||||
current: {
|
||||
username: current.username_lower,
|
||||
display_username: current.username,
|
||||
avatar_template: current.avatar_template
|
||||
}
|
||||
previous: {
|
||||
username: previous.username_lower,
|
||||
display_username: previous.username,
|
||||
avatar_template: previous.avatar_template,
|
||||
},
|
||||
current: {
|
||||
username: current.username_lower,
|
||||
display_username: current.username,
|
||||
avatar_template: current.avatar_template,
|
||||
},
|
||||
}
|
||||
end
|
||||
|
||||
def tags_changes
|
||||
changes = {
|
||||
previous: filter_visible_tags(previous["tags"]),
|
||||
current: filter_visible_tags(current["tags"])
|
||||
current: filter_visible_tags(current["tags"]),
|
||||
}
|
||||
changes[:previous] == changes[:current] ? nil : changes
|
||||
end
|
||||
@ -184,18 +183,15 @@ class PostRevisionSerializer < ApplicationSerializer
|
||||
end
|
||||
|
||||
def revisions
|
||||
@revisions ||= all_revisions.select { |r| scope.can_view_hidden_post_revisions? || !r["hidden"] }
|
||||
@revisions ||=
|
||||
all_revisions.select { |r| scope.can_view_hidden_post_revisions? || !r["hidden"] }
|
||||
end
|
||||
|
||||
def all_revisions
|
||||
return @all_revisions if @all_revisions
|
||||
|
||||
post_revisions = PostRevision
|
||||
.where(post_id: object.post_id)
|
||||
.order(number: :desc)
|
||||
.limit(99)
|
||||
.to_a
|
||||
.reverse
|
||||
post_revisions =
|
||||
PostRevision.where(post_id: object.post_id).order(number: :desc).limit(99).to_a.reverse
|
||||
|
||||
latest_modifications = {
|
||||
"raw" => [post.raw],
|
||||
@ -203,24 +199,24 @@ class PostRevisionSerializer < ApplicationSerializer
|
||||
"edit_reason" => [post.edit_reason],
|
||||
"wiki" => [post.wiki],
|
||||
"post_type" => [post.post_type],
|
||||
"user_id" => [post.user_id]
|
||||
"user_id" => [post.user_id],
|
||||
}
|
||||
|
||||
# Retrieve any `tracked_topic_fields`
|
||||
PostRevisor.tracked_topic_fields.each_key do |field|
|
||||
next if field == :tags # Special handling below
|
||||
if topic.respond_to?(field)
|
||||
latest_modifications[field.to_s] = [topic.public_send(field)]
|
||||
end
|
||||
latest_modifications[field.to_s] = [topic.public_send(field)] if topic.respond_to?(field)
|
||||
end
|
||||
|
||||
latest_modifications["featured_link"] = [post.topic.featured_link] if SiteSetting.topic_featured_link_enabled
|
||||
latest_modifications["featured_link"] = [
|
||||
post.topic.featured_link,
|
||||
] if SiteSetting.topic_featured_link_enabled
|
||||
latest_modifications["tags"] = [topic.tags.pluck(:name)] if scope.can_see_tags?(topic)
|
||||
|
||||
post_revisions << PostRevision.new(
|
||||
number: post_revisions.last.number + 1,
|
||||
hidden: post.hidden,
|
||||
modifications: latest_modifications
|
||||
modifications: latest_modifications,
|
||||
)
|
||||
|
||||
@all_revisions = []
|
||||
@ -231,22 +227,20 @@ class PostRevisionSerializer < ApplicationSerializer
|
||||
revision[:revision] = pr.number
|
||||
revision[:hidden] = pr.hidden
|
||||
|
||||
pr.modifications.each_key do |field|
|
||||
revision[field] = pr.modifications[field][0]
|
||||
end
|
||||
pr.modifications.each_key { |field| revision[field] = pr.modifications[field][0] }
|
||||
|
||||
@all_revisions << revision
|
||||
end
|
||||
|
||||
# waterfall
|
||||
(@all_revisions.count - 1).downto(1).each do |r|
|
||||
cur = @all_revisions[r]
|
||||
prev = @all_revisions[r - 1]
|
||||
(@all_revisions.count - 1)
|
||||
.downto(1)
|
||||
.each do |r|
|
||||
cur = @all_revisions[r]
|
||||
prev = @all_revisions[r - 1]
|
||||
|
||||
cur.each_key do |field|
|
||||
prev[field] = prev.has_key?(field) ? prev[field] : cur[field]
|
||||
cur.each_key { |field| prev[field] = prev.has_key?(field) ? prev[field] : cur[field] }
|
||||
end
|
||||
end
|
||||
|
||||
@all_revisions
|
||||
end
|
||||
@ -272,5 +266,4 @@ class PostRevisionSerializer < ApplicationSerializer
|
||||
tags
|
||||
end
|
||||
end
|
||||
|
||||
end
|
||||
|
Reference in New Issue
Block a user