This repository has been archived by the owner on Aug 22, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathupdates.cpp
199 lines (161 loc) · 6.16 KB
/
updates.cpp
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
#include "telegramclient.h"
#include "tlschema.h"
#include <QDebug>
void TelegramClient::handleUpdatesTooLong(QByteArray data, qint64 mtm)
{
getUpdatesDifference();
}
void TelegramClient::handleUpdateShortMessage(QByteArray data, qint64 mtm)
{
//Update about outcoming (incoming) user message.
TelegramPacket packet(data);
QVariant var;
readTLUpdates(packet, var);
TelegramObject obj = var.toMap();
if (this->updatePts + obj["pts_count"].toInt() == obj["pts"].toInt()) {
applyUpdate(obj, mtm);
this->updatePts = qMax(this->updatePts, obj["pts"].toInt());
this->updateDate = qMax(this->updateDate, obj["date"].toInt());
} else if (this->updatePts + obj["pts_count"].toInt() < obj["pts"].toInt()) {
getUpdatesDifference();
} else {
qDebug() << "[UPD] Got an UpdateShortMessage that already applied. Weird.";
}
}
void TelegramClient::handleUpdateShortChatMessage(QByteArray data, qint64 mtm)
{
//Update about incoming chat message.
TelegramPacket packet(data);
QVariant var;
readTLUpdates(packet, var);
TelegramObject obj = var.toMap();
if (this->updatePts + obj["pts_count"].toInt() == obj["pts"].toInt()) {
applyUpdate(obj, mtm);
this->updatePts = qMax(this->updatePts, obj["pts"].toInt());
this->updateDate = qMax(this->updateDate, obj["date"].toInt());
} else if (this->updatePts + obj["pts_count"].toInt() < obj["pts"].toInt()) {
getUpdatesDifference();
} else {
qDebug() << "[UPD] Got an UpdateShortChatMessage that already applied. Weird.";
}
}
void TelegramClient::handleUpdateShort(QByteArray data, qint64 mtm)
{
TelegramPacket packet(data);
QVariant var;
readTLUpdates(packet, var);
TelegramObject obj = var.toMap();
applyUpdate(obj["update"].toMap(), mtm);
this->updateDate = qMax(this->updateDate, obj["date"].toInt());
}
void TelegramClient::handleUpdatesCombined(QByteArray data, qint64 mtm)
{
TelegramPacket packet(data);
QVariant var;
readTLUpdates(packet, var);
TelegramObject obj = var.toMap();
if (this->updateSeq + 1 == obj["seq_start"].toInt()) {
TelegramVector updates = obj["updates"].toList();
for (qint32 i = 0; i < updates.size(); ++i)
applyUpdate(updates[i].toMap(), mtm);
this->updateSeq = qMax(this->updateSeq, obj["seq"].toInt());
this->updateDate = qMax(this->updateDate, obj["date"].toInt());
} else if (this->updateSeq + 1 < obj["seq_start"].toInt()) {
getUpdatesDifference();
} else {
qDebug() << "[UPD] Got an UpdatesCombined that already applied. Weird.";
}
}
void TelegramClient::handleUpdates(QByteArray data, qint64 mtm)
{
TelegramPacket packet(data);
QVariant var;
readTLUpdates(packet, var);
TelegramObject obj = var.toMap();
//Sequence always is zero.
// if (this->updateSeq + 1 == obj["seq"].toInt()) {
TelegramVector updates = obj["updates"].toList();
for (qint32 i = 0; i < updates.size(); ++i)
applyUpdate(updates[i].toMap(), mtm);
this->updateSeq = qMax(this->updateSeq, obj["seq"].toInt());
this->updateDate = qMax(this->updateDate, obj["date"].toInt());
// } else if (this->updateSeq + 1 < obj["seq"].toInt()) {
// getUpdatesDifference();
// } else {
// qDebug() << "[UPD] Got an Updates that already applied. Weird.";
// }
}
void TelegramClient::handleUpdateShortSentMessage(QByteArray data, qint64 mtm)
{
//Update about sent message.
TelegramPacket packet(data);
QVariant var;
readTLUpdates(packet, var);
TelegramObject obj = var.toMap();
if (this->updatePts + obj["pts_count"].toInt() == obj["pts"].toInt()) {
//TODO: handle this update.
this->updatePts = qMax(this->updatePts, obj["pts"].toInt());
this->updateDate = qMax(this->updateDate, obj["date"].toInt());
} else if (this->updatePts + obj["pts_count"].toInt() < obj["pts"].toInt()) {
getUpdatesDifference();
} else {
qDebug() << "[UPD] Got an UpdateShortSentMessage that already applied. Weird.";
}
}
void TelegramClient::applyUpdate(TelegramObject obj, qint64 mtm)
{
switch (GETID(obj)) {
case TLType::UpdateNewMessage:
case TLType::UpdateNewChannelMessage:
emit updateNewMessage(obj["message"].toMap(), obj["pts"].toInt(), obj["pts_count"].toInt());
break;
case TLType::UpdateEditMessage:
case TLType::UpdateEditChannelMessage:
emit updateEditMessage(obj["message"].toMap(), obj["pts"].toInt(), obj["pts_count"].toInt());
break;
case TLType::UpdateDeleteMessages:
case TLType::UpdateDeleteChannelMessages:
emit updateDeleteMessages(obj["messages"].toList(), obj["pts"].toInt(), obj["pts_count"].toInt());
break;
case TLType::UpdateShortMessage:
{
ID_PROPERTY(obj) = TLType::Message;
TOBJECT(newPeer, TLType::PeerUser);
newPeer["user_id"] = obj["user_id"];
obj["from_id"] = obj["peer_id"] = newPeer;
emit updateNewMessage(obj, obj["pts"].toInt(), obj["pts_count"].toInt());
break;
}
case TLType::UpdateShortChatMessage:
{
ID_PROPERTY(obj) = TLType::Message;
TOBJECT(newUserPeer, TLType::PeerUser);
newUserPeer["user_id"] = obj["from_id"];
obj["from_id"] = newUserPeer;
TOBJECT(newChatPeer, TLType::PeerChat);
newChatPeer["chat_id"] = obj["chat_id"];
obj["peer_id"] = newChatPeer;
emit updateNewMessage(obj, obj["pts"].toInt(), obj["pts_count"].toInt());
break;
}
default:
qDebug() << "[UPD] Got an unhandled update with ID:" << QString::number((quint32) GETID(obj), 16);
break;
}
}
//void TelegramClient::handleUpdateLoginToken(QByteArray data, qint64 mtm)
//{
// exportLoginToken();
//}
//void TelegramClient::handleUpdateNewMessage(QByteArray data, qint64 mtm)
//{
// TelegramPacket packet(data);
// QVariant var;
// readTLUpdate(packet, var);
// TelegramObject obj = var.toMap();
// emit gotNewMessage(mtm, TLMessage(obj["message"].toMap()));
//}
//void TelegramClient::handleUpdateNewChannelMessage(QByteArray data, qint64 mtm)
//{
// handleUpdateNewMessage(data, mtm);
//}