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.

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