Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Strange behavior of getHistoryChat when only_local == true #3166

Closed
gmb15551 opened this issue Dec 5, 2024 · 3 comments
Closed

Strange behavior of getHistoryChat when only_local == true #3166

gmb15551 opened this issue Dec 5, 2024 · 3 comments

Comments

@gmb15551
Copy link

gmb15551 commented Dec 5, 2024

I'm trying to understand the details of how the libtdjson library works. Below is a piece of code that attempts to retrieve messages for a specified chat with limit number of messages. When the requests are made, the only_local flag is set to true to ensure that the data is read only from the local database without making any requests to the server. For the sake of the experiment, the database is wiped before the first run, and it is reinitialized afterwards.

It is expected that when the getHistory function is executed for the first time, the result will be empty because there are no messages in the database, as it is recreated from scratch. However, after the first run, it turns out that when limit=1000, the function returns 1 message. This is likely because the library loads a small part of the history into the local storage during the first run. In this case, it’s just 1 message.

Then, I restart the program. Now, after executing the function, I can read 51 messages. This is strange, given that the only_local flag is applied. Because if, during the previous run, the function fetched an additional 50 messages from the server just in case, it means that, first of all, it made a request to the server, fetched a batch of messages to respond to user queries, but did not return those messages in the function result. On the other hand, if the function requested 50 new messages after the restart, why didn’t it do that the previous time? I’m a bit confused here.

If I restart the program again, the function returns 50 more messages than the previous run.

Could you please help answer the following questions?

  1. Why, despite setting only_local == true, does the number of messages returned by the getHistory function increase by 50 after each restart compared to the previous run?

  2. Does the libtdjson library automatically load the chat history or part of it into the local storage during the first run, without any explicit command from the user like getHistoryChat? Does this depend on the settings used when initializing the library? Does it do this every time the program is restarted if the local database already exists?

  3. In the Messages class, it is mentioned that the list of messages can be null. In what case is the message list null when retrieving chat history, and in what case is it simply an empty list?

Future<void> getHistory(Int53 chatId, int limit) async {
    var fromMessageId = Int53(0);
    int left = limit;
    int receivedMessagesCount = 0;
    while (left > 0) {
      Messages messages = await client.getChatHistory(
        chat_id: chatId,
        from_message_id: fromMessageId,
        limit: Int32(left),
        offset: Int32(0),
        only_local: true,
      );
      if (messages.messages == null || messages.messages!.isEmpty) {
        break;
      }
      for (Message m in messages.messages!) {
        receivedMessagesCount++;
      }

      fromMessageId = messages.messages!.last.id;
      left -= messages.messages!.length;
    }
    log.i(receivedMessagesCount, "receivedMessagesCount");
  }

Настройки библиотеки применяются такие:

useMessageDatabase: true
useFileDatabase: true
useChatInfoDatabase: true
useSecretChats: true
@levlam
Copy link
Contributor

levlam commented Dec 5, 2024

  1. TDLib has message history preloading strategies to provide smooth scrollback experience.
  2. No, TDLib doesn't load data without app request. You run a request that required loading of the chat, and the last message is maintained for all chats from a chat list.
  3. Individual messages in the list can be null, but not the list itself. You can just skip null messages in the response.

@gmb15551 gmb15551 closed this as completed Dec 5, 2024
@gmb15551 gmb15551 reopened this Dec 5, 2024
@gmb15551
Copy link
Author

gmb15551 commented Dec 5, 2024

I will do that, I’ll skip the null messages, but my curiosity is getting the best of me. Why can a message even be null? Is it some kind of deleted message?

@levlam
Copy link
Contributor

levlam commented Dec 5, 2024

Actually, getChatHistory will never return null messages in the recent TDLib versions, but previously null could have been returned in place of just deleted messages, which were deleted right before the response was returned to the app.

@gmb15551 gmb15551 closed this as completed Dec 5, 2024
@gmb15551 gmb15551 reopened this Dec 5, 2024
@gmb15551 gmb15551 closed this as completed Dec 5, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants