2020-08-28 06:37:58 +08:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
require "csv"
|
|
|
|
|
|
|
|
module Jobs
|
|
|
|
class ExportUserArchive < ::Jobs::Base
|
|
|
|
sidekiq_options retry: false
|
|
|
|
|
|
|
|
attr_accessor :current_user
|
2020-08-28 06:54:25 +08:00
|
|
|
# note: contents provided entirely by user
|
2020-08-28 06:37:58 +08:00
|
|
|
attr_accessor :extra
|
|
|
|
|
2024-10-16 10:09:07 +08:00
|
|
|
COMPONENTS = %w[
|
2020-08-28 06:37:58 +08:00
|
|
|
user_archive
|
2020-10-07 06:51:53 +08:00
|
|
|
preferences
|
|
|
|
auth_tokens
|
|
|
|
auth_token_logs
|
2020-09-01 06:26:51 +08:00
|
|
|
badges
|
2020-09-11 09:03:22 +08:00
|
|
|
bookmarks
|
2020-08-29 04:16:31 +08:00
|
|
|
category_preferences
|
2020-12-10 07:34:13 +08:00
|
|
|
flags
|
|
|
|
likes
|
|
|
|
post_actions
|
2020-10-27 22:48:48 +08:00
|
|
|
queued_posts
|
2020-09-01 06:26:51 +08:00
|
|
|
visits
|
2023-01-09 20:20:10 +08:00
|
|
|
]
|
|
|
|
|
2024-10-16 10:09:07 +08:00
|
|
|
HEADER_ATTRS_FOR =
|
2020-08-28 06:37:58 +08:00
|
|
|
HashWithIndifferentAccess.new(
|
2022-11-11 19:07:32 +08:00
|
|
|
user_archive: %w[
|
|
|
|
topic_title
|
|
|
|
categories
|
|
|
|
is_pm
|
|
|
|
post_raw
|
|
|
|
post_cooked
|
|
|
|
like_count
|
|
|
|
reply_count
|
|
|
|
url
|
|
|
|
created_at
|
|
|
|
],
|
2020-08-28 06:37:58 +08:00
|
|
|
user_archive_profile: %w[location website bio views],
|
2020-10-07 06:51:53 +08:00
|
|
|
auth_tokens: %w[
|
|
|
|
id
|
|
|
|
auth_token_hash
|
|
|
|
prev_auth_token_hash
|
|
|
|
auth_token_seen
|
|
|
|
client_ip
|
|
|
|
user_agent
|
|
|
|
seen_at
|
|
|
|
rotated_at
|
|
|
|
created_at
|
|
|
|
updated_at
|
|
|
|
],
|
|
|
|
auth_token_logs: %w[
|
|
|
|
id
|
|
|
|
action
|
|
|
|
user_auth_token_id
|
|
|
|
client_ip
|
|
|
|
auth_token_hash
|
|
|
|
created_at
|
|
|
|
path
|
|
|
|
user_agent
|
|
|
|
],
|
2020-09-01 06:26:51 +08:00
|
|
|
badges: %w[
|
|
|
|
badge_id
|
|
|
|
badge_name
|
|
|
|
granted_at
|
|
|
|
post_id
|
|
|
|
seq
|
|
|
|
granted_manually
|
|
|
|
notification_id
|
|
|
|
featured_rank
|
2022-05-23 08:07:15 +08:00
|
|
|
],
|
|
|
|
bookmarks: %w[
|
|
|
|
bookmarkable_id
|
|
|
|
bookmarkable_type
|
|
|
|
link
|
|
|
|
name
|
|
|
|
created_at
|
|
|
|
updated_at
|
|
|
|
reminder_at
|
|
|
|
reminder_last_sent_at
|
|
|
|
reminder_set_at
|
|
|
|
auto_delete_preference
|
2020-08-29 04:16:31 +08:00
|
|
|
],
|
|
|
|
category_preferences: %w[
|
|
|
|
category_id
|
|
|
|
category_names
|
|
|
|
notification_level
|
|
|
|
dismiss_new_timestamp
|
2020-12-10 07:34:13 +08:00
|
|
|
],
|
|
|
|
flags: %w[
|
|
|
|
id
|
|
|
|
post_id
|
|
|
|
flag_type
|
|
|
|
created_at
|
|
|
|
updated_at
|
|
|
|
deleted_at
|
|
|
|
deleted_by
|
|
|
|
related_post_id
|
|
|
|
targets_topic
|
|
|
|
was_take_action
|
|
|
|
],
|
|
|
|
likes: %w[id post_id topic_id post_number created_at updated_at deleted_at deleted_by],
|
|
|
|
post_actions: %w[
|
|
|
|
id
|
|
|
|
post_id
|
|
|
|
post_action_type
|
|
|
|
created_at
|
|
|
|
updated_at
|
|
|
|
deleted_at
|
|
|
|
deleted_by
|
|
|
|
related_post_id
|
|
|
|
],
|
2020-10-27 22:48:48 +08:00
|
|
|
queued_posts: %w[id verdict category_id topic_id post_raw other_json],
|
2020-09-01 06:26:51 +08:00
|
|
|
visits: %w[visited_at posts_read mobile time_read],
|
2020-08-28 06:37:58 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
def execute(args)
|
|
|
|
@current_user = User.find_by(id: args[:user_id])
|
|
|
|
@extra = HashWithIndifferentAccess.new(args[:args]) if args[:args]
|
|
|
|
@timestamp ||= Time.now.strftime("%y%m%d-%H%M%S")
|
|
|
|
|
|
|
|
components = []
|
|
|
|
|
|
|
|
COMPONENTS.each do |name|
|
2022-05-23 08:07:15 +08:00
|
|
|
export_method = "#{name}_export"
|
2022-05-09 07:37:23 +08:00
|
|
|
h = { name: name, method: :"#{export_method}" }
|
2020-08-28 06:37:58 +08:00
|
|
|
h[:filetype] = :csv
|
2020-10-07 06:51:53 +08:00
|
|
|
filetype_method = :"#{name}_filetype"
|
|
|
|
h[:filetype] = public_send(filetype_method) if respond_to? filetype_method
|
|
|
|
condition_method = :"include_#{name}?"
|
|
|
|
h[:skip] = !public_send(condition_method) if respond_to? condition_method
|
|
|
|
h[:filename] = name
|
2020-08-28 06:37:58 +08:00
|
|
|
components.push(h)
|
|
|
|
end
|
|
|
|
|
|
|
|
export_title = "user_archive".titleize
|
2020-09-01 06:26:51 +08:00
|
|
|
filename = "user_archive-#{@current_user.username}-#{@timestamp}"
|
2020-08-28 06:37:58 +08:00
|
|
|
user_export = UserExport.create(file_name: filename, user_id: @current_user.id)
|
|
|
|
|
|
|
|
filename = "#{filename}-#{user_export.id}"
|
|
|
|
dirname = "#{UserExport.base_directory}/#{filename}"
|
|
|
|
|
|
|
|
# ensure directory exists
|
2022-01-06 01:45:08 +08:00
|
|
|
FileUtils.mkdir_p(dirname) unless Dir.exist?(dirname)
|
2020-08-28 06:37:58 +08:00
|
|
|
|
|
|
|
# Generate a compressed CSV file
|
|
|
|
zip_filename = nil
|
|
|
|
begin
|
|
|
|
components.each do |component|
|
2020-10-07 06:51:53 +08:00
|
|
|
next if component[:skip]
|
2020-08-28 06:37:58 +08:00
|
|
|
case component[:filetype]
|
|
|
|
when :csv
|
|
|
|
CSV.open("#{dirname}/#{component[:filename]}.csv", "w") do |csv|
|
|
|
|
csv << get_header(component[:name])
|
2020-08-28 08:19:37 +08:00
|
|
|
public_send(component[:method]) { |d| csv << d }
|
2020-08-28 06:37:58 +08:00
|
|
|
end
|
2020-10-07 06:51:53 +08:00
|
|
|
when :json
|
|
|
|
File.open("#{dirname}/#{component[:filename]}.json", "w") do |file|
|
|
|
|
file.write MultiJson.dump(public_send(component[:method]), indent: 4)
|
|
|
|
end
|
2020-08-28 06:37:58 +08:00
|
|
|
else
|
|
|
|
raise "unknown export filetype"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
zip_filename = Compression::Zip.new.compress(UserExport.base_directory, filename)
|
|
|
|
ensure
|
|
|
|
FileUtils.rm_rf(dirname)
|
|
|
|
end
|
|
|
|
|
|
|
|
# create upload
|
|
|
|
upload = nil
|
|
|
|
|
|
|
|
if File.exist?(zip_filename)
|
|
|
|
File.open(zip_filename) do |file|
|
|
|
|
upload =
|
|
|
|
UploadCreator.new(
|
|
|
|
file,
|
|
|
|
File.basename(zip_filename),
|
|
|
|
type: "csv_export",
|
|
|
|
for_export: "true",
|
|
|
|
).create_for(@current_user.id)
|
|
|
|
|
|
|
|
if upload.persisted?
|
|
|
|
user_export.update_columns(upload_id: upload.id)
|
|
|
|
else
|
2021-04-23 01:21:31 +08:00
|
|
|
Rails.logger.warn(
|
|
|
|
"Failed to upload the file #{zip_filename}: #{upload.errors.full_messages}",
|
|
|
|
)
|
2020-08-28 06:37:58 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
File.delete(zip_filename)
|
|
|
|
end
|
|
|
|
ensure
|
|
|
|
post = notify_user(upload, export_title)
|
|
|
|
|
|
|
|
if user_export.present? && post.present?
|
|
|
|
topic = post.topic
|
|
|
|
user_export.update_columns(topic_id: topic.id)
|
|
|
|
topic.update_status("closed", true, Discourse.system_user)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def user_archive_export
|
|
|
|
return enum_for(:user_archive_export) unless block_given?
|
|
|
|
|
|
|
|
Post
|
|
|
|
.includes(topic: :category)
|
|
|
|
.where(user_id: @current_user.id)
|
2022-11-11 19:07:32 +08:00
|
|
|
.select(:topic_id, :post_number, :raw, :cooked, :like_count, :reply_count, :created_at)
|
2020-08-28 06:37:58 +08:00
|
|
|
.order(:created_at)
|
|
|
|
.with_deleted
|
|
|
|
.each { |user_archive| yield get_user_archive_fields(user_archive) }
|
|
|
|
end
|
|
|
|
|
|
|
|
def user_archive_profile_export
|
|
|
|
return enum_for(:user_archive_profile_export) unless block_given?
|
|
|
|
|
|
|
|
UserProfile
|
|
|
|
.where(user_id: @current_user.id)
|
|
|
|
.select(:location, :website, :bio_raw, :views)
|
|
|
|
.each { |user_profile| yield get_user_archive_profile_fields(user_profile) }
|
|
|
|
end
|
|
|
|
|
2020-10-07 06:51:53 +08:00
|
|
|
def preferences_export
|
|
|
|
UserSerializer.new(@current_user, scope: guardian)
|
|
|
|
end
|
|
|
|
|
|
|
|
def preferences_filetype
|
|
|
|
:json
|
|
|
|
end
|
|
|
|
|
|
|
|
def auth_tokens_export
|
|
|
|
return enum_for(:auth_tokens) unless block_given?
|
|
|
|
|
|
|
|
UserAuthToken
|
|
|
|
.where(user_id: @current_user.id)
|
|
|
|
.each do |token|
|
|
|
|
yield(
|
|
|
|
[
|
|
|
|
token.id,
|
|
|
|
token.auth_token.to_s[0..4] + "...", # hashed and truncated
|
|
|
|
token.prev_auth_token[0..4] + "...",
|
|
|
|
token.auth_token_seen,
|
|
|
|
token.client_ip,
|
|
|
|
token.user_agent,
|
|
|
|
token.seen_at,
|
|
|
|
token.rotated_at,
|
|
|
|
token.created_at,
|
|
|
|
token.updated_at,
|
|
|
|
]
|
2023-01-09 20:20:10 +08:00
|
|
|
)
|
2020-10-07 06:51:53 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def include_auth_token_logs?
|
|
|
|
# SiteSetting.verbose_auth_token_logging
|
|
|
|
UserAuthTokenLog.where(user_id: @current_user.id).exists?
|
|
|
|
end
|
|
|
|
|
|
|
|
def auth_token_logs_export
|
|
|
|
return enum_for(:auth_token_logs) unless block_given?
|
|
|
|
|
|
|
|
UserAuthTokenLog
|
|
|
|
.where(user_id: @current_user.id)
|
|
|
|
.each do |log|
|
|
|
|
yield(
|
|
|
|
[
|
|
|
|
log.id,
|
|
|
|
log.action,
|
|
|
|
log.user_auth_token_id,
|
|
|
|
log.client_ip,
|
|
|
|
log.auth_token.to_s[0..4] + "...", # hashed and truncated
|
|
|
|
log.created_at,
|
|
|
|
log.path,
|
|
|
|
log.user_agent,
|
|
|
|
]
|
2023-01-09 20:20:10 +08:00
|
|
|
)
|
2020-10-07 06:51:53 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-09-01 06:26:51 +08:00
|
|
|
def badges_export
|
|
|
|
return enum_for(:badges_export) unless block_given?
|
|
|
|
|
|
|
|
UserBadge
|
|
|
|
.where(user_id: @current_user.id)
|
|
|
|
.joins(:badge)
|
|
|
|
.select(
|
|
|
|
:badge_id,
|
|
|
|
:granted_at,
|
|
|
|
:post_id,
|
|
|
|
:seq,
|
|
|
|
:granted_by_id,
|
|
|
|
:notification_id,
|
|
|
|
:featured_rank,
|
|
|
|
)
|
|
|
|
.order(:granted_at)
|
|
|
|
.each do |ub|
|
|
|
|
yield(
|
|
|
|
[
|
|
|
|
ub.badge_id,
|
|
|
|
ub.badge.display_name,
|
|
|
|
ub.granted_at,
|
|
|
|
ub.post_id,
|
|
|
|
ub.seq,
|
|
|
|
# Hide the admin's identity, simply indicate human or system
|
|
|
|
User.human_user_id?(ub.granted_by_id),
|
|
|
|
ub.notification_id,
|
|
|
|
ub.featured_rank,
|
|
|
|
]
|
2023-01-09 20:20:10 +08:00
|
|
|
)
|
2020-09-01 06:26:51 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2022-05-23 08:07:15 +08:00
|
|
|
def bookmarks_export
|
|
|
|
return enum_for(:bookmarks_export) unless block_given?
|
2022-05-09 07:37:23 +08:00
|
|
|
|
|
|
|
@current_user
|
|
|
|
.bookmarks
|
|
|
|
.where.not(bookmarkable_type: nil)
|
|
|
|
.order(:id)
|
|
|
|
.each do |bookmark|
|
|
|
|
link = ""
|
|
|
|
if guardian.can_see_bookmarkable?(bookmark)
|
|
|
|
if bookmark.bookmarkable.respond_to?(:full_url)
|
|
|
|
link = bookmark.bookmarkable.full_url
|
|
|
|
else
|
|
|
|
link = bookmark.bookmarkable.url
|
2023-01-09 20:20:10 +08:00
|
|
|
end
|
2022-05-09 07:37:23 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
yield(
|
|
|
|
[
|
|
|
|
bookmark.bookmarkable_id,
|
|
|
|
bookmark.bookmarkable_type,
|
|
|
|
link,
|
|
|
|
bookmark.name,
|
|
|
|
bookmark.created_at,
|
|
|
|
bookmark.updated_at,
|
|
|
|
bookmark.reminder_at,
|
|
|
|
bookmark.reminder_last_sent_at,
|
|
|
|
bookmark.reminder_set_at,
|
|
|
|
Bookmark.auto_delete_preferences[bookmark.auto_delete_preference],
|
|
|
|
]
|
2023-01-09 20:20:10 +08:00
|
|
|
)
|
2022-05-09 07:37:23 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-08-29 04:16:31 +08:00
|
|
|
def category_preferences_export
|
|
|
|
return enum_for(:category_preferences_export) unless block_given?
|
|
|
|
|
|
|
|
CategoryUser
|
|
|
|
.where(user_id: @current_user.id)
|
2022-03-24 13:38:44 +08:00
|
|
|
.includes(:category)
|
|
|
|
.merge(Category.secured(guardian))
|
2020-08-29 04:16:31 +08:00
|
|
|
.each do |cu|
|
2022-03-24 13:38:44 +08:00
|
|
|
yield(
|
|
|
|
[
|
|
|
|
cu.category_id,
|
|
|
|
piped_category_name(cu.category_id, cu.category),
|
|
|
|
NotificationLevels.all[cu.notification_level],
|
|
|
|
cu.last_seen_at,
|
|
|
|
]
|
2023-01-09 20:20:10 +08:00
|
|
|
)
|
2022-03-24 13:38:44 +08:00
|
|
|
end
|
2020-08-29 04:16:31 +08:00
|
|
|
end
|
|
|
|
|
FIX: serialize Flags instead of PostActionType (#28362)
### Why?
Before, all flags were static. Therefore, they were stored in class variables and serialized by SiteSerializer. Recently, we added an option for admins to add their own flags or disable existing flags. Therefore, the class variable had to be dropped because it was unsafe for a multisite environment. However, it started causing performance problems.
### Solution
When a new Flag system is used, instead of using PostActionType, we can serialize Flags and use fragment cache for performance reasons.
At the same time, we are still supporting deprecated `replace_flags` API call. When it is used, we fall back to the old solution and the admin cannot add custom flags. In a couple of months, we will be able to drop that API function and clean that code properly. However, because it may still be used, redis cache was introduced to improve performance.
To test backward compatibility you can add this code to any plugin
```ruby
replace_flags do |flag_settings|
flag_settings.add(
4,
:inappropriate,
topic_type: true,
notify_type: true,
auto_action_type: true,
)
flag_settings.add(1001, :trolling, topic_type: true, notify_type: true, auto_action_type: true)
end
```
2024-08-14 10:13:46 +08:00
|
|
|
def post_action_type_view
|
|
|
|
@post_action_type_view ||= PostActionTypeView.new
|
|
|
|
end
|
|
|
|
|
2020-12-10 07:34:13 +08:00
|
|
|
def flags_export
|
|
|
|
return enum_for(:flags_export) unless block_given?
|
|
|
|
|
|
|
|
PostAction
|
|
|
|
.with_deleted
|
|
|
|
.where(user_id: @current_user.id)
|
FIX: serialize Flags instead of PostActionType (#28362)
### Why?
Before, all flags were static. Therefore, they were stored in class variables and serialized by SiteSerializer. Recently, we added an option for admins to add their own flags or disable existing flags. Therefore, the class variable had to be dropped because it was unsafe for a multisite environment. However, it started causing performance problems.
### Solution
When a new Flag system is used, instead of using PostActionType, we can serialize Flags and use fragment cache for performance reasons.
At the same time, we are still supporting deprecated `replace_flags` API call. When it is used, we fall back to the old solution and the admin cannot add custom flags. In a couple of months, we will be able to drop that API function and clean that code properly. However, because it may still be used, redis cache was introduced to improve performance.
To test backward compatibility you can add this code to any plugin
```ruby
replace_flags do |flag_settings|
flag_settings.add(
4,
:inappropriate,
topic_type: true,
notify_type: true,
auto_action_type: true,
)
flag_settings.add(1001, :trolling, topic_type: true, notify_type: true, auto_action_type: true)
end
```
2024-08-14 10:13:46 +08:00
|
|
|
.where(post_action_type_id: post_action_type_view.flag_types.values)
|
2021-06-09 13:55:52 +08:00
|
|
|
.order(:created_at)
|
2020-12-10 07:34:13 +08:00
|
|
|
.each do |pa|
|
|
|
|
yield(
|
|
|
|
[
|
|
|
|
pa.id,
|
|
|
|
pa.post_id,
|
FIX: serialize Flags instead of PostActionType (#28362)
### Why?
Before, all flags were static. Therefore, they were stored in class variables and serialized by SiteSerializer. Recently, we added an option for admins to add their own flags or disable existing flags. Therefore, the class variable had to be dropped because it was unsafe for a multisite environment. However, it started causing performance problems.
### Solution
When a new Flag system is used, instead of using PostActionType, we can serialize Flags and use fragment cache for performance reasons.
At the same time, we are still supporting deprecated `replace_flags` API call. When it is used, we fall back to the old solution and the admin cannot add custom flags. In a couple of months, we will be able to drop that API function and clean that code properly. However, because it may still be used, redis cache was introduced to improve performance.
To test backward compatibility you can add this code to any plugin
```ruby
replace_flags do |flag_settings|
flag_settings.add(
4,
:inappropriate,
topic_type: true,
notify_type: true,
auto_action_type: true,
)
flag_settings.add(1001, :trolling, topic_type: true, notify_type: true, auto_action_type: true)
end
```
2024-08-14 10:13:46 +08:00
|
|
|
post_action_type_view.flag_types[pa.post_action_type_id],
|
2020-12-10 07:34:13 +08:00
|
|
|
pa.created_at,
|
|
|
|
pa.updated_at,
|
|
|
|
pa.deleted_at,
|
|
|
|
self_or_other(pa.deleted_by_id),
|
|
|
|
pa.related_post_id,
|
|
|
|
pa.targets_topic,
|
|
|
|
# renamed to 'was_take_action' to avoid possibility of thinking this is a synonym of agreed_at
|
|
|
|
pa.staff_took_action,
|
|
|
|
]
|
2023-01-09 20:20:10 +08:00
|
|
|
)
|
2020-12-10 07:34:13 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def likes_export
|
|
|
|
return enum_for(:likes_export) unless block_given?
|
|
|
|
PostAction
|
|
|
|
.with_deleted
|
|
|
|
.where(user_id: @current_user.id)
|
FIX: serialize Flags instead of PostActionType (#28362)
### Why?
Before, all flags were static. Therefore, they were stored in class variables and serialized by SiteSerializer. Recently, we added an option for admins to add their own flags or disable existing flags. Therefore, the class variable had to be dropped because it was unsafe for a multisite environment. However, it started causing performance problems.
### Solution
When a new Flag system is used, instead of using PostActionType, we can serialize Flags and use fragment cache for performance reasons.
At the same time, we are still supporting deprecated `replace_flags` API call. When it is used, we fall back to the old solution and the admin cannot add custom flags. In a couple of months, we will be able to drop that API function and clean that code properly. However, because it may still be used, redis cache was introduced to improve performance.
To test backward compatibility you can add this code to any plugin
```ruby
replace_flags do |flag_settings|
flag_settings.add(
4,
:inappropriate,
topic_type: true,
notify_type: true,
auto_action_type: true,
)
flag_settings.add(1001, :trolling, topic_type: true, notify_type: true, auto_action_type: true)
end
```
2024-08-14 10:13:46 +08:00
|
|
|
.where(post_action_type_id: post_action_type_view.types[:like])
|
2021-06-09 13:55:52 +08:00
|
|
|
.order(:created_at)
|
2020-12-10 07:34:13 +08:00
|
|
|
.each do |pa|
|
2021-03-18 01:22:05 +08:00
|
|
|
post = Post.with_deleted.find_by(id: pa.post_id)
|
2020-12-10 07:34:13 +08:00
|
|
|
yield(
|
|
|
|
[
|
|
|
|
pa.id,
|
|
|
|
pa.post_id,
|
|
|
|
post&.topic_id,
|
|
|
|
post&.post_number,
|
|
|
|
pa.created_at,
|
|
|
|
pa.updated_at,
|
|
|
|
pa.deleted_at,
|
|
|
|
self_or_other(pa.deleted_by_id),
|
|
|
|
]
|
2023-01-09 20:20:10 +08:00
|
|
|
)
|
2020-12-10 07:34:13 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def include_post_actions?
|
|
|
|
# Most forums should not have post_action records other than flags and likes, but they are possible in historical oddities.
|
|
|
|
PostAction
|
|
|
|
.where(user_id: @current_user.id)
|
2022-05-10 08:42:18 +08:00
|
|
|
.where.not(
|
FIX: serialize Flags instead of PostActionType (#28362)
### Why?
Before, all flags were static. Therefore, they were stored in class variables and serialized by SiteSerializer. Recently, we added an option for admins to add their own flags or disable existing flags. Therefore, the class variable had to be dropped because it was unsafe for a multisite environment. However, it started causing performance problems.
### Solution
When a new Flag system is used, instead of using PostActionType, we can serialize Flags and use fragment cache for performance reasons.
At the same time, we are still supporting deprecated `replace_flags` API call. When it is used, we fall back to the old solution and the admin cannot add custom flags. In a couple of months, we will be able to drop that API function and clean that code properly. However, because it may still be used, redis cache was introduced to improve performance.
To test backward compatibility you can add this code to any plugin
```ruby
replace_flags do |flag_settings|
flag_settings.add(
4,
:inappropriate,
topic_type: true,
notify_type: true,
auto_action_type: true,
)
flag_settings.add(1001, :trolling, topic_type: true, notify_type: true, auto_action_type: true)
end
```
2024-08-14 10:13:46 +08:00
|
|
|
post_action_type_id:
|
|
|
|
post_action_type_view.flag_types.values + [post_action_type_view.types[:like]],
|
2022-05-10 08:42:18 +08:00
|
|
|
)
|
2020-12-10 07:34:13 +08:00
|
|
|
.exists?
|
|
|
|
end
|
|
|
|
|
|
|
|
def post_actions_export
|
|
|
|
return enum_for(:likes_export) unless block_given?
|
|
|
|
PostAction
|
|
|
|
.with_deleted
|
|
|
|
.where(user_id: @current_user.id)
|
2022-05-10 08:42:18 +08:00
|
|
|
.where.not(
|
FIX: serialize Flags instead of PostActionType (#28362)
### Why?
Before, all flags were static. Therefore, they were stored in class variables and serialized by SiteSerializer. Recently, we added an option for admins to add their own flags or disable existing flags. Therefore, the class variable had to be dropped because it was unsafe for a multisite environment. However, it started causing performance problems.
### Solution
When a new Flag system is used, instead of using PostActionType, we can serialize Flags and use fragment cache for performance reasons.
At the same time, we are still supporting deprecated `replace_flags` API call. When it is used, we fall back to the old solution and the admin cannot add custom flags. In a couple of months, we will be able to drop that API function and clean that code properly. However, because it may still be used, redis cache was introduced to improve performance.
To test backward compatibility you can add this code to any plugin
```ruby
replace_flags do |flag_settings|
flag_settings.add(
4,
:inappropriate,
topic_type: true,
notify_type: true,
auto_action_type: true,
)
flag_settings.add(1001, :trolling, topic_type: true, notify_type: true, auto_action_type: true)
end
```
2024-08-14 10:13:46 +08:00
|
|
|
post_action_type_id:
|
|
|
|
post_action_type_view.flag_types.values + [post_action_type_view.types[:like]],
|
2022-05-10 08:42:18 +08:00
|
|
|
)
|
2021-06-09 13:55:52 +08:00
|
|
|
.order(:created_at)
|
2020-12-10 07:34:13 +08:00
|
|
|
.each do |pa|
|
|
|
|
yield(
|
|
|
|
[
|
|
|
|
pa.id,
|
|
|
|
pa.post_id,
|
FIX: serialize Flags instead of PostActionType (#28362)
### Why?
Before, all flags were static. Therefore, they were stored in class variables and serialized by SiteSerializer. Recently, we added an option for admins to add their own flags or disable existing flags. Therefore, the class variable had to be dropped because it was unsafe for a multisite environment. However, it started causing performance problems.
### Solution
When a new Flag system is used, instead of using PostActionType, we can serialize Flags and use fragment cache for performance reasons.
At the same time, we are still supporting deprecated `replace_flags` API call. When it is used, we fall back to the old solution and the admin cannot add custom flags. In a couple of months, we will be able to drop that API function and clean that code properly. However, because it may still be used, redis cache was introduced to improve performance.
To test backward compatibility you can add this code to any plugin
```ruby
replace_flags do |flag_settings|
flag_settings.add(
4,
:inappropriate,
topic_type: true,
notify_type: true,
auto_action_type: true,
)
flag_settings.add(1001, :trolling, topic_type: true, notify_type: true, auto_action_type: true)
end
```
2024-08-14 10:13:46 +08:00
|
|
|
post_action_type_view.types[pa.post_action_type] || pa.post_action_type,
|
2020-12-10 07:34:13 +08:00
|
|
|
pa.created_at,
|
|
|
|
pa.updated_at,
|
|
|
|
pa.deleted_at,
|
|
|
|
self_or_other(pa.deleted_by_id),
|
|
|
|
pa.related_post_id,
|
|
|
|
]
|
2023-01-09 20:20:10 +08:00
|
|
|
)
|
2020-12-10 07:34:13 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-10-27 22:48:48 +08:00
|
|
|
def queued_posts_export
|
|
|
|
return enum_for(:queued_posts_export) unless block_given?
|
|
|
|
|
|
|
|
# Most Reviewable fields staff-private, but post content needs to be exported.
|
|
|
|
ReviewableQueuedPost
|
2023-07-29 00:16:23 +08:00
|
|
|
.where(target_created_by_id: @current_user.id)
|
2021-06-09 13:55:52 +08:00
|
|
|
.order(:created_at)
|
2020-10-27 22:48:48 +08:00
|
|
|
.each do |rev|
|
|
|
|
yield(
|
|
|
|
[
|
|
|
|
rev.id,
|
2021-12-09 01:12:24 +08:00
|
|
|
rev.status,
|
2020-10-27 22:48:48 +08:00
|
|
|
rev.category_id,
|
|
|
|
rev.topic_id,
|
|
|
|
rev.payload["raw"],
|
|
|
|
MultiJson.dump(rev.payload.slice(*queued_posts_payload_permitted_keys)),
|
|
|
|
]
|
2023-01-09 20:20:10 +08:00
|
|
|
)
|
2020-10-27 22:48:48 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-09-01 06:26:51 +08:00
|
|
|
def visits_export
|
|
|
|
return enum_for(:visits_export) unless block_given?
|
|
|
|
|
|
|
|
UserVisit
|
|
|
|
.where(user_id: @current_user.id)
|
|
|
|
.order(visited_at: :asc)
|
|
|
|
.each { |uv| yield [uv.visited_at, uv.posts_read, uv.mobile, uv.time_read] }
|
|
|
|
end
|
|
|
|
|
2020-08-28 06:37:58 +08:00
|
|
|
def get_header(entity)
|
|
|
|
if entity == "user_list"
|
|
|
|
header_array =
|
|
|
|
HEADER_ATTRS_FOR["user_list"] + HEADER_ATTRS_FOR["user_stats"] +
|
|
|
|
HEADER_ATTRS_FOR["user_profile"]
|
2021-02-08 18:04:33 +08:00
|
|
|
header_array.concat(HEADER_ATTRS_FOR["user_sso"]) if SiteSetting.enable_discourse_connect
|
2020-08-28 06:37:58 +08:00
|
|
|
user_custom_fields = UserField.all
|
|
|
|
if user_custom_fields.present?
|
|
|
|
user_custom_fields.each do |custom_field|
|
|
|
|
header_array.push("#{custom_field.name} (custom user field)")
|
|
|
|
end
|
|
|
|
end
|
|
|
|
header_array.push("group_names")
|
|
|
|
else
|
|
|
|
header_array = HEADER_ATTRS_FOR[entity]
|
|
|
|
end
|
|
|
|
|
|
|
|
header_array
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
2020-09-11 09:03:22 +08:00
|
|
|
def guardian
|
|
|
|
@guardian ||= Guardian.new(@current_user)
|
|
|
|
end
|
|
|
|
|
2022-03-24 13:38:44 +08:00
|
|
|
def piped_category_name(category_id, category)
|
|
|
|
return "#{category_id}" if category_id && !category
|
|
|
|
return "-" if !guardian.can_see_category?(category)
|
2020-08-28 08:17:58 +08:00
|
|
|
categories = [category.name]
|
|
|
|
while category.parent_category_id && category = category.parent_category
|
|
|
|
categories << category.name
|
|
|
|
end
|
|
|
|
categories.reverse.join("|")
|
|
|
|
end
|
|
|
|
|
2020-12-10 07:34:13 +08:00
|
|
|
def self_or_other(user_id)
|
|
|
|
if user_id.nil?
|
|
|
|
nil
|
|
|
|
elsif user_id == @current_user.id
|
|
|
|
"self"
|
|
|
|
else
|
|
|
|
"other"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-08-28 06:37:58 +08:00
|
|
|
def get_user_archive_fields(user_archive)
|
|
|
|
user_archive_array = []
|
|
|
|
topic_data = user_archive.topic
|
2024-07-02 04:08:48 +08:00
|
|
|
user_archive =
|
|
|
|
user_archive.as_json(
|
|
|
|
only: %i[topic_id post_number raw cooked like_count reply_count created_at id],
|
|
|
|
)
|
2022-03-24 13:38:44 +08:00
|
|
|
topic_data =
|
|
|
|
Topic
|
|
|
|
.with_deleted
|
|
|
|
.includes(:category)
|
|
|
|
.find_by(id: user_archive["topic_id"]) if topic_data.nil?
|
2020-08-28 06:37:58 +08:00
|
|
|
return user_archive_array if topic_data.nil?
|
|
|
|
|
2022-03-24 13:38:44 +08:00
|
|
|
categories = piped_category_name(topic_data.category_id, topic_data.category)
|
2020-08-28 06:37:58 +08:00
|
|
|
is_pm =
|
2023-01-09 20:20:10 +08:00
|
|
|
(
|
2020-08-28 06:37:58 +08:00
|
|
|
if topic_data.archetype == "private_message"
|
|
|
|
I18n.t("csv_export.boolean_yes")
|
2023-01-09 20:20:10 +08:00
|
|
|
else
|
2020-08-28 06:37:58 +08:00
|
|
|
I18n.t("csv_export.boolean_no")
|
2023-01-09 20:20:10 +08:00
|
|
|
end
|
|
|
|
)
|
2020-08-28 06:37:58 +08:00
|
|
|
url =
|
|
|
|
"#{Discourse.base_url}/t/#{topic_data.slug}/#{topic_data.id}/#{user_archive["post_number"]}"
|
|
|
|
|
2022-11-11 19:07:32 +08:00
|
|
|
topic_hash = {
|
|
|
|
"post_raw" => user_archive["raw"],
|
|
|
|
"post_cooked" => user_archive["cooked"],
|
|
|
|
"topic_title" => topic_data.title,
|
|
|
|
"categories" => categories,
|
|
|
|
"is_pm" => is_pm,
|
|
|
|
"url" => url,
|
|
|
|
}
|
|
|
|
|
2020-08-28 06:37:58 +08:00
|
|
|
user_archive.merge!(topic_hash)
|
|
|
|
|
|
|
|
HEADER_ATTRS_FOR["user_archive"].each { |attr| user_archive_array.push(user_archive[attr]) }
|
|
|
|
|
|
|
|
user_archive_array
|
|
|
|
end
|
|
|
|
|
|
|
|
def get_user_archive_profile_fields(user_profile)
|
|
|
|
user_archive_profile = []
|
|
|
|
|
|
|
|
HEADER_ATTRS_FOR["user_archive_profile"].each do |attr|
|
|
|
|
data =
|
|
|
|
if attr == "bio"
|
|
|
|
user_profile.attributes["bio_raw"]
|
|
|
|
else
|
|
|
|
user_profile.attributes[attr]
|
|
|
|
end
|
|
|
|
|
|
|
|
user_archive_profile.push(data)
|
|
|
|
end
|
|
|
|
|
|
|
|
user_archive_profile
|
|
|
|
end
|
|
|
|
|
2020-10-27 22:48:48 +08:00
|
|
|
def queued_posts_payload_permitted_keys
|
|
|
|
# Generated with:
|
|
|
|
#
|
|
|
|
# SELECT distinct json_object_keys(payload) from reviewables
|
|
|
|
# where type = 'ReviewableQueuedPost' and (payload->'old_queued_post_id') IS NULL
|
|
|
|
#
|
|
|
|
# except raw, created_topic_id, created_post_id
|
|
|
|
%w[composer_open_duration_msecs is_poll reply_to_post_number tags title typing_duration_msecs]
|
|
|
|
end
|
|
|
|
|
2020-08-28 06:37:58 +08:00
|
|
|
def notify_user(upload, export_title)
|
|
|
|
post = nil
|
|
|
|
|
|
|
|
if @current_user
|
2021-04-23 01:21:31 +08:00
|
|
|
post =
|
|
|
|
if upload.persisted?
|
2020-08-28 06:37:58 +08:00
|
|
|
SystemMessage.create_from_system_user(
|
|
|
|
@current_user,
|
|
|
|
:csv_export_succeeded,
|
|
|
|
download_link: UploadMarkdown.new(upload).attachment_markdown,
|
|
|
|
export_title: export_title,
|
|
|
|
)
|
|
|
|
else
|
|
|
|
SystemMessage.create_from_system_user(@current_user, :csv_export_failed)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
post
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|