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.

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