From 92a10ca738f7c7fa89b6f2d99ab0c339fa9d9bb0 Mon Sep 17 00:00:00 2001 From: Louis Date: Sat, 19 Mar 2022 10:45:16 +0100 Subject: [PATCH] fix: Auto-convert start_time to a since_id when until ID is required in paginators --- src/paginators/tweet.paginator.v2.ts | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/src/paginators/tweet.paginator.v2.ts b/src/paginators/tweet.paginator.v2.ts index 0722482..bed3349 100644 --- a/src/paginators/tweet.paginator.v2.ts +++ b/src/paginators/tweet.paginator.v2.ts @@ -51,6 +51,16 @@ abstract class TweetTimelineV2Paginator< if (this._realData.meta.next_token) { params.next_token = this._realData.meta.next_token; } else { + if (params.start_time) { + // until_id and start_time are forbidden together for some reason, so convert start_time to a since_id. + params.since_id = this.dateStringToSnowflakeId(params.start_time as string); + delete params.start_time; + } + if (params.end_time) { + // until_id overrides end_time, so delete it + delete params.end_time; + } + params.until_id = this._realData.meta.oldest_id; } @@ -82,6 +92,18 @@ abstract class TweetTimelineV2Paginator< return this.tweets; } + protected dateStringToSnowflakeId(dateStr: string) { + const TWITTER_START_EPOCH = BigInt('1288834974657'); + const date = new Date(dateStr); + + if (isNaN(date.valueOf())) { + throw new Error('Unable to convert start_time/end_time to a valid date. A ISO 8601 DateTime is excepted, please check your input.'); + } + + const dateTimestamp = BigInt(date.valueOf()); + return ((dateTimestamp - TWITTER_START_EPOCH) << BigInt('22')).toString(); + } + /** * Tweets returned by paginator. */