Class: Lita::User

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

Overview

A user in the chat service. Persisted in Redis.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(id, metadata = {}) ⇒ User

Returns a new instance of User

Parameters:

  • id (Integer, String)

    The user's unique ID.

  • metadata (Hash) (defaults to: {})

    Arbitrary user metadata.

Options Hash (metadata):

  • name (String) — default: id

    The user's display name.



89
90
91
92
93
94
# File 'lib/lita/user.rb', line 89

def initialize(id,  = {})
  @id = id.to_s
  @metadata = Util.stringify_keys()
  @name = @metadata["name"] || @id
  
end

Instance Attribute Details

#idString (readonly)

The user's unique ID.

Returns:

  • (String)

    The user's ID.



76
77
78
# File 'lib/lita/user.rb', line 76

def id
  @id
end

#metadataHash (readonly)

A hash of arbitrary metadata about the user.

Returns:

  • (Hash)

    The user's metadata.



80
81
82
# File 'lib/lita/user.rb', line 80

def 
  @metadata
end

#nameString (readonly)

The user's name as displayed in the chat.

Returns:

  • (String)

    The user's name.



84
85
86
# File 'lib/lita/user.rb', line 84

def name
  @name
end

Class Method Details

.create(id, metadata = {}) ⇒ Lita::User

Creates a new user with the given ID, or merges and saves supplied metadata to an existing user with the given ID.

Parameters:

  • id (Integer, String)

    A unique identifier for the user.

  • metadata (Hash) (defaults to: {})

    An optional hash of metadata about the user.

Options Hash (metadata):

  • name (String) — default: id

    The display name of the user.

Returns:



17
18
19
20
21
22
23
24
# File 'lib/lita/user.rb', line 17

def create(id,  = {})
  existing_user = find_by_id(id)
   = Util.stringify_keys()
   = existing_user..merge() if existing_user
  user = new(id, )
  user.save
  user
end

.find_by_id(id) ⇒ Lita::User?

Finds a user by ID.

Parameters:

  • id (Integer, String)

    The user's unique ID.

Returns:

  • (Lita::User, nil)

    The user or nil if no such user is known.



29
30
31
32
# File 'lib/lita/user.rb', line 29

def find_by_id(id)
   = redis.hgetall("id:#{id}")
  new(id, ) if .key?("name")
end

.find_by_mention_name(mention_name) ⇒ Lita::User?

Finds a user by mention name.

Parameters:

  • mention_name (String)

    The user's mention name.

Returns:

  • (Lita::User, nil)

    The user or nil if no such user is known.

Since:

  • 3.0.0



38
39
40
41
# File 'lib/lita/user.rb', line 38

def find_by_mention_name(mention_name)
  id = redis.get("mention_name:#{mention_name}")
  find_by_id(id) if id
end

.find_by_name(name) ⇒ Lita::User?

Finds a user by display name.

Parameters:

  • name (String)

    The user's name.

Returns:

  • (Lita::User, nil)

    The user or nil if no such user is known.



46
47
48
49
# File 'lib/lita/user.rb', line 46

def find_by_name(name)
  id = redis.get("name:#{name}")
  find_by_id(id) if id
end

.find_by_partial_name(name) ⇒ Lita::User?

Attempts to find a user with a name starting with the provided string.

Parameters:

  • name (String)

    The first characters in the user's name.

Returns:

  • (Lita::User, nil)

    The user, or nil if zero or greater than 1 matches were found.

Since:

  • 3.0.0



55
56
57
58
59
60
61
62
# File 'lib/lita/user.rb', line 55

def find_by_partial_name(name)
  keys = redis.keys("name:#{name}*")

  if keys.length == 1
    id = redis.get(keys.first)
    find_by_id(id)
  end
end

.fuzzy_find(identifier) ⇒ Lita::User?

Finds a user by ID, mention name, name, or partial name.

Parameters:

  • identifier (String)

    The user's ID, name, partial name, or mention name.

Returns:

  • (Lita::User, nil)

    The user or nil if no users were found.

Since:

  • 3.0.0



68
69
70
71
# File 'lib/lita/user.rb', line 68

def fuzzy_find(identifier)
  find_by_id(identifier) || find_by_mention_name(identifier) ||
    find_by_name(identifier) || find_by_partial_name(identifier)
end

.redisRedis::Namespace

The Redis::Namespace for user persistence.

Returns:

  • (Redis::Namespace)

    The Redis connection.



7
8
9
# File 'lib/lita/user.rb', line 7

def redis
  @redis ||= Redis::Namespace.new("users", redis: Lita.redis)
end

Instance Method Details

#==(other) ⇒ Boolean

Compares the user against another user object to determine equality. Users are considered equal if they have the same ID and name.

Parameters:

  • other (Lita::User)

    The user to compare against.

Returns:

  • (Boolean)

    True if users are equal, false otherwise.



120
121
122
# File 'lib/lita/user.rb', line 120

def ==(other)
  other.respond_to?(:id) && id == other.id && other.respond_to?(:name) && name == other.name
end

#mention_nameString

The name used to “mention” the user in a group chat.

Returns:

  • (String)

    The user's mention name.

Since:

  • 3.1.0



99
100
101
# File 'lib/lita/user.rb', line 99

def mention_name
  ["mention_name"] || name
end

#savevoid

This method returns an undefined value.

Saves the user record to Redis, overwriting an previous data for the current ID and user name.



106
107
108
109
110
111
112
113
114
# File 'lib/lita/user.rb', line 106

def save
  mention_name = [:mention_name] || ["mention_name"]

  redis.pipelined do
    redis.hmset("id:#{id}", *.to_a.flatten)
    redis.set("name:#{name}", id)
    redis.set("mention_name:#{mention_name}", id) if mention_name
  end
end