зеркало из
https://github.com/glebtv/telegram-bot.git
synced 2026-09-07 19:35:52 +03:00
Fixed controller crashes on unsupported update types
Этот коммит содержится в:
@@ -143,13 +143,13 @@ module Telegram
|
|||||||
# Accessor to `'chat'` field of payload. Can be overriden with `chat` option
|
# Accessor to `'chat'` field of payload. Can be overriden with `chat` option
|
||||||
# for #initialize.
|
# for #initialize.
|
||||||
def chat
|
def chat
|
||||||
@_chat || payload && payload['chat']
|
@_chat ||= payload && payload['chat']
|
||||||
end
|
end
|
||||||
|
|
||||||
# Accessor to `'from'` field of payload. Can be overriden with `from` option
|
# Accessor to `'from'` field of payload. Can be overriden with `from` option
|
||||||
# for #initialize.
|
# for #initialize.
|
||||||
def from
|
def from
|
||||||
@_from || payload && payload['from']
|
@_from ||= payload && payload['from']
|
||||||
end
|
end
|
||||||
|
|
||||||
# Processes current update.
|
# Processes current update.
|
||||||
@@ -164,7 +164,11 @@ module Telegram
|
|||||||
# it uses fallback with action same as payload type.
|
# it uses fallback with action same as payload type.
|
||||||
# Returns array `[is_command?, action, args]`.
|
# Returns array `[is_command?, action, args]`.
|
||||||
def action_for_payload
|
def action_for_payload
|
||||||
|
if payload_type
|
||||||
send("action_for_#{payload_type}") || [false, payload_type, [payload]]
|
send("action_for_#{payload_type}") || [false, payload_type, [payload]]
|
||||||
|
else
|
||||||
|
[false, :unsupported_payload_type, []]
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
# If payload is a message with command, then returned action is an
|
# If payload is a message with command, then returned action is an
|
||||||
|
|||||||
@@ -17,11 +17,15 @@ module Telegram
|
|||||||
protected
|
protected
|
||||||
|
|
||||||
def session
|
def session
|
||||||
@_session ||= SessionHash.new(self.class.session_store, session_key)
|
@_session ||= begin
|
||||||
|
key = session_key
|
||||||
|
key ? SessionHash.new(self.class.session_store, key) : NullSessionHash.new
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def session_key
|
def session_key
|
||||||
"#{bot.username}:#{from ? "from:#{from['id']}" : "chat:#{chat['id']}"}"
|
subject = from || chat
|
||||||
|
"#{bot.username}:#{subject['id']}" if subject
|
||||||
end
|
end
|
||||||
|
|
||||||
# Rack::Session::Abstract::SessionHash is taken to provide lazy loading.
|
# Rack::Session::Abstract::SessionHash is taken to provide lazy loading.
|
||||||
@@ -59,6 +63,18 @@ module Telegram
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
class NullSessionHash < Session::SessionHash
|
||||||
|
def initialize
|
||||||
|
@data = {}
|
||||||
|
@loaded = true
|
||||||
|
@exists = true
|
||||||
|
end
|
||||||
|
|
||||||
|
alias_method :destroy, :clear
|
||||||
|
alias_method :load!, :id
|
||||||
|
alias_method :commit, :id
|
||||||
|
end
|
||||||
|
|
||||||
module ConfigMethods
|
module ConfigMethods
|
||||||
delegate :session_store, to: :config
|
delegate :session_store, to: :config
|
||||||
|
|
||||||
|
|||||||
@@ -27,19 +27,7 @@ module Telegram
|
|||||||
|
|
||||||
# Stubs session.
|
# Stubs session.
|
||||||
def session
|
def session
|
||||||
@_session ||= Testing::SessionHash.new
|
@_session ||= Session::NullSessionHash.new
|
||||||
end
|
|
||||||
|
|
||||||
class SessionHash < Session::SessionHash
|
|
||||||
def initialize
|
|
||||||
@data = {}
|
|
||||||
@loaded = true
|
|
||||||
@exists = true
|
|
||||||
end
|
|
||||||
|
|
||||||
alias_method :destroy, :clear
|
|
||||||
alias_method :load!, :id
|
|
||||||
alias_method :commit, :id
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -26,6 +26,12 @@ RSpec.describe Telegram::Bot::UpdatesController::Session do
|
|||||||
def read
|
def read
|
||||||
session[:text]
|
session[:text]
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def action_missing(*)
|
||||||
|
[:action_missing, session[:text]].tap do
|
||||||
|
session[:text] = 'test'
|
||||||
|
end
|
||||||
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
@@ -39,5 +45,13 @@ RSpec.describe Telegram::Bot::UpdatesController::Session do
|
|||||||
expect(subject.call(bot, build_message('/read', id: 2))).to eq nil
|
expect(subject.call(bot, build_message('/read', id: 2))).to eq nil
|
||||||
expect(subject.call(other_bot, build_message('/read', id: 1))).to eq nil
|
expect(subject.call(other_bot, build_message('/read', id: 1))).to eq nil
|
||||||
end
|
end
|
||||||
|
|
||||||
|
context 'payload is not supported' do
|
||||||
|
let(:payload_type) { '_unsupported_' }
|
||||||
|
it 'provides empty session' do
|
||||||
|
2.times { expect(subject.call(bot)).to eq [:action_missing, nil] }
|
||||||
|
expect(subject.call(other_bot)).to eq [:action_missing, nil]
|
||||||
|
end
|
||||||
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -102,6 +102,11 @@ RSpec.describe Telegram::Bot::UpdatesController do
|
|||||||
it { should eq [false, payload_type, payload.values_at(:data)] }
|
it { should eq [false, payload_type, payload.values_at(:data)] }
|
||||||
end
|
end
|
||||||
|
|
||||||
|
context 'when payload is not supported' do
|
||||||
|
let(:payload_type) { '_unsupported_' }
|
||||||
|
it { should eq [false, :unsupported_payload_type, []] }
|
||||||
|
end
|
||||||
|
|
||||||
context 'when payload is message' do
|
context 'when payload is message' do
|
||||||
let(:payload_type) { 'message' }
|
let(:payload_type) { 'message' }
|
||||||
let(:payload) { {'text' => text} }
|
let(:payload) { {'text' => text} }
|
||||||
|
|||||||
Ссылка в новой задаче
Block a user