闭社主体 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.

116 lines
3.3 KiB

  1. # frozen_string_literal: true
  2. # == Schema Information
  3. #
  4. # Table name: account_conversations
  5. #
  6. # id :bigint(8) not null, primary key
  7. # account_id :bigint(8)
  8. # conversation_id :bigint(8)
  9. # participant_account_ids :bigint(8) default([]), not null, is an Array
  10. # status_ids :bigint(8) default([]), not null, is an Array
  11. # last_status_id :bigint(8)
  12. # lock_version :integer default(0), not null
  13. # unread :boolean default(FALSE), not null
  14. #
  15. class AccountConversation < ApplicationRecord
  16. after_commit :push_to_streaming_api
  17. belongs_to :account
  18. belongs_to :conversation
  19. belongs_to :last_status, class_name: 'Status'
  20. before_validation :set_last_status
  21. def participant_account_ids=(arr)
  22. self[:participant_account_ids] = arr.sort
  23. end
  24. def participant_accounts
  25. if participant_account_ids.empty?
  26. [account]
  27. else
  28. Account.where(id: participant_account_ids)
  29. end
  30. end
  31. class << self
  32. def paginate_by_id(limit, options = {})
  33. if options[:min_id]
  34. paginate_by_min_id(limit, options[:min_id]).reverse
  35. else
  36. paginate_by_max_id(limit, options[:max_id], options[:since_id])
  37. end
  38. end
  39. def paginate_by_min_id(limit, min_id = nil)
  40. query = order(arel_table[:last_status_id].asc).limit(limit)
  41. query = query.where(arel_table[:last_status_id].gt(min_id)) if min_id.present?
  42. query
  43. end
  44. def paginate_by_max_id(limit, max_id = nil, since_id = nil)
  45. query = order(arel_table[:last_status_id].desc).limit(limit)
  46. query = query.where(arel_table[:last_status_id].lt(max_id)) if max_id.present?
  47. query = query.where(arel_table[:last_status_id].gt(since_id)) if since_id.present?
  48. query
  49. end
  50. def add_status(recipient, status)
  51. conversation = find_or_initialize_by(account: recipient, conversation_id: status.conversation_id, participant_account_ids: participants_from_status(recipient, status))
  52. return conversation if conversation.status_ids.include?(status.id)
  53. conversation.status_ids << status.id
  54. conversation.unread = status.account_id != recipient.id
  55. conversation.save
  56. conversation
  57. rescue ActiveRecord::StaleObjectError
  58. retry
  59. end
  60. def remove_status(recipient, status)
  61. conversation = find_by(account: recipient, conversation_id: status.conversation_id, participant_account_ids: participants_from_status(recipient, status))
  62. return if conversation.nil?
  63. conversation.status_ids.delete(status.id)
  64. if conversation.status_ids.empty?
  65. conversation.destroy
  66. else
  67. conversation.save
  68. end
  69. conversation
  70. rescue ActiveRecord::StaleObjectError
  71. retry
  72. end
  73. private
  74. def participants_from_status(recipient, status)
  75. ((status.active_mentions.pluck(:account_id) + [status.account_id]).uniq - [recipient.id]).sort
  76. end
  77. end
  78. private
  79. def set_last_status
  80. self.status_ids = status_ids.sort
  81. self.last_status_id = status_ids.last
  82. end
  83. def push_to_streaming_api
  84. return if destroyed? || !subscribed_to_timeline?
  85. PushConversationWorker.perform_async(id)
  86. end
  87. def subscribed_to_timeline?
  88. Redis.current.exists("subscribed:#{streaming_channel}")
  89. end
  90. def streaming_channel
  91. "timeline:direct:#{account_id}"
  92. end
  93. end