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>, items: Array<Claim>,
page: number, page: number,
page_size: number, page_size: number,
total_items: number, total_items?: number,
total_pages: number, total_pages?: number,
}; };
declare type ClaimListResponse = { declare type ClaimListResponse = {

View file

@ -615,11 +615,16 @@ export function doClaimSearch(
order_by?: Array<string>, order_by?: Array<string>,
release_time?: string, release_time?: string,
has_source?: boolean, has_source?: boolean,
has_no_souce?: boolean, has_no_source?: boolean,
} = { } = {
no_totals: true, no_totals: true,
page_size: 10, page_size: 10,
page: 1, page: 1,
},
settings: {
useAutoPagination?: boolean,
} = {
useAutoPagination: false,
} }
) { ) {
const query = createNormalizedClaimSearchKey(options); const query = createNormalizedClaimSearchKey(options);
@ -659,7 +664,39 @@ export function doClaimSearch(
return false; 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,31 +74,41 @@ const filterUpcomingLiveStreamClaims = (upcomingClaims) => {
}; };
const fetchUpcomingLivestreamClaims = (channelIds: Array<string>) => { const fetchUpcomingLivestreamClaims = (channelIds: Array<string>) => {
return doClaimSearch({ return doClaimSearch(
page: 1, {
page_size: 50, page: 1,
has_no_source: true, page_size: 50,
channel_ids: channelIds, has_no_source: true,
claim_type: ['stream'], channel_ids: channelIds,
order_by: ['^release_time'], claim_type: ['stream'],
release_time: `>${moment().subtract(5, 'minutes').unix()}`, order_by: ['^release_time'],
limit_claims_per_channel: 1, release_time: `>${moment().subtract(5, 'minutes').unix()}`,
no_totals: true, limit_claims_per_channel: 1,
}); no_totals: true,
},
{
useAutoPagination: true,
}
);
}; };
const fetchMostRecentLivestreamClaims = (channelIds: Array<string>, orderBy: Array<string> = ['release_time']) => { const fetchMostRecentLivestreamClaims = (channelIds: Array<string>, orderBy: Array<string> = ['release_time']) => {
return doClaimSearch({ return doClaimSearch(
page: 1, {
page_size: 50, page: 1,
has_no_source: true, page_size: 50,
channel_ids: channelIds, has_no_source: true,
claim_type: ['stream'], channel_ids: channelIds,
order_by: orderBy, claim_type: ['stream'],
release_time: `<${moment().unix()}`, order_by: orderBy,
limit_claims_per_channel: 2, release_time: `<${moment().unix()}`,
no_totals: true, limit_claims_per_channel: 2,
}); no_totals: true,
},
{
useAutoPagination: true,
}
);
}; };
const distanceFromStreamStart = (claimA: any, claimB: any, channelStartedStreaming) => { const distanceFromStreamStart = (claimA: any, claimB: any, channelStartedStreaming) => {