data_poll.h 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  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. namespace Data {
  9. class Session;
  10. } // namespace Data
  11. namespace Main {
  12. class Session;
  13. } // namespace Main
  14. struct PollAnswer {
  15. TextWithEntities text;
  16. QByteArray option;
  17. int votes = 0;
  18. bool chosen = false;
  19. bool correct = false;
  20. };
  21. inline bool operator==(const PollAnswer &a, const PollAnswer &b) {
  22. return (a.text == b.text)
  23. && (a.option == b.option);
  24. }
  25. inline bool operator!=(const PollAnswer &a, const PollAnswer &b) {
  26. return !(a == b);
  27. }
  28. struct PollData {
  29. PollData(not_null<Data::Session*> owner, PollId id);
  30. [[nodiscard]] Data::Session &owner() const;
  31. [[nodiscard]] Main::Session &session() const;
  32. enum class Flag {
  33. Closed = 0x01,
  34. PublicVotes = 0x02,
  35. MultiChoice = 0x04,
  36. Quiz = 0x08,
  37. };
  38. friend inline constexpr bool is_flag_type(Flag) { return true; };
  39. using Flags = base::flags<Flag>;
  40. bool closeByTimer();
  41. bool applyChanges(const MTPDpoll &poll);
  42. bool applyResults(const MTPPollResults &results);
  43. [[nodiscard]] bool checkResultsReload(crl::time now);
  44. [[nodiscard]] PollAnswer *answerByOption(const QByteArray &option);
  45. [[nodiscard]] const PollAnswer *answerByOption(
  46. const QByteArray &option) const;
  47. void setFlags(Flags flags);
  48. [[nodiscard]] Flags flags() const;
  49. [[nodiscard]] bool voted() const;
  50. [[nodiscard]] bool closed() const;
  51. [[nodiscard]] bool publicVotes() const;
  52. [[nodiscard]] bool multiChoice() const;
  53. [[nodiscard]] bool quiz() const;
  54. PollId id = 0;
  55. TextWithEntities question;
  56. std::vector<PollAnswer> answers;
  57. std::vector<not_null<PeerData*>> recentVoters;
  58. std::vector<QByteArray> sendingVotes;
  59. TextWithEntities solution;
  60. TimeId closePeriod = 0;
  61. TimeId closeDate = 0;
  62. int totalVoters = 0;
  63. int version = 0;
  64. static constexpr auto kMaxOptions = 10;
  65. private:
  66. bool applyResultToAnswers(
  67. const MTPPollAnswerVoters &result,
  68. bool isMinResults);
  69. const not_null<Data::Session*> _owner;
  70. Flags _flags = Flags();
  71. crl::time _lastResultsUpdate = 0; // < 0 means force reload.
  72. };
  73. [[nodiscard]] MTPPoll PollDataToMTP(
  74. not_null<const PollData*> poll,
  75. bool close = false);
  76. [[nodiscard]] MTPInputMedia PollDataToInputMedia(
  77. not_null<const PollData*> poll,
  78. bool close = false);