File indexing completed on 2024-04-14 15:03:13

0001 // SPDX-FileCopyrightText: 2020 Tobias Fella <tobias.fella@kde.org>
0002 // SPDX-License-Identifier: GPL-2.0-or-later
0003 
0004 #pragma once
0005 
0006 #include <QImage>
0007 #include <QJsonObject>
0008 #include <QMap>
0009 #include <QObject>
0010 #include <QPointer>
0011 #include <QString>
0012 #include <Quotient/csapi/notifications.h>
0013 #include <Quotient/jobs/basejob.h>
0014 
0015 namespace Quotient
0016 {
0017 class Connection;
0018 }
0019 
0020 class KNotification;
0021 class NeoChatRoom;
0022 
0023 class PushNotificationAction : public QObject
0024 {
0025     Q_OBJECT
0026 
0027 public:
0028     /**
0029      * @brief Defines the global push notification actions.
0030      */
0031     enum Action {
0032         Unknown = 0, /**< The action has not yet been obtained from the server. */
0033         Off, /**< No push notifications are to be sent. */
0034         On, /**< Push notifications are on. */
0035         Noisy, /**< Push notifications are on, also trigger a notification sound. */
0036         Highlight, /**< Push notifications are on, also the event should be highlighted in chat. */
0037         NoisyHighlight, /**< Push notifications are on, also trigger a notification sound and highlight in chat. */
0038     };
0039     Q_ENUM(Action)
0040 };
0041 
0042 /**
0043  * @class NotificationsManager
0044  *
0045  * This class is responsible for managing notifications.
0046  *
0047  * This includes sending native notifications on mobile or desktop if available as
0048  * well as managing the push notification rules for the current active account.
0049  *
0050  * @note Matrix manages push notifications centrally for an account and these are
0051  *       stored on the home server. This is to allow a users settings to move between clients.
0052  *       See https://spec.matrix.org/v1.3/client-server-api/#push-rules for more
0053  *       details.
0054  */
0055 class NotificationsManager : public QObject
0056 {
0057     Q_OBJECT
0058 
0059 public:
0060     static NotificationsManager &instance();
0061 
0062     /**
0063      * @brief Display a native notification for an message.
0064      */
0065     Q_INVOKABLE void
0066     postNotification(NeoChatRoom *room, const QString &sender, const QString &text, const QImage &icon, const QString &replyEventId, bool canReply);
0067 
0068     /**
0069      * @brief Display a native notification for an invite.
0070      */
0071     void postInviteNotification(NeoChatRoom *room, const QString &title, const QString &sender, const QImage &icon);
0072 
0073     /**
0074      * @brief Clear an existing invite notification for the given room.
0075      *
0076      * Nothing happens if the given room doesn't have an invite notification.
0077      */
0078     void clearInvitationNotification(const QString &roomId);
0079 
0080     /**
0081      * @brief Handle the notifications for the given connection.
0082      */
0083     void handleNotifications(QPointer<Quotient::Connection> connection);
0084 
0085 private:
0086     explicit NotificationsManager(QObject *parent = nullptr);
0087 
0088     QHash<QString, qint64> m_initialTimestamp;
0089     QHash<QString, QStringList> m_oldNotifications;
0090 
0091     QStringList m_connActiveJob;
0092 
0093     bool shouldPostNotification(QPointer<Quotient::Connection> connection, const QJsonValue &notification);
0094 
0095     QHash<QString, KNotification *> m_notifications;
0096     QHash<QString, QPointer<KNotification>> m_invitations;
0097 
0098 private Q_SLOTS:
0099     void processNotificationJob(QPointer<Quotient::Connection> connection, Quotient::GetNotificationsJob *job, bool initialization);
0100 
0101 private:
0102     QPixmap createNotificationImage(const QImage &icon, NeoChatRoom *room);
0103 };