From 98de258b0e19a9190bc112217f9a92322ac8cd6a Mon Sep 17 00:00:00 2001 From: Peter Ferguson Date: Fri, 27 Oct 2023 08:57:22 +0100 Subject: [PATCH 1/3] feat: allow numeric timestamp in listMessages --- src/index.ts | 12 ++++++------ src/lib/Conversation.ts | 4 ++-- src/lib/Query.ts | 4 ++-- 3 files changed, 10 insertions(+), 10 deletions(-) diff --git a/src/index.ts b/src/index.ts index 22ed13739..58867f50c 100644 --- a/src/index.ts +++ b/src/index.ts @@ -117,16 +117,16 @@ export async function listMessages( clientAddress: string, conversationTopic: string, limit?: number | undefined, - before?: Date | undefined, - after?: Date | undefined, + before?: number | Date | undefined, + after?: number | Date | undefined, direction?: "SORT_DIRECTION_ASCENDING" | "SORT_DIRECTION_DESCENDING" | undefined, ): Promise { const messages = await XMTPModule.loadMessages( clientAddress, conversationTopic, limit, - before?.getTime(), - after?.getTime(), + typeof before === 'number' ? before : before?.getTime(), + typeof after === 'number' ? after : after?.getTime(), direction || "SORT_DIRECTION_DESCENDING", ); return messages.map((json: string) => { @@ -142,8 +142,8 @@ export async function listBatchMessages( return JSON.stringify({ limit: item.pageSize || 0, topic: item.contentTopic, - after: item.startTime?.getTime() || 0, - before: item.endTime?.getTime() || 0, + after: (typeof item.startTime === 'number' ? item.startTime : item.startTime?.getTime()) || 0, + before: (typeof item.endTime === 'number' ? item.endTime : item.endTime?.getTime()) || 0, direction: item.direction || "SORT_DIRECTION_DESCENDING", }); }); diff --git a/src/lib/Conversation.ts b/src/lib/Conversation.ts index c400f285a..59f475106 100644 --- a/src/lib/Conversation.ts +++ b/src/lib/Conversation.ts @@ -39,8 +39,8 @@ export class Conversation { // TODO: Support pagination and conversation ID here async messages( limit?: number | undefined, - before?: Date | undefined, - after?: Date | undefined, + before?: number | Date | undefined, + after?: number | Date | undefined, direction?: "SORT_DIRECTION_ASCENDING" | "SORT_DIRECTION_DESCENDING" | undefined, ): Promise { try { diff --git a/src/lib/Query.ts b/src/lib/Query.ts index cacb975df..9ff918c16 100644 --- a/src/lib/Query.ts +++ b/src/lib/Query.ts @@ -1,6 +1,6 @@ export type Query = { - startTime?: Date | undefined; - endTime?: Date | undefined; + startTime?: number | Date | undefined; + endTime?: number | Date | undefined; contentTopic: string; pageSize?: number | undefined; direction?: "SORT_DIRECTION_ASCENDING" | "SORT_DIRECTION_DESCENDING" | undefined; From b5b897cc61c2054334f2fd8262dcb32a37d565d8 Mon Sep 17 00:00:00 2001 From: Peter Ferguson Date: Tue, 7 Nov 2023 22:00:42 +0000 Subject: [PATCH 2/3] chore: add tests for timestamp fields --- example/src/tests.ts | 30 +++++++++++++++++++++++++++--- 1 file changed, 27 insertions(+), 3 deletions(-) diff --git a/example/src/tests.ts b/example/src/tests.ts index e0ed091c7..0f5277662 100644 --- a/example/src/tests.ts +++ b/example/src/tests.ts @@ -52,21 +52,45 @@ test("can pass a custom filter date and receive message objects with expected da let sentAt = Date.now(); await bobConversation.send({ text: "hello" }); + const initialQueryDate = new Date("2023-01-01") + const finalQueryDate = new Date("2025-01-01") + // Show all messages before date in the past const messages1: DecodedMessage[] = await aliceConversation.messages( undefined, - new Date("2023-01-01") + initialQueryDate ); // Show all messages before date in the future const messages2: DecodedMessage[] = await aliceConversation.messages( undefined, - new Date("2025-01-01") + finalQueryDate ); const isAboutRightSendTime = Math.abs(messages2[0].sent - sentAt) < 1000; + if (!isAboutRightSendTime) return false + + const passingDateFieldSuccessful = !messages1.length && messages2.length === 1; + + if (!passingDateFieldSuccessful) return false + + // repeat the above test with a numeric date value + + // Show all messages before date in the past + const messages3: DecodedMessage[] = await aliceConversation.messages( + undefined, + initialQueryDate.getTime() + ); + + // Show all messages before date in the future + const messages4: DecodedMessage[] = await aliceConversation.messages( + undefined, + initialQueryDate.getTime() + ); + + const passingTimestampFieldSuccessful = !messages3.length && messages4.length === 1; - return !messages1.length && messages2.length === 1 && isAboutRightSendTime; + return passingTimestampFieldSuccessful } catch (e) { return false; } From 8685677a344f051435d07cc1888b3631d0d210a0 Mon Sep 17 00:00:00 2001 From: Naomi Plasterer Date: Tue, 7 Nov 2023 18:59:40 -0800 Subject: [PATCH 3/3] Small tweak to the test --- example/src/tests.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/example/src/tests.ts b/example/src/tests.ts index 0f5277662..2b8eee4be 100644 --- a/example/src/tests.ts +++ b/example/src/tests.ts @@ -85,7 +85,7 @@ test("can pass a custom filter date and receive message objects with expected da // Show all messages before date in the future const messages4: DecodedMessage[] = await aliceConversation.messages( undefined, - initialQueryDate.getTime() + finalQueryDate.getTime() ); const passingTimestampFieldSuccessful = !messages3.length && messages4.length === 1;