data_forum_icons.cpp 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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 "data/data_forum_icons.h"
  8. #include "main/main_session.h"
  9. #include "data/data_session.h"
  10. #include "data/data_document.h"
  11. #include "data/data_forum.h"
  12. #include "data/data_forum_topic.h"
  13. #include "apiwrap.h"
  14. namespace Data {
  15. ForumIcons::ForumIcons(not_null<Session*> owner)
  16. : _owner(owner)
  17. , _resetUserpicsTimer([=] { resetUserpics(); }) {
  18. }
  19. ForumIcons::~ForumIcons() = default;
  20. Main::Session &ForumIcons::session() const {
  21. return _owner->session();
  22. }
  23. void ForumIcons::requestDefaultIfUnknown() {
  24. if (_default.empty()) {
  25. requestDefault();
  26. }
  27. }
  28. void ForumIcons::refreshDefault() {
  29. requestDefault();
  30. }
  31. const std::vector<DocumentId> &ForumIcons::list() const {
  32. return _default;
  33. }
  34. rpl::producer<> ForumIcons::defaultUpdates() const {
  35. return _defaultUpdated.events();
  36. }
  37. void ForumIcons::requestDefault() {
  38. if (_defaultRequestId) {
  39. return;
  40. }
  41. auto &api = _owner->session().api();
  42. _defaultRequestId = api.request(MTPmessages_GetStickerSet(
  43. MTP_inputStickerSetEmojiDefaultTopicIcons(),
  44. MTP_int(0) // hash
  45. )).done([=](const MTPmessages_StickerSet &result) {
  46. _defaultRequestId = 0;
  47. result.match([&](const MTPDmessages_stickerSet &data) {
  48. updateDefault(data);
  49. }, [](const MTPDmessages_stickerSetNotModified &) {
  50. LOG(("API Error: Unexpected messages.stickerSetNotModified."));
  51. });
  52. }).fail([=] {
  53. _defaultRequestId = 0;
  54. }).send();
  55. }
  56. void ForumIcons::updateDefault(const MTPDmessages_stickerSet &data) {
  57. const auto &list = data.vdocuments().v;
  58. _default.clear();
  59. _default.reserve(list.size());
  60. for (const auto &sticker : list) {
  61. _default.push_back(_owner->processDocument(sticker)->id);
  62. }
  63. _defaultUpdated.fire({});
  64. }
  65. void ForumIcons::scheduleUserpicsReset(not_null<Forum*> forum) {
  66. const auto duration = crl::time(st::slideDuration);
  67. _resetUserpicsWhen[forum] = crl::now() + duration;
  68. if (!_resetUserpicsTimer.isActive()) {
  69. _resetUserpicsTimer.callOnce(duration);
  70. }
  71. }
  72. void ForumIcons::clearUserpicsReset(not_null<Forum*> forum) {
  73. _resetUserpicsWhen.remove(forum);
  74. }
  75. void ForumIcons::resetUserpics() {
  76. auto nearest = crl::time();
  77. auto now = crl::now();
  78. for (auto i = begin(_resetUserpicsWhen); i != end(_resetUserpicsWhen);) {
  79. if (i->second > now) {
  80. if (!nearest || nearest > i->second) {
  81. nearest = i->second;
  82. }
  83. ++i;
  84. } else {
  85. const auto forum = i->first;
  86. i = _resetUserpicsWhen.erase(i);
  87. resetUserpicsFor(forum);
  88. }
  89. }
  90. if (nearest) {
  91. _resetUserpicsTimer.callOnce(
  92. std::min(nearest - now, 86400 * crl::time(1000)));
  93. } else {
  94. _resetUserpicsTimer.cancel();
  95. }
  96. }
  97. void ForumIcons::resetUserpicsFor(not_null<Forum*> forum) {
  98. forum->enumerateTopics([](not_null<ForumTopic*> topic) {
  99. topic->clearUserpicLoops();
  100. });
  101. }
  102. } // namespace Data