mastodon.rake 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. # frozen_string_literal: true
  2. namespace :mastodon do
  3. desc 'Execute daily tasks'
  4. task :daily do
  5. %w(
  6. mastodon:feeds:clear
  7. mastodon:media:clear
  8. mastodon:users:clear
  9. mastodon:push:refresh
  10. ).each do |task|
  11. puts "Starting #{task} at #{Time.now.utc}"
  12. Rake::Task[task].invoke
  13. end
  14. puts "Completed daily tasks at #{Time.now.utc}"
  15. end
  16. desc 'Turn a user into an admin, identified by the USERNAME environment variable'
  17. task make_admin: :environment do
  18. include RoutingHelper
  19. user = Account.find_local(ENV.fetch('USERNAME')).user
  20. user.update(admin: true)
  21. puts "Congrats! #{user.account.username} is now an admin. \\o/\nNavigate to #{edit_admin_settings_url} to get started"
  22. end
  23. desc 'Manually confirms a user with associated user email address stored in USER_EMAIL environment variable.'
  24. task confirm_email: :environment do
  25. email = ENV.fetch('USER_EMAIL')
  26. user = User.find_by(email: email)
  27. if user
  28. user.update(confirmed_at: Time.now.utc)
  29. puts "#{email} confirmed"
  30. else
  31. abort "#{email} not found"
  32. end
  33. end
  34. namespace :media do
  35. desc 'Removes media attachments that have not been assigned to any status for longer than a day'
  36. task clear: :environment do
  37. MediaAttachment.where(status_id: nil).where('created_at < ?', 1.day.ago).find_each(&:destroy)
  38. end
  39. desc 'Remove media attachments attributed to silenced accounts'
  40. task remove_silenced: :environment do
  41. MediaAttachment.where(account: Account.silenced).find_each(&:destroy)
  42. end
  43. desc 'Remove cached remote media attachments that are older than a week'
  44. task remove_remote: :environment do
  45. MediaAttachment.where.not(remote_url: '').where('created_at < ?', 1.week.ago).find_each do |media|
  46. media.file.destroy
  47. media.type = :unknown
  48. media.save
  49. end
  50. end
  51. desc 'Set unknown attachment type for remote-only attachments'
  52. task set_unknown: :environment do
  53. Rails.logger.debug 'Setting unknown attachment type for remote-only attachments...'
  54. MediaAttachment.where(file_file_name: nil).where.not(type: :unknown).in_batches.update_all(type: :unknown)
  55. Rails.logger.debug 'Done!'
  56. end
  57. end
  58. namespace :push do
  59. desc 'Unsubscribes from PuSH updates of feeds nobody follows locally'
  60. task clear: :environment do
  61. Account.remote.without_followers.where.not(subscription_expires_at: nil).find_each do |a|
  62. Rails.logger.debug "PuSH unsubscribing from #{a.acct}"
  63. UnsubscribeService.new.call(a)
  64. end
  65. end
  66. desc 'Re-subscribes to soon expiring PuSH subscriptions'
  67. task refresh: :environment do
  68. # No-op
  69. # This task is now executed via sidekiq-scheduler
  70. end
  71. end
  72. namespace :feeds do
  73. desc 'Clear timelines of inactive users'
  74. task clear: :environment do
  75. User.confirmed.where('current_sign_in_at < ?', 14.days.ago).find_each do |user|
  76. Redis.current.del(FeedManager.instance.key(:home, user.account_id))
  77. end
  78. end
  79. desc 'Clears all timelines'
  80. task clear_all: :environment do
  81. Redis.current.keys('feed:*').each { |key| Redis.current.del(key) }
  82. end
  83. end
  84. namespace :emails do
  85. desc 'Send out digest e-mails'
  86. task digest: :environment do
  87. User.confirmed.joins(:account).where(accounts: { silenced: false, suspended: false }).where('current_sign_in_at < ?', 20.days.ago).find_each do |user|
  88. DigestMailerWorker.perform_async(user.id)
  89. end
  90. end
  91. end
  92. namespace :users do
  93. desc 'Clear out unconfirmed users'
  94. task clear: :environment do
  95. # Users that never confirmed e-mail never signed in, means they
  96. # only have a user record and an avatar record, with no files uploaded
  97. User.where('confirmed_at is NULL AND confirmation_sent_at <= ?', 2.days.ago).find_in_batches do |batch|
  98. Account.where(id: batch.map(&:account_id)).delete_all
  99. User.where(id: batch.map(&:id)).delete_all
  100. end
  101. end
  102. desc 'List all admin users'
  103. task admins: :environment do
  104. puts 'Admin user emails:'
  105. puts User.admins.map(&:email).join("\n")
  106. end
  107. end
  108. namespace :settings do
  109. desc 'Open registrations on this instance'
  110. task open_registrations: :environment do
  111. setting = Setting.where(var: 'open_registrations').first
  112. setting.value = true
  113. setting.save
  114. end
  115. desc 'Close registrations on this instance'
  116. task close_registrations: :environment do
  117. setting = Setting.where(var: 'open_registrations').first
  118. setting.value = false
  119. setting.save
  120. end
  121. end
  122. namespace :maintenance do
  123. desc 'Update counter caches'
  124. task update_counter_caches: :environment do
  125. Rails.logger.debug 'Updating counter caches for accounts...'
  126. Account.unscoped.select('id').find_in_batches do |batch|
  127. Account.where(id: batch.map(&:id)).update_all('statuses_count = (select count(*) from statuses where account_id = accounts.id), followers_count = (select count(*) from follows where target_account_id = accounts.id), following_count = (select count(*) from follows where account_id = accounts.id)')
  128. end
  129. Rails.logger.debug 'Updating counter caches for statuses...'
  130. Status.unscoped.select('id').find_in_batches do |batch|
  131. Status.where(id: batch.map(&:id)).update_all('favourites_count = (select count(*) from favourites where favourites.status_id = statuses.id), reblogs_count = (select count(*) from statuses as reblogs where reblogs.reblog_of_id = statuses.id)')
  132. end
  133. Rails.logger.debug 'Done!'
  134. end
  135. desc 'Generate static versions of GIF avatars/headers'
  136. task add_static_avatars: :environment do
  137. Rails.logger.debug 'Generating static avatars/headers for GIF ones...'
  138. Account.unscoped.where(avatar_content_type: 'image/gif').or(Account.unscoped.where(header_content_type: 'image/gif')).find_each do |account|
  139. begin
  140. account.avatar.reprocess! if account.avatar_content_type == 'image/gif' && !account.avatar.exists?(:static)
  141. account.header.reprocess! if account.header_content_type == 'image/gif' && !account.header.exists?(:static)
  142. rescue StandardError => e
  143. Rails.logger.error "Error while generating static avatars/headers for account #{account.id}: #{e}"
  144. next
  145. end
  146. end
  147. Rails.logger.debug 'Done!'
  148. end
  149. end
  150. end