Class: MTProto::DraftStream

Inherits:
Object
  • Object
show all
Defined in:
lib/mtproto/draft_stream.rb

Overview

Telegram caps the underlying call at 20 per 5s and 40 per 30s per peer, and breaching that returns FLOOD_WAIT and blocks what follows. A frame with no room left is therefore dropped rather than queued — the next frame carries newer text anyway.

Constant Summary collapse

WINDOWS =
[[5.0, 20], [30.0, 40]].freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(api:, peer:, random_id: nil, top_msg_id: nil, clock: nil) ⇒ DraftStream

Returns a new instance of DraftStream.



16
17
18
19
20
21
22
23
# File 'lib/mtproto/draft_stream.rb', line 16

def initialize(api:, peer:, random_id: nil, top_msg_id: nil, clock: nil)
  @api = api
  @peer = peer
  @random_id = random_id || SecureRandom.random_number(2**63)
  @top_msg_id = top_msg_id
  @clock = clock || -> { Process.clock_gettime(Process::CLOCK_MONOTONIC) }
  @sent_at = []
end

Instance Attribute Details

#random_idObject (readonly)

Returns the value of attribute random_id.



14
15
16
# File 'lib/mtproto/draft_stream.rb', line 14

def random_id
  @random_id
end

Instance Method Details

#finish(text) ⇒ Object

Send the finished answer as an ordinary message, which also clears the draft.



40
41
42
# File 'lib/mtproto/draft_stream.rb', line 40

def finish(text, **)
  @api.send_message(peer: @peer, message: text, **)
end

#update(text, entities: []) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/mtproto/draft_stream.rb', line 25

def update(text, entities: [])
  now = @clock.call
  return false unless room?(now)

  @api.set_typing(
    peer: @peer, top_msg_id: @top_msg_id,
    action: TL::SendMessageTextDraftAction.new(random_id: @random_id, text: text, entities: entities)
  )
  @sent_at << now
  true
rescue RpcError
  false
end