FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
tdesktop/Telegram/SourceFiles/api/api_single_message_search.cpp at dev · dmitry/tdesktop · GitHub
dmitry
tdesktop
Repository navigation
Code
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
tdesktop
/
Telegram
/
SourceFiles
/
api
/
api_single_message_search.cpp
Copy path
More file actions
More file actions
Latest commit
History
History
History
237 lines (211 loc) · 6.35 KB
Breadcrumbs
tdesktop
/
Telegram
/
SourceFiles
/
api
/
api_single_message_search.cpp
Copy path
File metadata and controls
237 lines (211 loc) · 6.35 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
/*
This file is part of Telegram Desktop,
the official desktop application for the Telegram messaging service.
For license and copyright information please follow this link:
https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL
*/
#
include
"
api/api_single_message_search.h
"
#
include
"
main/main_session.h
"
#
include
"
data/data_session.h
"
#
include
"
data/data_channel.h
"
#
include
"
data/data_search_controller.h
"
#
include
"
core/local_url_handlers.h
"
#
include
"
history/history_item.h
"
#
include
"
base/qthelp_url.h
"
#
include
"
apiwrap.h
"
namespace
Api
{
namespace
{
using
Key = details::SingleMessageSearchKey;
Key
ExtractKey
(
const
QString &query) {
const
auto
trimmed = query.
trimmed
();
const
auto
local =
Core::TryConvertUrlToLocal
(trimmed);
const
auto
check = local.
isEmpty
() ? trimmed : local;
const
auto
parse = [&] {
const
auto
delimeter = check.
indexOf
(
'
?
'
);
return
(delimeter >
0
)
?
qthelp::url_parse_params
(
check.
mid
(delimeter +
1
),
qthelp::UrlParamNameTransform::ToLower)
: QMap<QString, QString>();
};
if
(check.
startsWith
(
u"
tg://privatepost
"
_q, Qt::CaseInsensitive)) {
const
auto
params =
parse
();
const
auto
channel = params.
value
(
"
channel
"
);
const
auto
post
= params.
value
(
"
post
"
).
toInt
();
return
(channel.
toULongLong
() &&
post
) ? Key{ channel,
post
} :
Key
();
}
else
if
(check.
startsWith
(
u"
tg://resolve
"
_q, Qt::CaseInsensitive)) {
const
auto
params =
parse
();
const
auto
domain = params.
value
(
"
domain
"
);
const
auto
post
= params.
value
(
"
post
"
).
toInt
();
return
(!domain.
isEmpty
() &&
post
) ? Key{ domain,
post
} :
Key
();
}
return
Key
();
}
}
//
namespace
SingleMessageSearch::SingleMessageSearch
(not_null<Main::Session*> session)
: _session(session) {
}
SingleMessageSearch::~SingleMessageSearch
() {
clear
();
}
void
SingleMessageSearch::clear
() {
_cache.
clear
();
_requestKey =
Key
();
_session->
api
().
request
(
base::take
(_requestId)).
cancel
();
}
std::optional<HistoryItem*>
SingleMessageSearch::lookup
(
const
QString &query,
Fn<
void
()> ready) {
const
auto
key =
ExtractKey
(query);
if
(!key) {
return
nullptr
;
}
const
auto
i = _cache.
find
(key);
if
(i !=
end
(_cache)) {
return
_session->
data
().
message
(i->
second
);
}
if
(!(_requestKey == key)) {
_session->
api
().
request
(
base::take
(_requestId)).
cancel
();
_requestKey = key;
}
return
performLookup
(ready);
}
std::optional<HistoryItem*>
SingleMessageSearch::performLookupByChannel
(
not_null<ChannelData*> channel,
Fn<
void
()> ready) {
Expects
(!_requestKey.
empty
());
const
auto
postId = _requestKey.
postId
;
if
(
const
auto
item = _session->
data
().
message
(channel->
id
, postId)) {
_cache.
emplace
(_requestKey, item->
fullId
());
return
item;
}
else
if
(!ready) {
return
nullptr
;
}
const
auto
fail = [=] {
_cache.
emplace
(_requestKey,
FullMsgId
());
ready
();
};
_requestId = _session->
api
().
request
(
MTPchannels_GetMessages
(
channel->
inputChannel
(),
MTP_vector<MTPInputMessage>(
1
,
MTP_inputMessageID
(
MTP_int
(postId)))
)).
done
([=](
const
MTPmessages_Messages &result) {
const
auto
received =
Api::ParseSearchResult
(
channel,
Storage::SharedMediaType::
kCount
,
postId,
Data::LoadDirection::Around,
result);
if
(!received.
messageIds
.
empty
()
&& received.
messageIds
.
front
() == postId) {
_cache.
emplace
(
_requestKey,
FullMsgId
(channel->
id
, postId));
ready
();
}
else
{
fail
();
}
}).
fail
([=] {
fail
();
}).
send
();
return
std::
nullopt
;
}
std::optional<HistoryItem*>
SingleMessageSearch::performLookupById
(
ChannelId channelId,
Fn<
void
()> ready) {
Expects
(!_requestKey.
empty
());
if
(
const
auto
channel = _session->
data
().
channelLoaded
(channelId)) {
return
performLookupByChannel
(channel, ready);
}
else
if
(!ready) {
return
nullptr
;
}
const
auto
fail = [=] {
_cache.
emplace
(_requestKey,
FullMsgId
());
ready
();
};
_requestId = _session->
api
().
request
(
MTPchannels_GetChannels
(
MTP_vector<MTPInputChannel>(
1
,
MTP_inputChannel
(
MTP_long
(channelId.
bare
),
MTP_long
(
0
)))
)).
done
([=](
const
MTPmessages_Chats &result) {
result.
match
([&](
const
auto
&data) {
const
auto
peer = _session->
data
().
processChats
(data.
vchats
());
if
(peer && peer->
id
==
peerFromChannel
(channelId)) {
if
(
performLookupByChannel
(peer->
asChannel
(), ready)) {
ready
();
}
}
else
{
fail
();
}
});
}).
fail
([=] {
fail
();
}).
send
();
return
std::
nullopt
;
}
std::optional<HistoryItem*>
SingleMessageSearch::performLookupByUsername
(
const
QString &username,
Fn<
void
()> ready) {
Expects
(!_requestKey.
empty
());
if
(
const
auto
peer = _session->
data
().
peerByUsername
(username)) {
if
(
const
auto
channel = peer->
asChannel
()) {
return
performLookupByChannel
(channel, ready);
}
_cache.
emplace
(_requestKey,
FullMsgId
());
return
nullptr
;
}
else
if
(!ready) {
return
nullptr
;
}
const
auto
fail = [=] {
_cache.
emplace
(_requestKey,
FullMsgId
());
ready
();
};
_requestId = _session->
api
().
request
(
MTPcontacts_ResolveUsername
(
MTP_flags
(
0
),
MTP_string
(username),
MTP_string
()
)).
done
([=](
const
MTPcontacts_ResolvedPeer &result) {
result.
match
([&](
const
MTPDcontacts_resolvedPeer &data) {
_session->
data
().
processUsers
(data.
vusers
());
_session->
data
().
processChats
(data.
vchats
());
const
auto
peerId =
peerFromMTP
(data.
vpeer
());
const
auto
peer = peerId
? _session->
data
().
peerLoaded
(peerId)
:
nullptr
;
if
(
const
auto
channel = peer ? peer->
asChannel
() :
nullptr
) {
if
(
performLookupByChannel
(channel, ready)) {
ready
();
}
}
else
{
fail
();
}
});
}).
fail
([=] {
fail
();
}).
send
();
return
std::
nullopt
;
}
std::optional<HistoryItem*>
SingleMessageSearch::performLookup
(
Fn<
void
()> ready) {
Expects
(!_requestKey.
empty
());
if
(!_requestKey.
domainOrId
[
0
].
isDigit
()) {
return
performLookupByUsername
(_requestKey.
domainOrId
, ready);
}
const
auto
channelId =
ChannelId
(_requestKey.
domainOrId
.
toULongLong
());
return
performLookupById
(channelId, ready);
}
QString
ConvertPeerSearchQuery
(
const
QString &query) {
const
auto
trimmed = query.
trimmed
();
const
auto
local =
Core::TryConvertUrlToLocal
(trimmed);
const
auto
check = local.
isEmpty
() ? trimmed : local;
if
(!check.
startsWith
(
u"
tg://resolve
"
_q, Qt::CaseInsensitive)) {
return
query;
}
const
auto
delimeter = check.
indexOf
(
'
?
'
);
const
auto
params = (delimeter >
0
)
?
qthelp::url_parse_params
(
check.
mid
(delimeter +
1
),
qthelp::UrlParamNameTransform::ToLower)
: QMap<QString, QString>();
return
params.
value
(
"
domain
"
, query);
}
}
//
namespace Api
Back
|
FazBrowse Home
|
New Git URL