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"
@@ -25,6 +26,12 @@ using std::placeholders::_1;
25
26
using std::placeholders::_2;
26
27
using std::placeholders::_3;
27
28
29
+ namespace {
30
+
31
+ const int kHardLimit = 100 ;
32
+
33
+ } // namespace
34
+
28
35
namespace braveledger_publisher {
29
36
30
37
PublisherServerList::PublisherServerList (bat_ledger::LedgerImpl* ledger) :
@@ -38,19 +45,29 @@ PublisherServerList::~PublisherServerList() {
38
45
void PublisherServerList::OnTimer (uint32_t timer_id) {
39
46
if (timer_id == server_list_timer_id_) {
40
47
server_list_timer_id_ = 0 ;
41
- Download ([](const ledger::Result _){});
48
+ Start ([](const ledger::Result _){});
49
+ }
50
+ }
51
+
52
+ void PublisherServerList::Start (ledger::ResultCallback callback) {
53
+ if (in_progress_) {
54
+ BLOG (ledger_, ledger::LogLevel::LOG_INFO) << " Publisher list in progress" ;
55
+ callback (ledger::Result::LEDGER_OK);
56
+ return ;
42
57
}
58
+
59
+ in_progress_ = true ;
60
+ current_page_ = 1 ;
61
+
62
+ Download (callback);
43
63
}
44
64
45
- void PublisherServerList::Download (
46
- DownloadServerPublisherListCallback callback) {
65
+ void PublisherServerList::Download (ledger::ResultCallback callback) {
47
66
std::vector<std::string> headers;
48
67
headers.push_back (" Accept-Encoding: gzip" );
49
68
50
- const std::string url = braveledger_request_util::BuildUrl (
51
- GET_PUBLISHERS_LIST,
52
- " " ,
53
- braveledger_request_util::ServerTypes::PUBLISHER_DISTRO);
69
+ const std::string url =
70
+ braveledger_request_util::GetPublisherListUrl (current_page_);
54
71
55
72
const ledger::LoadURLCallback download_callback = std::bind (
56
73
&PublisherServerList::OnDownload,
@@ -73,13 +90,20 @@ void PublisherServerList::OnDownload(
73
90
int response_status_code,
74
91
const std::string& response,
75
92
const std::map<std::string, std::string>& headers,
76
- DownloadServerPublisherListCallback callback) {
93
+ ledger::ResultCallback callback) {
77
94
ledger_->LogResponse (
78
95
__func__,
79
96
response_status_code,
80
97
" Publisher list" ,
81
98
headers);
82
99
100
+ // we iterated through all pages
101
+ if (response_status_code == net::HTTP_NO_CONTENT) {
102
+ in_progress_ = false ;
103
+ OnParsePublisherList (ledger::Result::LEDGER_OK, callback);
104
+ return ;
105
+ }
106
+
83
107
if (response_status_code == net::HTTP_OK && !response.empty ()) {
84
108
const auto parse_callback =
85
109
std::bind (&PublisherServerList::OnParsePublisherList, this , _1, callback);
@@ -103,15 +127,22 @@ void PublisherServerList::OnDownload(
103
127
104
128
void PublisherServerList::OnParsePublisherList (
105
129
const ledger::Result result,
106
- DownloadServerPublisherListCallback callback) {
130
+ ledger::ResultCallback callback) {
131
+ if (result == ledger::Result::CONTINUE && current_page_ < kHardLimit ) {
132
+ current_page_++;
133
+ Download (callback);
134
+ return ;
135
+ }
136
+
107
137
uint64_t new_time = 0ull ;
108
- if (result == ledger::Result::LEDGER_OK ) {
138
+ if (result != ledger::Result::LEDGER_ERROR ) {
109
139
ledger_->ContributeUnverifiedPublishers ();
110
140
new_time = braveledger_time_util::GetCurrentTimeStamp ();
111
141
}
112
142
113
143
ledger_->SetUint64State (ledger::kStateServerPublisherListStamp , new_time);
114
144
145
+ in_progress_ = false ;
115
146
bool retry_after_error = result != ledger::Result::LEDGER_OK;
116
147
SetTimer (retry_after_error);
117
148
@@ -166,7 +197,7 @@ uint64_t PublisherServerList::GetTimerTime(
166
197
? 0ull
167
198
: now_seconds - last_download;
168
199
169
- uint64_t interval =
200
+ const uint64_t interval =
170
201
ledger_->GetUint64Option (ledger::kOptionPublisherListRefreshInterval );
171
202
172
203
if (now_seconds == last_download) {
@@ -196,7 +227,7 @@ ledger::PublisherStatus PublisherServerList::ParsePublisherStatus(
196
227
197
228
void PublisherServerList::ParsePublisherList (
198
229
const std::string& data,
199
- ParsePublisherListCallback callback) {
230
+ ledger::ResultCallback callback) {
200
231
auto list_publisher =
201
232
std::make_shared<std::vector<ledger::ServerPublisherPartial>>();
202
233
auto list_banner = std::make_shared<std::vector<ledger::PublisherBanner>>();
@@ -247,18 +278,29 @@ void PublisherServerList::ParsePublisherList(
247
278
}
248
279
249
280
if (list_publisher->empty ()) {
281
+ BLOG (ledger_, ledger::LogLevel::LOG_ERROR) << " Publisher list is empty" ;
250
282
callback (ledger::Result::LEDGER_ERROR);
251
283
return ;
252
284
}
253
285
254
- auto clear_callback = std::bind (&PublisherServerList::SaveParsedData,
286
+ // we need to clear table when we process first page, but only once
287
+ if (current_page_ == 1 ) {
288
+ auto clear_callback = std::bind (&PublisherServerList::SaveParsedData,
255
289
this ,
256
290
_1,
257
291
list_publisher,
258
292
list_banner,
259
293
callback);
260
294
261
- ledger_->ClearServerPublisherList (clear_callback);
295
+ ledger_->ClearServerPublisherList (clear_callback);
296
+ return ;
297
+ }
298
+
299
+ SaveParsedData (
300
+ ledger::Result::LEDGER_OK,
301
+ list_publisher,
302
+ list_banner,
303
+ callback);
262
304
}
263
305
264
306
void PublisherServerList::ParsePublisherBanner (
@@ -312,91 +354,63 @@ void PublisherServerList::SaveParsedData(
312
354
const ledger::Result result,
313
355
const SharedServerPublisherPartial& list_publisher,
314
356
const SharedPublisherBanner& list_banner,
315
- ParsePublisherListCallback callback) {
357
+ ledger::ResultCallback callback) {
316
358
if (result != ledger::Result::LEDGER_OK) {
359
+ BLOG (ledger_, ledger::LogLevel::LOG_ERROR) << " DB was not cleared" ;
317
360
callback (result);
318
361
return ;
319
362
}
320
363
321
- if (list_publisher && !list_publisher->empty ()) {
322
- SavePublishers (list_publisher, list_banner, callback);
364
+ if (!list_publisher || list_publisher->empty ()) {
365
+ BLOG (ledger_, ledger::LogLevel::LOG_ERROR) << " Publisher list is null" ;
366
+ callback (ledger::Result::LEDGER_ERROR);
323
367
return ;
324
368
}
325
369
326
- if (list_banner && !list_banner->empty ()) {
327
- SaveBanners (list_banner, callback);
328
- return ;
329
- }
370
+ auto save_callback = std::bind (&PublisherServerList::SaveBanners,
371
+ this ,
372
+ _1,
373
+ list_banner,
374
+ callback);
330
375
331
- callback (ledger::Result::LEDGER_OK );
376
+ ledger_-> InsertServerPublisherList (*list_publisher, save_callback );
332
377
}
333
378
334
- void PublisherServerList::SavePublishers (
335
- const SharedServerPublisherPartial& list_publisher ,
379
+ void PublisherServerList::SaveBanners (
380
+ const ledger::Result result ,
336
381
const SharedPublisherBanner& list_banner,
337
- ParsePublisherListCallback callback) {
338
- if (!list_publisher) {
339
- callback (ledger::Result::LEDGER_OK);
382
+ ledger::ResultCallback callback) {
383
+ if (!list_banner || result != ledger::Result::LEDGER_OK) {
384
+ BLOG (ledger_, ledger::LogLevel::LOG_ERROR) <<
385
+ " Publisher list was not saved" ;
386
+ callback (ledger::Result::LEDGER_ERROR);
340
387
return ;
341
388
}
342
389
343
- const int max_insert_records_ = 100000 ;
344
-
345
- int32_t interval = max_insert_records_;
346
- const auto list_size = list_publisher->size ();
347
- if (list_size < max_insert_records_) {
348
- interval = list_size;
390
+ if (list_banner->empty ()) {
391
+ callback (ledger::Result::CONTINUE);
392
+ return ;
349
393
}
350
394
351
- std::vector<ledger::ServerPublisherPartial> save_list (
352
- list_publisher->begin (),
353
- list_publisher->begin () + interval);
354
- auto new_list_publisher =
355
- std::make_shared<std::vector<ledger::ServerPublisherPartial>>(
356
- list_publisher->begin () + interval,
357
- list_publisher->end ());
358
-
359
- auto save_callback = std::bind (&PublisherServerList::SaveParsedData,
395
+ auto save_callback = std::bind (&PublisherServerList::BannerSaved,
360
396
this ,
361
397
_1,
362
- new_list_publisher,
363
- list_banner,
364
398
callback);
365
399
366
- ledger_->InsertServerPublisherList (save_list , save_callback);
400
+ ledger_->InsertPublisherBannerList (*list_banner , save_callback);
367
401
}
368
402
369
- void PublisherServerList::SaveBanners (
370
- const SharedPublisherBanner& list_banner ,
371
- ParsePublisherListCallback callback) {
372
- if (!list_banner ) {
373
- callback (ledger::Result::LEDGER_OK );
403
+ void PublisherServerList::BannerSaved (
404
+ const ledger::Result result ,
405
+ ledger::ResultCallback callback) {
406
+ if (result == ledger::Result::LEDGER_OK ) {
407
+ callback (ledger::Result::CONTINUE );
374
408
return ;
375
409
}
376
410
377
- const int max_insert_records_ = 80000 ;
378
-
379
- int32_t interval = max_insert_records_;
380
- const auto list_size = list_banner->size ();
381
- if (list_size < max_insert_records_) {
382
- interval = list_size;
383
- }
384
411
385
- std::vector<ledger::PublisherBanner> save_list (
386
- list_banner->begin (),
387
- list_banner->begin () + interval);
388
- auto new_list_banner = std::make_shared<std::vector<ledger::PublisherBanner>>(
389
- list_banner->begin () + interval,
390
- list_banner->end ());
391
-
392
- auto save_callback = std::bind (&PublisherServerList::SaveParsedData,
393
- this ,
394
- _1,
395
- nullptr ,
396
- new_list_banner,
397
- callback);
398
-
399
- ledger_->InsertPublisherBannerList (save_list, save_callback);
412
+ BLOG (ledger_, ledger::LogLevel::LOG_ERROR) << " Banners were not saved" ;
413
+ callback (result);
400
414
}
401
415
402
416
void PublisherServerList::ClearTimer () {
0 commit comments