api_attached_stickers.cpp 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  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_attached_stickers.h"
  8. #include "apiwrap.h"
  9. #include "ui/boxes/confirm_box.h"
  10. #include "boxes/sticker_set_box.h"
  11. #include "boxes/stickers_box.h"
  12. #include "data/data_document.h"
  13. #include "data/data_photo.h"
  14. #include "lang/lang_keys.h"
  15. #include "window/window_session_controller.h"
  16. namespace Api {
  17. AttachedStickers::AttachedStickers(not_null<ApiWrap*> api)
  18. : _api(&api->instance()) {
  19. }
  20. void AttachedStickers::request(
  21. not_null<Window::SessionController*> controller,
  22. MTPmessages_GetAttachedStickers &&mtpRequest) {
  23. const auto weak = base::make_weak(controller);
  24. _api.request(_requestId).cancel();
  25. _requestId = _api.request(
  26. std::move(mtpRequest)
  27. ).done([=](const MTPVector<MTPStickerSetCovered> &result) {
  28. _requestId = 0;
  29. const auto strongController = weak.get();
  30. if (!strongController) {
  31. return;
  32. }
  33. if (result.v.isEmpty()) {
  34. strongController->show(
  35. Ui::MakeInformBox(tr::lng_stickers_not_found()));
  36. return;
  37. } else if (result.v.size() > 1) {
  38. strongController->show(
  39. Box<StickersBox>(strongController->uiShow(), result.v));
  40. return;
  41. }
  42. // Single attached sticker pack.
  43. const auto data = result.v.front().match([&](const auto &data) {
  44. return &data.vset().data();
  45. });
  46. const auto setId = (data->vid().v && data->vaccess_hash().v)
  47. ? StickerSetIdentifier{
  48. .id = data->vid().v,
  49. .accessHash = data->vaccess_hash().v }
  50. : StickerSetIdentifier{ .shortName = qs(data->vshort_name()) };
  51. strongController->show(Box<StickerSetBox>(
  52. strongController->uiShow(),
  53. setId,
  54. (data->is_emojis()
  55. ? Data::StickersType::Emoji
  56. : data->is_masks()
  57. ? Data::StickersType::Masks
  58. : Data::StickersType::Stickers)));
  59. }).fail([=] {
  60. _requestId = 0;
  61. if (const auto strongController = weak.get()) {
  62. strongController->show(
  63. Ui::MakeInformBox(tr::lng_stickers_not_found()));
  64. }
  65. }).send();
  66. }
  67. void AttachedStickers::requestAttachedStickerSets(
  68. not_null<Window::SessionController*> controller,
  69. not_null<PhotoData*> photo) {
  70. request(
  71. controller,
  72. MTPmessages_GetAttachedStickers(
  73. MTP_inputStickeredMediaPhoto(photo->mtpInput())));
  74. }
  75. void AttachedStickers::requestAttachedStickerSets(
  76. not_null<Window::SessionController*> controller,
  77. not_null<DocumentData*> document) {
  78. request(
  79. controller,
  80. MTPmessages_GetAttachedStickers(
  81. MTP_inputStickeredMediaDocument(document->mtpInput())));
  82. }
  83. } // namespace Api