File: database.h

package info (click to toggle)
spacebar 1%3A6.5.4-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 1,448 kB
  • sloc: cpp: 4,708; xml: 293; sql: 22; makefile: 3; sh: 1
file content (200 lines) | stat: -rw-r--r-- 6,970 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
// SPDX-FileCopyrightText: 2020 Jonah BrĂ¼chert <jbb@kaidan.im>
//
// SPDX-License-Identifier: GPL-2.0-only OR GPL-3.0-only OR LicenseRef-KDE-Accepted-GPL

#pragma once

#include <QDateTime>
#include <QObject>
#include <QSqlDatabase>
#include <QSqlQuery>

#include <ThreadedDatabase>

#include <QCoroTask>

#include <global.h>
#include <phonenumberlist.h>

enum MessageState {
    Unknown = false,
    Sent = true,
    Pending,
    Failed,
    Received,
};
Q_DECLARE_METATYPE(MessageState)

inline MessageState parseMessageState(const QString &state)
{
    if (state == SL("pending")) {
        return MessageState::Pending;
    } else if (state == SL("sent")) {
        return MessageState::Sent;
    } else if (state == SL("failed")) {
        return MessageState::Failed;
    }

    Q_UNREACHABLE();
}

// For use with DBus
typedef QMap<QString, QString> StringMap;
typedef QList<StringMap> StringMapList;
Q_DECLARE_METATYPE(StringMap)
Q_DECLARE_METATYPE(StringMapList)

struct Message {
    using ColumnTypes =
        std::tuple<QString, QString, QString, qint64, bool, int, bool, QString, QString, QString, QString, int, QString, bool, QString, qint64, int, QString>;

    static Message fromSql(ColumnTypes &&tuple);

    QString id;
    PhoneNumberList phoneNumberList;
    QString text;
    QDateTime datetime;
    bool read;
    MessageState deliveryStatus;
    bool sentByMe;
    QString attachments;
    QString smil;
    QString fromNumber;
    QString messageId;
    int deliveryReport = 0;
    QString readReport;
    bool pendingDownload = false;
    QString contentLocation;
    QDateTime expires;
    int size = 0;
    QString tapbacks;

    Message() = default;

    Message(const StringMap &map)
    {
        id = map[SL("id")];
        phoneNumberList = PhoneNumberList{map[SL("phoneNumberList")]};
        text = map[SL("text")];
        datetime = QDateTime::fromString(map[SL("datetime")]);
        read = QVariant{map[SL("read")]}.toBool();
        deliveryStatus = static_cast<MessageState>(map[SL("deliveryStatus")].toUInt());
        sentByMe = QVariant{map[SL("sentByMe")]}.toBool();
        attachments = map[SL("attachments")];
        smil = map[SL("smil")];
        fromNumber = map[SL("fromNumber")];
        messageId = map[SL("messageId")];
        deliveryReport = map[SL("deliveryReport")].toInt();
        readReport = map[SL("readReport")];
        pendingDownload = QVariant{map[SL("pendingDownload")]}.toBool();
        contentLocation = map[SL("contentLocation")];
        expires = QDateTime::fromString(map[SL("expires")]);
        size = map[SL("size")].toInt();
        tapbacks = map[SL("tapbacks")];
    }

    StringMap toMap() const
    {
        return {
            {SL("id"), id},
            {SL("phoneNumberList"), phoneNumberList.toString()},
            {SL("text"), text},
            {SL("datetime"), datetime.toString()},
            {SL("read"), QString::number(read)},
            {SL("deliveryStatus"), QString::number(deliveryStatus)},
            {SL("sentByMe"), QString::number(sentByMe)},
            {SL("attachments"), attachments},
            {SL("smil"), smil},
            {SL("fromNumber"), fromNumber},
            {SL("messageId"), messageId},
            {SL("deliveryReport"), QString::number(deliveryReport)},
            {SL("readReport"), readReport},
            {SL("pendingDownload"), QString::number(pendingDownload)},
            {SL("contentLocation"), contentLocation},
            {SL("expires"), expires.toString()},
            {SL("size"), QString::number(size)},
            {SL("tapbacks"), tapbacks},
        };
    }
};
Q_DECLARE_METATYPE(Message)

struct Chat {
    PhoneNumberList phoneNumberList;
    int unreadMessages = 0;
    QString lastMessage;
    QDateTime lastDateTime;
    bool lastSentByMe = false;
    QString lastAttachment;

    Chat() = default;

    Chat(const StringMap &map)
    {
        phoneNumberList = PhoneNumberList{map[SL("phoneNumberList")]};
        unreadMessages = map[SL("unreadMessages")].toInt();
        lastMessage = map[SL("lastMessage")];
        lastDateTime = QDateTime::fromString(map[SL("lastDateTime")]);
        lastSentByMe = QVariant{map[SL("lastSentByMe")]}.toBool();
        lastAttachment = map[SL("lastAttachment")];
    }

    StringMap toMap() const
    {
        return {{SL("phoneNumberList"), phoneNumberList.toString()},
                {SL("unreadMessages"), QString::number(unreadMessages)},
                {SL("lastMessage"), lastMessage},
                {SL("lastDateTime"), lastDateTime.toString()},
                {SL("lastSentByMe"), QString::number(lastSentByMe)},
                {SL("lastAttachment"), lastAttachment}};
    }
};

Q_DECLARE_METATYPE(Chat)

class Database : public QObject
{
    Q_OBJECT

public:
    explicit Database(QObject *parent = nullptr);

    // Messages
    QCoro::Task<> addMessage(const Message &message);
    QCoro::Task<> deleteMessage(const QString &id);
    QCoro::Task<std::vector<Message>> messagesForNumber(const PhoneNumberList &phoneNumberList, const QString &id = QString(), const int limit = 0) const;
    QCoro::Task<> updateMessageDeliveryState(const QString &id, const MessageState state);
    QCoro::Task<> updateMessageSent(const QString &id, const QString &messageId, const QString &contentLocation);
    QCoro::Task<> updateMessageDeliveryReport(const QString &messageId);
    QCoro::Task<> updateMessageReadReport(const QString &messageId, const PhoneNumber &fromNumber);
    QCoro::Task<> markMessageRead(const int id);
    QCoro::Task<> updateMessageTapbacks(const QString &id, const QString tapbacks);
    QCoro::Task<std::optional<QString>> lastMessageWithText(const PhoneNumberList &phoneNumberList, const QString &text);
    QCoro::Task<std::optional<QString>> lastMessageWithAttachment(const PhoneNumberList &phoneNumberList);

    // Chats
    QCoro::Task<QVector<Chat>> chats(const PhoneNumberList &phoneNumberList) const;
    QCoro::Task<std::optional<int>> unreadMessagesForNumber(const PhoneNumberList &phoneNumberList) const;
    QCoro::Task<> markChatAsRead(const PhoneNumberList &phoneNumberList);
    QCoro::Task<> deleteChat(const PhoneNumberList &phoneNumberList);
    QCoro::Task<> mergeChats(const QString &fromNumbers, const QString toNumbers);

    static QString generateRandomId();

    static void exec(QSqlQuery &query);

    QCoro::Task<> migrate();

private:
    // Ran on the database thread
    void migrationV1(const QSqlDatabase &db, uint current);
    void migrationV2(const QSqlDatabase &db, uint current);
    void migrationV3(const QSqlDatabase &db, uint current);
    void migrationV4(const QSqlDatabase &db, uint current);
    void migrationV5(const QSqlDatabase &db, uint current);
    void migrationV6(const QSqlDatabase &db, uint current);
    void migrationV7(const QSqlDatabase &db, uint current);
    void migrationV8(const QSqlDatabase &db, uint current);

    std::unique_ptr<ThreadedDatabase> m_database;
};