class Lita::User

A user in the chat service. Persisted in Redis.

Attributes

id[R]

The user's unique ID. @return [String] The user's ID.

metadata[R]

A hash of arbitrary metadata about the user. @return [Hash] The user's metadata.

name[R]

The user's name as displayed in the chat. @return [String] The user's name.

Public Class Methods

create(id, metadata = {}) click to toggle source

Creates a new user with the given ID, or merges and saves supplied metadata to an existing user with the given ID. @param id [Integer, String] A unique identifier for the user. @param metadata [Hash] An optional hash of metadata about the user. @option metadata [String] name (id) The display name of the user. @return [Lita::User] The user.

# File lib/lita/user.rb, line 17
def create(id, metadata = {})
  existing_user = find_by_id(id)
  metadata = Util.stringify_keys(metadata)
  metadata = existing_user.metadata.merge(metadata) if existing_user
  user = new(id, metadata)
  user.save
  user
end
find_by_id(id) click to toggle source

Finds a user by ID. @param id [Integer, String] The user's unique ID. @return [Lita::User, nil] The user or nil if no such user is known.

# File lib/lita/user.rb, line 29
def find_by_id(id)
  metadata = redis.hgetall("id:#{id}")
  new(id, metadata) if metadata.key?("name")
end
find_by_mention_name(mention_name) click to toggle source

Finds a user by mention name. @param #mention_name [String] The user's mention name. @return [Lita::User, nil] The user or nil if no such user is known. @since 3.0.0

# 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) click to toggle source

Finds a user by display name. @param name [String] The user's name. @return [Lita::User, nil] The user or nil if no such user is known.

# 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) click to toggle source

Attempts to find a user with a name starting with the provided string. @param name [String] The first characters in the user's name. @return [Lita::User, nil] The user, or nil if zero or greater than 1 matches were found. @since 3.0.0

# 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) click to toggle source

Finds a user by ID, mention name, name, or partial name. @param identifier [String] The user's ID, name, partial name, or mention name. @return [Lita::User, nil] The user or nil if no users were found. @since 3.0.0

# 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
new(id, metadata = {}) click to toggle source

@param id [Integer, String] The user's unique ID. @param metadata [Hash] Arbitrary user metadata. @option metadata [String] name (id) The user's display name.

# File lib/lita/user.rb, line 89
def initialize(id, metadata = {})
  @id = id.to_s
  @metadata = Util.stringify_keys(metadata)
  @name = @metadata["name"] || @id
  ensure_name_metadata_set
end
redis() click to toggle source

The Redis::Namespace for user persistence. @return [Redis::Namespace] The Redis connection.

# File lib/lita/user.rb, line 7
def redis
  @redis ||= Redis::Namespace.new("users", redis: Lita.redis)
end

Public Instance Methods

==(other) click to toggle source

Compares the user against another user object to determine equality. Users are considered equal if they have the same ID and name. @param other (Lita::User) The user to compare against. @return [Boolean] True if users are equal, false otherwise.

# File lib/lita/user.rb, line 120
def ==(other)
  other.respond_to?(:id) && id == other.id && other.respond_to?(:name) && name == other.name
end
Also aliased as: eql?
eql?(other)
Alias for: ==
hash() click to toggle source

Generates a Fixnum hash value for this user object. Implemented to support equality. @return [Fixnum] The hash value. @see Object#hash

# File lib/lita/user.rb, line 128
def hash
  id.hash ^ name.hash
end
mention_name() click to toggle source

The name used to “mention” the user in a group chat. @return [String] The user's mention name. @since 3.1.0

# File lib/lita/user.rb, line 99
def mention_name
  metadata["mention_name"] || name
end
save() click to toggle source

Saves the user record to Redis, overwriting any previous data for the current ID and user name. @return [void]

# File lib/lita/user.rb, line 106
def save
  mention_name = metadata[:mention_name] || metadata["mention_name"]

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

Private Instance Methods

ensure_name_metadata_set() click to toggle source

Ensure the user's metadata contains their name, to ensure their Redis hash contains at least one value. It's not possible to store an empty hash key in Redis.

# File lib/lita/user.rb, line 136
def ensure_name_metadata_set
  username = metadata.delete("name")
  metadata["name"] = username || id
end
redis() click to toggle source

The Redis connection for user persistence.

# File lib/lita/user.rb, line 142
def redis
  self.class.redis
end