11
11
#include " bat/ledger/internal/ledger_impl.h"
12
12
#include " bat/ledger/internal/publisher/publisher_server_list.h"
13
13
#include " bat/ledger/internal/state_keys.h"
14
+ #include " bat/ledger/internal/request/request_publisher.h"
14
15
#include " bat/ledger/internal/request/request_util.h"
15
16
#include " bat/ledger/internal/static_values.h"
16
17
#include " bat/ledger/option_keys.h"
@@ -21,6 +22,12 @@ using std::placeholders::_1;
21
22
using std::placeholders::_2;
22
23
using std::placeholders::_3;
23
24
25
+ namespace {
26
+
27
+ const int kHardLimit = 100 ;
28
+
29
+ } // namespace
30
+
24
31
namespace braveledger_publisher {
25
32
26
33
PublisherServerList::PublisherServerList (bat_ledger::LedgerImpl* ledger) :
@@ -34,19 +41,29 @@ PublisherServerList::~PublisherServerList() {
34
41
void PublisherServerList::OnTimer (uint32_t timer_id) {
35
42
if (timer_id == server_list_timer_id_) {
36
43
server_list_timer_id_ = 0 ;
37
- Download ([](const ledger::Result _){});
44
+ Start ([](const ledger::Result _){});
45
+ }
46
+ }
47
+
48
+ void PublisherServerList::Start (ledger::ResultCallback callback) {
49
+ if (in_progress_) {
50
+ BLOG (ledger_, ledger::LogLevel::LOG_INFO) << " Publisher list in progress" ;
51
+ callback (ledger::Result::LEDGER_OK);
52
+ return ;
38
53
}
54
+
55
+ in_progress_ = true ;
56
+ current_page_ = 1 ;
57
+
58
+ Download (callback);
39
59
}
40
60
41
- void PublisherServerList::Download (
42
- DownloadServerPublisherListCallback callback) {
61
+ void PublisherServerList::Download (ledger::ResultCallback callback) {
43
62
std::vector<std::string> headers;
44
63
headers.push_back (" Accept-Encoding: gzip" );
45
64
46
- const std::string url = braveledger_request_util::BuildUrl (
47
- GET_PUBLISHERS_LIST,
48
- " " ,
49
- braveledger_request_util::ServerTypes::PUBLISHER_DISTRO);
65
+ const std::string url =
66
+ braveledger_request_util::GetPublisherListUrl (current_page_);
50
67
51
68
const ledger::LoadURLCallback download_callback = std::bind (
52
69
&PublisherServerList::OnDownload,
@@ -69,13 +86,20 @@ void PublisherServerList::OnDownload(
69
86
int response_status_code,
70
87
const std::string& response,
71
88
const std::map<std::string, std::string>& headers,
72
- DownloadServerPublisherListCallback callback) {
89
+ ledger::ResultCallback callback) {
73
90
ledger_->LogResponse (
74
91
__func__,
75
92
response_status_code,
76
93
" Publisher list" ,
77
94
headers);
78
95
96
+ // we iterated through all pages
97
+ if (response_status_code == net::HTTP_NO_CONTENT) {
98
+ in_progress_ = false ;
99
+ OnParsePublisherList (ledger::Result::LEDGER_OK, callback);
100
+ return ;
101
+ }
102
+
79
103
if (response_status_code == net::HTTP_OK && !response.empty ()) {
80
104
const auto parse_callback =
81
105
std::bind (&PublisherServerList::OnParsePublisherList, this , _1, callback);
@@ -90,15 +114,22 @@ void PublisherServerList::OnDownload(
90
114
91
115
void PublisherServerList::OnParsePublisherList (
92
116
const ledger::Result result,
93
- DownloadServerPublisherListCallback callback) {
117
+ ledger::ResultCallback callback) {
118
+ if (result == ledger::Result::CONTINUE && current_page_ < kHardLimit ) {
119
+ current_page_++;
120
+ Download (callback);
121
+ return ;
122
+ }
123
+
94
124
uint64_t new_time = 0ull ;
95
- if (result == ledger::Result::LEDGER_OK ) {
125
+ if (result != ledger::Result::LEDGER_ERROR ) {
96
126
ledger_->ContributeUnverifiedPublishers ();
97
127
new_time = braveledger_time_util::GetCurrentTimeStamp ();
98
128
}
99
129
100
130
ledger_->SetUint64State (ledger::kStateServerPublisherListStamp , new_time);
101
131
132
+ in_progress_ = false ;
102
133
bool retry_after_error = result != ledger::Result::LEDGER_OK;
103
134
SetTimer (retry_after_error);
104
135
@@ -153,7 +184,7 @@ uint64_t PublisherServerList::GetTimerTime(
153
184
? 0ull
154
185
: now_seconds - last_download;
155
186
156
- uint64_t interval =
187
+ const uint64_t interval =
157
188
ledger_->GetUint64Option (ledger::kOptionPublisherListRefreshInterval );
158
189
159
190
if (now_seconds == last_download) {
@@ -183,7 +214,7 @@ ledger::PublisherStatus PublisherServerList::ParsePublisherStatus(
183
214
184
215
void PublisherServerList::ParsePublisherList (
185
216
const std::string& data,
186
- ParsePublisherListCallback callback) {
217
+ ledger::ResultCallback callback) {
187
218
auto list_publisher =
188
219
std::make_shared<std::vector<ledger::ServerPublisherPartial>>();
189
220
auto list_banner = std::make_shared<std::vector<ledger::PublisherBanner>>();
@@ -234,18 +265,29 @@ void PublisherServerList::ParsePublisherList(
234
265
}
235
266
236
267
if (list_publisher->empty ()) {
268
+ BLOG (ledger_, ledger::LogLevel::LOG_ERROR) << " Publisher list is empty" ;
237
269
callback (ledger::Result::LEDGER_ERROR);
238
270
return ;
239
271
}
240
272
241
- auto clear_callback = std::bind (&PublisherServerList::SaveParsedData,
273
+ // we need to clear table when we process first page, but only once
274
+ if (current_page_ == 1 ) {
275
+ auto clear_callback = std::bind (&PublisherServerList::SaveParsedData,
242
276
this ,
243
277
_1,
244
278
list_publisher,
245
279
list_banner,
246
280
callback);
247
281
248
- ledger_->ClearServerPublisherList (clear_callback);
282
+ ledger_->ClearServerPublisherList (clear_callback);
283
+ return ;
284
+ }
285
+
286
+ SaveParsedData (
287
+ ledger::Result::LEDGER_OK,
288
+ list_publisher,
289
+ list_banner,
290
+ callback);
249
291
}
250
292
251
293
void PublisherServerList::ParsePublisherBanner (
@@ -299,91 +341,63 @@ void PublisherServerList::SaveParsedData(
299
341
const ledger::Result result,
300
342
const SharedServerPublisherPartial& list_publisher,
301
343
const SharedPublisherBanner& list_banner,
302
- ParsePublisherListCallback callback) {
344
+ ledger::ResultCallback callback) {
303
345
if (result != ledger::Result::LEDGER_OK) {
346
+ BLOG (ledger_, ledger::LogLevel::LOG_ERROR) << " DB was not cleared" ;
304
347
callback (result);
305
348
return ;
306
349
}
307
350
308
- if (list_publisher && !list_publisher->empty ()) {
309
- SavePublishers (list_publisher, list_banner, callback);
351
+ if (!list_publisher || list_publisher->empty ()) {
352
+ BLOG (ledger_, ledger::LogLevel::LOG_ERROR) << " Publisher list is null" ;
353
+ callback (ledger::Result::LEDGER_ERROR);
310
354
return ;
311
355
}
312
356
313
- if (list_banner && !list_banner->empty ()) {
314
- SaveBanners (list_banner, callback);
315
- return ;
316
- }
357
+ auto save_callback = std::bind (&PublisherServerList::SaveBanners,
358
+ this ,
359
+ _1,
360
+ list_banner,
361
+ callback);
317
362
318
- callback (ledger::Result::LEDGER_OK );
363
+ ledger_-> InsertServerPublisherList (*list_publisher, save_callback );
319
364
}
320
365
321
- void PublisherServerList::SavePublishers (
322
- const SharedServerPublisherPartial& list_publisher ,
366
+ void PublisherServerList::SaveBanners (
367
+ const ledger::Result result ,
323
368
const SharedPublisherBanner& list_banner,
324
- ParsePublisherListCallback callback) {
325
- if (!list_publisher) {
326
- callback (ledger::Result::LEDGER_OK);
369
+ ledger::ResultCallback callback) {
370
+ if (!list_banner || result != ledger::Result::LEDGER_OK) {
371
+ BLOG (ledger_, ledger::LogLevel::LOG_ERROR) <<
372
+ " Publisher list was not saved" ;
373
+ callback (ledger::Result::LEDGER_ERROR);
327
374
return ;
328
375
}
329
376
330
- const int max_insert_records_ = 100000 ;
331
-
332
- int32_t interval = max_insert_records_;
333
- const auto list_size = list_publisher->size ();
334
- if (list_size < max_insert_records_) {
335
- interval = list_size;
377
+ if (list_banner->empty ()) {
378
+ callback (ledger::Result::CONTINUE);
379
+ return ;
336
380
}
337
381
338
- std::vector<ledger::ServerPublisherPartial> save_list (
339
- list_publisher->begin (),
340
- list_publisher->begin () + interval);
341
- auto new_list_publisher =
342
- std::make_shared<std::vector<ledger::ServerPublisherPartial>>(
343
- list_publisher->begin () + interval,
344
- list_publisher->end ());
345
-
346
- auto save_callback = std::bind (&PublisherServerList::SaveParsedData,
382
+ auto save_callback = std::bind (&PublisherServerList::BannerSaved,
347
383
this ,
348
384
_1,
349
- new_list_publisher,
350
- list_banner,
351
385
callback);
352
386
353
- ledger_->InsertServerPublisherList (save_list , save_callback);
387
+ ledger_->InsertPublisherBannerList (*list_banner , save_callback);
354
388
}
355
389
356
- void PublisherServerList::SaveBanners (
357
- const SharedPublisherBanner& list_banner ,
358
- ParsePublisherListCallback callback) {
359
- if (!list_banner ) {
360
- callback (ledger::Result::LEDGER_OK );
390
+ void PublisherServerList::BannerSaved (
391
+ const ledger::Result result ,
392
+ ledger::ResultCallback callback) {
393
+ if (result == ledger::Result::LEDGER_OK ) {
394
+ callback (ledger::Result::CONTINUE );
361
395
return ;
362
396
}
363
397
364
- const int max_insert_records_ = 80000 ;
365
-
366
- int32_t interval = max_insert_records_;
367
- const auto list_size = list_banner->size ();
368
- if (list_size < max_insert_records_) {
369
- interval = list_size;
370
- }
371
398
372
- std::vector<ledger::PublisherBanner> save_list (
373
- list_banner->begin (),
374
- list_banner->begin () + interval);
375
- auto new_list_banner = std::make_shared<std::vector<ledger::PublisherBanner>>(
376
- list_banner->begin () + interval,
377
- list_banner->end ());
378
-
379
- auto save_callback = std::bind (&PublisherServerList::SaveParsedData,
380
- this ,
381
- _1,
382
- nullptr ,
383
- new_list_banner,
384
- callback);
385
-
386
- ledger_->InsertPublisherBannerList (save_list, save_callback);
399
+ BLOG (ledger_, ledger::LogLevel::LOG_ERROR) << " Banners were not saved" ;
400
+ callback (result);
387
401
}
388
402
389
403
void PublisherServerList::ClearTimer () {
0 commit comments