20181024224956_migrate_account_conversations.rb 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. class MigrateAccountConversations < ActiveRecord::Migration[5.2]
  2. disable_ddl_transaction!
  3. class Mention < ApplicationRecord
  4. belongs_to :account, inverse_of: :mentions
  5. belongs_to :status, -> { unscope(where: :deleted_at) }
  6. delegate(
  7. :username,
  8. :acct,
  9. to: :account,
  10. prefix: true
  11. )
  12. end
  13. class Notification < ApplicationRecord
  14. belongs_to :account, optional: true
  15. belongs_to :activity, polymorphic: true, optional: true
  16. belongs_to :status, foreign_type: 'Status', foreign_key: 'activity_id', optional: true
  17. belongs_to :mention, foreign_type: 'Mention', foreign_key: 'activity_id', optional: true
  18. def target_status
  19. mention&.status
  20. end
  21. end
  22. class AccountConversation < ApplicationRecord
  23. belongs_to :account
  24. belongs_to :conversation
  25. belongs_to :last_status, -> { unscope(where: :deleted_at) }, class_name: 'Status'
  26. before_validation :set_last_status
  27. class << self
  28. def add_status(recipient, status)
  29. conversation = find_or_initialize_by(account: recipient, conversation_id: status.conversation_id, participant_account_ids: participants_from_status(recipient, status))
  30. return conversation if conversation.status_ids.include?(status.id)
  31. conversation.status_ids << status.id
  32. conversation.unread = status.account_id != recipient.id
  33. conversation.save
  34. conversation
  35. rescue ActiveRecord::StaleObjectError
  36. retry
  37. end
  38. private
  39. def participants_from_status(recipient, status)
  40. ((status.active_mentions.pluck(:account_id) + [status.account_id]).uniq - [recipient.id]).sort
  41. end
  42. end
  43. private
  44. def set_last_status
  45. self.status_ids = status_ids.sort
  46. self.last_status_id = status_ids.last
  47. end
  48. end
  49. def up
  50. if $stdout.isatty
  51. say ''
  52. say 'WARNING: This migration may take a *long* time for large instances'
  53. say 'It will *not* lock tables for any significant time, but it may run'
  54. say 'for a very long time. We will pause for 10 seconds to allow you to'
  55. say 'interrupt this migration if you are not ready.'
  56. say ''
  57. 10.downto(1) do |i|
  58. say "Continuing in #{i} second#{i == 1 ? '' : 's'}...", true
  59. sleep 1
  60. end
  61. end
  62. migrated = 0
  63. last_time = Time.zone.now
  64. local_direct_statuses.includes(:account, mentions: :account).find_each do |status|
  65. AccountConversation.add_status(status.account, status)
  66. migrated += 1
  67. if Time.zone.now - last_time > 1
  68. say_progress(migrated)
  69. last_time = Time.zone.now
  70. end
  71. end
  72. notifications_about_direct_statuses.includes(:account, mention: { status: [:account, mentions: :account] }).find_each do |notification|
  73. AccountConversation.add_status(notification.account, notification.target_status)
  74. migrated += 1
  75. if Time.zone.now - last_time > 1
  76. say_progress(migrated)
  77. last_time = Time.zone.now
  78. end
  79. end
  80. end
  81. def down
  82. end
  83. private
  84. def say_progress(migrated)
  85. say "Migrated #{migrated} rows", true
  86. end
  87. def local_direct_statuses
  88. Status.unscoped.local.where(visibility: :direct)
  89. end
  90. def notifications_about_direct_statuses
  91. Notification.joins('INNER JOIN mentions ON mentions.id = notifications.activity_id INNER JOIN statuses ON statuses.id = mentions.status_id').where(activity_type: 'Mention', statuses: { visibility: :direct })
  92. end
  93. end