Class: Async::Matrix::ApplicationService::TransactionHandler

Inherits:
Object
  • Object
show all
Defined in:
lib/async/matrix/application_service/transaction_handler.rb

Overview

Routes incoming Matrix events to registered handler objects.

Each handler declares the event types it handles via #event_types. When an event arrives, the transaction_handler finds all matching handlers and calls them. Errors in one handler do not prevent others from running.

Instance Method Summary collapse

Constructor Details

#initialize(txn_store: TransactionStore.new) ⇒ TransactionHandler

Returns a new instance of TransactionHandler.



17
18
19
20
# File 'lib/async/matrix/application_service/transaction_handler.rb', line 17

def initialize(txn_store: TransactionStore.new)
  @handlers  = Hash.new { |h, k| h[k] = [] }
  @txn_store = txn_store
end

Instance Method Details

#dispatch(event) ⇒ Object



59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/async/matrix/application_service/transaction_handler.rb', line 59

def dispatch(event)
  type     = event.type
  handlers = @handlers[type]

  if handlers.empty?
    Console.debug(self) { "No handler for event type: #{type}" }
  else
    handlers.each do |handler|
      begin
        handler.call(event)
      rescue => e
        Console.error(self) { "Handler #{handler.class.name} raised #{e.class}: #{e.message}" }
      end
    end
  end
end

#dispatch_transaction(body) ⇒ Object



52
53
54
55
56
57
# File 'lib/async/matrix/application_service/transaction_handler.rb', line 52

def dispatch_transaction(body)
  Transaction.new(body).then do |txn|
    txn.events.each    { |event| dispatch(event) }
    txn.ephemeral.each { |event| dispatch(event) }
  end
end

#handler_countObject



76
77
78
# File 'lib/async/matrix/application_service/transaction_handler.rb', line 76

def handler_count
  @handlers.values.flatten.size
end

#receive_transaction(txn_id, body) ⇒ Object

Idempotently process a homeserver transaction. Duplicate transaction IDs (already seen) are skipped. Returns :processed or :duplicate.

The idempotency store lives here rather than in the HTTP layer because the Grape server is stateless across requests — the transaction_handler is the stable, long-lived object.



41
42
43
44
45
46
47
48
49
50
# File 'lib/async/matrix/application_service/transaction_handler.rb', line 41

def receive_transaction(txn_id, body)
  if @txn_store.seen?(txn_id)
    Console.debug(self) { "Duplicate transaction #{txn_id} — skipping" }
    :duplicate
  else
    dispatch_transaction(body)
    @txn_store.mark_seen(txn_id)
    :processed
  end
end

#register(handler) ⇒ Object

Register a Bot (responds to #handlers) or a plain handler (responds to #event_types and #call). Bots are expanded into their handlers.



24
25
26
27
28
29
30
31
32
33
# File 'lib/async/matrix/application_service/transaction_handler.rb', line 24

def register(handler)
  if handler.respond_to?(:handlers)
    handler.handlers.each { |h| register(h) }
  else
    handler.event_types.each do |type|
      @handlers[type] << handler
      Console.info(self) { "Registered #{handler.class.name} for #{type}" }
    end
  end
end