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.
 
 
 
 

276 lines
15 KiB

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