Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: allow numeric timestamp in listMessages #143

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 27 additions & 3 deletions example/src/tests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
finalQueryDate.getTime()
);

const passingTimestampFieldSuccessful = !messages3.length && messages4.length === 1;

return !messages1.length && messages2.length === 1 && isAboutRightSendTime;
return passingTimestampFieldSuccessful
} catch (e) {
return false;
}
Expand Down
12 changes: 6 additions & 6 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<DecodedMessage[]> {
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) => {
Expand All @@ -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",
});
});
Expand Down
4 changes: 2 additions & 2 deletions src/lib/Conversation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<DecodedMessage[]> {
try {
Expand Down
4 changes: 2 additions & 2 deletions src/lib/Query.ts
Original file line number Diff line number Diff line change
@@ -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;
Expand Down
Loading