FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
tdesktop/Telegram/SourceFiles/api/api_peer_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_peer_search.cpp
Copy path
More file actions
More file actions
Latest commit
History
History
History
170 lines (153 loc) · 4.9 KB
Breadcrumbs
tdesktop
/
Telegram
/
SourceFiles
/
api
/
api_peer_search.cpp
Copy path
File metadata and controls
170 lines (153 loc) · 4.9 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
/*
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_peer_search.h
"
#
include
"
api/api_single_message_search.h
"
#
include
"
apiwrap.h
"
#
include
"
data/data_session.h
"
#
include
"
dialogs/ui/chat_search_in.h
"
//
IsHashOrCashtagSearchQuery
#
include
"
main/main_session.h
"
namespace
Api
{
namespace
{
constexpr
auto
kMinSponsoredQueryLength
=
4
;
}
//
namespace
PeerSearch::PeerSearch
(not_null<Main::Session*> session, Type type)
: _session(session)
, _type(type) {
}
PeerSearch::~PeerSearch
() {
clear
();
}
void
PeerSearch::request
(
const
QString &query,
Fn<
void
(PeerSearchResult)> callback,
RequestType type) {
using
namespace
Dialogs
;
_query =
Api::ConvertPeerSearchQuery
(query);
_callback = callback;
if
(_query.
isEmpty
()
||
IsHashOrCashtagSearchQuery
(_query) != HashOrCashtag::None) {
finish
(PeerSearchResult{});
return
;
}
auto
&cache = _cache[_query];
if
(cache.
peersReady
&& cache.
sponsoredReady
) {
finish
(cache.
result
);
return
;
}
else
if
(type == RequestType::CacheOnly) {
_callback =
nullptr
;
return
;
}
else
if
(cache.
requested
) {
return
;
}
cache.
requested
=
true
;
cache.
result
.
query
= _query;
if
(_query.
size
() <
kMinSponsoredQueryLength
) {
cache.
sponsoredReady
=
true
;
}
else
if
(_type == Type::WithSponsored) {
requestSponsored
();
}
requestPeers
();
}
void
PeerSearch::requestPeers
() {
const
auto
requestId = _session->
api
().
request
(
MTPcontacts_Search
(
MTP_string
(_query),
MTP_int
(SearchPeopleLimit)
)).
done
([=](
const
MTPcontacts_Found &result, mtpRequestId requestId) {
const
auto
&data = result.
data
();
_session->
data
().
processUsers
(data.
vusers
());
_session->
data
().
processChats
(data.
vchats
());
auto
parsed =
PeerSearchResult
();
parsed.
my
.
reserve
(data.
vmy_results
().
v
.
size
());
for
(
const
auto
&id : data.
vmy_results
().
v
) {
const
auto
peerId =
peerFromMTP
(id);
parsed.
my
.
push_back
(_session->
data
().
peer
(peerId));
}
parsed.
peers
.
reserve
(data.
vresults
().
v
.
size
());
for
(
const
auto
&id : data.
vresults
().
v
) {
const
auto
peerId =
peerFromMTP
(id);
parsed.
peers
.
push_back
(_session->
data
().
peer
(peerId));
}
finishPeers
(requestId,
std::move
(parsed));
}).
fail
([=](
const
MTP
::Error &error, mtpRequestId requestId) {
finishPeers
(requestId, PeerSearchResult{});
}).
send
();
_peerRequests.
emplace
(requestId, _query);
}
void
PeerSearch::requestSponsored
() {
const
auto
requestId = _session->
api
().
request
(
MTPcontacts_GetSponsoredPeers
(
MTP_string
(_query))
).
done
([=](
const
MTPcontacts_SponsoredPeers &result,
mtpRequestId requestId) {
result.
match
([&](
const
MTPDcontacts_sponsoredPeersEmpty &) {
finishSponsored
(requestId, PeerSearchResult{});
}, [&](
const
MTPDcontacts_sponsoredPeers &data) {
_session->
data
().
processUsers
(data.
vusers
());
_session->
data
().
processChats
(data.
vchats
());
auto
parsed =
PeerSearchResult
();
parsed.
sponsored
.
reserve
(data.
vpeers
().
v
.
size
());
for
(
const
auto
&peer : data.
vpeers
().
v
) {
const
auto
&data = peer.
data
();
const
auto
peerId =
peerFromMTP
(data.
vpeer
());
parsed.
sponsored
.
push_back
({
.
peer
= _session->
data
().
peer
(peerId),
.
randomId
= data.
vrandom_id
().
v
,
.
sponsorInfo
=
TextWithEntities::Simple
(
qs
(data.
vsponsor_info
().
value_or_empty
())),
.
additionalInfo
=
TextWithEntities::Simple
(
qs
(data.
vadditional_info
().
value_or_empty
())),
});
}
finishSponsored
(requestId,
std::move
(parsed));
});
}).
fail
([=](
const
MTP
::Error &error, mtpRequestId requestId) {
finishSponsored
(requestId, PeerSearchResult{});
}).
send
();
_sponsoredRequests.
emplace
(requestId, _query);
}
void
PeerSearch::finishPeers
(
mtpRequestId requestId,
PeerSearchResult result) {
const
auto
query = _peerRequests.
take
(requestId);
Assert
(query.
has_value
());
auto
&cache = _cache[*query];
cache.
peersReady
=
true
;
cache.
result
.
my
=
std::move
(result.
my
);
cache.
result
.
peers
=
std::move
(result.
peers
);
if
(cache.
sponsoredReady
&& _query == *query) {
finish
(cache.
result
);
}
}
void
PeerSearch::finishSponsored
(
mtpRequestId requestId,
PeerSearchResult result) {
const
auto
query = _sponsoredRequests.
take
(requestId);
Assert
(query.
has_value
());
auto
&cache = _cache[*query];
cache.
sponsoredReady
=
true
;
cache.
result
.
sponsored
=
std::move
(result.
sponsored
);
if
(cache.
peersReady
&& _query == *query) {
finish
(cache.
result
);
}
}
void
PeerSearch::finish
(PeerSearchResult result) {
if
(
const
auto
onstack =
base::take
(_callback)) {
onstack
(
std::move
(result));
}
}
void
PeerSearch::clear
() {
_query =
QString
();
_callback =
nullptr
;
_cache.
clear
();
for
(
const
auto
&[requestId, query] :
base::take
(_peerRequests)) {
_session->
api
().
request
(requestId).
cancel
();
}
for
(
const
auto
&[requestId, query] :
base::take
(_sponsoredRequests)) {
_session->
api
().
request
(requestId).
cancel
();
}
}
}
//
namespace Api
Back
|
FazBrowse Home
|
New Git URL