lib/lita/user.rb in lita-3.0.3 vs lib/lita/user.rb in lita-3.0.4

- old
+ new

@@ -6,22 +6,22 @@ # @return [Redis::Namespace] The Redis connection. def redis @redis ||= Redis::Namespace.new("users", redis: Lita.redis) end - # Finds or creates a user. Attempts to find a user with the given ID. If - # none is found, creates a user with the provided ID and metadata. + # 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. def create(id, metadata = {}) - user = find_by_id(id) - unless user - user = new(id, metadata) - user.save - end + 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 # @deprecated Use {.create} instead. def find(id, metadata = {}) @@ -92,12 +92,12 @@ # @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. def initialize(id, metadata = {}) @id = id.to_s - @metadata = metadata - @name = @metadata[:name] || @metadata["name"] || @id + @metadata = Util.stringify_keys(metadata) + @name = @metadata["name"] || @id ensure_name_metadata_set end # Saves the user record to Redis, overwriting an previous data for the # current ID and user name. @@ -124,10 +124,9 @@ # 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. def ensure_name_metadata_set username = metadata.delete("name") - username = metadata.delete(:name) unless username metadata["name"] = username || id end # The Redis connection for user persistence. def redis