This repository has been archived by the owner on Apr 15, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmessages.cpp
202 lines (173 loc) · 6.49 KB
/
messages.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
200
201
202
#include "messages.h"
#include <QStringList>
#include <QDebug>
#include <QApplication>
#include "tlschema.h"
#include "avatars.h"
using namespace TLType;
QString peerNameToHtml(TObject peer)
{
QString peerName;
switch (ID(peer)) {
case TLType::UserEmpty:
case TLType::User:
peerName = peer["first_name"].toString() + " " + peer["last_name"].toString();
break;
default:
//this is a chat, probably.
peerName = peer["title"].toString();
break;
}
peerName = peerName.mid(0, 40) + (peerName.length() > 40 ? "..." : "");
return "<span style=\"font-weight:bold;color:" + userColor(peer["id"].toLongLong()).name() + "\">" + peerName + "</span>";
}
QString replyToHtml(TObject reply, TObject replyPeer)
{
qint32 id = ID(reply);
if (id != Message && id != MessageEmpty) {
qWarning() << "[replyToHtml] Invalid object." << ID(reply);
return QString();
}
QString replyText = id == MessageEmpty ? QApplication::translate("Messages", "Loading...", 0, QApplication::UnicodeUTF8) : reply["message"].toString().replace('\n', " ");
QString peerName = id == MessageEmpty ? replyText : peerNameToHtml(replyPeer);
return "<table><tr><td style=\"background-color:" + userColor(replyPeer["id"].toLongLong()).name() + ";\"> </td><td> " + peerName + "<br> " + replyText.mid(0, 40) + (replyText.length() > 40 ? "..." : "") + "</td></tr></table>";
}
QString messageToHtml(TObject message)
{
if (ID(message) != Message) {
qWarning() << "[messageToHtml] Invalid object." << ID(message);
return QString();
}
QStringList items;
QList<char> count;
QString originalText = message["message"].toString();
items << QString(originalText).replace('<', "<").replace('>', ">") + ' ';
count << true;
TVector entities = message["entities"].toList();
for (qint32 i = 0; i < entities.size(); ++i) {
TObject entity = entities[i].toMap();
qint32 offset = entity["offset"].toInt();
qint32 length = entity["length"].toInt();
QString textPart = originalText.mid(offset, length);
QString sTag;
QString eTag;
switch (ID(entity)) {
case MessageEntityUnknown: //just ignore it.
//sTag = "<unknown>";
//eTag = "</unknown>";
break;
case MessageEntityMention:
sTag = "<a href=\"kutegram://profile/" + textPart + "\">";
eTag = "</a>";
break;
case MessageEntityHashtag:
sTag = "<a href=\"kutegram://search/" + textPart + "\">";
eTag = "</a>";
break;
case MessageEntityBotCommand:
sTag = "<a href=\"kutegram://execute/" + textPart + "\">";
eTag = "</a>";
break;
case MessageEntityUrl:
sTag = "<a href=\"" + textPart + "\">";
eTag = "</a>";
break;
case MessageEntityEmail:
sTag = "<a href=\"mailto:" + textPart + "\">";
eTag = "</a>";
break;
case MessageEntityBold:
sTag = "<b>";
eTag = "</b>";
break;
case MessageEntityItalic:
sTag = "<i>";
eTag = "</i>";
break;
case MessageEntityCode:
sTag = "<code>";
eTag = "</code>";
break;
case MessageEntityPre: //pre causes some issues with paddings
sTag = "<code language=\"" + entity["language"].toString() + "\">";
eTag = "</code>";
break;
case MessageEntityTextUrl:
sTag = "<a href=\"" + entity["url"].toString() + "\">";
eTag = "</a>";
break;
case MessageEntityMentionName:
sTag = "<a href=\"kutegram://profile/" + entity["user_id"].toString() + "\">";
eTag = "</a>";
break;
case InputMessageEntityMentionName:
sTag = "<a href=\"kutegram://profile/" + getPeerId(entity["user_id"].toMap()).toString() + "\">";
eTag = "</a>";
break;
case MessageEntityPhone:
sTag = "<a href=\"tel:" + textPart + "\">";
eTag = "</a>";
break;
case MessageEntityCashtag:
sTag = "<a href=\"kutegram://search/" + textPart + "\">";
eTag = "</a>";
break;
case MessageEntityUnderline:
sTag = "<u>";
eTag = "</u>";
break;
case MessageEntityStrike:
sTag = "<s>";
eTag = "</s>";
break;
case MessageEntityBlockquote:
sTag = "<blockquote>";
eTag = "</blockquote>";
break;
case MessageEntityBankCard:
sTag = "<a href=\"kutegram://card/" + textPart + "\">";
eTag = "</a>";
break;
case MessageEntitySpoiler:
sTag = "<a href=\"kutegram://spoiler/" + QString::number(i) + "\" style=\"background-color:gray;color:gray;\">";
eTag = "</a>";
break;
}
qint32 posStart = 0;
qint32 jidStart = -1;
qint32 posEnd = 0;
qint32 jidEnd = -1;
for (qint32 j = 0; j < items.size(); ++j) {
if (!count[j]) continue;
posStart += items[j].length();
posEnd += items[j].length();
if (posStart > offset && jidStart == -1) {
posStart = items[j].length() - posStart + offset;
jidStart = j;
}
if (posEnd > offset + length && jidEnd == -1) {
posEnd = items[j].length() - posEnd + offset + length;
jidEnd = j;
}
if (jidStart != -1 && jidEnd != -1) break;
}
if (jidStart == -1 || jidEnd == -1) continue;
QString sItem = items[jidStart];
items[jidStart] = sItem.mid(0, posStart);
items.insert(jidStart + 1, sTag);
count.insert(jidStart + 1, false);
items.insert(jidStart + 2, sItem.mid(posStart));
count.insert(jidStart + 2, true);
if (jidEnd == jidStart) posEnd -= posStart;
jidEnd += 2;
QString eItem = items[jidEnd];
items[jidEnd] = eItem.mid(0, posEnd);
items.insert(jidEnd + 1, eTag);
count.insert(jidEnd + 1, false);
items.insert(jidEnd + 2, eItem.mid(posEnd));
count.insert(jidEnd + 2, true);
}
if (items.last() == " ") items.removeLast();
else items.last().chop(1);
return items.join("").replace('\n', "<br>");
}