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.

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