Add ability to have claim searches auto-fetch up to 3 pages. (#504)

* Add ability to have claim searches auto-fetch up to 3 pages.

* make total_items and total_pages optional

* use auto pagination strategy when determining live claim

* Bump page size back to 50
This commit is contained in:
Dan Peterson 2021-12-20 12:25:16 -06:00 committed by GitHub
parent 3b6d3bf107
commit 2be14c86c3
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 73 additions and 26 deletions

4
flow-typed/Lbry.js vendored
View file

@ -101,8 +101,8 @@ declare type ClaimSearchResponse = {
items: Array<Claim>,
page: number,
page_size: number,
total_items: number,
total_pages: number,
total_items?: number,
total_pages?: number,
};
declare type ClaimListResponse = {

View file

@ -615,11 +615,16 @@ export function doClaimSearch(
order_by?: Array<string>,
release_time?: string,
has_source?: boolean,
has_no_souce?: boolean,
has_no_source?: boolean,
} = {
no_totals: true,
page_size: 10,
page: 1,
},
settings: {
useAutoPagination?: boolean,
} = {
useAutoPagination: false,
}
) {
const query = createNormalizedClaimSearchKey(options);
@ -659,7 +664,39 @@ export function doClaimSearch(
return false;
};
return await Lbry.claim_search(options).then(success, failure);
const autoPaginate = () => {
let allClaims = [];
const next = async (data: ClaimSearchResponse) => {
allClaims = allClaims.concat(data.items);
const moreData = data.items.length === options.page_size;
options.page++;
if (options.page > 3 || !moreData) {
// Flow doesn't understand that page_size is an optional property with a guaranteed default value.
// $FlowFixMe
return success({
items: allClaims,
page: options.page,
page_size: options.page_size,
});
}
try {
const data = await Lbry.claim_search(options);
return next(data);
} catch (err) {
failure(err);
}
};
return next;
};
const successCallback = settings.useAutoPagination ? autoPaginate() : success;
return await Lbry.claim_search(options).then(successCallback, failure);
};
}

View file

@ -74,7 +74,8 @@ const filterUpcomingLiveStreamClaims = (upcomingClaims) => {
};
const fetchUpcomingLivestreamClaims = (channelIds: Array<string>) => {
return doClaimSearch({
return doClaimSearch(
{
page: 1,
page_size: 50,
has_no_source: true,
@ -84,11 +85,16 @@ const fetchUpcomingLivestreamClaims = (channelIds: Array<string>) => {
release_time: `>${moment().subtract(5, 'minutes').unix()}`,
limit_claims_per_channel: 1,
no_totals: true,
});
},
{
useAutoPagination: true,
}
);
};
const fetchMostRecentLivestreamClaims = (channelIds: Array<string>, orderBy: Array<string> = ['release_time']) => {
return doClaimSearch({
return doClaimSearch(
{
page: 1,
page_size: 50,
has_no_source: true,
@ -98,7 +104,11 @@ const fetchMostRecentLivestreamClaims = (channelIds: Array<string>, orderBy: Arr
release_time: `<${moment().unix()}`,
limit_claims_per_channel: 2,
no_totals: true,
});
},
{
useAutoPagination: true,
}
);
};
const distanceFromStreamStart = (claimA: any, claimB: any, channelStartedStreaming) => {