闭社主体 forked from https://github.com/tootsuite/mastodon
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.

117 lines
3.4 KiB

  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. say ''
  51. say 'WARNING: This migration may take a *long* time for large instances'
  52. say 'It will *not* lock tables for any significant time, but it may run'
  53. say 'for a very long time. We will pause for 10 seconds to allow you to'
  54. say 'interrupt this migration if you are not ready.'
  55. say ''
  56. 10.downto(1) do |i|
  57. say "Continuing in #{i} second#{i == 1 ? '' : 's'}...", true
  58. sleep 1
  59. end
  60. migrated = 0
  61. last_time = Time.zone.now
  62. local_direct_statuses.includes(:account, mentions: :account).find_each do |status|
  63. AccountConversation.add_status(status.account, status)
  64. migrated += 1
  65. if Time.zone.now - last_time > 1
  66. say_progress(migrated)
  67. last_time = Time.zone.now
  68. end
  69. end
  70. notifications_about_direct_statuses.includes(:account, mention: { status: [:account, mentions: :account] }).find_each do |notification|
  71. AccountConversation.add_status(notification.account, notification.target_status)
  72. migrated += 1
  73. if Time.zone.now - last_time > 1
  74. say_progress(migrated)
  75. last_time = Time.zone.now
  76. end
  77. end
  78. end
  79. def down
  80. end
  81. private
  82. def say_progress(migrated)
  83. say "Migrated #{migrated} rows", true
  84. end
  85. def local_direct_statuses
  86. Status.unscoped.local.where(visibility: :direct)
  87. end
  88. def notifications_about_direct_statuses
  89. 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 })
  90. end
  91. end