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.

96 lines
3.5 KiB

  1. # frozen_string_literal: true
  2. namespace :mastodon do
  3. task make_admin: :environment do
  4. include RoutingHelper
  5. user = Account.find_local(ENV.fetch('USERNAME')).user
  6. user.update(admin: true)
  7. puts "Congrats! #{user.account.username} is now an admin. \\o/\nNavigate to #{admin_settings_url} to get started"
  8. end
  9. desc 'Manually confirms a user with associated user email address stored in USER_EMAIL environment variable.'
  10. task confirm_email: :environment do
  11. email = ENV.fetch('USER_EMAIL')
  12. user = User.where(email: email).first
  13. if user
  14. user.update(confirmed_at: Time.now.utc)
  15. puts "User #{email} confirmed."
  16. else
  17. abort "User #{email} not found."
  18. end
  19. end
  20. namespace :media do
  21. desc 'Removes media attachments that have not been assigned to any status for longer than a day'
  22. task clear: :environment do
  23. MediaAttachment.where(status_id: nil).where('created_at < ?', 1.day.ago).find_each(&:destroy)
  24. end
  25. desc 'Remove media attachments attributed to silenced accounts'
  26. task remove_silenced: :environment do
  27. MediaAttachment.where(account: Account.silenced).find_each(&:destroy)
  28. end
  29. end
  30. namespace :push do
  31. desc 'Unsubscribes from PuSH updates of feeds nobody follows locally'
  32. task clear: :environment do
  33. Account.remote.without_followers.where.not(subscription_expires_at: nil).find_each do |a|
  34. Rails.logger.debug "PuSH unsubscribing from #{a.acct}"
  35. UnsubscribeService.new.call(a)
  36. end
  37. end
  38. desc 'Re-subscribes to soon expiring PuSH subscriptions'
  39. task refresh: :environment do
  40. Account.expiring(1.day.from_now).find_each do |a|
  41. Rails.logger.debug "PuSH re-subscribing to #{a.acct}"
  42. SubscribeService.new.call(a)
  43. end
  44. end
  45. end
  46. namespace :feeds do
  47. desc 'Clear timelines of inactive users'
  48. task clear: :environment do
  49. User.confirmed.where('current_sign_in_at < ?', 14.days.ago).find_each do |user|
  50. Redis.current.del(FeedManager.instance.key(:home, user.account_id))
  51. end
  52. end
  53. desc 'Clears all timelines so that they would be regenerated on next hit'
  54. task clear_all: :environment do
  55. Redis.current.keys('feed:*').each { |key| Redis.current.del(key) }
  56. end
  57. end
  58. namespace :emails do
  59. desc 'Send out digest e-mails'
  60. task digest: :environment do
  61. User.confirmed.joins(:account).where(accounts: { silenced: false, suspended: false }).where('current_sign_in_at < ?', 20.days.ago).find_each do |user|
  62. DigestMailerWorker.perform_async(user.id)
  63. end
  64. end
  65. end
  66. namespace :maintenance do
  67. desc 'Update counter caches'
  68. task update_counter_caches: :environment do
  69. Rails.logger.debug 'Updating counter caches for accounts...'
  70. Account.unscoped.select('id').find_in_batches do |batch|
  71. 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)')
  72. end
  73. Rails.logger.debug 'Updating counter caches for statuses...'
  74. Status.unscoped.select('id').find_in_batches do |batch|
  75. 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)')
  76. end
  77. Rails.logger.debug 'Done!'
  78. end
  79. end
  80. end