Class: Teems::Services::SyncEngine

Inherits:
Object
  • Object
show all
Includes:
SyncPagination, SyncSerializer
Defined in:
lib/teems/services/sync_engine.rb

Overview

Core sync logic: fetch, merge, and write chat messages. Extracted from Commands::Sync to keep the command thin.

Constant Summary

Constants included from SyncPagination

Teems::Services::SyncPagination::API_DELAY_SECONDS, Teems::Services::SyncPagination::MAX_PAGES

Instance Method Summary collapse

Constructor Details

#initialize(runner:, sync_store:, state:, output:) ⇒ SyncEngine

Returns a new instance of SyncEngine.



130
131
132
133
134
135
# File 'lib/teems/services/sync_engine.rb', line 130

def initialize(runner:, sync_store:, state:, output:)
  @runner = runner
  @sync_store = sync_store
  @state = state
  @output = output
end

Instance Method Details

#fetch_all_messages(chat_id, start_time) ⇒ Object

Fetch all messages from a chat since start_time with pagination



138
139
140
141
# File 'lib/teems/services/sync_engine.rb', line 138

def fetch_all_messages(chat_id, start_time)
  messages = paginate_messages(chat_id, start_time)
  filter_and_sort_messages(messages, start_time)
end

#merge_and_write(chat, existing_raw, new_messages) ⇒ Object



143
144
145
146
147
148
# File 'lib/teems/services/sync_engine.rb', line 143

def merge_and_write(chat, existing_raw, new_messages)
  existing = existing_raw.map { |data| message_from_stored(data) }
  all_messages = merge_messages(existing, new_messages)
  write_chat_files(chat, all_messages)
  all_messages
end

#message_from_stored(data) ⇒ Object



150
# File 'lib/teems/services/sync_engine.rb', line 150

def message_from_stored(data) = Models::Message.new(**stored_msg_attrs(data))