Skip to content

Commit

Permalink
Add reaction timestamp, sort reactions by it
Browse files Browse the repository at this point in the history
  • Loading branch information
rygine committed Oct 5, 2023
1 parent 11e62a3 commit 3f32a73
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -43,27 +43,39 @@ describe("ContentTypeReaction caching", () => {

describe("saveReaction", () => {
it("should save a reaction to the cache", async () => {
const firstSentAt = new Date();
const testReaction = {
content: "test",
referenceXmtpID: "testXmtpId",
schema: "custom",
senderAddress: "testWalletAddress",
sentAt: firstSentAt,
xmtpID: "testXmtpId",
} satisfies CachedReaction;

const reactionId = await saveReaction(testReaction, db);
expect(reactionId).toEqual(1);

const testReactions = await getReactionsByXmtpID("testXmtpId", db);
expect(testReactions.length).toEqual(1);
expect(testReactions[0].sentAt).toEqual(firstSentAt);

const secondSentAt = new Date();
const testReaction2 = {
content: "test",
referenceXmtpID: "testXmtpId",
schema: "custom",
senderAddress: "testWalletAddress",
sentAt: secondSentAt,
xmtpID: "testXmtpId",
} satisfies CachedReaction;

const reactionId2 = await saveReaction(testReaction2, db);
expect(reactionId2).toEqual(1);

const testReactions2 = await getReactionsByXmtpID("testXmtpId", db);
expect(testReactions2.length).toEqual(1);
expect(testReactions2[0].sentAt).toEqual(secondSentAt);
});
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ export type CachedReaction = {
referenceXmtpID: Reaction["reference"];
schema: Reaction["schema"];
senderAddress: string;
sentAt: Date;
xmtpID: string;
};

Expand Down Expand Up @@ -75,6 +76,10 @@ export const saveReaction = async (reaction: CachedReaction, db: Dexie) => {
// check if reaction already exists
const existing = await findReaction(reaction, db);
if (existing) {
// update when the reaction was sent
await reactionsTable.update(existing.id, {
sentAt: reaction.sentAt,
});
return existing.id;
}

Expand Down Expand Up @@ -108,7 +113,7 @@ export const getReactionsByXmtpID = async (
db: Dexie,
) => {
const reactionsTable = db.table("reactions") as CachedReactionsTable;
return reactionsTable.where({ referenceXmtpID: xmtpID }).toArray();
return reactionsTable.where({ referenceXmtpID: xmtpID }).sortBy("sentAt");
};

/**
Expand Down Expand Up @@ -178,6 +183,7 @@ export const processReaction: ContentTypeMessageProcessor = async ({
referenceXmtpID: reaction.reference,
schema: reaction.schema,
senderAddress: message.senderAddress,
sentAt: message.sentAt,
xmtpID: message.xmtpID,
} satisfies CachedReaction;

Expand Down Expand Up @@ -210,6 +216,7 @@ export const reactionContentTypeConfig: ContentTypeConfiguration = {
content,
schema,
senderAddress,
sentAt,
xmtpID
`,
},
Expand Down
2 changes: 1 addition & 1 deletion packages/react-sdk/src/hooks/useReactions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ export const useReactions = (message?: CachedMessage) => {
return await reactionsTable
.where("referenceXmtpID")
.equals(message.xmtpID)
.toArray();
.sortBy("sentAt");
} catch {
return [];
}
Expand Down

0 comments on commit 3f32a73

Please sign in to comment.