import { Client , Discussion , DisqussionQuery } from 'dsteem' ; export type FilterFn = ( Discussion ) => boolean ; const allowAll = ( post ) => true ; export async function fetchPosts ( client , author , limit = 10 , filter = allowAll , from ? ) { let results = [ ] ; let seen = { } ; let query = { tag : author , limit : limit + 1 } ; if ( from ) { query . start_author = author ; query . start_permlink = from ; } do { const posts = await client . database . getDiscussions ( 'blog' , query ) ; let valid = [ ] ; for ( const post of posts ) { if ( post . author === author && ! seen [ post . id ] ) { seen [ post . id ] = true ; valid . push ( post ) ; if ( results . length === limit ) { break ; } } } results = results . concat ( valid . filter ( filter ) ) ; if ( posts . length !== query . limit ) { break ; } query . start_author = posts [ posts . length - 1 ] . author ; query . start_permlink = posts [ posts . length - 1 ] . permlink ; } while ( results . length < limit ) ; return results ; } export async function fetchPost ( client , author , permalink ) { let query = { tag : author , limit : 1 , start_author : author , start_permlink : permalink } ; let results = await client . database . getDiscussions ( 'blog' , query ) ; if ( results . length !== 1 ) { throw new Error ( `Unable to find post: @${ author }/${ permalink }` ) ; } return results [ 0 ] ; }