tdesktop/Telegram/SourceFiles/data/data_poll.h

97 lines
2.4 KiB
C
Raw Normal View History

2018-12-18 05:43:11 +00:00
/*
This file is part of Telegram Desktop,
the official desktop application for the Telegram messaging service.
For license and copyright information please follow this link:
https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL
*/
#pragma once
2020-01-09 17:24:54 +00:00
namespace Data {
class Session;
} // namespace Data
2020-06-08 08:03:45 +00:00
namespace Main {
class Session;
} // namespace Main
2018-12-18 05:43:11 +00:00
struct PollAnswer {
QString text;
QByteArray option;
int votes = 0;
bool chosen = false;
bool correct = false;
2018-12-18 05:43:11 +00:00
};
inline bool operator==(const PollAnswer &a, const PollAnswer &b) {
return (a.text == b.text)
&& (a.option == b.option);
}
inline bool operator!=(const PollAnswer &a, const PollAnswer &b) {
return !(a == b);
}
struct PollData {
2020-01-09 17:24:54 +00:00
PollData(not_null<Data::Session*> owner, PollId id);
2018-12-18 05:43:11 +00:00
2020-06-08 08:03:45 +00:00
[[nodiscard]] Data::Session &owner() const;
[[nodiscard]] Main::Session &session() const;
enum class Flag {
Closed = 0x01,
PublicVotes = 0x02,
MultiChoice = 0x04,
Quiz = 0x08,
};
friend inline constexpr bool is_flag_type(Flag) { return true; };
using Flags = base::flags<Flag>;
2020-04-07 12:10:34 +00:00
bool closeByTimer();
2018-12-18 05:43:11 +00:00
bool applyChanges(const MTPDpoll &poll);
bool applyResults(const MTPPollResults &results);
void checkResultsReload(not_null<HistoryItem*> item, crl::time now);
2018-12-18 05:43:11 +00:00
[[nodiscard]] PollAnswer *answerByOption(const QByteArray &option);
[[nodiscard]] const PollAnswer *answerByOption(
const QByteArray &option) const;
2018-12-18 05:43:11 +00:00
2020-01-13 10:49:18 +00:00
void setFlags(Flags flags);
[[nodiscard]] Flags flags() const;
[[nodiscard]] bool voted() const;
[[nodiscard]] bool closed() const;
[[nodiscard]] bool publicVotes() const;
[[nodiscard]] bool multiChoice() const;
[[nodiscard]] bool quiz() const;
2018-12-19 11:20:04 +00:00
2018-12-18 05:43:11 +00:00
PollId id = 0;
QString question;
std::vector<PollAnswer> answers;
2020-01-09 17:24:54 +00:00
std::vector<not_null<UserData*>> recentVoters;
std::vector<QByteArray> sendingVotes;
2020-04-06 14:55:31 +00:00
TextWithEntities solution;
2020-04-07 12:10:34 +00:00
TimeId closePeriod = 0;
TimeId closeDate = 0;
2020-04-06 14:55:31 +00:00
int totalVoters = 0;
2018-12-18 13:56:38 +00:00
int version = 0;
static constexpr auto kMaxOptions = 10;
2018-12-18 05:43:11 +00:00
private:
bool applyResultToAnswers(
const MTPPollAnswerVoters &result,
bool isMinResults);
const not_null<Data::Session*> _owner;
Flags _flags = Flags();
crl::time _lastResultsUpdate = 0; // < 0 means force reload.
2018-12-18 05:43:11 +00:00
};
2020-04-06 14:55:31 +00:00
[[nodiscard]] MTPPoll PollDataToMTP(
not_null<const PollData*> poll,
bool close = false);
[[nodiscard]] MTPInputMedia PollDataToInputMedia(
not_null<const PollData*> poll,
bool close = false);