2019-05-02 17:17:27 -05:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2022-10-31 07:02:26 -05:00
|
|
|
class Admin::StaffActionLogsController < Admin::StaffController
|
2023-07-28 06:53:46 -05:00
|
|
|
INDEX_LIMIT = 200
|
|
|
|
|
2013-08-07 15:04:12 -05:00
|
|
|
def index
|
2023-01-09 06:20:10 -06:00
|
|
|
filters = params.slice(*UserHistory.staff_filters + %i[page limit])
|
2017-05-30 10:25:42 -05:00
|
|
|
|
2019-08-13 12:55:05 -05:00
|
|
|
page = (params[:page] || 0).to_i
|
2023-07-28 06:53:46 -05:00
|
|
|
page_size = fetch_limit_from_params(default: INDEX_LIMIT, max: INDEX_LIMIT)
|
2019-08-13 12:55:05 -05:00
|
|
|
|
|
|
|
staff_action_logs = UserHistory.staff_action_records(current_user, filters)
|
|
|
|
count = staff_action_logs.count
|
|
|
|
staff_action_logs = staff_action_logs.offset(page * page_size).limit(page_size).to_a
|
|
|
|
|
|
|
|
load_more_params = params.permit(UserHistory.staff_filters)
|
|
|
|
load_more_params.merge!(page: page + 1, page_size: page_size)
|
|
|
|
|
|
|
|
render_json_dump(
|
|
|
|
staff_action_logs: serialize_data(staff_action_logs, UserHistorySerializer),
|
|
|
|
total_rows_staff_action_logs: count,
|
|
|
|
load_more_staff_action_logs: admin_staff_action_logs_path(load_more_params),
|
|
|
|
extras: {
|
2023-01-09 06:20:10 -06:00
|
|
|
user_history_actions: staff_available_actions,
|
|
|
|
},
|
2019-08-13 12:55:05 -05:00
|
|
|
)
|
2013-08-07 15:04:12 -05:00
|
|
|
end
|
|
|
|
|
2017-04-12 09:52:52 -05:00
|
|
|
def diff
|
|
|
|
@history = UserHistory.find(params[:id])
|
|
|
|
prev = @history.previous_value
|
|
|
|
cur = @history.new_value
|
|
|
|
|
|
|
|
prev = JSON.parse(prev) if prev
|
|
|
|
cur = JSON.parse(cur) if cur
|
|
|
|
|
|
|
|
diff_fields = {}
|
|
|
|
|
2019-11-25 16:17:14 -06:00
|
|
|
output = +"<h2>#{CGI.escapeHTML(cur&.dig("name").to_s)}</h2><p></p>"
|
2017-04-12 09:52:52 -05:00
|
|
|
|
2023-01-09 06:20:10 -06:00
|
|
|
diff_fields["name"] = { prev: prev&.dig("name").to_s, cur: cur&.dig("name").to_s }
|
2017-04-12 09:52:52 -05:00
|
|
|
|
2023-01-09 06:20:10 -06:00
|
|
|
%w[default user_selectable].each do |f|
|
|
|
|
diff_fields[f] = { prev: (!!prev&.dig(f)).to_s, cur: (!!cur&.dig(f)).to_s }
|
2017-04-12 09:52:52 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
diff_fields["color scheme"] = {
|
2019-11-25 16:17:14 -06:00
|
|
|
prev: prev&.dig("color_scheme", "name").to_s,
|
|
|
|
cur: cur&.dig("color_scheme", "name").to_s,
|
2017-04-12 09:52:52 -05:00
|
|
|
}
|
|
|
|
|
2023-01-09 06:20:10 -06:00
|
|
|
diff_fields["included themes"] = { prev: child_themes(prev), cur: child_themes(cur) }
|
2017-04-12 09:52:52 -05:00
|
|
|
|
|
|
|
load_diff(diff_fields, :cur, cur)
|
|
|
|
load_diff(diff_fields, :prev, prev)
|
|
|
|
|
2017-07-27 20:20:09 -05:00
|
|
|
diff_fields.delete_if { |k, v| v[:cur] == v[:prev] }
|
2017-04-12 09:52:52 -05:00
|
|
|
|
2017-07-27 20:20:09 -05:00
|
|
|
diff_fields.each do |k, v|
|
2017-04-12 09:52:52 -05:00
|
|
|
output << "<h3>#{k}</h3><p></p>"
|
|
|
|
diff = DiscourseDiff.new(v[:prev] || "", v[:cur] || "")
|
|
|
|
output << diff.side_by_side_markdown
|
|
|
|
end
|
|
|
|
|
2017-07-27 20:20:09 -05:00
|
|
|
render json: { side_by_side: output }
|
2017-04-12 09:52:52 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
protected
|
|
|
|
|
|
|
|
def child_themes(theme)
|
2019-11-25 16:17:14 -06:00
|
|
|
return "" unless children = theme&.dig("child_themes")
|
2017-04-12 09:52:52 -05:00
|
|
|
|
2017-07-27 20:20:09 -05:00
|
|
|
children.map { |row| row["name"] }.join(" ").to_s
|
2017-04-12 09:52:52 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def load_diff(hash, key, val)
|
2019-11-25 16:17:14 -06:00
|
|
|
if f = val&.dig("theme_fields")
|
2017-04-12 09:52:52 -05:00
|
|
|
f.each do |row|
|
|
|
|
entry = hash[row["target"] + " " + row["name"]] ||= {}
|
|
|
|
entry[key] = row["value"]
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-04-10 07:53:17 -05:00
|
|
|
private
|
|
|
|
|
|
|
|
def staff_available_actions
|
|
|
|
UserHistory.staff_actions.sort.map do |name|
|
2023-01-09 06:20:10 -06:00
|
|
|
{ id: name, action_id: UserHistory.actions[name] || UserHistory.actions[:custom_staff] }
|
2019-04-10 07:53:17 -05:00
|
|
|
end
|
|
|
|
end
|
2013-08-07 15:04:12 -05:00
|
|
|
end
|