Commit Graph

15 Commits

Author SHA1 Message Date
Mikhail Kilin
72c4a886fa fix: implement dynamic message history loading with retry logic
Проблема:
- При открытии чата видно только последнее сообщение
- TDLib возвращал 1 сообщение при первом запросе
- Не было retry логики для ожидания синхронизации с сервером

Решение:
1. Динамическая загрузка с retry (до 20 попыток на чанк)
2. Загрузка всей доступной истории (без лимита)
3. Retry при получении малого количества сообщений
4. Корректная чанковая загрузка по 50 сообщений

Алгоритм:
- При открытии чата: get_chat_history(i32::MAX) - загружает всё
- Чанками по 50: TDLIB_MESSAGE_LIMIT
- Retry если получено < 50 при первой загрузке
- Остановка если 3 раза подряд пусто
- Порядок: старые чанки вставляются в начало (splice)
- При скролле: load_older_messages_if_needed() подгружает автоматически

Изменения:
src/tdlib/messages.rs:
- Убрана фиксированная задержка 100ms после open_chat
- Добавлен счетчик consecutive_empty_results
- Retry логика без искусственных sleep()
- Проверка: если получено мало - продолжить попытки

src/input/main_input.rs:
- limit: 100 → i32::MAX (без ограничений)
- timeout: 10s → 30s

tests/chat_list.rs:
- test_chat_history_chunked_loading: проверка 100, 120, 200 сообщений
- test_chat_history_loads_all_without_limit: загрузка 200 без лимита
- test_load_older_messages_pagination: подгрузка при скролле

Все тесты: 104/104 

Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
2026-02-04 03:56:58 +03:00
Mikhail Kilin
222a21770c fix: implement chunked message loading to fill screen on chat open
Проблема:
- get_chat_history() загружала только один чанк (50 сообщений max)
- При запросе 100 сообщений возвращалось только 50
- Экран не заполнялся полностью при открытии чата

Решение:
- Добавлена чанковая загрузка по TDLIB_MESSAGE_LIMIT (50) сообщений
- Автоматическая подгрузка пока не достигнут запрошенный limit
- Правильная сборка сообщений (старые чанки вставляются в начало)
- Retry логика для каждого чанка (до 3 попыток)

Изменения в src/tdlib/messages.rs:
- get_chat_history(): цикл загрузки чанков вместо одного запроса
- Вставка более старых чанков в начало списка (splice)
- Обработка edge cases (пустые результаты, ошибки, конец истории)

Тесты:
- test_chat_history_chunked_loading: проверка загрузки 100, 120, 200 сообщений
- Проверка правильного порядка сообщений (от старых к новым)
- Проверка границы между чанками (messages 50/51)

Все тесты пройдены: 343/343 

Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
2026-02-04 02:48:30 +03:00
Mikhail Kilin
c881f74ecb refactor: complete nesting simplification (category 3 - 100%)
Simplified deep nesting across the codebase using modern Rust patterns:
let-else guards, early returns, iterator chains, and extracted functions.

**Files improved:**

1. src/tdlib/messages.rs (44 → 28 spaces max indent)
   - fetch_missing_reply_info(): 7 → 2-3 levels
     * Extracted fetch_and_update_reply()
     * Used filter_map and iterator chains
   - get_chat_history() retry loop: 6 → 3 levels
     * Early continue for empty results
     * Used .flatten() instead of nested if-let

2. src/input/main_input.rs (40 → 36 spaces max indent)
   - handle_forward_mode(): 7 → 2-3 levels
     * Extracted forward_selected_message()
   - Reaction picker: 5 → 2-3 levels
     * Extracted send_reaction()
   - Scroll + load older: 6 → 2-3 levels
     * Extracted load_older_messages_if_needed()

3. src/config.rs (36 → 32 spaces max indent)
   - load_credentials(): 7 → 2-3 levels
     * Extracted load_credentials_from_file()
     * Extracted load_credentials_from_env()
     * Used ? operator for Option chains

**Results:**
- Max nesting in entire project: ≤32 spaces (8 levels)
- 8 new functions extracted for better separation of concerns
- All 343 tests passing 

Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
2026-02-04 02:35:56 +03:00
Mikhail Kilin
dec60ea74e fix: mark incoming messages as read when opening chat and load all history
Fixes two critical bugs:
1. Unread badge not clearing when opening chat - incoming messages weren't marked as viewed
2. Only last 2-3 messages loaded instead of full history due to incorrect break condition

Changes:
- Add incoming message IDs to pending_view_messages queue on chat open
- Remove premature break in get_chat_history() that stopped after 2 messages
- Add FakeTdClient.pending_view_messages field for testing
- Implement process_pending_view_messages() in FakeTdClient

Tests added:
- test_incoming_message_shows_unread_badge: verify "(1)" appears for unread
- test_opening_chat_clears_unread_badge: verify badge clears after opening
- test_opening_chat_loads_many_messages: verify all 50 messages load, not just last few

All 28 chat_list tests pass.
2026-02-04 02:07:47 +03:00
Mikhail Kilin
5ac10ea24c refactor: complete large files/functions refactoring (Phase 6-7)
Phase 6: Refactor tdlib/client.rs 
- Extract update handlers to update_handlers.rs (302 lines, 8 functions)
- Extract message converter to message_converter.rs (250 lines, 6 functions)
- Extract chat helpers to chat_helpers.rs (149 lines, 3 functions)
- Result: client.rs 1259 → 599 lines (-52%)

Phase 7: Refactor tdlib/messages.rs 
- Create message_conversion.rs module (158 lines)
- Extract 6 helper functions:
  - extract_content_text() - content extraction (~80 lines)
  - extract_entities() - formatting extraction (~10 lines)
  - extract_sender_name() - sender name with API call (~15 lines)
  - extract_forward_info() - forward info (~12 lines)
  - extract_reply_info() - reply info (~15 lines)
  - extract_reactions() - reactions extraction (~26 lines)
- Result: convert_message() 150 → 57 lines (-62%)
- Result: messages.rs 850 → 757 lines (-11%)

Summary:
-  All 4 large files refactored (100%)
-  All 629 tests passing
-  Category #2 "Large files/functions" COMPLETE
-  Documentation updated (REFACTORING_OPPORTUNITIES.md, CONTEXT.md)

Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
2026-02-04 01:29:26 +03:00
Mikhail Kilin
2b04b785c0 refactor: cleanup unused code and warnings
Some checks failed
CI / Check (pull_request) Has been cancelled
CI / Format (pull_request) Has been cancelled
CI / Clippy (pull_request) Has been cancelled
CI / Build (macos-latest) (pull_request) Has been cancelled
CI / Build (ubuntu-latest) (pull_request) Has been cancelled
CI / Build (windows-latest) (pull_request) Has been cancelled
Comprehensive cleanup of unused methods, dead code, and compiler warnings:

## Removed Methods (15):
- Duplicate delegation methods: is_authenticated, get_typing_text, get_user_name from TdClient
- Obsolete methods: TdClient::init() (duplicated in main.rs)
- Unused getters: UserCache::{get_username, get_name, get_user_id_by_chat}
- Unused builder methods: MessageBuilder::{edited, add_reaction}
- Unused utility: ChatState::is_normal()
- Dead code: HotkeysConfig::{matches, key_matches} (kept for tests)
- Unused method: UserCache::register_private_chat()
- Getter replaced with direct field access: MessageInfo::edit_date()

## Removed Module:
- error.rs - Unused error handling module (TeletuiError, ErrorVariant, IntoTeletuiError)

## Removed Constants (8):
- EMOJI_PICKER_COLUMNS, EMOJI_PICKER_ROWS, MAX_INPUT_HEIGHT
- MIN_TERMINAL_WIDTH, MIN_TERMINAL_HEIGHT
- TDLIB_CHAT_LIMIT, MAX_USERNAME_DISPLAY_LENGTH, MESSAGE_TEXT_INDENT

## Fixed Warnings:
- Removed unused imports (8 instances)
- Fixed unreachable patterns (10 instances)
- Fixed irrefutable if let patterns (2 instances)
- Fixed unused variables (1 instance)
- Removed dead_code annotations where appropriate

## Improvements:
- Integrated Config::load_credentials() into TdClient::new() for better credential management
- Replaced edit_date() getter with direct field access (message.metadata.edit_date)
- Updated tests to use direct field access instead of removed getters

## Test Results:
All tests passing: 499 passed, 0 failed

Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
2026-02-01 18:57:55 +03:00
Mikhail Kilin
f1a26b906c style: use _chat_id instead of let _ = chat_id
More idiomatic way to mark unused parameter.

Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
2026-02-01 02:48:44 +03:00
Mikhail Kilin
c27d027ebf chore: remove dead code and unnecessary allow(dead_code) attributes
Cleaned up warnings by removing unused code:
- Removed unused format_timestamp() function from utils.rs
- Removed unused len() method from LruCache
- Removed unused date field from ForwardInfo struct
- Removed unnecessary #[allow(dead_code)] attributes from:
  * AuthState enum (actually used)
  * ChatInfo struct (actually used)
  * TdClient impl block (actually used)

This reduces code noise and makes real warnings more visible.

Changes:
- 20 lines removed
- 1 line added
- 6 files changed

Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
2026-02-01 02:47:51 +03:00
Mikhail Kilin
93e43a59d0 docs: complete rustdoc documentation for all public APIs (P4.12)
Added comprehensive rustdoc documentation for all TDLib modules,
configuration, and utility functions.

TDLib modules documented:
- src/tdlib/auth.rs - AuthManager, AuthState (6 doctests)
- src/tdlib/chats.rs - ChatManager (8 doctests)
- src/tdlib/messages.rs - MessageManager (14 methods, 6 doctests)
- src/tdlib/reactions.rs - ReactionManager (3 doctests)
- src/tdlib/users.rs - UserCache, LruCache (2 doctests)

Configuration and utilities:
- src/config.rs - Config, ColorsConfig, GeneralConfig (4 doctests)
- src/formatting.rs - format_text_with_entities (2 doctests)

Documentation includes:
- Detailed descriptions of all public structs and methods
- Usage examples with code snippets
- Parameter and return value documentation
- Notes about async behavior and edge cases
- Cross-references between related functions

Total: 34 doctests (30 ignored for async, 4 compiled)
All 464 unit tests passing 

Priority 4.12 (Rustdoc) - 100% complete

Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
2026-02-01 01:03:30 +03:00
Mikhail Kilin
0ae8a2fb88 fix: update UI after editing message
Issue: Message edits worked on server (other users saw changes),
but local UI didn't update - edited text wasn't visible.

Root cause: Code was updating individual fields instead of replacing
the whole message, and wasn't triggering UI redraw.

Solution:
- Replace entire message with edited_msg (not individual fields)
- Set needs_redraw = true to trigger UI update
- Remove debug logging

Now edited messages immediately appear in local UI.

Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
2026-02-01 00:16:47 +03:00
Mikhail Kilin
fe924faff4 debug: add logging for edit_message to diagnose 'Message not found' error
- Log chat_id, message_id, text length before calling edit_message_text
- Log success or exact TDLib error
- This will help identify the root cause

Temporary debug commit to investigate issue.
2026-02-01 00:08:00 +03:00
Mikhail Kilin
07c401e0f9 fix: исправлены баги с сообщениями, редактированием и reply
- Изменён порядок хранения сообщений (теперь от старых к новым)
- Исправлена логика выбора сообщений для редактирования
- Исправлена отправка reply (структура условий)
- Добавлено сохранение reply_info при отправке
- Удалены отладочные логи

Fixes: сообщения теперь отображаются корректно в UI
Fixes: редактирование работает без ошибки 'Message not found'
Fixes: reply показывает превью исходного сообщения
2026-01-31 18:29:02 +03:00
Mikhail Kilin
644e36597d fixes 2026-01-31 03:48:50 +03:00
Mikhail Kilin
7081a886ad refactor: implement newtype pattern for IDs (P2.4)
Добавлены типобезопасные обёртки ChatId, MessageId, UserId для предотвращения
смешивания разных типов идентификаторов на этапе компиляции.

Изменения:
- Создан src/types.rs с тремя newtype структурами
- Реализованы методы: new(), as_i64(), From<i64>, Display
- Добавлены traits: Hash, Eq, Serialize, Deserialize
- Обновлены 15+ модулей для использования новых типов:
  * tdlib: types.rs, chats.rs, messages.rs, users.rs, reactions.rs, client.rs
  * app: mod.rs, chat_state.rs
  * input: main_input.rs
  * tests: app_builder.rs, test_data.rs
- Исправлены 53 ошибки компиляции связанные с type conversions

Преимущества:
- Компилятор предотвращает смешивание разных типов ID
- Улучшенная читаемость кода (явные типы вместо i64)
- Самодокументирующиеся типы

Статус: Priority 2 теперь 60% (3/5 задач)
-  Error enum
-  Config validation
-  Newtype для ID
-  MessageInfo реструктуризация
-  MessageBuilder pattern

Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
2026-01-31 01:33:18 +03:00
Mikhail Kilin
bba5cbd22d fixes 2026-01-30 23:55:01 +03:00