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.

256 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'
  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 a week'
  77. task remove_remote: :environment do
  78. MediaAttachment.where.not(remote_url: '').where('created_at < ?', 1.week.ago).find_each do |media|
  79. media.file.destroy
  80. media.type = :unknown
  81. media.save
  82. end
  83. end
  84. desc 'Set unknown attachment type for remote-only attachments'
  85. task set_unknown: :environment do
  86. Rails.logger.debug 'Setting unknown attachment type for remote-only attachments...'
  87. MediaAttachment.where(file_file_name: nil).where.not(type: :unknown).in_batches.update_all(type: :unknown)
  88. Rails.logger.debug 'Done!'
  89. end
  90. end
  91. namespace :push do
  92. desc 'Unsubscribes from PuSH updates of feeds nobody follows locally'
  93. task clear: :environment do
  94. Account.remote.without_followers.where.not(subscription_expires_at: nil).find_each do |a|
  95. Rails.logger.debug "PuSH unsubscribing from #{a.acct}"
  96. UnsubscribeService.new.call(a)
  97. end
  98. end
  99. desc 'Re-subscribes to soon expiring PuSH subscriptions'
  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'
  107. task clear: :environment do
  108. # No-op
  109. # This task is now executed via sidekiq-scheduler
  110. end
  111. desc 'Clears all timelines'
  112. task clear_all: :environment do
  113. Redis.current.keys('feed:*').each { |key| Redis.current.del(key) }
  114. end
  115. end
  116. namespace :emails do
  117. desc 'Send out digest e-mails'
  118. task digest: :environment do
  119. User.confirmed.joins(:account).where(accounts: { silenced: false, suspended: false }).where('current_sign_in_at < ?', 20.days.ago).find_each do |user|
  120. DigestMailerWorker.perform_async(user.id)
  121. end
  122. end
  123. end
  124. namespace :users do
  125. desc 'Clear out unconfirmed users'
  126. task clear: :environment do
  127. # Users that never confirmed e-mail never signed in, means they
  128. # only have a user record and an avatar record, with no files uploaded
  129. User.where('confirmed_at is NULL AND confirmation_sent_at <= ?', 2.days.ago).find_in_batches do |batch|
  130. Account.where(id: batch.map(&:account_id)).delete_all
  131. User.where(id: batch.map(&:id)).delete_all
  132. end
  133. end
  134. desc 'List all admin users'
  135. task admins: :environment do
  136. puts 'Admin user emails:'
  137. puts User.admins.map(&:email).join("\n")
  138. end
  139. end
  140. namespace :settings do
  141. desc 'Open registrations on this instance'
  142. task open_registrations: :environment do
  143. setting = Setting.where(var: 'open_registrations').first
  144. setting.value = true
  145. setting.save
  146. end
  147. desc 'Close registrations on this instance'
  148. task close_registrations: :environment do
  149. setting = Setting.where(var: 'open_registrations').first
  150. setting.value = false
  151. setting.save
  152. end
  153. end
  154. namespace :maintenance do
  155. desc 'Update counter caches'
  156. task update_counter_caches: :environment do
  157. Rails.logger.debug 'Updating counter caches for accounts...'
  158. Account.unscoped.select('id').find_in_batches do |batch|
  159. 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)')
  160. end
  161. Rails.logger.debug 'Updating counter caches for statuses...'
  162. Status.unscoped.select('id').find_in_batches do |batch|
  163. 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)')
  164. end
  165. Rails.logger.debug 'Done!'
  166. end
  167. desc 'Generate static versions of GIF avatars/headers'
  168. task add_static_avatars: :environment do
  169. Rails.logger.debug 'Generating static avatars/headers for GIF ones...'
  170. Account.unscoped.where(avatar_content_type: 'image/gif').or(Account.unscoped.where(header_content_type: 'image/gif')).find_each do |account|
  171. begin
  172. account.avatar.reprocess! if account.avatar_content_type == 'image/gif' && !account.avatar.exists?(:static)
  173. account.header.reprocess! if account.header_content_type == 'image/gif' && !account.header.exists?(:static)
  174. rescue StandardError => e
  175. Rails.logger.error "Error while generating static avatars/headers for account #{account.id}: #{e}"
  176. next
  177. end
  178. end
  179. Rails.logger.debug 'Done!'
  180. end
  181. desc 'Ensure referencial integrity'
  182. task prepare_for_foreign_keys: :environment do
  183. # All the deletes:
  184. 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')
  185. 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')
  186. 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')
  187. 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')
  188. 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')
  189. 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')
  190. 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')
  191. 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')
  192. 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')
  193. 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')
  194. 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')
  195. 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')
  196. 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')
  197. 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')
  198. 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')
  199. 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')
  200. 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')
  201. 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')
  202. 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')
  203. 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')
  204. 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')
  205. 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')
  206. 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')
  207. 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')
  208. 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')
  209. 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')
  210. 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')
  211. 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')
  212. 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')
  213. 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')
  214. 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')
  215. 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')
  216. # All the nullifies:
  217. 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')
  218. 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')
  219. 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')
  220. 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')
  221. 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')
  222. end
  223. end
  224. end