diff --git a/example/src/tests.ts b/example/src/tests.ts index 5ce075b5f..e837af9ad 100644 --- a/example/src/tests.ts +++ b/example/src/tests.ts @@ -48,21 +48,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, + finalQueryDate.getTime() + ); + + const passingTimestampFieldSuccessful = !messages3.length && messages4.length === 1; - return !messages1.length && messages2.length === 1 && isAboutRightSendTime; + return passingTimestampFieldSuccessful } catch (e) { return false; } diff --git a/src/index.ts b/src/index.ts index 4961613a3..4988d4e77 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 19637a9e3..0f3df09fd 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;