~libqtelegram-team/telegram-app/telegram-app-dev

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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
#include <QDebug>
#include <QFile>
#include <QJsonDocument>
#include <QJsonArray>
#include <QStringList>
#include <QTextStream>

#include "pushhelper.h"

PushHelper::PushHelper(QString appId, QString infile, QString outfile,
                       QObject *parent) : QObject(parent) {
    connect(&mPushClient, SIGNAL(persistentCleared()),
                    this, SLOT(notificationDismissed()));

    mPushClient.setAppId(appId);
    mPushClient.registerApp(appId);
    mInfile = infile;
    mOutfile = outfile;
}

PushHelper::~PushHelper() {
}

void PushHelper::process() {
    QString tag = "";

    QJsonObject pushMessage = readPushMessage(mInfile);
    mPostalMessage = pushToPostalMessage(pushMessage, tag);
    if (!tag.isEmpty()) {
        dismissNotification(tag);
    }
}

void PushHelper::notificationDismissed() {
    writePostalMessage(mPostalMessage, mOutfile);
    Q_EMIT done();
}

QJsonObject PushHelper::readPushMessage(const QString &filename) {
    QFile file(filename);
    file.open(QIODevice::ReadOnly | QIODevice::Text);

    QString val = file.readAll();
    file.close();
    return QJsonDocument::fromJson(val.toUtf8()).object();
}

void PushHelper::writePostalMessage(const QJsonObject &postalMessage, const QString &filename) {
    QFile out;
    out.setFileName(filename);
    out.open(QIODevice::WriteOnly | QIODevice::Text | QIODevice::Truncate);

    QTextStream(&out) << QJsonDocument(postalMessage).toJson();
    out.close();
}

QJsonObject PushHelper::pushToPostalMessage(const QJsonObject &push, QString &tag) {
    QString summary = "";
    QString body = "";
    qint32 count = 0;

    QJsonObject message = push["message"].toObject();
    QJsonObject custom = message["custom"].toObject();

    QString key = "";
    if (message.keys().contains("loc_key")) {
        key = message["loc_key"].toString();
    }
    QJsonArray args;
    if (message.keys().contains("loc_args")) {
        args = message["loc_args"].toArray();
    }

    QString chatId = "0"; // More useful as string in this context.
    if (custom.keys().contains("from_id")) {
        chatId = custom["from_id"].toString();
    }
    if (custom.keys().contains("chat_id")) {
        chatId = custom["chat_id"].toString();
    }

    QString tg = QString("Telegram");
    summary = args[0].toString();

    if (key == "MESSAGE_TEXT") {

        body = args[1].toString();

    } else if (key == "MESSAGE_NOTEXT") {

        body = "sent you a message";

    } else if (key == "MESSAGE_PHOTO") {

        body = "sent you a photo";

    } else if (key == "MESSAGE_VIDEO") {

        body = "sent you a video";

    } else if (key == "MESSAGE_DOC") {

        body = "sent you a document";

    } else if (key == "MESSAGE_AUDIO") {

        body = "sent you a voice message";

    } else if (key == "MESSAGE_CONTACT") {

        body = "shared a contact with you";

    } else if (key == "MESSAGE_GEO") {

        body = "sent you a map";

    } else if (key == "CHAT_MESSAGE_TEXT") {

        summary = args[1].toString();
        body = args[0].toString() + ": " + args[2].toString();

    } else if (key == "CHAT_MESSAGE_NOTEXT") {

        summary = args[1].toString();
        body = args[0].toString() + " sent a message to the group";

    } else if (key == "CHAT_MESSAGE_PHOTO") {

        summary = args[1].toString();
        body = args[0].toString() + " sent a photo to the group";

    } else if (key == "CHAT_MESSAGE_VIDEO") {

        summary = args[1].toString();
        body = args[0].toString() + " sent a video to the group";

    } else if (key == "CHAT_MESSAGE_DOC") {

        summary = args[1].toString();
        body = args[0].toString() + " sent a document to the group";

    } else if (key == "CHAT_MESSAGE_AUDIO") {

        summary = args[1].toString();
        body = args[0].toString() + " sent a voice message to the group";

    } else if (key == "CHAT_MESSAGE_CONTACT") {

        summary = args[1].toString();
        body = args[0].toString() + " sent a contact to the group";

    } else if (key == "CHAT_MESSAGE_GEO") {

        summary = args[1].toString();
        body = args[0].toString() + " sent a map to the group";

    } else if (key == "CHAT_CREATED") {

        summary = args[1].toString();
        body = args[0].toString() + " invited you to the group";

    } else if (key == "CHAT_TITLE_EDITED") {

        summary = args[1].toString();
        body = args[0].toString() + " changed group name";

    } else if (key == "CHAT_PHOTO_EDITED") {

        summary = args[1].toString();
        body = args[0].toString() + " changed group photo";

    } else if (key == "CHAT_ADD_MEMBER") {

        summary = args[1].toString();
        body = args[0].toString() + " invited " + args[2].toString();

    } else if (key == "CHAT_ADD_YOU") {

        summary = args[1].toString();
        body = args[0].toString() + " invited you to the group";

    } else if (key == "CHAT_DELETE_MEMBER") {

        summary = args[1].toString();
        body = args[0].toString() + " kicked " + args[2].toString();

    } else if (key == "CHAT_DELETE_YOU") {

        summary = args[1].toString();
        body = args[0].toString() + " kicked you from the group";

    } else if (key == "CHAT_LEFT") {

        summary = args[1].toString();
        body = args[0].toString() + " has left the group";

    } else if (key == "CHAT_RETURNED") {

        summary = args[1].toString();
        body = args[0].toString() + " has returned to the group";

    } else if (key == "GEOCHAT_CHECKIN") {

        summary = "@ " + args[1].toString();
        body = args[0].toString() + " has checked-in";

    } else if (key == "CONTACT_JOINED") {

        summary = tg;
        body = args[0].toString() + " joined Telegram!";

    } else if (key == "AUTH_UNKNOWN") {

        summary = args[0].toString();
        body = "New login from unrecognized device";

    } else if (key == "AUTH_REGION") {

        summary = args[0].toString() + " @ " + args[1].toString();
        body = "New login from unrecognized device";

    } else if (key == "CONTACT_PHOTO") {

        body = "updated profile photo";

    } else if (key == "ENCRYPTION_REQUEST") {

        summary = tg;
        body = "You have a new message";

    } else if (key == "ENCRYPTION_ACCEPT") {

        summary = tg;
        body = "You have a new message";

    } else if (key == "ENCRYPTED_MESSAGE") {

        summary = tg;
        body = "You have a new message";

    } else {
        qDebug() << "Unhandled push type: " << key;
        return QJsonObject();
    }

    QJsonArray actions = QJsonArray();
    QString actionUri = QString("telegram://chat/%1").arg(chatId);
    actions.append(actionUri);

    if (message.keys().contains("badge")) {
        count = message["badge"].toInt();
    } else if (push.keys().contains("notification")) {
        // Legacy. Notification section is only used to retrieve the unread count.
        count = push["notification"]
                .toObject()["emblem-counter"]
                .toObject()["count"]
                .toInt();
    }

    tag = chatId;

    QJsonObject card;
    card["summary"] = summary;
    card["body"]    = body;
    card["actions"] = actions;
    card["popup"]   = true;             // TODO make setting
    card["persist"] = true;             // TODO make setting

    QJsonObject emblem;
    emblem["count"] = count;
    emblem["visible"] = count > 0;

    QJsonObject notification;
    notification["tag"] = tag;
    notification["card"] = card;
    notification["emblem-counter"] = emblem;
    notification["sound"] = true;       // TODO make setting
    notification["vibrate"] = true;     // TODO make setting

    QJsonObject postalMessage = QJsonObject();
    postalMessage["notification"] = notification;

    return postalMessage;
}

void PushHelper::dismissNotification(const QString &tag) {
    QStringList tags;
    tags << tag;
    mPushClient.clearPersistent(tags);
}