Skip to content

Commit

Permalink
Merge pull request #143 from peterferguson/feat/allow-numeric-timesta…
Browse files Browse the repository at this point in the history
…mp-in-listMessages

feat: allow numeric timestamp in listMessages
  • Loading branch information
nplasterer authored Nov 8, 2023
2 parents 7111dd1 + 8685677 commit f1e74ef
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 13 deletions.
30 changes: 27 additions & 3 deletions example/src/tests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
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

0 comments on commit f1e74ef

Please sign in to comment.