闭社主体 forked from https://github.com/tootsuite/mastodon
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.

93 lines
3.4 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)
  13. user.update(confirmed_at: Time.now.utc)
  14. puts "User #{email} confirmed."
  15. end
  16. namespace :media do
  17. desc 'Removes media attachments that have not been assigned to any status for longer than a day'
  18. task clear: :environment do
  19. MediaAttachment.where(status_id: nil).where('created_at < ?', 1.day.ago).find_each(&:destroy)
  20. end
  21. desc 'Remove media attachments attributed to silenced accounts'
  22. task remove_silenced: :environment do
  23. MediaAttachment.where(account: Account.silenced).find_each(&:destroy)
  24. end
  25. end
  26. namespace :push do
  27. desc 'Unsubscribes from PuSH updates of feeds nobody follows locally'
  28. task clear: :environment do
  29. Account.remote.without_followers.where.not(subscription_expires_at: nil).find_each do |a|
  30. Rails.logger.debug "PuSH unsubscribing from #{a.acct}"
  31. UnsubscribeService.new.call(a)
  32. end
  33. end
  34. desc 'Re-subscribes to soon expiring PuSH subscriptions'
  35. task refresh: :environment do
  36. Account.expiring(1.day.from_now).find_each do |a|
  37. Rails.logger.debug "PuSH re-subscribing to #{a.acct}"
  38. SubscribeService.new.call(a)
  39. end
  40. end
  41. end
  42. namespace :feeds do
  43. desc 'Clear timelines of inactive users'
  44. task clear: :environment do
  45. User.confirmed.where('current_sign_in_at < ?', 14.days.ago).find_each do |user|
  46. Redis.current.del(FeedManager.instance.key(:home, user.account_id))
  47. end
  48. end
  49. desc 'Clears all timelines so that they would be regenerated on next hit'
  50. task clear_all: :environment do
  51. Redis.current.keys('feed:*').each { |key| Redis.current.del(key) }
  52. end
  53. end
  54. namespace :emails do
  55. desc 'Send out digest e-mails'
  56. task digest: :environment do
  57. User.confirmed.joins(:account).where(accounts: { silenced: false, suspended: false }).where('current_sign_in_at < ?', 20.days.ago).find_each do |user|
  58. DigestMailerWorker.perform_async(user.id)
  59. end
  60. end
  61. end
  62. namespace :maintenance do
  63. desc 'Update counter caches'
  64. task update_counter_caches: :environment do
  65. Rails.logger.debug 'Updating counter caches for accounts...'
  66. Account.unscoped.select('id').find_in_batches do |batch|
  67. 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)')
  68. end
  69. Rails.logger.debug 'Updating counter caches for statuses...'
  70. Status.unscoped.select('id').find_in_batches do |batch|
  71. 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)')
  72. end
  73. Rails.logger.debug 'Done!'
  74. end
  75. end
  76. end