data_peer_bot_commands.cpp 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  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_peer_bot_commands.h"
  8. namespace Data {
  9. ChatBotCommands::Changed ChatBotCommands::update(
  10. const std::vector<BotCommands> &list) {
  11. auto changed = false;
  12. if (list.empty()) {
  13. changed = (list.empty() != empty());
  14. clear();
  15. } else {
  16. for (const auto &commands : list) {
  17. if (commands.commands.empty()) {
  18. changed |= remove(commands.userId);
  19. } else {
  20. auto &value = operator[](commands.userId);
  21. const auto isEqual = ranges::equal(value, commands.commands);
  22. changed |= !isEqual;
  23. if (!isEqual) {
  24. value = commands.commands;
  25. }
  26. }
  27. }
  28. }
  29. return changed;
  30. }
  31. BotCommands BotCommandsFromTL(const MTPBotInfo &result) {
  32. return result.match([](const MTPDbotInfo &data) {
  33. const auto userId = data.vuser_id()
  34. ? UserId(*data.vuser_id())
  35. : UserId();
  36. if (!data.vcommands()) {
  37. return BotCommands{ .userId = userId };
  38. }
  39. auto commands = ranges::views::all(
  40. data.vcommands()->v
  41. ) | ranges::views::transform(BotCommandFromTL) | ranges::to_vector;
  42. return BotCommands{
  43. .userId = userId,
  44. .commands = std::move(commands),
  45. };
  46. });
  47. }
  48. } // namespace Data