You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

170 lines
6.0 KiB

  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.utc.now}"
  12. Rake::Task[task].invoke
  13. end
  14. puts "Completed daily tasks at #{Time.utc.now}"
  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 #{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. end
  48. end
  49. end
  50. namespace :push do
  51. desc 'Unsubscribes from PuSH updates of feeds nobody follows locally'
  52. task clear: :environment do
  53. Account.remote.without_followers.where.not(subscription_expires_at: nil).find_each do |a|
  54. Rails.logger.debug "PuSH unsubscribing from #{a.acct}"
  55. UnsubscribeService.new.call(a)
  56. end
  57. end
  58. desc 'Re-subscribes to soon expiring PuSH subscriptions'
  59. task refresh: :environment do
  60. Account.expiring(1.day.from_now).find_each do |a|
  61. Rails.logger.debug "PuSH re-subscribing to #{a.acct}"
  62. SubscribeService.new.call(a)
  63. end
  64. end
  65. end
  66. namespace :feeds do
  67. desc 'Clear timelines of inactive users'
  68. task clear: :environment do
  69. User.confirmed.where('current_sign_in_at < ?', 14.days.ago).find_each do |user|
  70. Redis.current.del(FeedManager.instance.key(:home, user.account_id))
  71. end
  72. end
  73. desc 'Clears all timelines'
  74. task clear_all: :environment do
  75. Redis.current.keys('feed:*').each { |key| Redis.current.del(key) }
  76. end
  77. end
  78. namespace :emails do
  79. desc 'Send out digest e-mails'
  80. task digest: :environment do
  81. User.confirmed.joins(:account).where(accounts: { silenced: false, suspended: false }).where('current_sign_in_at < ?', 20.days.ago).find_each do |user|
  82. DigestMailerWorker.perform_async(user.id)
  83. end
  84. end
  85. end
  86. namespace :users do
  87. desc 'Clear out unconfirmed users'
  88. task clear: :environment do
  89. # Users that never confirmed e-mail never signed in, means they
  90. # only have a user record and an avatar record, with no files uploaded
  91. User.where('confirmed_at is NULL AND confirmation_sent_at <= ?', 2.days.ago).find_in_batches do |batch|
  92. Account.where(id: batch.map(&:account_id)).delete_all
  93. User.where(id: batch.map(&:id)).delete_all
  94. end
  95. end
  96. desc 'List all admin users'
  97. task admins: :environment do
  98. puts 'Admin user emails:'
  99. puts User.admins.map(&:email).join("\n")
  100. end
  101. end
  102. namespace :settings do
  103. desc 'Open registrations on this instance'
  104. task open_registrations: :environment do
  105. setting = Setting.where(var: 'open_registrations').first
  106. setting.value = true
  107. setting.save
  108. end
  109. desc 'Close registrations on this instance'
  110. task close_registrations: :environment do
  111. setting = Setting.where(var: 'open_registrations').first
  112. setting.value = false
  113. setting.save
  114. end
  115. end
  116. namespace :maintenance do
  117. desc 'Update counter caches'
  118. task update_counter_caches: :environment do
  119. Rails.logger.debug 'Updating counter caches for accounts...'
  120. Account.unscoped.select('id').find_in_batches do |batch|
  121. 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)')
  122. end
  123. Rails.logger.debug 'Updating counter caches for statuses...'
  124. Status.unscoped.select('id').find_in_batches do |batch|
  125. 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)')
  126. end
  127. Rails.logger.debug 'Done!'
  128. end
  129. desc 'Generate static versions of GIF avatars/headers'
  130. task add_static_avatars: :environment do
  131. Rails.logger.debug 'Generating static avatars/headers for GIF ones...'
  132. Account.unscoped.where(avatar_content_type: 'image/gif').or(Account.unscoped.where(header_content_type: 'image/gif')).find_each do |account|
  133. begin
  134. account.avatar.reprocess! if account.avatar_content_type == 'image/gif' && !account.avatar.exists?(:static)
  135. account.header.reprocess! if account.header_content_type == 'image/gif' && !account.header.exists?(:static)
  136. rescue StandardError => e
  137. Rails.logger.error "Error while generating static avatars/headers for account #{account.id}: #{e}"
  138. next
  139. end
  140. end
  141. Rails.logger.debug 'Done!'
  142. end
  143. end
  144. end