File indexing completed on 2024-04-28 16:10:25

0001 // SPDX-FileCopyrightText: 2022 Tobias Fella <tobias.fella@kde.org>
0002 // SPDX-License-Identifier: LGPL-2.0-or-later
0003 
0004 #pragma once
0005 
0006 #include <QJsonObject>
0007 #include <QObject>
0008 #include <QPair>
0009 
0010 class NeoChatRoom;
0011 
0012 /**
0013  * @class PollHandler
0014  *
0015  * A class to help manage a poll in a room.
0016  *
0017  * A poll is made up of a start event that poses the question and possible answers,
0018  * and is followed by a series of response events as users in the room select
0019  * their choice. This purpose of the poll handler is to keep track of all this as
0020  * the poll is displayed as a single event in the timeline which merges all this
0021  * information.
0022  */
0023 class PollHandler : public QObject
0024 {
0025     Q_OBJECT
0026 
0027     /**
0028      * @brief The current room for the poll.
0029      */
0030     Q_PROPERTY(NeoChatRoom *room READ room WRITE setRoom NOTIFY roomChanged)
0031 
0032     /**
0033      * @brief The Matrix event ID for the event that started the poll.
0034      */
0035     Q_PROPERTY(QString pollStartEventId READ pollStartEventId WRITE setPollStartEventId NOTIFY pollStartEventIdChanged)
0036 
0037     /**
0038      * @brief The list of answers to the poll from users in the room.
0039      */
0040     Q_PROPERTY(QJsonObject answers READ answers NOTIFY answersChanged)
0041 
0042     /**
0043      * @brief The list number of votes for each answer in the poll.
0044      */
0045     Q_PROPERTY(QJsonObject counts READ counts NOTIFY answersChanged)
0046 
0047     /**
0048      * @brief Whether the poll has ended.
0049      */
0050     Q_PROPERTY(bool hasEnded READ hasEnded NOTIFY hasEndedChanged)
0051 
0052     /**
0053      * @brief The total number of answers to the poll.
0054      */
0055     Q_PROPERTY(int answerCount READ answerCount NOTIFY answersChanged)
0056 
0057 public:
0058     PollHandler(QObject *parent = nullptr);
0059 
0060     NeoChatRoom *room() const;
0061     void setRoom(NeoChatRoom *room);
0062 
0063     QString pollStartEventId() const;
0064     void setPollStartEventId(const QString &eventId);
0065 
0066     bool hasEnded() const;
0067     int answerCount() const;
0068 
0069     QJsonObject answers() const;
0070     QJsonObject counts() const;
0071 
0072     /**
0073      * @brief Send an answer to the poll.
0074      */
0075     Q_INVOKABLE void sendPollAnswer(const QString &eventId, const QString &answerId);
0076 
0077 Q_SIGNALS:
0078     void roomChanged();
0079     void pollStartEventIdChanged();
0080     void answersChanged();
0081     void hasEndedChanged();
0082 
0083 private:
0084     NeoChatRoom *m_room = nullptr;
0085     QString m_pollStartEventId;
0086 
0087     void checkLoadRelations();
0088     void handleAnswer(const QJsonObject &object, const QString &sender, QDateTime timestamp);
0089     QMap<QString, QDateTime> m_answerTimestamps;
0090     QJsonObject m_answers;
0091     int m_maxVotes = 1;
0092     bool m_hasEnded = false;
0093     QDateTime m_endedTimestamp;
0094 };