Module: X::Resources::Relationships

Included in:
User
Defined in:
x-resources/lib/x/resources/relationships.rb

Overview

The relationships of a user with other users, read as the authenticated user sees them

It reads them and changes none: a relationship changes as the authenticated user alone, so the client changes it, as in client.follow(user), rather than a user, which may be anyone.

Internal to x-resources: the methods it gives a user, such as follows?, are public API, but the module is only how they are shared, and which classes extend or include it can change within 1.x.

Instance Method Summary collapse

Instance Method Details

#follows?(user, max_pages: nil) ⇒ Boolean

Check whether this user follows a user

When either user is the authenticated user, one lookup of the other's connection_status answers. Otherwise the users this user follows are scanned until one matches, up to 1,000 a page, and the API bills every user returned, so checking an account that follows thousands can cost dollars, and max_pages limits the pages the scan reads, raising PageLimitReached rather than read past them. A client that authenticates as the app alone has no authenticated user, which the API refuses to look up, so it scans. Any other failure to look the authenticated user up raises, rather than scan every user this one follows.

Examples:

Check whether the authenticated user follows someone, in one lookup

client.current_user!.follows?(other)

Scan no more than five pages of the users another user follows

X::User.find("jack", client: client).follows?(other, max_pages: 5)

Parameters:

  • user (User, String, Integer) —

    the user or their identifier

  • max_pages (Integer, nil) (defaults to: nil) —

    the most pages of followed users to scan, or nil for no limit

Returns:

  • (Boolean) —

    true if this user follows the user

Raises:

  • (ArgumentError) —

    if max_pages is neither an Integer of at least 1 nor nil, before a request

  • (PageLimitReached) —

    if the scan reads max_pages pages without the user, and the API names another



38
39
40
41
42
43
44
45
# File 'x-resources/lib/x/resources/relationships.rb', line 38

def follows?(user, max_pages: nil)
  target, max_pages = User.from_id(user), PageLimit.check!(max_pages)
  case authenticated_user_id
  when id then connection_status_of(target).include?("following")
  when target.id then connection_status_of(self).include?("followed_by")
  else PageLimit.scan(following.stubs, target, what: "User#follows?", max_pages:)
  end
end