-
Notifications
You must be signed in to change notification settings - Fork 4
Message history #38
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
Open
hzrd149
wants to merge
7
commits into
master
Choose a base branch
from
message-history
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Message history #38
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
9f5a5cf
refactor(chat): update group creation logic and improve error handling
hzrd149 2fcc818
feat(chat): enhance group management and history tracking
hzrd149 124b9d9
fix tests
hzrd149 0594502
format code
hzrd149 db7b2b4
simplify class names
hzrd149 d1bda19
Rename MarmotGroupHistoryStore to GroupRumorHistory
hzrd149 ad001ae
Cleanup storage interfaces for chat app.
hzrd149 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,88 @@ | ||
| import type { Rumor } from "applesauce-common/helpers/gift-wrap"; | ||
| import { type MarmotGroup, type GroupRumorHistory } from "marmot-ts"; | ||
| import { useCallback, useEffect, useMemo, useState } from "react"; | ||
|
|
||
| /** | ||
| * Hook that loads and subscribes to group messages: paginated history from the | ||
| * group history store and live rumors via the history "rumor" listener. | ||
| * | ||
| * @param group - The current group, or null when none is selected | ||
| * @returns messages, loadMoreMessages, loadingMore, loadingDone, and | ||
| * addNewMessages for optimistic updates (e.g. after sending) | ||
| * | ||
| * @example | ||
| * ```tsx | ||
| * const { messages, loadMoreMessages, loadingMore, loadingDone, addNewMessages } = | ||
| * useGroupMessages(group); | ||
| * // After send: addNewMessages([sentRumor]); | ||
| * ``` | ||
| */ | ||
| export function useGroupMessages( | ||
| group: MarmotGroup<GroupRumorHistory> | null, | ||
| ): { | ||
| messages: Rumor[]; | ||
| loadMoreMessages: () => Promise<void>; | ||
| loadingMore: boolean; | ||
| loadingDone: boolean; | ||
| } { | ||
| const [messages, setMessages] = useState<Rumor[]>([]); | ||
| const [loadingDone, setLoadingDone] = useState(false); | ||
| const [loadingMore, setLoadingMore] = useState(false); | ||
|
|
||
| const paginatedLoader = useMemo(() => { | ||
| if (!group?.history) return null; | ||
| return group.history.createPaginatedLoader({ limit: 50 }); | ||
| }, [group]); | ||
|
|
||
| const addNewMessages = useCallback((newMessages: Rumor[]) => { | ||
| setMessages((prev) => { | ||
| const messageMap = new Map<string, Rumor>(); | ||
| prev.forEach((msg) => { | ||
| if (msg.id) messageMap.set(msg.id, msg); | ||
| }); | ||
| newMessages.forEach((msg) => { | ||
| if (msg.id) messageMap.set(msg.id, msg); | ||
| }); | ||
| const combined = Array.from(messageMap.values()); | ||
| return combined.sort((a, b) => a.created_at - b.created_at); | ||
| }); | ||
| }, []); | ||
|
|
||
| const loadMoreMessages = useCallback(async () => { | ||
| if (!paginatedLoader) return; | ||
| setLoadingMore(true); | ||
| const page = await paginatedLoader.next(); | ||
| addNewMessages(page.value); | ||
| if (page.done) setLoadingDone(page.done); | ||
| setLoadingMore(false); | ||
| }, [paginatedLoader, addNewMessages]); | ||
|
|
||
| // Clear messages and reset loading state when group changes | ||
| useEffect(() => { | ||
| setMessages([]); | ||
| setLoadingDone(false); | ||
| setLoadingMore(false); | ||
| }, [group]); | ||
|
|
||
| // Load initial messages | ||
| useEffect(() => { | ||
| loadMoreMessages(); | ||
| }, [loadMoreMessages]); | ||
|
|
||
| // Subscribe to new rumors | ||
| useEffect(() => { | ||
| if (!group?.history) return; | ||
| const listener = (rumor: Rumor) => addNewMessages([rumor]); | ||
| group.history.addListener("rumor", listener); | ||
| return () => { | ||
| group.history.removeListener("rumor", listener); | ||
| }; | ||
| }, [group, addNewMessages]); | ||
|
|
||
| return { | ||
| messages, | ||
| loadMoreMessages, | ||
| loadingMore, | ||
| loadingDone, | ||
| }; | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,203 @@ | ||
| import { Rumor } from "applesauce-common/helpers"; | ||
| import { | ||
| bytesToHex, | ||
| Filter, | ||
| matchFilter, | ||
| NostrEvent, | ||
| } from "applesauce-core/helpers"; | ||
| import { IDBPDatabase, openDB } from "idb"; | ||
| import localforage from "localforage"; | ||
| import { | ||
| defaultMarmotClientConfig, | ||
| GroupHistoryFactory, | ||
| GroupRumorHistory, | ||
| GroupRumorHistoryBackend, | ||
| GroupStore, | ||
| KeyPackageStore, | ||
| } from "marmot-ts"; | ||
|
|
||
| const DB_VERSION = 1; | ||
|
|
||
| export interface StoredRumor { | ||
| groupId: string; | ||
| created_at: number; | ||
| id: string; | ||
| rumor: Rumor; | ||
| } | ||
|
|
||
| /** Schema for an indexeddb for a single account */ | ||
| export type RumorDatabaseSchema = { | ||
| rumors: { | ||
| value: StoredRumor; | ||
| key: [string, string]; // [groupId, rumorId] | ||
| indexes: { by_group_created_at: [string, number] }; // [groupId, created_at] | ||
| }; | ||
| }; | ||
|
|
||
| /** | ||
| * IndexedDB-backed implementation of {@link MarmotGroupHistoryStoreBackend}. | ||
| * Stores and retrieves group history (MIP-03 rumors) using the `idb` package. | ||
| * | ||
| * TODO: once this is stable this should be moved to the marmot-ts package | ||
| */ | ||
| class IdbRumorHistoryBackend implements GroupRumorHistoryBackend { | ||
| private groupKey: string; | ||
| private database: IDBPDatabase<RumorDatabaseSchema>; | ||
|
|
||
| constructor( | ||
| database: IDBPDatabase<RumorDatabaseSchema>, | ||
| groupId: Uint8Array, | ||
| ) { | ||
| this.database = database; | ||
| this.groupKey = bytesToHex(groupId); | ||
| } | ||
|
|
||
| /** Load rumors from the indexeddb database based on the given filter */ | ||
| async queryRumors(filter: Filter): Promise<Rumor[]> { | ||
| const { since, until, limit } = filter; | ||
|
|
||
| // Always use a bounded range so we only get this group's rumors (avoids | ||
| // lowerBound/upperBound including other groups when groupId hex is a prefix). | ||
| const range = IDBKeyRange.bound( | ||
| [this.groupKey, since ?? 0], | ||
| [this.groupKey, until ?? Number.MAX_SAFE_INTEGER], | ||
| false, | ||
| false, | ||
| ); | ||
|
|
||
| const tx = this.database.transaction("rumors", "readonly"); | ||
| const index = tx.objectStore("rumors").index("by_group_created_at"); | ||
| const cursor = await index.openCursor(range, "prev"); | ||
| const stored: StoredRumor[] = []; | ||
| let cur = cursor; | ||
| while (cur && (limit === undefined || stored.length < limit)) { | ||
| stored.push(cur.value); | ||
| cur = await cur.continue(); | ||
| } | ||
|
|
||
| return ( | ||
| stored | ||
| .map((s) => s.rumor) | ||
| // Filter down by extra nostr filters if provided | ||
| .filter((r) => matchFilter(filter, r as NostrEvent)) | ||
| ); | ||
| } | ||
|
|
||
| /** Saves a new rumor event to the database */ | ||
| async addRumor(rumor: Rumor): Promise<void> { | ||
| const entry: StoredRumor = { | ||
| groupId: this.groupKey, | ||
| created_at: rumor.created_at, | ||
| id: rumor.id, | ||
| rumor: rumor, | ||
| }; | ||
| await this.database.put("rumors", entry); | ||
| } | ||
|
|
||
| /** Clear all stored rumors for the group */ | ||
| async clear(): Promise<void> { | ||
| const range = IDBKeyRange.bound( | ||
| [this.groupKey, 0], | ||
| [this.groupKey, Number.MAX_SAFE_INTEGER], | ||
| false, | ||
| false, | ||
| ); | ||
|
|
||
| // Get all keys for the groups rumors | ||
| const keys = await this.database.getAllKeysFromIndex( | ||
| "rumors", | ||
| "by_group_created_at", | ||
| range, | ||
| ); | ||
|
|
||
| // Delete all keys | ||
| const tx = this.database.transaction("rumors", "readwrite"); | ||
| const store = tx.objectStore("rumors"); | ||
| for (const key of keys) { | ||
| await store.delete(key); | ||
| } | ||
| await tx.done; | ||
| } | ||
| } | ||
|
|
||
| type StorageInterfaces = { | ||
| groupStore: GroupStore; | ||
| historyFactory: GroupHistoryFactory<GroupRumorHistory>; | ||
| keyPackageStore: KeyPackageStore; | ||
| }; | ||
|
|
||
| /** A singleton class that manages databases for */ | ||
| export class MultiAccountDatabaseBroker { | ||
| private customDatabases: Map< | ||
| string, | ||
| | IDBPDatabase<RumorDatabaseSchema> | ||
| | Promise<IDBPDatabase<RumorDatabaseSchema>> | ||
| > = new Map(); | ||
|
|
||
| /** Get, open, or create a database for a given account */ | ||
| private async getCustomDatabaseForAccount( | ||
| pubkey: string, | ||
| ): Promise<IDBPDatabase<RumorDatabaseSchema>> { | ||
| const existing = this.customDatabases.get(pubkey); | ||
| if (existing) return existing; | ||
|
|
||
| // Create a new database for the account | ||
| const db = openDB<RumorDatabaseSchema>(pubkey, DB_VERSION, { | ||
| upgrade(db) { | ||
| const rumors = db.createObjectStore("rumors", { | ||
| keyPath: ["groupId", "id"], | ||
| }); | ||
| rumors.createIndex("by_group_created_at", ["groupId", "created_at"]); | ||
| }, | ||
| }).then((open) => { | ||
| this.customDatabases.set(pubkey, open); | ||
| return open; | ||
| }); | ||
|
|
||
| this.customDatabases.set(pubkey, db); | ||
| return db; | ||
| } | ||
|
|
||
| #storageInterfaces = new Map<string, StorageInterfaces>(); | ||
|
|
||
| /** Gets or creates a set of storage interfaces for a given account */ | ||
| async getStorageInterfacesForAccount(pubkey: string) { | ||
| const existing = this.#storageInterfaces.get(pubkey); | ||
| if (existing) return existing; | ||
|
|
||
| const groupStore = new GroupStore( | ||
| localforage.createInstance({ | ||
| name: `${pubkey}-key-value`, | ||
| storeName: "groups", | ||
| }), | ||
| // TODO: this should be provided by the MarmotClient somehow. | ||
| // this does not seem like something that should be passed to the generic group store interface | ||
| defaultMarmotClientConfig, | ||
| ); | ||
|
|
||
| const keyPackageStore = new KeyPackageStore( | ||
| localforage.createInstance({ | ||
| name: `${pubkey}-key-value`, | ||
| storeName: "keyPackages", | ||
| }), | ||
| ); | ||
|
|
||
| const rumorDatabase = await this.getCustomDatabaseForAccount(pubkey); | ||
| const historyFactory = (groupId: Uint8Array) => | ||
| new GroupRumorHistory(new IdbRumorHistoryBackend(rumorDatabase, groupId)); | ||
|
|
||
| const storageInterfaces: StorageInterfaces = { | ||
| groupStore, | ||
| keyPackageStore, | ||
| historyFactory, | ||
| }; | ||
|
|
||
| this.#storageInterfaces.set(pubkey, storageInterfaces); | ||
| return storageInterfaces; | ||
| } | ||
| } | ||
|
|
||
| // Create singleton instance of the database broker | ||
| const databaseBroker = new MultiAccountDatabaseBroker(); | ||
|
|
||
| export default databaseBroker; | ||
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🧩 Analysis chain
🏁 Script executed:
Repository: marmot-protocol/marmot-ts
Length of output: 1513
🏁 Script executed:
Repository: marmot-protocol/marmot-ts
Length of output: 690
🏁 Script executed:
rg "getCustomDatabaseForAccount" chat/src/lib/account-database.ts -B 2 -A 2Repository: marmot-protocol/marmot-ts
Length of output: 463
🏁 Script executed:
rg "this\.customDatabases" chat/src/lib/account-database.ts -B 1 -A 1Repository: marmot-protocol/marmot-ts
Length of output: 361
Clear failed DB promises to allow recovery on retry.
If
openDBfails, the rejected promise is cached incustomDatabases, so subsequent calls retrieve the same rejected promise and never recover. Remove the entry from the cache on failure and throw a descriptive error.Suggested update
private async getCustomDatabaseForAccount( pubkey: string, ): Promise<IDBPDatabase<RumorDatabaseSchema>> { const existing = this.customDatabases.get(pubkey); if (existing) return existing; // Create a new database for the account - const db = openDB<RumorDatabaseSchema>(pubkey, DB_VERSION, { - upgrade(db) { - const rumors = db.createObjectStore("rumors", { - keyPath: ["groupId", "id"], - }); - rumors.createIndex("by_group_created_at", ["groupId", "created_at"]); - }, - }).then((open) => { - this.customDatabases.set(pubkey, open); - return open; - }); - - this.customDatabases.set(pubkey, db); - return db; + const dbPromise = openDB<RumorDatabaseSchema>(pubkey, DB_VERSION, { + upgrade(db) { + const rumors = db.createObjectStore("rumors", { + keyPath: ["groupId", "id"], + }); + rumors.createIndex("by_group_created_at", ["groupId", "created_at"]); + }, + }); + + this.customDatabases.set(pubkey, dbPromise); + + try { + const db = await dbPromise; + this.customDatabases.set(pubkey, db); + return db; + } catch (error) { + this.customDatabases.delete(pubkey); + throw new Error( + `Failed to open rumor database for account ${pubkey}: ${String(error)}`, + ); + }🤖 Prompt for AI Agents