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.

54 lines
1.3 KiB

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