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.

136 lines
4.9 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 :users do
  67. desc 'Clear out unconfirmed users'
  68. task clear: :environment do
  69. # Users that never confirmed e-mail never signed in, means they
  70. # only have a user record and an avatar record, with no files uploaded
  71. User.where('confirmed_at is NULL AND confirmation_sent_at <= ?', 2.days.ago).find_in_batches do |batch|
  72. Account.where(id: batch.map(&:account_id)).delete_all
  73. batch.delete_all
  74. end
  75. end
  76. end
  77. namespace :settings do
  78. desc 'Open registrations on this instance'
  79. task open_registrations: :environment do
  80. setting = Setting.where(var: 'open_registrations').first
  81. setting.value = true
  82. setting.save
  83. end
  84. desc 'Close registrations on this instance'
  85. task close_registrations: :environment do
  86. setting = Setting.where(var: 'open_registrations').first
  87. setting.value = false
  88. setting.save
  89. end
  90. end
  91. namespace :maintenance do
  92. desc 'Update counter caches'
  93. task update_counter_caches: :environment do
  94. Rails.logger.debug 'Updating counter caches for accounts...'
  95. Account.unscoped.select('id').find_in_batches do |batch|
  96. 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)')
  97. end
  98. Rails.logger.debug 'Updating counter caches for statuses...'
  99. Status.unscoped.select('id').find_in_batches do |batch|
  100. 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)')
  101. end
  102. Rails.logger.debug 'Done!'
  103. end
  104. desc 'Generate static versions of GIF avatars/headers'
  105. task add_static_avatars: :environment do
  106. Rails.logger.debug 'Generating static avatars/headers for GIF ones...'
  107. Account.unscoped.where(avatar_content_type: 'image/gif').or(Account.unscoped.where(header_content_type: 'image/gif')).find_each do |account|
  108. account.avatar.reprocess!
  109. account.header.reprocess!
  110. end
  111. Rails.logger.debug 'Done!'
  112. end
  113. end
  114. end