export.rb 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. # frozen_string_literal: true
  2. require 'csv'
  3. class Export
  4. attr_reader :account
  5. def initialize(account)
  6. @account = account
  7. end
  8. def to_bookmarks_csv
  9. CSV.generate do |csv|
  10. account.bookmarks.includes(:status).reorder(id: :desc).each do |bookmark|
  11. csv << [ActivityPub::TagManager.instance.uri_for(bookmark.status)]
  12. end
  13. end
  14. end
  15. def to_blocked_accounts_csv
  16. to_csv account.blocking.select(:username, :domain)
  17. end
  18. def to_muted_accounts_csv
  19. CSV.generate(headers: ['Account address', 'Hide notifications'], write_headers: true) do |csv|
  20. account.mute_relationships.includes(:target_account).reorder(id: :desc).each do |mute|
  21. csv << [acct(mute.target_account), mute.hide_notifications]
  22. end
  23. end
  24. end
  25. def to_following_accounts_csv
  26. CSV.generate(headers: ['Account address', 'Show boosts', 'Notify on new posts', 'Languages'], write_headers: true) do |csv|
  27. account.active_relationships.includes(:target_account).reorder(id: :desc).each do |follow|
  28. csv << [acct(follow.target_account), follow.show_reblogs, follow.notify, follow.languages&.join(', ')]
  29. end
  30. end
  31. end
  32. def to_lists_csv
  33. CSV.generate do |csv|
  34. account.owned_lists.select(:title, :id).each do |list|
  35. list.accounts.select(:username, :domain).each do |account|
  36. csv << [list.title, acct(account)]
  37. end
  38. end
  39. end
  40. end
  41. def to_blocked_domains_csv
  42. CSV.generate do |csv|
  43. account.domain_blocks.pluck(:domain).each do |domain|
  44. csv << [domain]
  45. end
  46. end
  47. end
  48. private
  49. def to_csv(accounts)
  50. CSV.generate do |csv|
  51. accounts.each do |account|
  52. csv << [acct(account)]
  53. end
  54. end
  55. end
  56. def acct(account)
  57. account.local? ? account.local_username_and_domain : account.acct
  58. end
  59. end