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.

225 lines
14 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. desc 'Ensure referencial integrity'
  155. task prepare_for_foreign_keys: :environment do
  156. # All the deletes:
  157. ActiveRecord::Base.connection.execute('DELETE FROM statuses USING statuses s LEFT JOIN accounts a ON a.id = s.account_id WHERE statuses.id = s.id AND a.id IS NULL')
  158. ActiveRecord::Base.connection.execute('DELETE FROM account_domain_blocks USING account_domain_blocks adb LEFT JOIN accounts a ON a.id = adb.account_id WHERE account_domain_blocks.id = adb.id AND a.id IS NULL')
  159. ActiveRecord::Base.connection.execute('DELETE FROM conversation_mutes USING conversation_mutes cm LEFT JOIN accounts a ON a.id = cm.account_id WHERE conversation_mutes.id = cm.id AND a.id IS NULL')
  160. ActiveRecord::Base.connection.execute('DELETE FROM conversation_mutes USING conversation_mutes cm LEFT JOIN conversations c ON c.id = cm.conversation_id WHERE conversation_mutes.id = cm.id AND c.id IS NULL')
  161. ActiveRecord::Base.connection.execute('DELETE FROM favourites USING favourites f LEFT JOIN accounts a ON a.id = f.account_id WHERE favourites.id = f.id AND a.id IS NULL')
  162. ActiveRecord::Base.connection.execute('DELETE FROM favourites USING favourites f LEFT JOIN statuses s ON s.id = f.status_id WHERE favourites.id = f.id AND s.id IS NULL')
  163. ActiveRecord::Base.connection.execute('DELETE FROM blocks USING blocks b LEFT JOIN accounts a ON a.id = b.account_id WHERE blocks.id = b.id AND a.id IS NULL')
  164. ActiveRecord::Base.connection.execute('DELETE FROM blocks USING blocks b LEFT JOIN accounts a ON a.id = b.target_account_id WHERE blocks.id = b.id AND a.id IS NULL')
  165. ActiveRecord::Base.connection.execute('DELETE FROM follow_requests USING follow_requests fr LEFT JOIN accounts a ON a.id = fr.account_id WHERE follow_requests.id = fr.id AND a.id IS NULL')
  166. ActiveRecord::Base.connection.execute('DELETE FROM follow_requests USING follow_requests fr LEFT JOIN accounts a ON a.id = fr.target_account_id WHERE follow_requests.id = fr.id AND a.id IS NULL')
  167. ActiveRecord::Base.connection.execute('DELETE FROM follows USING follows f LEFT JOIN accounts a ON a.id = f.account_id WHERE follows.id = f.id AND a.id IS NULL')
  168. ActiveRecord::Base.connection.execute('DELETE FROM follows USING follows f LEFT JOIN accounts a ON a.id = f.target_account_id WHERE follows.id = f.id AND a.id IS NULL')
  169. ActiveRecord::Base.connection.execute('DELETE FROM mutes USING mutes m LEFT JOIN accounts a ON a.id = m.account_id WHERE mutes.id = m.id AND a.id IS NULL')
  170. ActiveRecord::Base.connection.execute('DELETE FROM mutes USING mutes m LEFT JOIN accounts a ON a.id = m.target_account_id WHERE mutes.id = m.id AND a.id IS NULL')
  171. ActiveRecord::Base.connection.execute('DELETE FROM imports USING imports i LEFT JOIN accounts a ON a.id = i.account_id WHERE imports.id = i.id AND a.id IS NULL')
  172. ActiveRecord::Base.connection.execute('DELETE FROM mentions USING mentions m LEFT JOIN accounts a ON a.id = m.account_id WHERE mentions.id = m.id AND a.id IS NULL')
  173. ActiveRecord::Base.connection.execute('DELETE FROM mentions USING mentions m LEFT JOIN statuses s ON s.id = m.status_id WHERE mentions.id = m.id AND s.id IS NULL')
  174. ActiveRecord::Base.connection.execute('DELETE FROM notifications USING notifications n LEFT JOIN accounts a ON a.id = n.account_id WHERE notifications.id = n.id AND a.id IS NULL')
  175. ActiveRecord::Base.connection.execute('DELETE FROM notifications USING notifications n LEFT JOIN accounts a ON a.id = n.from_account_id WHERE notifications.id = n.id AND a.id IS NULL')
  176. ActiveRecord::Base.connection.execute('DELETE FROM preview_cards USING preview_cards pc LEFT JOIN statuses s ON s.id = pc.status_id WHERE preview_cards.id = pc.id AND s.id IS NULL')
  177. ActiveRecord::Base.connection.execute('DELETE FROM reports USING reports r LEFT JOIN accounts a ON a.id = r.account_id WHERE reports.id = r.id AND a.id IS NULL')
  178. ActiveRecord::Base.connection.execute('DELETE FROM reports USING reports r LEFT JOIN accounts a ON a.id = r.target_account_id WHERE reports.id = r.id AND a.id IS NULL')
  179. ActiveRecord::Base.connection.execute('DELETE FROM statuses_tags USING statuses_tags st LEFT JOIN statuses s ON s.id = st.status_id WHERE statuses_tags.tag_id = st.tag_id AND statuses_tags.status_id = st.status_id AND s.id IS NULL')
  180. ActiveRecord::Base.connection.execute('DELETE FROM statuses_tags USING statuses_tags st LEFT JOIN tags t ON t.id = st.tag_id WHERE statuses_tags.tag_id = st.tag_id AND statuses_tags.status_id = st.status_id AND t.id IS NULL')
  181. ActiveRecord::Base.connection.execute('DELETE FROM stream_entries USING stream_entries se LEFT JOIN accounts a ON a.id = se.account_id WHERE stream_entries.id = se.id AND a.id IS NULL')
  182. ActiveRecord::Base.connection.execute('DELETE FROM subscriptions USING subscriptions s LEFT JOIN accounts a ON a.id = s.account_id WHERE subscriptions.id = s.id AND a.id IS NULL')
  183. ActiveRecord::Base.connection.execute('DELETE FROM users USING users u LEFT JOIN accounts a ON a.id = u.account_id WHERE users.id = u.id AND a.id IS NULL')
  184. ActiveRecord::Base.connection.execute('DELETE FROM web_settings USING web_settings ws LEFT JOIN users u ON u.id = ws.user_id WHERE web_settings.id = ws.id AND u.id IS NULL')
  185. ActiveRecord::Base.connection.execute('DELETE FROM oauth_access_grants USING oauth_access_grants oag LEFT JOIN users u ON u.id = oag.resource_owner_id WHERE oauth_access_grants.id = oag.id AND oag.resource_owner_id IS NOT NULL AND u.id IS NULL')
  186. ActiveRecord::Base.connection.execute('DELETE FROM oauth_access_grants USING oauth_access_grants oag LEFT JOIN oauth_applications a ON a.id = oag.application_id WHERE oauth_access_grants.id = oag.id AND oag.application_id IS NOT NULL AND a.id IS NULL')
  187. ActiveRecord::Base.connection.execute('DELETE FROM oauth_access_tokens USING oauth_access_tokens oat LEFT JOIN users u ON u.id = oat.resource_owner_id WHERE oauth_access_tokens.id = oat.id AND oat.resource_owner_id IS NOT NULL AND u.id IS NULL')
  188. ActiveRecord::Base.connection.execute('DELETE FROM oauth_access_tokens USING oauth_access_tokens oat LEFT JOIN oauth_applications a ON a.id = oat.application_id WHERE oauth_access_tokens.id = oat.id AND oat.application_id IS NOT NULL AND a.id IS NULL')
  189. # All the nullifies:
  190. ActiveRecord::Base.connection.execute('UPDATE statuses SET in_reply_to_id = NULL FROM statuses s LEFT JOIN statuses rs ON rs.id = s.in_reply_to_id WHERE statuses.id = s.id AND s.in_reply_to_id IS NOT NULL AND rs.id IS NULL')
  191. ActiveRecord::Base.connection.execute('UPDATE statuses SET in_reply_to_account_id = NULL FROM statuses s LEFT JOIN accounts a ON a.id = s.in_reply_to_account_id WHERE statuses.id = s.id AND s.in_reply_to_account_id IS NOT NULL AND a.id IS NULL')
  192. ActiveRecord::Base.connection.execute('UPDATE media_attachments SET status_id = NULL FROM media_attachments ma LEFT JOIN statuses s ON s.id = ma.status_id WHERE media_attachments.id = ma.id AND ma.status_id IS NOT NULL AND s.id IS NULL')
  193. ActiveRecord::Base.connection.execute('UPDATE media_attachments SET account_id = NULL FROM media_attachments ma LEFT JOIN accounts a ON a.id = ma.account_id WHERE media_attachments.id = ma.id AND ma.account_id IS NOT NULL AND a.id IS NULL')
  194. ActiveRecord::Base.connection.execute('UPDATE reports SET action_taken_by_account_id = NULL FROM reports r LEFT JOIN accounts a ON a.id = r.action_taken_by_account_id WHERE reports.id = r.id AND r.action_taken_by_account_id IS NOT NULL AND a.id IS NULL')
  195. end
  196. end
  197. end