rita 0.1.0 → 5.0.0.alpha.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.gitignore +19 -0
- data/.rubocop.yml +51 -0
- data/.ruby-version +1 -0
- data/.travis.yml +16 -0
- data/CONTRIBUTING.md +18 -0
- data/Gemfile +5 -0
- data/{LICENSE.txt → LICENSE} +1 -3
- data/README.md +17 -24
- data/Rakefile +5 -5
- data/bin/lita +7 -0
- data/lib/lita/adapter.rb +147 -0
- data/lib/lita/adapters/shell.rb +126 -0
- data/lib/lita/adapters/test.rb +62 -0
- data/lib/lita/authorization.rb +112 -0
- data/lib/lita/callback.rb +39 -0
- data/lib/lita/cli.rb +218 -0
- data/lib/lita/configurable.rb +47 -0
- data/lib/lita/configuration_builder.rb +247 -0
- data/lib/lita/configuration_validator.rb +95 -0
- data/lib/lita/default_configuration.rb +122 -0
- data/lib/lita/errors.rb +25 -0
- data/lib/lita/handler/chat_router.rb +141 -0
- data/lib/lita/handler/common.rb +208 -0
- data/lib/lita/handler/event_router.rb +84 -0
- data/lib/lita/handler/http_router.rb +31 -0
- data/lib/lita/handler.rb +15 -0
- data/lib/lita/handlers/authorization.rb +129 -0
- data/lib/lita/handlers/help.rb +171 -0
- data/lib/lita/handlers/info.rb +66 -0
- data/lib/lita/handlers/room.rb +36 -0
- data/lib/lita/handlers/users.rb +37 -0
- data/lib/lita/http_callback.rb +46 -0
- data/lib/lita/http_route.rb +83 -0
- data/lib/lita/logger.rb +43 -0
- data/lib/lita/message.rb +124 -0
- data/lib/lita/middleware_registry.rb +39 -0
- data/lib/lita/namespace.rb +29 -0
- data/lib/lita/plugin_builder.rb +43 -0
- data/lib/lita/rack_app.rb +100 -0
- data/lib/lita/registry.rb +164 -0
- data/lib/lita/response.rb +65 -0
- data/lib/lita/robot.rb +273 -0
- data/lib/lita/room.rb +119 -0
- data/lib/lita/route_validator.rb +82 -0
- data/lib/lita/rspec/handler.rb +127 -0
- data/lib/lita/rspec/matchers/chat_route_matcher.rb +53 -0
- data/lib/lita/rspec/matchers/event_route_matcher.rb +29 -0
- data/lib/lita/rspec/matchers/http_route_matcher.rb +34 -0
- data/lib/lita/rspec.rb +48 -0
- data/lib/lita/source.rb +81 -0
- data/lib/lita/store.rb +23 -0
- data/lib/lita/target.rb +3 -0
- data/lib/lita/template.rb +71 -0
- data/lib/lita/template_resolver.rb +52 -0
- data/lib/lita/timer.rb +49 -0
- data/lib/lita/user.rb +157 -0
- data/lib/lita/util.rb +31 -0
- data/lib/lita/version.rb +6 -0
- data/lib/lita.rb +166 -0
- data/lib/rita.rb +2 -7
- data/rita.gemspec +50 -0
- data/spec/lita/adapter_spec.rb +54 -0
- data/spec/lita/adapters/shell_spec.rb +99 -0
- data/spec/lita/authorization_spec.rb +122 -0
- data/spec/lita/configuration_builder_spec.rb +247 -0
- data/spec/lita/configuration_validator_spec.rb +114 -0
- data/spec/lita/default_configuration_spec.rb +242 -0
- data/spec/lita/handler/chat_router_spec.rb +236 -0
- data/spec/lita/handler/common_spec.rb +289 -0
- data/spec/lita/handler/event_router_spec.rb +121 -0
- data/spec/lita/handler/http_router_spec.rb +155 -0
- data/spec/lita/handler_spec.rb +62 -0
- data/spec/lita/handlers/authorization_spec.rb +111 -0
- data/spec/lita/handlers/help_spec.rb +124 -0
- data/spec/lita/handlers/info_spec.rb +67 -0
- data/spec/lita/handlers/room_spec.rb +24 -0
- data/spec/lita/handlers/users_spec.rb +35 -0
- data/spec/lita/logger_spec.rb +28 -0
- data/spec/lita/message_spec.rb +178 -0
- data/spec/lita/plugin_builder_spec.rb +41 -0
- data/spec/lita/response_spec.rb +62 -0
- data/spec/lita/robot_spec.rb +285 -0
- data/spec/lita/room_spec.rb +136 -0
- data/spec/lita/rspec/handler_spec.rb +33 -0
- data/spec/lita/rspec_spec.rb +162 -0
- data/spec/lita/source_spec.rb +68 -0
- data/spec/lita/store_spec.rb +23 -0
- data/spec/lita/template_resolver_spec.rb +42 -0
- data/spec/lita/template_spec.rb +52 -0
- data/spec/lita/timer_spec.rb +32 -0
- data/spec/lita/user_spec.rb +167 -0
- data/spec/lita/util_spec.rb +18 -0
- data/spec/lita_spec.rb +227 -0
- data/spec/spec_helper.rb +35 -0
- data/spec/templates/basic.erb +1 -0
- data/spec/templates/basic.irc.erb +1 -0
- data/spec/templates/helpers.erb +1 -0
- data/spec/templates/interpolated.erb +1 -0
- data/templates/locales/en.yml +137 -0
- data/templates/plugin/Gemfile +5 -0
- data/templates/plugin/README.tt +29 -0
- data/templates/plugin/Rakefile +8 -0
- data/templates/plugin/gemspec.tt +27 -0
- data/templates/plugin/gitignore +18 -0
- data/templates/plugin/lib/lita/plugin_type/plugin.tt +19 -0
- data/templates/plugin/lib/plugin.tt +16 -0
- data/templates/plugin/locales/en.yml.tt +4 -0
- data/templates/plugin/spec/lita/plugin_type/plugin_spec.tt +6 -0
- data/templates/plugin/spec/spec_helper.tt +8 -0
- data/templates/plugin/templates/gitkeep +0 -0
- data/templates/robot/Gemfile +5 -0
- data/templates/robot/lita_config.rb +28 -0
- metadata +386 -20
- data/.standard.yml +0 -3
- data/CHANGELOG.md +0 -5
- data/CODE_OF_CONDUCT.md +0 -132
- data/lib/rita/version.rb +0 -5
- data/sig/rita.rbs +0 -4
@@ -0,0 +1,34 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require "rack"
|
4
|
+
|
5
|
+
module Lita
|
6
|
+
module RSpec
|
7
|
+
# A namespace to hold all of Lita's RSpec matchers.
|
8
|
+
module Matchers
|
9
|
+
# RSpec matchers for HTTP routes.
|
10
|
+
# @since 4.0.0
|
11
|
+
module HTTPRouteMatcher
|
12
|
+
extend ::RSpec::Matchers::DSL
|
13
|
+
|
14
|
+
matcher :route_http do |http_method, path|
|
15
|
+
match do
|
16
|
+
env = Rack::MockRequest.env_for(path, method: http_method)
|
17
|
+
|
18
|
+
matching_routes = robot.app.recognize(env)
|
19
|
+
|
20
|
+
if defined?(@method_name)
|
21
|
+
matching_routes.include?(@method_name)
|
22
|
+
else
|
23
|
+
!matching_routes.empty?
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
27
|
+
chain :to do |method_name|
|
28
|
+
@method_name = method_name
|
29
|
+
end
|
30
|
+
end
|
31
|
+
end
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|
data/lib/lita/rspec.rb
ADDED
@@ -0,0 +1,48 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
begin
|
4
|
+
require "rspec"
|
5
|
+
require "rspec/expectations"
|
6
|
+
require "rspec/mocks"
|
7
|
+
rescue LoadError
|
8
|
+
abort I18n.t("lita.rspec.full_suite_required")
|
9
|
+
end
|
10
|
+
|
11
|
+
major, *_unused = RSpec::Core::Version::STRING.split(/\./)
|
12
|
+
abort I18n.t("lita.rspec.version_3_required") if major.to_i < 3
|
13
|
+
|
14
|
+
require_relative "../lita"
|
15
|
+
require_relative "rspec/handler"
|
16
|
+
|
17
|
+
module Lita
|
18
|
+
# Extras for +RSpec+ that facilitate the testing of Lita code.
|
19
|
+
module RSpec
|
20
|
+
class << self
|
21
|
+
# Causes all interaction with Redis to use a test-specific namespace.
|
22
|
+
# Clears Redis before each example. Stubs the logger to prevent log
|
23
|
+
# messages from cluttering test output. Clears Lita's global
|
24
|
+
# configuration.
|
25
|
+
# @param base [Object] The class including the module.
|
26
|
+
# @return [void]
|
27
|
+
def included(base)
|
28
|
+
base.class_eval do
|
29
|
+
let(:registry) { Registry.new }
|
30
|
+
|
31
|
+
before do
|
32
|
+
logger = double("Logger").as_null_object
|
33
|
+
allow(Lita).to receive(:logger).and_return(logger)
|
34
|
+
keys = Lita.redis.keys("*")
|
35
|
+
Lita.redis.del(keys) unless keys.empty?
|
36
|
+
end
|
37
|
+
end
|
38
|
+
end
|
39
|
+
end
|
40
|
+
end
|
41
|
+
end
|
42
|
+
|
43
|
+
Lita.test_mode = true
|
44
|
+
|
45
|
+
RSpec.configure do |config|
|
46
|
+
config.include Lita::RSpec, lita: true
|
47
|
+
config.include Lita::RSpec::Handler, lita_handler: true
|
48
|
+
end
|
data/lib/lita/source.rb
ADDED
@@ -0,0 +1,81 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require "i18n"
|
4
|
+
|
5
|
+
require_relative "room"
|
6
|
+
|
7
|
+
module Lita
|
8
|
+
# A wrapper object representing the source of an incoming message (either the
|
9
|
+
# user who sent it, the room they sent it from, or both). If a room is set,
|
10
|
+
# the message is from a group chat room. If no room is set, the message is
|
11
|
+
# assumed to be a private message, though Source objects can be explicitly
|
12
|
+
# marked as private messages. Source objects are also used as "target" objects
|
13
|
+
# when sending an outgoing message or performing another operation on a user
|
14
|
+
# or a room.
|
15
|
+
class Source
|
16
|
+
# A flag indicating that a message was sent to the robot privately.
|
17
|
+
# @return [Boolean] The boolean flag.
|
18
|
+
attr_reader :private_message
|
19
|
+
alias private_message? private_message
|
20
|
+
|
21
|
+
# The room the message came from or should be sent to, as a string.
|
22
|
+
# @return [String, NilClass] A string uniquely identifying the room.
|
23
|
+
attr_reader :room
|
24
|
+
|
25
|
+
# The room the message came from or should be sent to, as a {Room} object.
|
26
|
+
# @return [Room, NilClass] The room.
|
27
|
+
# @since 4.4.0
|
28
|
+
attr_reader :room_object
|
29
|
+
|
30
|
+
# The user who sent the message or should receive the outgoing message.
|
31
|
+
# @return [User, NilClass] The user.
|
32
|
+
attr_reader :user
|
33
|
+
|
34
|
+
# @param user [User] The user who sent the message or should receive
|
35
|
+
# the outgoing message.
|
36
|
+
# @param room [Room, String] A string or {Room} uniquely identifying the room
|
37
|
+
# the user sent the message from, or the room where a reply should go. The format of this
|
38
|
+
# string (or the ID of the {Room} object) will differ depending on the chat service.
|
39
|
+
# @param private_message [Boolean] A flag indicating whether or not the
|
40
|
+
# message was sent privately.
|
41
|
+
def initialize(user: nil, room: nil, private_message: false)
|
42
|
+
@user = user
|
43
|
+
|
44
|
+
case room
|
45
|
+
when String
|
46
|
+
@room = room
|
47
|
+
@room_object = load_with_metadata(room)
|
48
|
+
when Room
|
49
|
+
@room = room.id
|
50
|
+
@room_object = room
|
51
|
+
end
|
52
|
+
|
53
|
+
@private_message = private_message
|
54
|
+
|
55
|
+
raise ArgumentError, I18n.t("lita.source.user_or_room_required") if user.nil? && room.nil?
|
56
|
+
|
57
|
+
@private_message = true if room.nil?
|
58
|
+
end
|
59
|
+
|
60
|
+
# Destructively marks the source as a private message, meaning an incoming
|
61
|
+
# message was sent to the robot privately, or an outgoing message should be
|
62
|
+
# sent to a user privately.
|
63
|
+
# @return [void]
|
64
|
+
def private_message!
|
65
|
+
@private_message = true
|
66
|
+
end
|
67
|
+
|
68
|
+
private
|
69
|
+
|
70
|
+
def load_with_metadata(room_id)
|
71
|
+
Room.find_by_id(room_id) || Room.new(room_id)
|
72
|
+
end
|
73
|
+
end
|
74
|
+
|
75
|
+
# An alias for {Source}. Since source objects are used for both incoming and outgoing messages, it
|
76
|
+
# might be helpful to think of an outgoing messsage's source as a "target" rather than a source.
|
77
|
+
#
|
78
|
+
# @see Source
|
79
|
+
# @since 5.0.0
|
80
|
+
Target = Source
|
81
|
+
end
|
data/lib/lita/store.rb
ADDED
@@ -0,0 +1,23 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Lita
|
4
|
+
# A simple, in-memory, thread-safe key-value store.
|
5
|
+
# @since 5.0.0
|
6
|
+
class Store
|
7
|
+
# @param internal_store [Hash] A hash-like object to use internally to store data.
|
8
|
+
def initialize(internal_store = {})
|
9
|
+
@store = internal_store
|
10
|
+
@lock = Mutex.new
|
11
|
+
end
|
12
|
+
|
13
|
+
# Get a key from the store.
|
14
|
+
def [](key)
|
15
|
+
@lock.synchronize { @store[key] }
|
16
|
+
end
|
17
|
+
|
18
|
+
# Set a key to the given value.
|
19
|
+
def []=(key, value)
|
20
|
+
@lock.synchronize { @store[key] = value }
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
data/lib/lita/target.rb
ADDED
@@ -0,0 +1,71 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require "erb"
|
4
|
+
require "set"
|
5
|
+
|
6
|
+
module Lita
|
7
|
+
# A simple wrapper around ERB to render text from files or strings.
|
8
|
+
# @since 4.2.0
|
9
|
+
class Template
|
10
|
+
# A clean room object to use as the binding for ERB rendering.
|
11
|
+
# @api private
|
12
|
+
class TemplateEvaluationContext
|
13
|
+
# Returns the evaluation context's binding.
|
14
|
+
# @return [Binding] The binding.
|
15
|
+
def __get_binding
|
16
|
+
binding
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
class << self
|
21
|
+
# Initializes a new Template with the contents of the file at the given path.
|
22
|
+
# @param path [String] The path to the file to use as the template content.
|
23
|
+
# @return Template
|
24
|
+
def from_file(path)
|
25
|
+
new(File.read(path).chomp)
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
29
|
+
# @param source [String] A string to use as the template's content.
|
30
|
+
def initialize(source)
|
31
|
+
@erb = ERB.new(source, trim_mode: "<>")
|
32
|
+
self.helpers = Set.new
|
33
|
+
end
|
34
|
+
|
35
|
+
# Add a module of helpers methods to be added to the template evalutation context.
|
36
|
+
# @param helper [Module] The module to extend onto the template evalutation context.
|
37
|
+
# @return [void]
|
38
|
+
# @since 4.5.0
|
39
|
+
def add_helper(helper)
|
40
|
+
helpers << helper
|
41
|
+
end
|
42
|
+
|
43
|
+
# Render the template with the provided variables.
|
44
|
+
# @param variables [Hash] A collection of variables for interpolation. Each key-value pair will
|
45
|
+
# make the value available inside the template as an instance variable with the key as its
|
46
|
+
# name.
|
47
|
+
def render(variables = {})
|
48
|
+
erb.result(context_binding(variables))
|
49
|
+
end
|
50
|
+
|
51
|
+
private
|
52
|
+
|
53
|
+
attr_accessor :helpers
|
54
|
+
|
55
|
+
# Create an empty object to use as the ERB context and set any provided variables in it.
|
56
|
+
def context_binding(variables)
|
57
|
+
context = TemplateEvaluationContext.new
|
58
|
+
|
59
|
+
helpers.each { |helper| context.extend(helper) }
|
60
|
+
|
61
|
+
variables.each do |k, v|
|
62
|
+
context.instance_variable_set("@#{k}", v)
|
63
|
+
end
|
64
|
+
|
65
|
+
context.__get_binding
|
66
|
+
end
|
67
|
+
|
68
|
+
# The underlying ERB object.
|
69
|
+
attr_reader :erb
|
70
|
+
end
|
71
|
+
end
|
@@ -0,0 +1,52 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require "i18n"
|
4
|
+
|
5
|
+
require_relative "errors"
|
6
|
+
|
7
|
+
module Lita
|
8
|
+
# Finds the file path of the most appropriate template for the given adapter.
|
9
|
+
# @api private
|
10
|
+
# @since 4.2.0
|
11
|
+
class TemplateResolver
|
12
|
+
# @param template_root [String] The directory to search for templates.
|
13
|
+
# @param template_name [String] The name of the template to search for.
|
14
|
+
# @param adapter_name [String, Symbol] The name of the current adapter.
|
15
|
+
def initialize(template_root, template_name, adapter_name)
|
16
|
+
@template_root = template_root
|
17
|
+
@template_name = template_name
|
18
|
+
@adapter_name = adapter_name
|
19
|
+
end
|
20
|
+
|
21
|
+
# Returns the adapter-specific template, falling back to a generic template.
|
22
|
+
# @return [String] The path of the template to use.
|
23
|
+
# @raise [MissingTemplateError] If no templates with the given name exist.
|
24
|
+
def resolve
|
25
|
+
return adapter_template if File.exist?(adapter_template)
|
26
|
+
return generic_template if File.exist?(generic_template)
|
27
|
+
|
28
|
+
raise MissingTemplateError, I18n.t("lita.template.missing_template", path: generic_template)
|
29
|
+
end
|
30
|
+
|
31
|
+
private
|
32
|
+
|
33
|
+
# The directory to search for templates.
|
34
|
+
attr_reader :template_root
|
35
|
+
|
36
|
+
# The name of the template to search for.
|
37
|
+
attr_reader :template_name
|
38
|
+
|
39
|
+
# The name of the current adapter.
|
40
|
+
attr_reader :adapter_name
|
41
|
+
|
42
|
+
# Path to the adapter-specific template.
|
43
|
+
def adapter_template
|
44
|
+
@adapter_template ||= File.join(template_root, "#{template_name}.#{adapter_name}.erb")
|
45
|
+
end
|
46
|
+
|
47
|
+
# Path to the generic template.
|
48
|
+
def generic_template
|
49
|
+
@generic_template ||= File.join(template_root, "#{template_name}.erb")
|
50
|
+
end
|
51
|
+
end
|
52
|
+
end
|
data/lib/lita/timer.rb
ADDED
@@ -0,0 +1,49 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Lita
|
4
|
+
# A timer that executes a block after a certain number of seconds, either once or repeatedly.
|
5
|
+
# @since 3.0.0
|
6
|
+
class Timer
|
7
|
+
# @param interval [Integer] The number of seconds to wait before calling the block.
|
8
|
+
# @param recurring [Boolean] If true, the timer will fire repeatedly until stopped.
|
9
|
+
# @yieldparam timer [Timer] The current {Timer} instance.
|
10
|
+
def initialize(interval: 0, recurring: false, &block)
|
11
|
+
@interval = interval
|
12
|
+
@recurring = recurring
|
13
|
+
@running = false
|
14
|
+
@block = block
|
15
|
+
end
|
16
|
+
|
17
|
+
# Starts running the timer.
|
18
|
+
def start
|
19
|
+
@running = true
|
20
|
+
run
|
21
|
+
end
|
22
|
+
|
23
|
+
# Stops the timer, preventing any further invocations of the block until started again.
|
24
|
+
def stop
|
25
|
+
@running = false
|
26
|
+
end
|
27
|
+
|
28
|
+
private
|
29
|
+
|
30
|
+
# Is this a recurring timer?
|
31
|
+
def recurring?
|
32
|
+
@recurring
|
33
|
+
end
|
34
|
+
|
35
|
+
# Sleep for the given interval, call the block, then run again if it's a recurring timer.
|
36
|
+
def run
|
37
|
+
loop do
|
38
|
+
sleep @interval
|
39
|
+
@block.call(self) if running? && @block
|
40
|
+
break unless running? && recurring?
|
41
|
+
end
|
42
|
+
end
|
43
|
+
|
44
|
+
# Is the timer currently running?
|
45
|
+
def running?
|
46
|
+
@running
|
47
|
+
end
|
48
|
+
end
|
49
|
+
end
|
data/lib/lita/user.rb
ADDED
@@ -0,0 +1,157 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require "redis-namespace"
|
4
|
+
|
5
|
+
require_relative "util"
|
6
|
+
|
7
|
+
module Lita
|
8
|
+
# A user in the chat service. Persisted in Redis.
|
9
|
+
class User
|
10
|
+
class << self
|
11
|
+
# The +Redis::Namespace+ for user persistence.
|
12
|
+
# @return [Redis::Namespace] The Redis connection.
|
13
|
+
def redis
|
14
|
+
@redis ||= Redis::Namespace.new("users", redis: Lita.redis)
|
15
|
+
end
|
16
|
+
|
17
|
+
# Creates a new user with the given ID, or merges and saves supplied
|
18
|
+
# metadata to an existing user with the given ID.
|
19
|
+
# @param id [Integer, String] A unique identifier for the user.
|
20
|
+
# @param metadata [Hash] An optional hash of metadata about the user.
|
21
|
+
# @option metadata [String] name (id) The display name of the user.
|
22
|
+
# @return [User] The user.
|
23
|
+
def create(id, metadata = {})
|
24
|
+
existing_user = find_by_id(id)
|
25
|
+
metadata = Util.stringify_keys(metadata)
|
26
|
+
metadata = existing_user.metadata.merge(metadata) if existing_user
|
27
|
+
user = new(id, metadata)
|
28
|
+
user.save
|
29
|
+
user
|
30
|
+
end
|
31
|
+
|
32
|
+
# Finds a user by ID.
|
33
|
+
# @param id [Integer, String] The user's unique ID.
|
34
|
+
# @return [User, nil] The user or +nil+ if no such user is known.
|
35
|
+
def find_by_id(id)
|
36
|
+
metadata = redis.hgetall("id:#{id}")
|
37
|
+
new(id, metadata) if metadata.key?("name")
|
38
|
+
end
|
39
|
+
|
40
|
+
# Finds a user by mention name.
|
41
|
+
# @param mention_name [String] The user's mention name.
|
42
|
+
# @return [User, nil] The user or +nil+ if no such user is known.
|
43
|
+
# @since 3.0.0
|
44
|
+
def find_by_mention_name(mention_name)
|
45
|
+
id = redis.get("mention_name:#{mention_name}")
|
46
|
+
find_by_id(id) if id
|
47
|
+
end
|
48
|
+
|
49
|
+
# Finds a user by display name.
|
50
|
+
# @param name [String] The user's name.
|
51
|
+
# @return [User, nil] The user or +nil+ if no such user is known.
|
52
|
+
def find_by_name(name)
|
53
|
+
id = redis.get("name:#{name}")
|
54
|
+
find_by_id(id) if id
|
55
|
+
end
|
56
|
+
|
57
|
+
# Attempts to find a user with a name starting with the provided string.
|
58
|
+
# @param name [String] The first characters in the user's name.
|
59
|
+
# @return [User, nil] The user, or +nil+ if zero or greater than 1 matches were found.
|
60
|
+
# @since 3.0.0
|
61
|
+
def find_by_partial_name(name)
|
62
|
+
keys = redis.keys("name:#{name}*")
|
63
|
+
|
64
|
+
if keys.length == 1
|
65
|
+
id = redis.get(keys.first)
|
66
|
+
find_by_id(id)
|
67
|
+
end
|
68
|
+
end
|
69
|
+
|
70
|
+
# Finds a user by ID, mention name, name, or partial name.
|
71
|
+
# @param identifier [String] The user's ID, name, partial name, or mention name.
|
72
|
+
# @return [User, nil] The user or +nil+ if no users were found.
|
73
|
+
# @since 3.0.0
|
74
|
+
def fuzzy_find(identifier)
|
75
|
+
find_by_id(identifier) || find_by_mention_name(identifier) ||
|
76
|
+
find_by_name(identifier) || find_by_partial_name(identifier)
|
77
|
+
end
|
78
|
+
end
|
79
|
+
|
80
|
+
# The user's unique ID.
|
81
|
+
# @return [String] The user's ID.
|
82
|
+
attr_reader :id
|
83
|
+
|
84
|
+
# A hash of arbitrary metadata about the user.
|
85
|
+
# @return [Hash] The user's metadata.
|
86
|
+
attr_reader :metadata
|
87
|
+
|
88
|
+
# The user's name as displayed in the chat.
|
89
|
+
# @return [String] The user's name.
|
90
|
+
attr_reader :name
|
91
|
+
|
92
|
+
# @param id [Integer, String] The user's unique ID.
|
93
|
+
# @param metadata [Hash] Arbitrary user metadata.
|
94
|
+
# @option metadata [String] name (id) The user's display name.
|
95
|
+
def initialize(id, metadata = {})
|
96
|
+
@id = id.to_s
|
97
|
+
@metadata = Util.stringify_keys(metadata)
|
98
|
+
@name = @metadata["name"] || @id
|
99
|
+
ensure_name_metadata_set
|
100
|
+
end
|
101
|
+
|
102
|
+
# The name used to "mention" the user in a group chat.
|
103
|
+
# @return [String] The user's mention name.
|
104
|
+
# @since 3.1.0
|
105
|
+
def mention_name
|
106
|
+
metadata["mention_name"] || name
|
107
|
+
end
|
108
|
+
|
109
|
+
# Saves the user record to Redis, overwriting any previous data for the
|
110
|
+
# current ID and user name.
|
111
|
+
# @return [void]
|
112
|
+
def save
|
113
|
+
mention_name = metadata[:mention_name] || metadata["mention_name"]
|
114
|
+
|
115
|
+
current_keys = metadata.keys
|
116
|
+
redis_keys = redis.hkeys("id:#{id}")
|
117
|
+
delete_keys = (redis_keys - current_keys)
|
118
|
+
|
119
|
+
redis.pipelined do
|
120
|
+
redis.hdel("id:#{id}", *delete_keys) if delete_keys.any?
|
121
|
+
redis.hmset("id:#{id}", *metadata.to_a.flatten)
|
122
|
+
redis.set("name:#{name}", id)
|
123
|
+
redis.set("mention_name:#{mention_name}", id) if mention_name
|
124
|
+
end
|
125
|
+
end
|
126
|
+
|
127
|
+
# Compares the user against another user object to determine equality. Users
|
128
|
+
# are considered equal if they have the same ID and name.
|
129
|
+
# @param other (User) The user to compare against.
|
130
|
+
# @return [Boolean] True if users are equal, false otherwise.
|
131
|
+
def ==(other)
|
132
|
+
other.respond_to?(:id) && id == other.id && other.respond_to?(:name) && name == other.name
|
133
|
+
end
|
134
|
+
alias eql? ==
|
135
|
+
|
136
|
+
# Generates a +Fixnum+ hash value for this user object. Implemented to support equality.
|
137
|
+
# @return [Fixnum] The hash value.
|
138
|
+
# @see Object#hash
|
139
|
+
def hash
|
140
|
+
id.hash ^ name.hash
|
141
|
+
end
|
142
|
+
|
143
|
+
private
|
144
|
+
|
145
|
+
# Ensure the user's metadata contains their name, to ensure their Redis hash contains at least
|
146
|
+
# one value. It's not possible to store an empty hash key in Redis.
|
147
|
+
def ensure_name_metadata_set
|
148
|
+
username = metadata.delete("name")
|
149
|
+
metadata["name"] = username || id
|
150
|
+
end
|
151
|
+
|
152
|
+
# The Redis connection for user persistence.
|
153
|
+
def redis
|
154
|
+
self.class.redis
|
155
|
+
end
|
156
|
+
end
|
157
|
+
end
|
data/lib/lita/util.rb
ADDED
@@ -0,0 +1,31 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Lita
|
4
|
+
# Handy utilities used by other Lita classes.
|
5
|
+
# @api private
|
6
|
+
module Util
|
7
|
+
class << self
|
8
|
+
# Returns a hash with any symbol keys converted to strings.
|
9
|
+
# @param hash [Hash] The hash to convert.
|
10
|
+
# @return [Hash] The converted hash.
|
11
|
+
def stringify_keys(hash)
|
12
|
+
result = {}
|
13
|
+
hash.each_key { |key| result[key.to_s] = hash[key] }
|
14
|
+
result
|
15
|
+
end
|
16
|
+
|
17
|
+
# Transforms a camel-cased string into a snaked-cased string. Taken from +ActiveSupport.+
|
18
|
+
# @param camel_cased_word [String] The word to transform.
|
19
|
+
# @return [String] The transformed word.
|
20
|
+
def underscore(camel_cased_word)
|
21
|
+
word = camel_cased_word.to_s.dup
|
22
|
+
word.gsub!("::", "/")
|
23
|
+
word.gsub!(/([A-Z\d]+)([A-Z][a-z])/, '\1_\2')
|
24
|
+
word.gsub!(/([a-z\d])([A-Z])/, '\1_\2')
|
25
|
+
word.tr!("-", "_")
|
26
|
+
word.downcase!
|
27
|
+
word
|
28
|
+
end
|
29
|
+
end
|
30
|
+
end
|
31
|
+
end
|