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.

303 lines
16 KiB

  1. # frozen_string_literal: true
  2. namespace :mastodon do
  3. desc 'Execute daily tasks (deprecated)'
  4. task :daily do
  5. # No-op
  6. # All of these tasks are now executed via sidekiq-scheduler
  7. end
  8. desc 'Turn a user into an admin, identified by the USERNAME environment variable'
  9. task make_admin: :environment do
  10. include RoutingHelper
  11. account_username = ENV.fetch('USERNAME')
  12. user = User.joins(:account).where(accounts: { username: account_username })
  13. if user.present?
  14. user.update(admin: true)
  15. puts "Congrats! #{account_username} is now an admin. \\o/\nNavigate to #{edit_admin_settings_url} to get started"
  16. else
  17. puts "User could not be found; please make sure an Account with the `#{account_username}` username exists."
  18. end
  19. end
  20. desc 'Manually confirms a user with associated user email address stored in USER_EMAIL environment variable.'
  21. task confirm_email: :environment do
  22. email = ENV.fetch('USER_EMAIL')
  23. user = User.find_by(email: email)
  24. if user
  25. user.update(confirmed_at: Time.now.utc)
  26. puts "#{email} confirmed"
  27. else
  28. abort "#{email} not found"
  29. end
  30. end
  31. desc 'Add a user by providing their email, username and initial password.' \
  32. 'The user will receive a confirmation email, then they must reset their password before logging in.'
  33. task add_user: :environment do
  34. print 'Enter email: '
  35. email = STDIN.gets.chomp
  36. print 'Enter username: '
  37. username = STDIN.gets.chomp
  38. print 'Create user and send them confirmation mail [y/N]: '
  39. confirm = STDIN.gets.chomp
  40. puts
  41. if confirm.casecmp('y').zero?
  42. password = SecureRandom.hex
  43. user = User.new(email: email, password: password, account_attributes: { username: username })
  44. if user.save
  45. puts 'User added and confirmation mail sent to user\'s email address.'
  46. puts "Here is the random password generated for the user: #{password}"
  47. else
  48. puts 'Following errors occured while creating new user:'
  49. user.errors.each do |key, val|
  50. puts "#{key}: #{val}"
  51. end
  52. end
  53. else
  54. puts 'Aborted by user.'
  55. end
  56. puts
  57. end
  58. namespace :media do
  59. desc 'Removes media attachments that have not been assigned to any status for longer than a day (deprecated)'
  60. task clear: :environment do
  61. # No-op
  62. # This task is now executed via sidekiq-scheduler
  63. end
  64. desc 'Remove media attachments attributed to silenced accounts'
  65. task remove_silenced: :environment do
  66. MediaAttachment.where(account: Account.silenced).find_each(&:destroy)
  67. end
  68. desc 'Remove cached remote media attachments that are older than NUM_DAYS. By default 7 (week)'
  69. task remove_remote: :environment do
  70. time_ago = ENV.fetch('NUM_DAYS') { 7 }.to_i.days.ago
  71. MediaAttachment.where.not(remote_url: '').where('created_at < ?', time_ago).find_each do |media|
  72. media.file.destroy
  73. media.save
  74. end
  75. end
  76. desc 'Set unknown attachment type for remote-only attachments'
  77. task set_unknown: :environment do
  78. puts 'Setting unknown attachment type for remote-only attachments...'
  79. MediaAttachment.where(file_file_name: nil).where.not(type: :unknown).in_batches.update_all(type: :unknown)
  80. puts 'Done!'
  81. end
  82. desc 'Redownload avatars/headers of remote users. Optionally limit to a particular domain with DOMAIN'
  83. task redownload_avatars: :environment do
  84. accounts = Account.remote
  85. accounts = accounts.where(domain: ENV['DOMAIN']) if ENV['DOMAIN'].present?
  86. accounts.find_each do |account|
  87. account.reset_avatar!
  88. account.reset_header!
  89. account.save
  90. end
  91. end
  92. end
  93. namespace :push do
  94. desc 'Unsubscribes from PuSH updates of feeds nobody follows locally'
  95. task clear: :environment do
  96. Pubsubhubbub::UnsubscribeWorker.push_bulk(Account.remote.without_followers.where.not(subscription_expires_at: nil).pluck(:id))
  97. end
  98. desc 'Re-subscribes to soon expiring PuSH subscriptions (deprecated)'
  99. task refresh: :environment do
  100. # No-op
  101. # This task is now executed via sidekiq-scheduler
  102. end
  103. end
  104. namespace :feeds do
  105. desc 'Clear timelines of inactive users (deprecated)'
  106. task clear: :environment do
  107. # No-op
  108. # This task is now executed via sidekiq-scheduler
  109. end
  110. desc 'Clear all timelines without regenerating them'
  111. task clear_all: :environment do
  112. Redis.current.keys('feed:*').each { |key| Redis.current.del(key) }
  113. end
  114. desc 'Generates home timelines for users who logged in in the past two weeks'
  115. task build: :environment do
  116. User.active.includes(:account).find_each do |u|
  117. PrecomputeFeedService.new.call(u.account)
  118. end
  119. end
  120. end
  121. namespace :emails do
  122. desc 'Send out digest e-mails'
  123. task digest: :environment do
  124. User.confirmed.joins(:account).where(accounts: { silenced: false, suspended: false }).where('current_sign_in_at < ?', 20.days.ago).find_each do |user|
  125. DigestMailerWorker.perform_async(user.id)
  126. end
  127. end
  128. end
  129. namespace :users do
  130. desc 'Clear out unconfirmed users (deprecated)'
  131. task clear: :environment do
  132. # No-op
  133. # This task is now executed via sidekiq-scheduler
  134. end
  135. desc 'List e-mails of all admin users'
  136. task admins: :environment do
  137. puts 'Admin user emails:'
  138. puts User.admins.map(&:email).join("\n")
  139. end
  140. end
  141. namespace :settings do
  142. desc 'Open registrations on this instance'
  143. task open_registrations: :environment do
  144. Setting.open_registrations = true
  145. end
  146. desc 'Close registrations on this instance'
  147. task close_registrations: :environment do
  148. Setting.open_registrations = false
  149. end
  150. end
  151. namespace :webpush do
  152. desc 'Generate VAPID key'
  153. task generate_vapid_key: :environment do
  154. vapid_key = Webpush.generate_key
  155. puts "VAPID_PRIVATE_KEY=#{vapid_key.private_key}"
  156. puts "VAPID_PUBLIC_KEY=#{vapid_key.public_key}"
  157. end
  158. end
  159. namespace :maintenance do
  160. desc 'Update counter caches'
  161. task update_counter_caches: :environment do
  162. puts 'Updating counter caches for accounts...'
  163. Account.unscoped.where.not(protocol: :activitypub).select('id').find_in_batches do |batch|
  164. 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)')
  165. end
  166. puts 'Updating counter caches for statuses...'
  167. Status.unscoped.select('id').find_in_batches do |batch|
  168. 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)')
  169. end
  170. puts 'Done!'
  171. end
  172. desc 'Generate static versions of GIF avatars/headers'
  173. task add_static_avatars: :environment do
  174. puts 'Generating static avatars/headers for GIF ones...'
  175. Account.unscoped.where(avatar_content_type: 'image/gif').or(Account.unscoped.where(header_content_type: 'image/gif')).find_each do |account|
  176. begin
  177. account.avatar.reprocess! if account.avatar_content_type == 'image/gif' && !account.avatar.exists?(:static)
  178. account.header.reprocess! if account.header_content_type == 'image/gif' && !account.header.exists?(:static)
  179. rescue StandardError => e
  180. Rails.logger.error "Error while generating static avatars/headers for account #{account.id}: #{e}"
  181. next
  182. end
  183. end
  184. puts 'Done!'
  185. end
  186. desc 'Ensure referencial integrity'
  187. task prepare_for_foreign_keys: :environment do
  188. # All the deletes:
  189. 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')
  190. if ActiveRecord::Base.connection.table_exists? :account_domain_blocks
  191. 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')
  192. end
  193. if ActiveRecord::Base.connection.table_exists? :conversation_mutes
  194. 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')
  195. 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')
  196. end
  197. 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')
  198. 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')
  199. 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')
  200. 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')
  201. 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')
  202. 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')
  203. 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')
  204. 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')
  205. 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')
  206. 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')
  207. 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')
  208. 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')
  209. 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')
  210. 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')
  211. 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')
  212. 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')
  213. 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')
  214. 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')
  215. 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')
  216. 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')
  217. 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')
  218. 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')
  219. 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')
  220. 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')
  221. 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')
  222. 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')
  223. 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')
  224. 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')
  225. # All the nullifies:
  226. 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')
  227. 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')
  228. 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')
  229. 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')
  230. 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')
  231. end
  232. desc 'Remove deprecated preview cards'
  233. task remove_deprecated_preview_cards: :environment do
  234. next unless ActiveRecord::Base.connection.table_exists? 'deprecated_preview_cards'
  235. class DeprecatedPreviewCard < ActiveRecord::Base
  236. self.inheritance_column = false
  237. path = '/preview_cards/:attachment/:id_partition/:style/:filename'
  238. if ENV['S3_ENABLED'] != 'true'
  239. path = (ENV['PAPERCLIP_ROOT_PATH'] || ':rails_root/public/system') + path
  240. end
  241. has_attached_file :image, styles: { original: '280x120>' }, convert_options: { all: '-quality 80 -strip' }, path: path
  242. end
  243. puts 'Delete records and associated files from deprecated preview cards? [y/N]: '
  244. confirm = STDIN.gets.chomp
  245. if confirm.casecmp('y').zero?
  246. DeprecatedPreviewCard.in_batches.destroy_all
  247. puts 'Drop deprecated preview cards table? [y/N]: '
  248. confirm = STDIN.gets.chomp
  249. if confirm.casecmp('y').zero?
  250. ActiveRecord::Migration.drop_table :deprecated_preview_cards
  251. end
  252. end
  253. end
  254. end
  255. end