Class: Talkbird::Entity::User

Inherits:
Object
  • Object
show all
Defined in:
lib/talkbird/entity/user.rb

Overview

A SendBird User entity.

Users can chat with each other by participanting in open channels and joining group channels. They are identified by their own unique ID, and may have a customized nickname and profile image.

Various attributes of each user, as well as their actions can be managed through the API.

Constant Summary collapse

DEFAULTS =
{
  nickname: '',
  profile_url: '',
  issue_session_token: true
}.freeze

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(data = {}) ⇒ User

Returns a new instance of User.



71
72
73
# File 'lib/talkbird/entity/user.rb', line 71

def initialize(data = {})
  @data = data
end

Class Method Details

.allObject

Find all the users in the application.

WARNING: This may take a lot of time.



59
60
61
62
63
64
65
66
67
# File 'lib/talkbird/entity/user.rb', line 59

def all
  result = Client.request(:get, 'users')

  if result.is_a?(Result::Success)
    result.body[:users].map { |data| User.new(data) }
  else
    []
  end
end

.create(id, opts = {}) ⇒ Object



37
38
39
40
41
42
43
44
45
46
# File 'lib/talkbird/entity/user.rb', line 37

def create(id, opts = {})
  body = DEFAULTS.merge(opts)
  result = Client.request(:post, 'users', body: body)

  if result.is_a?(Result::Success)
    User.new(result.body)
  else
    false
  end
end

.find(id) ⇒ User, Boolean

Find a user with a specific ID.

Parameters:

  • id (String)

    The user’s unique ID

Returns:



27
28
29
30
31
32
33
34
35
# File 'lib/talkbird/entity/user.rb', line 27

def find(id)
  result = Client.request(:get, "users/#{id}")

  if result.is_a?(Result::Success)
    User.new(result.body)
  else
    false
  end
end

.find_or_create(id, opts = {}) ⇒ Object



48
49
50
51
52
53
54
# File 'lib/talkbird/entity/user.rb', line 48

def find_or_create(id, opts = {})
  if id.is_a?(Entity::User)
    id
  else
    User.find(id) || User.create(id, opts)
  end
end

Instance Method Details

#access_tokenObject

An opaque string that identifies the user.

It is recommended that every user has their own access token and provides it uopn login for security.



96
97
98
# File 'lib/talkbird/entity/user.rb', line 96

def access_token
  @data[:access_token]
end

#active?Boolean

Indicates whether the user is currently active within the application.

Returns:

  • (Boolean)


119
120
121
# File 'lib/talkbird/entity/user.rb', line 119

def active?
  @data[:active]
end

#discovery_keysObject

An array of unique identifies of the user which are used as discovering keys when searching and adding friends.



130
131
132
# File 'lib/talkbird/entity/user.rb', line 130

def discovery_keys
  @data[:discovery_keys]
end

#ever_logged_in?Boolean

Indicates if the user has ever logged into the application so far.

Returns:

  • (Boolean)


114
115
116
# File 'lib/talkbird/entity/user.rb', line 114

def ever_logged_in?
  @data[:has_ever_logged_in]
end

#idObject

Basic user properties

The unique ID of the user



78
79
80
# File 'lib/talkbird/entity/user.rb', line 78

def id
  @data[:user_id]
end

#last_seen_atObject

The time recoreded when the user goes offline, to indicate when they were last seen online, in Unix miliseconds format.

If the user is online, the value is set to 0.



138
139
140
# File 'lib/talkbird/entity/user.rb', line 138

def last_seen_at
  @data[:last_seen_at]
end

#message(to, text, opts = {}) ⇒ Boolean

Send a message to a user

Parameters:

  • to (String)

    The Sendbird user ID that should receive the message

  • text (String)

    The message body

Returns:

  • (Boolean)


154
155
156
# File 'lib/talkbird/entity/user.rb', line 154

def message(to, text, opts = {})
  Entity::Message.new(self, to, text, opts).deliver
end

#metadataObject

An array of key-value pair items which store additional user information.



144
145
146
# File 'lib/talkbird/entity/user.rb', line 144

def 
  @data[:metadata]
end

#nicknameObject

The user’s nickname.



83
84
85
# File 'lib/talkbird/entity/user.rb', line 83

def nickname
  @data[:nickname]
end

#online?Boolean

Indicates whether the user is currently connected to a SendBird server.

Returns:

  • (Boolean)


124
125
126
# File 'lib/talkbird/entity/user.rb', line 124

def online?
  @data[:online]
end

#profile_urlObject

The URL of the user’s profile image.



88
89
90
# File 'lib/talkbird/entity/user.rb', line 88

def profile_url
  @data[:profile_url]
end

#session_tokensObject

An array of inforation of session tokens that identifies the user session and which have no validity after their own expiration time.

Each of items consists of two ‘session_token` and `expires_at` properties. The `session_token` is an opaque string and `expires_at` is a validation period of the session token.

It is recommended that a new session token is periodically isseud to every user, and provided upon the user’s login for security.



109
110
111
# File 'lib/talkbird/entity/user.rb', line 109

def session_tokens
  @data[:session_tokens]
end

#to_hObject



158
159
160
# File 'lib/talkbird/entity/user.rb', line 158

def to_h
  @data.to_h
end

#to_sObject



162
163
164
# File 'lib/talkbird/entity/user.rb', line 162

def to_s
  "#<Talkbird::Entity::User:#{id} active=#{active?} online=#{online?}>"
end