api_user_names.h 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  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. #pragma once
  8. #include "data/data_user_names.h"
  9. #include "mtproto/sender.h"
  10. class ApiWrap;
  11. class PeerData;
  12. namespace Main {
  13. class Session;
  14. } // namespace Main
  15. namespace Api {
  16. class Usernames final {
  17. public:
  18. enum class Error {
  19. TooMuch,
  20. Unknown,
  21. };
  22. explicit Usernames(not_null<ApiWrap*> api);
  23. [[nodiscard]] rpl::producer<Data::Usernames> loadUsernames(
  24. not_null<PeerData*> peer) const;
  25. [[nodiscard]] rpl::producer<rpl::no_value, Error> toggle(
  26. not_null<PeerData*> peer,
  27. const QString &username,
  28. bool active);
  29. [[nodiscard]] rpl::producer<> reorder(
  30. not_null<PeerData*> peer,
  31. const std::vector<QString> &usernames);
  32. void requestToCache(not_null<PeerData*> peer);
  33. [[nodiscard]] Data::Usernames cacheFor(PeerId id);
  34. static Data::Usernames FromTL(const MTPVector<MTPUsername> &usernames);
  35. private:
  36. const not_null<Main::Session*> _session;
  37. MTP::Sender _api;
  38. using Key = PeerId;
  39. struct Entry final {
  40. rpl::event_stream<rpl::no_value, Error> done;
  41. std::vector<QString> usernames;
  42. };
  43. base::flat_map<Key, Entry> _toggleRequests;
  44. base::flat_map<Key, mtpRequestId> _reorderRequests;
  45. // Used for a seamless display of usernames list.
  46. std::pair<Key, Data::Usernames> _tinyCache;
  47. };
  48. } // namespace Api