import { Repository } from '../core/repository' ; import { StoryServiceInput , StoryServiceSeenInputItems , StoryServiceSeenInputReels } from '../types' ; export class StoryService extends Repository { seen ( input , sourceId = null ) { let items ; if ( input instanceof Array ) { items = input ; } else { items = Object . values ( input ) . reduce ( ( accumulator , current ) => accumulator . concat ( current . items ) , [ ] ) ; } const reels = { } ; const maxSeenAt = Math . floor ( Date . now ( ) / 1000 ) ; let seenAt = maxSeenAt - items . length ; for ( const item of items ) { const itemTakenAt = item . taken_at ; if ( seenAt < itemTakenAt ) { seenAt = itemTakenAt + 1 ; } if ( seenAt > maxSeenAt ) { seenAt = maxSeenAt ; } const itemSourceId = sourceId === null ? item . user . pk : sourceId ; const reelId = `${ item . id }_${ itemSourceId }` ; reels [ reelId ] = [ `${ itemTakenAt }_${ seenAt }` ] ; seenAt += 1 ; } return this . client . media . seen ( reels ) ; } }