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.

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