api_toggling_media.cpp 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  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 "api/api_toggling_media.h"
  8. #include "apiwrap.h"
  9. #include "data/data_document.h"
  10. #include "data/data_file_origin.h"
  11. #include "data/data_session.h"
  12. #include "data/stickers/data_stickers.h"
  13. #include "window/window_session_controller.h"
  14. #include "main/main_session.h"
  15. namespace Api {
  16. namespace {
  17. template <typename ToggleRequestCallback, typename DoneCallback>
  18. void ToggleExistingMedia(
  19. not_null<DocumentData*> document,
  20. Data::FileOrigin origin,
  21. ToggleRequestCallback toggleRequest,
  22. DoneCallback &&done) {
  23. const auto api = &document->owner().session().api();
  24. auto performRequest = [=](const auto &repeatRequest) -> void {
  25. const auto usedFileReference = document->fileReference();
  26. api->request(
  27. toggleRequest()
  28. ).done(done).fail([=](const MTP::Error &error) {
  29. if (error.code() == 400
  30. && error.type().startsWith(u"FILE_REFERENCE_"_q)) {
  31. auto refreshed = [=](const Data::UpdatedFileReferences &d) {
  32. if (document->fileReference() != usedFileReference) {
  33. repeatRequest(repeatRequest);
  34. }
  35. };
  36. api->refreshFileReference(origin, std::move(refreshed));
  37. }
  38. }).send();
  39. };
  40. performRequest(performRequest);
  41. }
  42. } // namespace
  43. void ToggleFavedSticker(
  44. std::shared_ptr<ChatHelpers::Show> show,
  45. not_null<DocumentData*> document,
  46. Data::FileOrigin origin) {
  47. ToggleFavedSticker(
  48. std::move(show),
  49. document,
  50. std::move(origin),
  51. !document->owner().stickers().isFaved(document));
  52. }
  53. void ToggleFavedSticker(
  54. std::shared_ptr<ChatHelpers::Show> show,
  55. not_null<DocumentData*> document,
  56. Data::FileOrigin origin,
  57. bool faved) {
  58. if (faved && !document->sticker()) {
  59. return;
  60. }
  61. auto done = [=] {
  62. document->owner().stickers().setFaved(show, document, faved);
  63. };
  64. ToggleExistingMedia(
  65. document,
  66. std::move(origin),
  67. [=, d = document] {
  68. return MTPmessages_FaveSticker(d->mtpInput(), MTP_bool(!faved));
  69. },
  70. std::move(done));
  71. }
  72. void ToggleRecentSticker(
  73. not_null<DocumentData*> document,
  74. Data::FileOrigin origin,
  75. bool saved) {
  76. if (!document->sticker()) {
  77. return;
  78. }
  79. auto done = [=] {
  80. if (!saved) {
  81. document->owner().stickers().removeFromRecentSet(document);
  82. }
  83. };
  84. ToggleExistingMedia(
  85. document,
  86. std::move(origin),
  87. [=] {
  88. return MTPmessages_SaveRecentSticker(
  89. MTP_flags(MTPmessages_SaveRecentSticker::Flag(0)),
  90. document->mtpInput(),
  91. MTP_bool(!saved));
  92. },
  93. std::move(done));
  94. }
  95. void ToggleSavedGif(
  96. std::shared_ptr<ChatHelpers::Show> show,
  97. not_null<DocumentData*> document,
  98. Data::FileOrigin origin,
  99. bool saved) {
  100. if (saved && !document->isGifv()) {
  101. return;
  102. }
  103. auto done = [=] {
  104. if (saved) {
  105. document->owner().stickers().addSavedGif(show, document);
  106. }
  107. };
  108. ToggleExistingMedia(
  109. document,
  110. std::move(origin),
  111. [=, d = document] {
  112. return MTPmessages_SaveGif(d->mtpInput(), MTP_bool(!saved));
  113. },
  114. std::move(done));
  115. }
  116. void ToggleSavedRingtone(
  117. not_null<DocumentData*> document,
  118. Data::FileOrigin origin,
  119. Fn<void()> &&done,
  120. bool saved) {
  121. ToggleExistingMedia(
  122. document,
  123. std::move(origin),
  124. [=, d = document] {
  125. return MTPaccount_SaveRingtone(d->mtpInput(), MTP_bool(!saved));
  126. },
  127. std::move(done));
  128. }
  129. } // namespace Api