Skip to content

Commit

Permalink
Refactor cached messages types
Browse files Browse the repository at this point in the history
  • Loading branch information
rygine committed Jul 22, 2024
1 parent ff793e2 commit 85802e8
Show file tree
Hide file tree
Showing 13 changed files with 72 additions and 60 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import {
hasAttachment,
attachmentContentTypeConfig,
} from "./attachment";
import { type CachedMessageWithId } from "@/helpers/caching/messages";
import { type CachedMessage } from "@/helpers/caching/messages";
import { createRandomWallet } from "@/helpers/testing";

const testWallet = createRandomWallet();
Expand Down Expand Up @@ -53,7 +53,7 @@ describe("ContentTypeRemoteAttachment caching", () => {
senderAddress: "testWalletAddress",
uuid: "testUuid",
xmtpID: "testXmtpId",
} satisfies CachedMessageWithId<Attachment>;
} satisfies CachedMessage<Attachment>;

const attachment = getAttachment(testMessage);
expect(attachment).toEqual(testContent);
Expand Down Expand Up @@ -82,7 +82,7 @@ describe("ContentTypeRemoteAttachment caching", () => {
senderAddress: "testWalletAddress",
uuid: "testUuid",
xmtpID: "testXmtpId",
} satisfies CachedMessageWithId<RemoteAttachment>;
} satisfies CachedMessage<RemoteAttachment>;

const attachment2 = getAttachment(testMessage2);
expect(attachment2).toEqual(testContent2);
Expand All @@ -101,7 +101,7 @@ describe("ContentTypeRemoteAttachment caching", () => {
senderAddress: "testWalletAddress",
uuid: "testUuid",
xmtpID: "testXmtpId",
} satisfies CachedMessageWithId;
} satisfies CachedMessage;

const attachment3 = getAttachment(testMessage3);
expect(attachment3).toBeUndefined();
Expand Down Expand Up @@ -129,7 +129,7 @@ describe("ContentTypeRemoteAttachment caching", () => {
senderAddress: "testWalletAddress",
uuid: "testUuid",
xmtpID: "testXmtpId",
} satisfies CachedMessageWithId<Attachment>;
} satisfies CachedMessage<Attachment>;

const attachment = hasAttachment(testMessage);
expect(attachment).toBe(true);
Expand All @@ -148,7 +148,7 @@ describe("ContentTypeRemoteAttachment caching", () => {
senderAddress: "testWalletAddress",
uuid: "testUuid",
xmtpID: "testXmtpId",
} satisfies CachedMessageWithId;
} satisfies CachedMessage;

const attachment2 = hasAttachment(testMessage2);
expect(attachment2).toBe(false);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import {
} from "./reaction";
import {
saveMessage,
type CachedMessageWithId,
type CachedMessage,
getMessageByXmtpID,
} from "@/helpers/caching/messages";
import { getDbInstance, clearCache } from "@/helpers/caching/db";
Expand Down Expand Up @@ -106,7 +106,7 @@ describe("ContentTypeReaction", () => {
senderAddress: "testWalletAddress",
uuid: "testUuid1",
xmtpID: "testXmtpId1",
} satisfies CachedMessageWithId;
} satisfies CachedMessage;

await saveMessage(testTextMessage, db);

Expand All @@ -131,7 +131,7 @@ describe("ContentTypeReaction", () => {
senderAddress: "testWalletAddress",
uuid: "testUuid2",
xmtpID: "testXmtpId2",
} satisfies CachedMessageWithId<Reaction>;
} satisfies CachedMessage<Reaction>;

const updateConversationMetadata = vi.fn();
await processReaction({
Expand Down Expand Up @@ -173,7 +173,7 @@ describe("ContentTypeReaction", () => {
senderAddress: "testWalletAddress",
uuid: "testUuid3",
xmtpID: "testXmtpId3",
} satisfies CachedMessageWithId<Reaction>;
} satisfies CachedMessage<Reaction>;

await processReaction({
client: testClient,
Expand Down Expand Up @@ -216,7 +216,7 @@ describe("ContentTypeReaction", () => {
senderAddress: "testWalletAddress",
uuid: "testUuid",
xmtpID: "testXmtpId",
} satisfies CachedMessageWithId;
} satisfies CachedMessage;

const updateConversationMetadata = vi.fn();
await processReaction({
Expand Down
20 changes: 10 additions & 10 deletions packages/react-sdk/src/helpers/caching/contentTypes/reply.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import {
} from "./reply";
import {
saveMessage,
type CachedMessageWithId,
type CachedMessage,
getMessageByXmtpID,
} from "@/helpers/caching/messages";
import { getDbInstance, clearCache } from "@/helpers/caching/db";
Expand Down Expand Up @@ -68,7 +68,7 @@ describe("ContentTypeReply", () => {
senderAddress: "testWalletAddress",
uuid: "testUuid1",
xmtpID: "testXmtpId1",
} satisfies CachedMessageWithId;
} satisfies CachedMessage;

await saveMessage(testTextMessage, db);

Expand All @@ -92,7 +92,7 @@ describe("ContentTypeReply", () => {
senderAddress: "testWalletAddress",
uuid: "testUuid2",
xmtpID: "testXmtpId2",
} satisfies CachedMessageWithId<Reply>;
} satisfies CachedMessage<Reply>;

await saveMessage(testReplyMessage, db);

Expand Down Expand Up @@ -145,7 +145,7 @@ describe("ContentTypeReply", () => {
senderAddress: "testWalletAddress",
uuid: "testUuid",
xmtpID: "testXmtpId",
} satisfies CachedMessageWithId;
} satisfies CachedMessage;

const updateConversationMetadata = vi.fn();
await processReply({
Expand Down Expand Up @@ -175,7 +175,7 @@ describe("ContentTypeReply", () => {
senderAddress: "testWalletAddress",
uuid: "testUuid1",
xmtpID: "testXmtpId1",
} satisfies CachedMessageWithId;
} satisfies CachedMessage;

const originalMessageFromReply = await getOriginalMessageFromReply(
testTextMessage,
Expand All @@ -201,7 +201,7 @@ describe("ContentTypeReply", () => {
senderAddress: "testWalletAddress",
uuid: "testUuid2",
xmtpID: "testXmtpId2",
} satisfies CachedMessageWithId<Reply>;
} satisfies CachedMessage<Reply>;

const originalMessageFromReply2 = await getOriginalMessageFromReply(
testReplyMessage,
Expand All @@ -227,7 +227,7 @@ describe("ContentTypeReply", () => {
senderAddress: "testWalletAddress",
uuid: "testUuid1",
xmtpID: "testXmtpId1",
} satisfies CachedMessageWithId;
} satisfies CachedMessage;

const replies = await getReplies(testTextMessage, db);
expect(replies).toEqual([]);
Expand All @@ -250,7 +250,7 @@ describe("ContentTypeReply", () => {
senderAddress: "testWalletAddress",
uuid: "testUuid1",
xmtpID: "testXmtpId1",
} satisfies CachedMessageWithId;
} satisfies CachedMessage;

const testReplyMessage1 = {
id: 2,
Expand All @@ -270,7 +270,7 @@ describe("ContentTypeReply", () => {
senderAddress: "testWalletAddress",
uuid: "testUuid2",
xmtpID: "testXmtpId2",
} satisfies CachedMessageWithId;
} satisfies CachedMessage;

await saveMessage(testReplyMessage1, db);

Expand All @@ -292,7 +292,7 @@ describe("ContentTypeReply", () => {
senderAddress: "testWalletAddress",
uuid: "testUuid3",
xmtpID: "testXmtpId3",
} satisfies CachedMessageWithId;
} satisfies CachedMessage;

await saveMessage(testReplyMessage2, db);

Expand Down
3 changes: 1 addition & 2 deletions packages/react-sdk/src/helpers/caching/contentTypes/reply.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import { z } from "zod";
import { ContentTypeId } from "@xmtp/content-type-primitives";
import type {
CachedMessage,
CachedMessageWithId,
CachedMessagesTable,
} from "@/helpers/caching/messages";
import type {
Expand Down Expand Up @@ -75,7 +74,7 @@ export const getReplies = async (message: CachedMessage, db: Dexie) => {
.where("xmtpID")
.anyOf(replies.map((reply) => reply.xmtpID))
.sortBy("sentAt");
return replyMessages as CachedMessageWithId[];
return replyMessages;
}
return [];
};
Expand Down
7 changes: 5 additions & 2 deletions packages/react-sdk/src/helpers/caching/db.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
import Dexie from "dexie";
import type { Client } from "@xmtp/xmtp-js";
import type { ContentCodec } from "@xmtp/content-type-primitives";
import type { CachedMessage } from "@/helpers/caching/messages";
import type {
CachedMessage,
CachedMessageWithOptionalId,
} from "@/helpers/caching/messages";
import type { CachedConversation } from "./conversations";
import { textContentTypeConfig } from "./contentTypes/text";

Expand Down Expand Up @@ -32,7 +35,7 @@ export type ContentTypeMessageProcessor<C = any> = (options: {
client: Client;
conversation: CachedConversation;
db: Dexie;
message: CachedMessage<C>;
message: CachedMessageWithOptionalId<C>;
processors?: ContentTypeMessageProcessors;
updateConversationMetadata: (
data: ContentTypeMetadataValues,
Expand Down
1 change: 1 addition & 0 deletions packages/react-sdk/src/helpers/caching/messages.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,7 @@ describe("saveMessage", () => {

it("should return a duplicate message", async () => {
const testMessage = {
id: 1,
walletAddress: "testWalletAddress",
conversationTopic: "testTopic",
content: "test",
Expand Down
41 changes: 24 additions & 17 deletions packages/react-sdk/src/helpers/caching/messages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ export type CachedMessage<C = any, M = ContentTypeMetadata> = {
conversationTopic: string;
hasLoadError: boolean;
hasSendError: boolean;
id?: number;
id: number;
isSending: boolean;
metadata?: M;
senderAddress: string;
Expand All @@ -44,12 +44,19 @@ export type CachedMessage<C = any, M = ContentTypeMetadata> = {
xmtpID: string;
};

export type CachedMessagesTable = Table<CachedMessage, number>;

export type CachedMessageWithId<C = any> = CachedMessage<C> & {
id: number;
export type CachedMessageWithOptionalId<C = any> = Omit<

Check warning on line 47 in packages/react-sdk/src/helpers/caching/messages.ts

View workflow job for this annotation

GitHub Actions / lint

Unexpected any. Specify a different type
CachedMessage<C>,
"id"
> & {
id?: CachedMessage<C>["id"];
};

export type CachedMessagesTable<C = any> = Table<

Check warning on line 54 in packages/react-sdk/src/helpers/caching/messages.ts

View workflow job for this annotation

GitHub Actions / lint

Unexpected any. Specify a different type
CachedMessage<C>,
number,
Omit<CachedMessage<C>, "id">
>;

/**
* Converts a DecodedMessage from the XMTP network to its cached format
*
Expand Down Expand Up @@ -83,7 +90,7 @@ export const toCachedMessage = (
uuid: v4(),
walletAddress,
xmtpID: message.id,
} satisfies CachedMessage;
} satisfies Omit<CachedMessage, "id">;
};

/**
Expand All @@ -96,7 +103,7 @@ export const toCachedMessage = (
export const getMessageByXmtpID = async (xmtpID: string, db: Dexie) => {
const messages = db.table("messages") as CachedMessagesTable;
const message = await messages.where("xmtpID").equals(xmtpID).first();
return message ? (message as CachedMessageWithId) : undefined;
return message;
};

export type SaveMessageOptions = Omit<
Expand All @@ -109,15 +116,18 @@ export type SaveMessageOptions = Omit<
*
* @returns The newly cached message, or an already existing cached message
*/
export const saveMessage = async (message: CachedMessage, db: Dexie) => {
export const saveMessage = async (
message: Omit<CachedMessage, "id">,
db: Dexie,
) => {
const messages = db.table("messages") as CachedMessagesTable;

// check if message already exists
const existing = await getMessageByXmtpID(message.xmtpID, db);

if (existing) {
// return the existing message
return existing as CachedMessageWithId;
return existing;
}

const id = await messages.add(message);
Expand All @@ -131,10 +141,7 @@ export const saveMessage = async (message: CachedMessage, db: Dexie) => {
/**
* Remove a message from the cache
*/
export const deleteMessage = async (
message: CachedMessageWithId,
db: Dexie,
) => {
export const deleteMessage = async (message: CachedMessage, db: Dexie) => {
const messagesTable = db.table("messages") as CachedMessagesTable;

// make sure message exists
Expand Down Expand Up @@ -208,7 +215,7 @@ export const prepareMessageForSending = async ({
conversation,
sendOptions,
}: PrepareMessageOptions): Promise<{
message: CachedMessage;
message: Omit<CachedMessage, "id">;
preparedMessage: Awaited<ReturnType<Conversation["prepareMessage"]>>;
}> => {
const networkConversation = await getConversationByTopic(
Expand Down Expand Up @@ -244,7 +251,7 @@ export const prepareMessageForSending = async ({
uuid: v4(),
walletAddress: client.address,
xmtpID: await preparedMessage.messageID(),
} satisfies CachedMessage;
} satisfies Omit<CachedMessage, "id">;

return {
message,
Expand Down Expand Up @@ -275,7 +282,7 @@ export type ProcessMessageOptions = {
client?: Client;
conversation: CachedConversation;
db: Dexie;
message: CachedMessage;
message: CachedMessageWithOptionalId;
namespaces: Record<string, string>;
processors: ContentTypeMessageProcessors;
validators: ContentTypeMessageValidators;
Expand Down Expand Up @@ -322,7 +329,7 @@ export const processMessage = async (
removeExisting = false,
): Promise<{
status: ProcessStatus;
message: CachedMessage;
message: CachedMessageWithOptionalId;
}> => {
// client is required
if (!client) {
Expand Down
Loading

0 comments on commit 85802e8

Please sign in to comment.