闭社主体 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.

165 lines
5.9 KiB

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