1
0

export.rb 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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. def total_storage
  49. account.media_attachments.sum(:file_file_size)
  50. end
  51. def total_statuses
  52. account.statuses_count
  53. end
  54. def total_bookmarks
  55. account.bookmarks.count
  56. end
  57. def total_follows
  58. account.following_count
  59. end
  60. def total_lists
  61. account.owned_lists.count
  62. end
  63. def total_followers
  64. account.followers_count
  65. end
  66. def total_blocks
  67. account.blocking.count
  68. end
  69. def total_mutes
  70. account.muting.count
  71. end
  72. def total_domain_blocks
  73. account.domain_blocks.count
  74. end
  75. private
  76. def to_csv(accounts)
  77. CSV.generate do |csv|
  78. accounts.each do |account|
  79. csv << [acct(account)]
  80. end
  81. end
  82. end
  83. def acct(account)
  84. account.local? ? account.local_username_and_domain : account.acct
  85. end
  86. end