The code powering m.abunchtell.com https://m.abunchtell.com
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.
 
 
 
 

298 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')
  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.type = :unknown
  74. media.save
  75. end
  76. end
  77. desc 'Set unknown attachment type for remote-only attachments'
  78. task set_unknown: :environment do
  79. Rails.logger.debug 'Setting unknown attachment type for remote-only attachments...'
  80. MediaAttachment.where(file_file_name: nil).where.not(type: :unknown).in_batches.update_all(type: :unknown)
  81. Rails.logger.debug 'Done!'
  82. end
  83. desc 'Redownload avatars/headers of remote users. Optionally limit to a particular domain with DOMAIN'
  84. task redownload_avatars: :environment do
  85. accounts = Account.remote
  86. accounts = accounts.where(domain: ENV['DOMAIN']) if ENV['DOMAIN'].present?
  87. accounts.find_each do |account|
  88. account.reset_avatar!
  89. account.reset_header!
  90. account.save
  91. end
  92. end
  93. end
  94. namespace :push do
  95. desc 'Unsubscribes from PuSH updates of feeds nobody follows locally'
  96. task clear: :environment do
  97. Pubsubhubbub::UnsubscribeWorker.push_bulk(Account.remote.without_followers.where.not(subscription_expires_at: nil).pluck(:id))
  98. end
  99. desc 'Re-subscribes to soon expiring PuSH subscriptions (deprecated)'
  100. task refresh: :environment do
  101. # No-op
  102. # This task is now executed via sidekiq-scheduler
  103. end
  104. end
  105. namespace :feeds do
  106. desc 'Clear timelines of inactive users (deprecated)'
  107. task clear: :environment do
  108. # No-op
  109. # This task is now executed via sidekiq-scheduler
  110. end
  111. desc 'Clear all timelines without regenerating them'
  112. task clear_all: :environment do
  113. Redis.current.keys('feed:*').each { |key| Redis.current.del(key) }
  114. end
  115. desc 'Generates home timelines for users who logged in in the past two weeks'
  116. task build: :environment do
  117. User.active.includes(:account).find_each do |u|
  118. PrecomputeFeedService.new.call(u.account)
  119. end
  120. end
  121. end
  122. namespace :emails do
  123. desc 'Send out digest e-mails'
  124. task digest: :environment do
  125. User.confirmed.joins(:account).where(accounts: { silenced: false, suspended: false }).where('current_sign_in_at < ?', 20.days.ago).find_each do |user|
  126. DigestMailerWorker.perform_async(user.id)
  127. end
  128. end
  129. end
  130. namespace :users do
  131. desc 'Clear out unconfirmed users (deprecated)'
  132. task clear: :environment do
  133. # No-op
  134. # This task is now executed via sidekiq-scheduler
  135. end
  136. desc 'List e-mails of all admin users'
  137. task admins: :environment do
  138. puts 'Admin user emails:'
  139. puts User.admins.map(&:email).join("\n")
  140. end
  141. end
  142. namespace :settings do
  143. desc 'Open registrations on this instance'
  144. task open_registrations: :environment do
  145. Setting.open_registrations = true
  146. end
  147. desc 'Close registrations on this instance'
  148. task close_registrations: :environment do
  149. Setting.open_registrations = false
  150. end
  151. end
  152. namespace :webpush do
  153. desc 'Generate VAPID key'
  154. task generate_vapid_key: :environment do
  155. vapid_key = Webpush.generate_key
  156. puts "VAPID_PRIVATE_KEY=#{vapid_key.private_key}"
  157. puts "VAPID_PUBLIC_KEY=#{vapid_key.public_key}"
  158. end
  159. end
  160. namespace :maintenance do
  161. desc 'Update counter caches'
  162. task update_counter_caches: :environment do
  163. Rails.logger.debug 'Updating counter caches for accounts...'
  164. Account.unscoped.select('id').find_in_batches do |batch|
  165. 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)')
  166. end
  167. Rails.logger.debug 'Updating counter caches for statuses...'
  168. Status.unscoped.select('id').find_in_batches do |batch|
  169. 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)')
  170. end
  171. Rails.logger.debug 'Done!'
  172. end
  173. desc 'Generate static versions of GIF avatars/headers'
  174. task add_static_avatars: :environment do
  175. Rails.logger.debug 'Generating static avatars/headers for GIF ones...'
  176. Account.unscoped.where(avatar_content_type: 'image/gif').or(Account.unscoped.where(header_content_type: 'image/gif')).find_each do |account|
  177. begin
  178. account.avatar.reprocess! if account.avatar_content_type == 'image/gif' && !account.avatar.exists?(:static)
  179. account.header.reprocess! if account.header_content_type == 'image/gif' && !account.header.exists?(:static)
  180. rescue StandardError => e
  181. Rails.logger.error "Error while generating static avatars/headers for account #{account.id}: #{e}"
  182. next
  183. end
  184. end
  185. Rails.logger.debug 'Done!'
  186. end
  187. desc 'Ensure referencial integrity'
  188. task prepare_for_foreign_keys: :environment do
  189. # All the deletes:
  190. 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')
  191. if ActiveRecord::Base.connection.table_exists? :account_domain_blocks
  192. 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')
  193. end
  194. if ActiveRecord::Base.connection.table_exists? :conversation_mutes
  195. 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')
  196. 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')
  197. end
  198. 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')
  199. 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')
  200. 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')
  201. 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')
  202. 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')
  203. 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')
  204. 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')
  205. 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')
  206. 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')
  207. 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')
  208. 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')
  209. 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')
  210. 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')
  211. 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')
  212. 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')
  213. 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')
  214. 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')
  215. 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')
  216. 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')
  217. 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')
  218. 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')
  219. 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')
  220. 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')
  221. 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')
  222. 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')
  223. 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')
  224. 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')
  225. 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')
  226. # All the nullifies:
  227. 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')
  228. 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')
  229. 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')
  230. 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')
  231. 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')
  232. end
  233. desc 'Remove deprecated preview cards'
  234. task remove_deprecated_preview_cards: :environment do
  235. return unless ActiveRecord::Base.connection.table_exists? 'deprecated_preview_cards'
  236. class DeprecatedPreviewCard < PreviewCard
  237. self.table_name = 'deprecated_preview_cards'
  238. end
  239. puts 'Delete records and associated files from deprecated preview cards? [y/N]: '
  240. confirm = STDIN.gets.chomp
  241. if confirm.casecmp?('y')
  242. DeprecatedPreviewCard.in_batches.destroy_all
  243. puts 'Drop deprecated preview cards table? [y/N]: '
  244. confirm = STDIN.gets.chomp
  245. if confirm.casecmp?('y')
  246. ActiveRecord::Migration.drop_table :deprecated_preview_cards
  247. end
  248. end
  249. end
  250. end
  251. end