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.

56 lines
1.4 KiB

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