mastodon.rake 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844
  1. # frozen_string_literal: true
  2. require 'optparse'
  3. require 'colorize'
  4. require 'tty-command'
  5. require 'tty-prompt'
  6. namespace :mastodon do
  7. desc 'Configure the instance for production use'
  8. task :setup do
  9. prompt = TTY::Prompt.new
  10. env = {}
  11. begin
  12. prompt.say('Your instance is identified by its domain name. Changing it afterward will break things.')
  13. env['LOCAL_DOMAIN'] = prompt.ask('Domain name:') do |q|
  14. q.required true
  15. q.modify :strip
  16. q.validate(/\A[a-z0-9\.\-]+\z/i)
  17. q.messages[:valid?] = 'Invalid domain. If you intend to use unicode characters, enter punycode here'
  18. end
  19. prompt.say "\n"
  20. prompt.say('Single user mode disables registrations and redirects the landing page to your public profile.')
  21. env['SINGLE_USER_MODE'] = prompt.yes?('Do you want to enable single user mode?', default: false)
  22. %w(SECRET_KEY_BASE OTP_SECRET).each do |key|
  23. env[key] = SecureRandom.hex(64)
  24. end
  25. vapid_key = Webpush.generate_key
  26. env['VAPID_PRIVATE_KEY'] = vapid_key.private_key
  27. env['VAPID_PUBLIC_KEY'] = vapid_key.public_key
  28. prompt.say "\n"
  29. using_docker = prompt.yes?('Are you using Docker to run Mastodon?')
  30. db_connection_works = false
  31. prompt.say "\n"
  32. loop do
  33. env['DB_HOST'] = prompt.ask('PostgreSQL host:') do |q|
  34. q.required true
  35. q.default using_docker ? 'db' : '/var/run/postgresql'
  36. q.modify :strip
  37. end
  38. env['DB_PORT'] = prompt.ask('PostgreSQL port:') do |q|
  39. q.required true
  40. q.default 5432
  41. q.convert :int
  42. end
  43. env['DB_NAME'] = prompt.ask('Name of PostgreSQL database:') do |q|
  44. q.required true
  45. q.default using_docker ? 'postgres' : 'mastodon_production'
  46. q.modify :strip
  47. end
  48. env['DB_USER'] = prompt.ask('Name of PostgreSQL user:') do |q|
  49. q.required true
  50. q.default using_docker ? 'postgres' : 'mastodon'
  51. q.modify :strip
  52. end
  53. env['DB_PASS'] = prompt.ask('Password of PostgreSQL user:') do |q|
  54. q.echo false
  55. end
  56. # The chosen database may not exist yet. Connect to default database
  57. # to avoid "database does not exist" error.
  58. db_options = {
  59. adapter: :postgresql,
  60. database: 'postgres',
  61. host: env['DB_HOST'],
  62. port: env['DB_PORT'],
  63. user: env['DB_USER'],
  64. password: env['DB_PASS'],
  65. }
  66. begin
  67. ActiveRecord::Base.establish_connection(db_options)
  68. ActiveRecord::Base.connection
  69. prompt.ok 'Database configuration works! 🎆'
  70. db_connection_works = true
  71. break
  72. rescue StandardError => e
  73. prompt.error 'Database connection could not be established with this configuration, try again.'
  74. prompt.error e.message
  75. break unless prompt.yes?('Try again?')
  76. end
  77. end
  78. prompt.say "\n"
  79. loop do
  80. env['REDIS_HOST'] = prompt.ask('Redis host:') do |q|
  81. q.required true
  82. q.default using_docker ? 'redis' : 'localhost'
  83. q.modify :strip
  84. end
  85. env['REDIS_PORT'] = prompt.ask('Redis port:') do |q|
  86. q.required true
  87. q.default 6379
  88. q.convert :int
  89. end
  90. env['REDIS_PASSWORD'] = prompt.ask('Redis password:') do |q|
  91. q.required false
  92. q.default nil
  93. q.modify :strip
  94. end
  95. redis_options = {
  96. host: env['REDIS_HOST'],
  97. port: env['REDIS_PORT'],
  98. password: env['REDIS_PASSWORD'],
  99. driver: :hiredis,
  100. }
  101. begin
  102. redis = Redis.new(redis_options)
  103. redis.ping
  104. prompt.ok 'Redis configuration works! 🎆'
  105. break
  106. rescue StandardError => e
  107. prompt.error 'Redis connection could not be established with this configuration, try again.'
  108. prompt.error e.message
  109. break unless prompt.yes?('Try again?')
  110. end
  111. end
  112. prompt.say "\n"
  113. if prompt.yes?('Do you want to store uploaded files on the cloud?', default: false)
  114. case prompt.select('Provider', ['Amazon S3', 'Wasabi', 'Minio'])
  115. when 'Amazon S3'
  116. env['S3_ENABLED'] = 'true'
  117. env['S3_PROTOCOL'] = 'https'
  118. env['S3_BUCKET'] = prompt.ask('S3 bucket name:') do |q|
  119. q.required true
  120. q.default "files.#{env['LOCAL_DOMAIN']}"
  121. q.modify :strip
  122. end
  123. env['S3_REGION'] = prompt.ask('S3 region:') do |q|
  124. q.required true
  125. q.default 'us-east-1'
  126. q.modify :strip
  127. end
  128. env['S3_HOSTNAME'] = prompt.ask('S3 hostname:') do |q|
  129. q.required true
  130. q.default 's3-us-east-1.amazonaws.com'
  131. q.modify :strip
  132. end
  133. env['AWS_ACCESS_KEY_ID'] = prompt.ask('S3 access key:') do |q|
  134. q.required true
  135. q.modify :strip
  136. end
  137. env['AWS_SECRET_ACCESS_KEY'] = prompt.ask('S3 secret key:') do |q|
  138. q.required true
  139. q.modify :strip
  140. end
  141. when 'Wasabi'
  142. env['S3_ENABLED'] = 'true'
  143. env['S3_PROTOCOL'] = 'https'
  144. env['S3_REGION'] = 'us-east-1'
  145. env['S3_HOSTNAME'] = 's3.wasabisys.com'
  146. env['S3_ENDPOINT'] = 'https://s3.wasabisys.com/'
  147. env['S3_BUCKET'] = prompt.ask('Wasabi bucket name:') do |q|
  148. q.required true
  149. q.default "files.#{env['LOCAL_DOMAIN']}"
  150. q.modify :strip
  151. end
  152. env['AWS_ACCESS_KEY_ID'] = prompt.ask('Wasabi access key:') do |q|
  153. q.required true
  154. q.modify :strip
  155. end
  156. env['AWS_SECRET_ACCESS_KEY'] = prompt.ask('Wasabi secret key:') do |q|
  157. q.required true
  158. q.modify :strip
  159. end
  160. when 'Minio'
  161. env['S3_ENABLED'] = 'true'
  162. env['S3_PROTOCOL'] = 'https'
  163. env['S3_REGION'] = 'us-east-1'
  164. env['S3_ENDPOINT'] = prompt.ask('Minio endpoint URL:') do |q|
  165. q.required true
  166. q.modify :strip
  167. end
  168. env['S3_PROTOCOL'] = env['S3_ENDPOINT'].start_with?('https') ? 'https' : 'http'
  169. env['S3_HOSTNAME'] = env['S3_ENDPOINT'].gsub(/\Ahttps?:\/\//, '')
  170. env['S3_BUCKET'] = prompt.ask('Minio bucket name:') do |q|
  171. q.required true
  172. q.default "files.#{env['LOCAL_DOMAIN']}"
  173. q.modify :strip
  174. end
  175. env['AWS_ACCESS_KEY_ID'] = prompt.ask('Minio access key:') do |q|
  176. q.required true
  177. q.modify :strip
  178. end
  179. env['AWS_SECRET_ACCESS_KEY'] = prompt.ask('Minio secret key:') do |q|
  180. q.required true
  181. q.modify :strip
  182. end
  183. end
  184. if prompt.yes?('Do you want to access the uploaded files from your own domain?')
  185. env['S3_ALIAS_HOST'] = prompt.ask('Domain for uploaded files:') do |q|
  186. q.required true
  187. q.default "files.#{env['LOCAL_DOMAIN']}"
  188. q.modify :strip
  189. end
  190. end
  191. end
  192. prompt.say "\n"
  193. loop do
  194. if prompt.yes?('Do you want to send e-mails from localhost?', default: false)
  195. env['SMTP_SERVER'] = 'localhost'
  196. env['SMTP_PORT'] = 25
  197. env['SMTP_AUTH_METHOD'] = 'none'
  198. env['SMTP_OPENSSL_VERIFY_MODE'] = 'none'
  199. else
  200. env['SMTP_SERVER'] = prompt.ask('SMTP server:') do |q|
  201. q.required true
  202. q.default 'smtp.mailgun.org'
  203. q.modify :strip
  204. end
  205. env['SMTP_PORT'] = prompt.ask('SMTP port:') do |q|
  206. q.required true
  207. q.default 587
  208. q.convert :int
  209. end
  210. env['SMTP_LOGIN'] = prompt.ask('SMTP username:') do |q|
  211. q.modify :strip
  212. end
  213. env['SMTP_PASSWORD'] = prompt.ask('SMTP password:') do |q|
  214. q.echo false
  215. end
  216. env['SMTP_AUTH_METHOD'] = prompt.ask('SMTP authentication:') do |q|
  217. q.required
  218. q.default 'plain'
  219. q.modify :strip
  220. end
  221. env['SMTP_OPENSSL_VERIFY_MODE'] = prompt.select('SMTP OpenSSL verify mode:', %w(none peer client_once fail_if_no_peer_cert))
  222. end
  223. env['SMTP_FROM_ADDRESS'] = prompt.ask('E-mail address to send e-mails "from":') do |q|
  224. q.required true
  225. q.default "Mastodon <notifications@#{env['LOCAL_DOMAIN']}>"
  226. q.modify :strip
  227. end
  228. break unless prompt.yes?('Send a test e-mail with this configuration right now?')
  229. send_to = prompt.ask('Send test e-mail to:', required: true)
  230. begin
  231. ActionMailer::Base.smtp_settings = {
  232. :port => env['SMTP_PORT'],
  233. :address => env['SMTP_SERVER'],
  234. :user_name => env['SMTP_LOGIN'].presence,
  235. :password => env['SMTP_PASSWORD'].presence,
  236. :domain => env['LOCAL_DOMAIN'],
  237. :authentication => env['SMTP_AUTH_METHOD'] == 'none' ? nil : env['SMTP_AUTH_METHOD'] || :plain,
  238. :openssl_verify_mode => env['SMTP_OPENSSL_VERIFY_MODE'],
  239. :enable_starttls_auto => true,
  240. }
  241. ActionMailer::Base.default_options = {
  242. from: env['SMTP_FROM_ADDRESS'],
  243. }
  244. mail = ActionMailer::Base.new.mail to: send_to, subject: 'Test', body: 'Mastodon SMTP configuration works!'
  245. mail.deliver
  246. break
  247. rescue StandardError => e
  248. prompt.error 'E-mail could not be sent with this configuration, try again.'
  249. prompt.error e.message
  250. break unless prompt.yes?('Try again?')
  251. end
  252. end
  253. prompt.say "\n"
  254. prompt.say 'This configuration will be written to .env.production'
  255. if prompt.yes?('Save configuration?')
  256. cmd = TTY::Command.new(printer: :quiet)
  257. File.write(Rails.root.join('.env.production'), "# Generated with mastodon:setup on #{Time.now.utc}\n\n" + env.each_pair.map { |key, value| "#{key}=#{value}" }.join("\n") + "\n")
  258. if using_docker
  259. prompt.ok 'Below is your configuration, save it to an .env.production file outside Docker:'
  260. prompt.say "\n"
  261. prompt.say File.read(Rails.root.join('.env.production'))
  262. prompt.say "\n"
  263. prompt.ok 'It is also saved within this container so you can proceed with this wizard.'
  264. end
  265. prompt.say "\n"
  266. prompt.say 'Now that configuration is saved, the database schema must be loaded.'
  267. prompt.warn 'If the database already exists, this will erase its contents.'
  268. if prompt.yes?('Prepare the database now?')
  269. prompt.say 'Running `RAILS_ENV=production rails db:setup` ...'
  270. prompt.say "\n"
  271. if cmd.run!({ RAILS_ENV: 'production', SAFETY_ASSURED: 1 }, :rails, 'db:setup').failure?
  272. prompt.say "\n"
  273. prompt.error 'That failed! Perhaps your configuration is not right'
  274. else
  275. prompt.say "\n"
  276. prompt.ok 'Done!'
  277. end
  278. end
  279. prompt.say "\n"
  280. prompt.say 'The final step is compiling CSS/JS assets.'
  281. prompt.say 'This may take a while and consume a lot of RAM.'
  282. if prompt.yes?('Compile the assets now?')
  283. prompt.say 'Running `RAILS_ENV=production rails assets:precompile` ...'
  284. prompt.say "\n"
  285. if cmd.run!({ RAILS_ENV: 'production' }, :rails, 'assets:precompile').failure?
  286. prompt.say "\n"
  287. prompt.error 'That failed! Maybe you need swap space?'
  288. else
  289. prompt.say "\n"
  290. prompt.say 'Done!'
  291. end
  292. end
  293. prompt.say "\n"
  294. prompt.ok 'All done! You can now power on the Mastodon server 🐘'
  295. prompt.say "\n"
  296. if db_connection_works && prompt.yes?('Do you want to create an admin user straight away?')
  297. env.each_pair do |key, value|
  298. ENV[key] = value.to_s
  299. end
  300. require_relative '../../config/environment'
  301. disable_log_stdout!
  302. username = prompt.ask('Username:') do |q|
  303. q.required true
  304. q.default 'admin'
  305. q.validate(/\A[a-z0-9_]+\z/i)
  306. q.modify :strip
  307. end
  308. email = prompt.ask('E-mail:') do |q|
  309. q.required true
  310. q.modify :strip
  311. end
  312. password = SecureRandom.hex(16)
  313. user = User.new(admin: true, email: email, password: password, confirmed_at: Time.now.utc, account_attributes: { username: username })
  314. user.save(validate: false)
  315. prompt.ok "You can login with the password: #{password}"
  316. prompt.warn 'You can change your password once you login.'
  317. end
  318. else
  319. prompt.warn 'Nothing saved. Bye!'
  320. end
  321. rescue TTY::Reader::InputInterrupt
  322. prompt.ok 'Aborting. Bye!'
  323. end
  324. end
  325. desc 'Execute daily tasks (deprecated)'
  326. task :daily do
  327. # No-op
  328. # All of these tasks are now executed via sidekiq-scheduler
  329. end
  330. desc 'Turn a user into an admin, identified by the USERNAME environment variable'
  331. task make_admin: :environment do
  332. include RoutingHelper
  333. account_username = ENV.fetch('USERNAME')
  334. user = User.joins(:account).where(accounts: { username: account_username })
  335. if user.present?
  336. user.update(admin: true)
  337. puts "Congrats! #{account_username} is now an admin. \\o/\nNavigate to #{edit_admin_settings_url} to get started"
  338. else
  339. puts "User could not be found; please make sure an account with the `#{account_username}` username exists."
  340. end
  341. end
  342. desc 'Turn a user into a moderator, identified by the USERNAME environment variable'
  343. task make_mod: :environment do
  344. account_username = ENV.fetch('USERNAME')
  345. user = User.joins(:account).where(accounts: { username: account_username })
  346. if user.present?
  347. user.update(moderator: true)
  348. puts "Congrats! #{account_username} is now a moderator \\o/"
  349. else
  350. puts "User could not be found; please make sure an account with the `#{account_username}` username exists."
  351. end
  352. end
  353. desc 'Remove admin and moderator privileges from user identified by the USERNAME environment variable'
  354. task revoke_staff: :environment do
  355. account_username = ENV.fetch('USERNAME')
  356. user = User.joins(:account).where(accounts: { username: account_username })
  357. if user.present?
  358. user.update(moderator: false, admin: false)
  359. puts "#{account_username} is no longer admin or moderator."
  360. else
  361. puts "User could not be found; please make sure an account with the `#{account_username}` username exists."
  362. end
  363. end
  364. desc 'Manually confirms a user with associated user email address stored in USER_EMAIL environment variable.'
  365. task confirm_email: :environment do
  366. email = ENV.fetch('USER_EMAIL')
  367. user = User.find_by(email: email)
  368. if user
  369. user.update(confirmed_at: Time.now.utc)
  370. puts "#{email} confirmed"
  371. else
  372. abort "#{email} not found"
  373. end
  374. end
  375. desc 'Add a user by providing their email, username and initial password.' \
  376. 'The user will receive a confirmation email, then they must reset their password before logging in.'
  377. task add_user: :environment do
  378. disable_log_stdout!
  379. prompt = TTY::Prompt.new
  380. begin
  381. email = prompt.ask('E-mail:', required: true) do |q|
  382. q.modify :strip
  383. end
  384. username = prompt.ask('Username:', required: true) do |q|
  385. q.modify :strip
  386. end
  387. role = prompt.select('Role:', %w(user moderator admin))
  388. if prompt.yes?('Proceed to create the user?')
  389. user = User.new(email: email, password: SecureRandom.hex, admin: role == 'admin', moderator: role == 'moderator', account_attributes: { username: username })
  390. if user.save
  391. prompt.ok 'User created and confirmation mail sent to the user\'s email address.'
  392. prompt.ok "Here is the random password generated for the user: #{user.password}"
  393. else
  394. prompt.warn 'User was not created because of the following errors:'
  395. user.errors.each do |key, val|
  396. prompt.error "#{key}: #{val}"
  397. end
  398. end
  399. else
  400. prompt.ok 'Aborting. Bye!'
  401. end
  402. rescue TTY::Reader::InputInterrupt
  403. prompt.ok 'Aborting. Bye!'
  404. end
  405. end
  406. namespace :media do
  407. desc 'Removes media attachments that have not been assigned to any status for longer than a day (deprecated)'
  408. task clear: :environment do
  409. # No-op
  410. # This task is now executed via sidekiq-scheduler
  411. end
  412. desc 'Remove media attachments attributed to silenced accounts'
  413. task remove_silenced: :environment do
  414. nb_media_attachments = 0
  415. MediaAttachment.where(account: Account.silenced).select(:id).reorder(nil).find_in_batches do |media_attachments|
  416. nb_media_attachments += media_attachments.length
  417. Maintenance::DestroyMediaWorker.push_bulk(media_attachments.map(&:id))
  418. end
  419. puts "Scheduled the deletion of #{nb_media_attachments} media attachments"
  420. end
  421. desc 'Remove cached remote media attachments that are older than NUM_DAYS. By default 7 (week)'
  422. task remove_remote: :environment do
  423. require_relative '../mastodon/media_cli'
  424. cli = Mastodon::MediaCLI.new([], days: ENV['NUM_DAYS'] || 7)
  425. cli.invoke(:remove)
  426. end
  427. desc 'Set unknown attachment type for remote-only attachments'
  428. task set_unknown: :environment do
  429. puts 'Setting unknown attachment type for remote-only attachments...'
  430. MediaAttachment.where(file_file_name: nil).where.not(type: :unknown).in_batches.update_all(type: :unknown)
  431. puts 'Done!'
  432. end
  433. desc 'Redownload avatars/headers of remote users. Optionally limit to a particular domain with DOMAIN'
  434. task redownload_avatars: :environment do
  435. accounts = Account.remote
  436. accounts = accounts.where(domain: ENV['DOMAIN']) if ENV['DOMAIN'].present?
  437. nb_accounts = 0
  438. accounts.select(:id).reorder(nil).find_in_batches do |accounts_batch|
  439. nb_accounts += accounts_batch.length
  440. Maintenance::RedownloadAccountMediaWorker.push_bulk(accounts_batch.map(&:id))
  441. end
  442. puts "Scheduled the download of avatars/headers for #{nb_accounts} remote users"
  443. end
  444. end
  445. namespace :push do
  446. desc 'Unsubscribes from PuSH updates of feeds nobody follows locally'
  447. task clear: :environment do
  448. Pubsubhubbub::UnsubscribeWorker.push_bulk(Account.remote.without_followers.where.not(subscription_expires_at: nil).pluck(:id))
  449. end
  450. desc 'Re-subscribes to soon expiring PuSH subscriptions (deprecated)'
  451. task refresh: :environment do
  452. # No-op
  453. # This task is now executed via sidekiq-scheduler
  454. end
  455. end
  456. namespace :feeds do
  457. desc 'Clear timelines of inactive users (deprecated)'
  458. task clear: :environment do
  459. # No-op
  460. # This task is now executed via sidekiq-scheduler
  461. end
  462. desc 'Clear all timelines without regenerating them'
  463. task clear_all: :environment do
  464. Redis.current.keys('feed:*').each { |key| Redis.current.del(key) }
  465. end
  466. desc 'Generates home timelines for users who logged in in the past two weeks'
  467. task build: :environment do
  468. User.active.select(:id, :account_id).reorder(nil).find_in_batches do |users|
  469. RegenerationWorker.push_bulk(users.map(&:account_id))
  470. end
  471. end
  472. end
  473. namespace :emails do
  474. desc 'Send out digest e-mails (deprecated)'
  475. task digest: :environment do
  476. # No-op
  477. # This task is now executed via sidekiq-scheduler
  478. end
  479. end
  480. namespace :users do
  481. desc 'Clear out unconfirmed users (deprecated)'
  482. task clear: :environment do
  483. # No-op
  484. # This task is now executed via sidekiq-scheduler
  485. end
  486. desc 'List e-mails of all admin users'
  487. task admins: :environment do
  488. puts 'Admin user emails:'
  489. puts User.admins.map(&:email).join("\n")
  490. end
  491. end
  492. namespace :settings do
  493. desc 'Open registrations on this instance'
  494. task open_registrations: :environment do
  495. Setting.open_registrations = true
  496. end
  497. desc 'Close registrations on this instance'
  498. task close_registrations: :environment do
  499. Setting.open_registrations = false
  500. end
  501. end
  502. namespace :webpush do
  503. desc 'Generate VAPID key'
  504. task generate_vapid_key: :environment do
  505. vapid_key = Webpush.generate_key
  506. puts "VAPID_PRIVATE_KEY=#{vapid_key.private_key}"
  507. puts "VAPID_PUBLIC_KEY=#{vapid_key.public_key}"
  508. end
  509. end
  510. namespace :maintenance do
  511. desc 'Update counter caches'
  512. task update_counter_caches: :environment do
  513. puts 'Updating counter caches for accounts...'
  514. Account.unscoped.where.not(protocol: :activitypub).select('id').find_in_batches do |batch|
  515. 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)')
  516. end
  517. puts 'Updating counter caches for statuses...'
  518. Status.unscoped.select('id').find_in_batches do |batch|
  519. 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)')
  520. end
  521. puts 'Done!'
  522. end
  523. desc 'Generate static versions of GIF avatars/headers'
  524. task add_static_avatars: :environment do
  525. puts 'Generating static avatars/headers for GIF ones...'
  526. Account.unscoped.where(avatar_content_type: 'image/gif').or(Account.unscoped.where(header_content_type: 'image/gif')).find_each do |account|
  527. begin
  528. account.avatar.reprocess! if account.avatar_content_type == 'image/gif' && !account.avatar.exists?(:static)
  529. account.header.reprocess! if account.header_content_type == 'image/gif' && !account.header.exists?(:static)
  530. rescue StandardError => e
  531. Rails.logger.error "Error while generating static avatars/headers for account #{account.id}: #{e}"
  532. next
  533. end
  534. end
  535. puts 'Done!'
  536. end
  537. desc 'Ensure referencial integrity'
  538. task prepare_for_foreign_keys: :environment do
  539. # All the deletes:
  540. 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')
  541. if ActiveRecord::Base.connection.table_exists? :account_domain_blocks
  542. 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')
  543. end
  544. if ActiveRecord::Base.connection.table_exists? :conversation_mutes
  545. 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')
  546. 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')
  547. end
  548. 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')
  549. 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')
  550. 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')
  551. 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')
  552. 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')
  553. 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')
  554. 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')
  555. 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')
  556. 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')
  557. 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')
  558. 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')
  559. 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')
  560. 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')
  561. 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')
  562. 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')
  563. 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')
  564. 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')
  565. 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')
  566. 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')
  567. 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')
  568. 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')
  569. 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')
  570. 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')
  571. 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')
  572. 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')
  573. 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')
  574. 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')
  575. 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')
  576. # All the nullifies:
  577. 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')
  578. 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')
  579. 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')
  580. 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')
  581. 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')
  582. end
  583. desc 'Remove deprecated preview cards'
  584. task remove_deprecated_preview_cards: :environment do
  585. next unless ActiveRecord::Base.connection.table_exists? 'deprecated_preview_cards'
  586. class DeprecatedPreviewCard < ActiveRecord::Base
  587. self.inheritance_column = false
  588. path = '/preview_cards/:attachment/:id_partition/:style/:filename'
  589. if ENV['S3_ENABLED'] != 'true'
  590. path = (ENV['PAPERCLIP_ROOT_PATH'] || ':rails_root/public/system') + path
  591. end
  592. has_attached_file :image, styles: { original: '280x120>' }, convert_options: { all: '-quality 80 -strip' }, path: path
  593. end
  594. puts 'Delete records and associated files from deprecated preview cards? [y/N]: '
  595. confirm = STDIN.gets.chomp
  596. if confirm.casecmp('y').zero?
  597. DeprecatedPreviewCard.in_batches.destroy_all
  598. puts 'Drop deprecated preview cards table? [y/N]: '
  599. confirm = STDIN.gets.chomp
  600. if confirm.casecmp('y').zero?
  601. ActiveRecord::Migration.drop_table :deprecated_preview_cards
  602. end
  603. end
  604. end
  605. desc 'Migrate photo preview cards made before 2.1'
  606. task migrate_photo_preview_cards: :environment do
  607. status_ids = Status.joins(:preview_cards)
  608. .where(preview_cards: { embed_url: '', type: :photo })
  609. .reorder(nil)
  610. .group(:id)
  611. .pluck(:id)
  612. PreviewCard.where(embed_url: '', type: :photo).delete_all
  613. LinkCrawlWorker.push_bulk status_ids
  614. end
  615. desc 'Find case-insensitive username duplicates of local users'
  616. task find_duplicate_usernames: :environment do
  617. include RoutingHelper
  618. disable_log_stdout!
  619. duplicate_masters = Account.find_by_sql('SELECT * FROM accounts WHERE id IN (SELECT min(id) FROM accounts WHERE domain IS NULL GROUP BY lower(username) HAVING count(*) > 1)')
  620. pastel = Pastel.new
  621. duplicate_masters.each do |account|
  622. puts pastel.yellow("First of their name: ") + pastel.bold(account.username) + " (#{admin_account_url(account.id)})"
  623. Account.where('lower(username) = ?', account.username.downcase).where.not(id: account.id).each do |duplicate|
  624. puts " " + pastel.red("Duplicate: ") + admin_account_url(duplicate.id)
  625. end
  626. end
  627. end
  628. desc 'Remove all home feed regeneration markers'
  629. task remove_regeneration_markers: :environment do
  630. keys = Redis.current.keys('account:*:regeneration')
  631. Redis.current.pipelined do
  632. keys.each { |key| Redis.current.del(key) }
  633. end
  634. end
  635. desc 'Check every known remote account and delete those that no longer exist in origin'
  636. task purge_removed_accounts: :environment do
  637. prepare_for_options!
  638. options = {}
  639. OptionParser.new do |opts|
  640. opts.banner = 'Usage: rails mastodon:maintenance:purge_removed_accounts [options]'
  641. opts.on('-f', '--force', 'Remove all encountered accounts without asking for confirmation') do
  642. options[:force] = true
  643. end
  644. opts.on('-h', '--help', 'Display this message') do
  645. puts opts
  646. exit
  647. end
  648. end.parse!
  649. disable_log_stdout!
  650. total = Account.remote.where(protocol: :activitypub).count
  651. progress_bar = ProgressBar.create(total: total, format: '%c/%C |%w>%i| %e')
  652. Account.remote.where(protocol: :activitypub).partitioned.find_each do |account|
  653. progress_bar.increment
  654. begin
  655. code = Request.new(:head, account.uri).perform(&:code)
  656. rescue StandardError
  657. # This could happen due to network timeout, DNS timeout, wrong SSL cert, etc,
  658. # which should probably not lead to perceiving the account as deleted, so
  659. # just skip till next time
  660. next
  661. end
  662. if [404, 410].include?(code)
  663. if options[:force]
  664. SuspendAccountService.new.call(account)
  665. account.destroy
  666. else
  667. progress_bar.pause
  668. progress_bar.clear
  669. print "\nIt seems like #{account.acct} no longer exists. Purge the account from the database? [Y/n]: ".colorize(:yellow)
  670. confirm = STDIN.gets.chomp
  671. puts ''
  672. progress_bar.resume
  673. if confirm.casecmp('n').zero?
  674. next
  675. else
  676. SuspendAccountService.new.call(account)
  677. account.destroy
  678. end
  679. end
  680. end
  681. end
  682. end
  683. end
  684. end
  685. def disable_log_stdout!
  686. dev_null = Logger.new('/dev/null')
  687. Rails.logger = dev_null
  688. ActiveRecord::Base.logger = dev_null
  689. HttpLog.configuration.logger = dev_null
  690. Paperclip.options[:log] = false
  691. end
  692. def prepare_for_options!
  693. 2.times { ARGV.shift }
  694. end