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

53 lines
1.3 KiB

  1. class Follow < ApplicationRecord
  2. include Streamable
  3. belongs_to :account
  4. belongs_to :target_account, class_name: 'Account'
  5. validates :account, :target_account, presence: true
  6. validates :account_id, uniqueness: { scope: :target_account_id }
  7. def verb
  8. destroyed? ? :unfollow : :follow
  9. end
  10. def target
  11. target_account
  12. end
  13. def object_type
  14. :person
  15. end
  16. def title
  17. destroyed? ? "#{account.acct} is no longer following #{target_account.acct}" : "#{account.acct} started following #{target_account.acct}"
  18. end
  19. after_create :add_to_graph
  20. after_destroy :remove_from_graph
  21. def sync!
  22. add_to_graph
  23. end
  24. private
  25. def add_to_graph
  26. neo = Neography::Rest.new
  27. a = neo.create_unique_node('account_index', 'Account', account_id.to_s, account_id: account_id)
  28. b = neo.create_unique_node('account_index', 'Account', target_account_id.to_s, account_id: target_account_id)
  29. neo.create_unique_relationship('follow_index', 'Follow', id.to_s, 'follows', a, b)
  30. rescue Neography::NeographyError, Excon::Error::Socket => e
  31. Rails.logger.error e
  32. end
  33. def remove_from_graph
  34. neo = Neography::Rest.new
  35. rel = neo.get_relationship_index('follow_index', 'Follow', id.to_s)
  36. neo.delete_relationship(rel)
  37. rescue Neography::NeographyError, Excon::Error::Socket => e
  38. Rails.logger.error e
  39. end
  40. end