support_preload.cpp 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. /*
  2. This file is part of Telegram Desktop,
  3. the official desktop application for the Telegram messaging service.
  4. For license and copyright information please follow this link:
  5. https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL
  6. */
  7. #include "support/support_preload.h"
  8. #include "history/history.h"
  9. #include "data/data_peer.h"
  10. #include "data/data_session.h"
  11. #include "data/data_histories.h"
  12. #include "main/main_session.h"
  13. #include "apiwrap.h"
  14. namespace Support {
  15. namespace {
  16. constexpr auto kPreloadMessagesCount = 50;
  17. } // namespace
  18. int SendPreloadRequest(not_null<History*> history, Fn<void()> retry) {
  19. auto offsetId = MsgId();
  20. auto offset = 0;
  21. auto loadCount = kPreloadMessagesCount;
  22. if (const auto around = history->loadAroundId()) {
  23. history->getReadyFor(ShowAtUnreadMsgId);
  24. offset = -loadCount / 2;
  25. offsetId = around;
  26. }
  27. const auto offsetDate = 0;
  28. const auto maxId = 0;
  29. const auto minId = 0;
  30. const auto historyHash = uint64(0);
  31. const auto type = Data::Histories::RequestType::History;
  32. auto &histories = history->owner().histories();
  33. return histories.sendRequest(history, type, [=](Fn<void()> finish) {
  34. return history->session().api().request(MTPmessages_GetHistory(
  35. history->peer->input,
  36. MTP_int(offsetId),
  37. MTP_int(offsetDate),
  38. MTP_int(offset),
  39. MTP_int(loadCount),
  40. MTP_int(maxId),
  41. MTP_int(minId),
  42. MTP_long(historyHash)
  43. )).done([=](const MTPmessages_Messages &result) {
  44. if (const auto around = history->loadAroundId()) {
  45. if (around != offsetId) {
  46. retry();
  47. return;
  48. }
  49. history->clear(History::ClearType::Unload);
  50. history->getReadyFor(ShowAtUnreadMsgId);
  51. } else if (offsetId) {
  52. retry();
  53. return;
  54. } else {
  55. history->clear(History::ClearType::Unload);
  56. history->getReadyFor(ShowAtTheEndMsgId);
  57. }
  58. result.match([](const MTPDmessages_messagesNotModified&) {
  59. }, [&](const auto &data) {
  60. history->owner().processUsers(data.vusers());
  61. history->owner().processChats(data.vchats());
  62. history->addOlderSlice(data.vmessages().v);
  63. });
  64. finish();
  65. }).fail([=](const MTP::Error &error) {
  66. finish();
  67. }).send();
  68. });
  69. }
  70. } // namespace Support