logo

mastofe

My custom branche(s) on git.pleroma.social/pleroma/mastofe git clone https://hacktivis.me/git/mastofe.git

mastodon.rake (34652B)


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