Class: Legion::Extensions::MicrosoftTeams::Helpers::SubscriptionRegistry
- Inherits:
-
Object
- Object
- Legion::Extensions::MicrosoftTeams::Helpers::SubscriptionRegistry
- Includes:
- Helpers::Lex
- Defined in:
- lib/legion/extensions/microsoft_teams/helpers/subscription_registry.rb
Constant Summary collapse
- MEMORY_KEY =
'teams_bot_subscriptions'
Instance Method Summary collapse
- #active_subscriptions ⇒ Object
- #find_by_peer_name(owner_id:, peer_name:) ⇒ Object
-
#initialize ⇒ SubscriptionRegistry
constructor
A new instance of SubscriptionRegistry.
- #list(owner_id:) ⇒ Object
- #load ⇒ Object
-
#pause(owner_id:, chat_id:) ⇒ Object
rubocop:disable Lint/UnusedMethodArgument.
- #persist ⇒ Object
-
#resume(owner_id:, chat_id:) ⇒ Object
rubocop:disable Lint/UnusedMethodArgument.
- #subscribe(owner_id:, chat_id:, peer_name:) ⇒ Object
-
#unsubscribe(owner_id:, chat_id:) ⇒ Object
rubocop:disable Lint/UnusedMethodArgument.
Constructor Details
#initialize ⇒ SubscriptionRegistry
Returns a new instance of SubscriptionRegistry.
15 16 17 18 19 |
# File 'lib/legion/extensions/microsoft_teams/helpers/subscription_registry.rb', line 15 def initialize @subscriptions = {} @mutex = Mutex.new load end |
Instance Method Details
#active_subscriptions ⇒ Object
62 63 64 65 66 67 |
# File 'lib/legion/extensions/microsoft_teams/helpers/subscription_registry.rb', line 62 def active_subscriptions @mutex.synchronize do @subscriptions.select { |_, v| v[:enabled] } .map { |chat_id, v| v.merge(chat_id: chat_id) } end end |
#find_by_peer_name(owner_id:, peer_name:) ⇒ Object
69 70 71 72 73 74 75 76 77 78 79 |
# File 'lib/legion/extensions/microsoft_teams/helpers/subscription_registry.rb', line 69 def find_by_peer_name(owner_id:, peer_name:) @mutex.synchronize do @subscriptions.each do |chat_id, v| next unless v[:owner_id] == owner_id next unless v[:peer_name].downcase == peer_name.downcase return v.merge(chat_id: chat_id) end nil end end |
#list(owner_id:) ⇒ Object
41 42 43 44 45 46 |
# File 'lib/legion/extensions/microsoft_teams/helpers/subscription_registry.rb', line 41 def list(owner_id:) @mutex.synchronize do @subscriptions.select { |_, v| v[:owner_id] == owner_id } .map { |chat_id, v| v.merge(chat_id: chat_id) } end end |
#load ⇒ Object
81 82 83 84 85 86 87 88 89 90 91 92 |
# File 'lib/legion/extensions/microsoft_teams/helpers/subscription_registry.rb', line 81 def load return unless memory_available? result = memory_runner.retrieve_by_domain(domain_tag: MEMORY_KEY, limit: 1) stored = result&.dig(:traces)&.first return unless stored&.dig(:content_payload) parsed = parse_stored(stored[:content_payload]) @mutex.synchronize { @subscriptions = parsed } if parsed.is_a?(Hash) rescue StandardError => e log.error("SubscriptionRegistry load failed: #{e.}") end |
#pause(owner_id:, chat_id:) ⇒ Object
rubocop:disable Lint/UnusedMethodArgument
48 49 50 51 52 53 |
# File 'lib/legion/extensions/microsoft_teams/helpers/subscription_registry.rb', line 48 def pause(owner_id:, chat_id:) # rubocop:disable Lint/UnusedMethodArgument @mutex.synchronize do @subscriptions[chat_id][:enabled] = false if @subscriptions.key?(chat_id) end persist end |
#persist ⇒ Object
94 95 96 97 98 99 100 101 102 103 104 105 106 |
# File 'lib/legion/extensions/microsoft_teams/helpers/subscription_registry.rb', line 94 def persist return unless memory_available? memory_runner.store_trace( type: :semantic, content_payload: serialize_subscriptions, domain_tags: [MEMORY_KEY], origin: :system, confidence: 1.0 ) rescue StandardError => e log.error("SubscriptionRegistry persist failed: #{e.}") end |
#resume(owner_id:, chat_id:) ⇒ Object
rubocop:disable Lint/UnusedMethodArgument
55 56 57 58 59 60 |
# File 'lib/legion/extensions/microsoft_teams/helpers/subscription_registry.rb', line 55 def resume(owner_id:, chat_id:) # rubocop:disable Lint/UnusedMethodArgument @mutex.synchronize do @subscriptions[chat_id][:enabled] = true if @subscriptions.key?(chat_id) end persist end |
#subscribe(owner_id:, chat_id:, peer_name:) ⇒ Object
21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
# File 'lib/legion/extensions/microsoft_teams/helpers/subscription_registry.rb', line 21 def subscribe(owner_id:, chat_id:, peer_name:) @mutex.synchronize do return if @subscriptions.key?(chat_id) @subscriptions[chat_id] = { owner_id: owner_id, peer_name: peer_name, enabled: true, notify: true, created_at: Time.now } end persist end |
#unsubscribe(owner_id:, chat_id:) ⇒ Object
rubocop:disable Lint/UnusedMethodArgument
36 37 38 39 |
# File 'lib/legion/extensions/microsoft_teams/helpers/subscription_registry.rb', line 36 def unsubscribe(owner_id:, chat_id:) # rubocop:disable Lint/UnusedMethodArgument @mutex.synchronize { @subscriptions.delete(chat_id) } persist end |