diff --git a/src/contexts/MessagesContext.tsx b/src/contexts/MessagesContext.tsx index 8dd78e3..bb74383 100644 --- a/src/contexts/MessagesContext.tsx +++ b/src/contexts/MessagesContext.tsx @@ -1,6 +1,7 @@ import { createStore, reconcile, unwrap } from "solid-js/store"; import { Kind, threadLenghtInMs } from "../constants"; import { + batch, createContext, createEffect, onCleanup, @@ -36,14 +37,14 @@ import { import { APP_ID } from "../App"; import { getMessageCounts, getNewMessages, getOldMessages, markAllAsRead, resetMessageCount, subscribeToMessagesStats, unsubscribeToMessagesStats } from "../lib/messages"; import { useAccountContext } from "./AccountContext"; -import { convertToUser } from "../stores/profile"; +import { convertToUser, emptyUser } from "../stores/profile"; import { getUserProfiles } from "../lib/profile"; import { getEvents } from "../lib/feed"; import { nip19 } from "nostr-tools"; import { convertToNotes } from "../stores/note"; import { sanitize, sendEvent } from "../lib/notes"; import { decrypt, encrypt } from "../lib/nostrAPI"; -import { loadMsgContacts, saveMsgContacts } from "../lib/localStore"; +import { loadDmCoversations, loadMsgContacts, saveDmConversations, saveMsgContacts } from "../lib/localStore"; import { useAppContext } from "./AppContext"; @@ -155,10 +156,39 @@ export const MessagesProvider = (props: { children: ContextChildren }) => { updateStore('activePubkey', () => account.publicKey) // @ts-ignore - const contacts = loadMsgContacts(account?.publicKey); + // const contacts = loadMsgContacts(account?.publicKey); - updateStore('senders', reconcile({ ...contacts.profiles[store.senderRelation]})); - updateStore('messageCountPerSender', () => ({ ...contacts.counts })); + const contacts = loadDmCoversations(account?.publicKey); + + const ids = Object.keys(contacts.profiles); + + let senders: Record = {}; + let counts: Record = {}; + + const tests = { + follows: (id: string) => account?.following.includes(id), + other: (id: string) => !account?.following.includes(id), + any: () => true, + }; + + for (let i =0; i { + // @ts-ignore + updateStore('senders', () => undefined); + // @ts-ignore + updateStore('messageCountPerSender', () => undefined); + + updateStore('senders', () => ({ ...senders})); + updateStore('messageCountPerSender', () => ({ ...counts })); + }) // @ts-ignore getMessageCounts(account.publicKey, store.senderRelation, subidMsgCountPerSender); @@ -574,7 +604,7 @@ export const MessagesProvider = (props: { children: ContextChildren }) => { const orderedSenders = () => { - const senders = store.senders; + const senders: Record = store.senders; if (!senders) { return []; @@ -625,14 +655,19 @@ export const MessagesProvider = (props: { children: ContextChildren }) => { if (content?.kind === Kind.MesagePerSenderStats) { const senderCount = JSON.parse(content.content); + const senders = Object.keys(senderCount).reduce((acc, pk) => { + return { ...acc, [pk]: emptyUser(pk)}; + }, {}); + updateStore('messageCountPerSender', () => ({ ...senderCount })); + updateStore('senders', () => ({...senders})); updateMessageTimings(); } if (content?.kind === Kind.Metadata) { - if (store.senders[content.pubkey]) { - return; - } + // if (store.senders[content.pubkey]) { + // return; + // } const isFollowing = account?.following.includes(content.pubkey); @@ -644,7 +679,7 @@ export const MessagesProvider = (props: { children: ContextChildren }) => { const user = convertToUser(content); - updateStore('senders', () => ({ [user.pubkey]: { ...user } })); + updateStore('senders', user.pubkey, () => ({ ...user })); } } @@ -652,7 +687,8 @@ export const MessagesProvider = (props: { children: ContextChildren }) => { const keys = Object.keys(store.senders); const cnt = keys.reduce((acc, k) => acc + (store.messageCountPerSender[k]?.cnt || 0) , 0); - saveMsgContacts(store.activePubkey, store.senders, store.messageCountPerSender, store.senderRelation); + // saveMsgContacts(store.activePubkey, store.senders, store.messageCountPerSender, store.senderRelation); + saveDmConversations(store.activePubkey, store.senders, store.messageCountPerSender); if (store.messageCount > cnt) { updateStore('hasMessagesInDifferentTab', () => true); diff --git a/src/lib/localStore.ts b/src/lib/localStore.ts index 523947e..9864f97 100644 --- a/src/lib/localStore.ts +++ b/src/lib/localStore.ts @@ -22,6 +22,10 @@ export type LocalStore = { profiles: Record>, counts: Record, }, + dmConversations: { + profiles: Record, + counts: Record, + }, emojiHistory: EmojiOption[], noteDraft: Record, noteDraftUserRefs: Record>, @@ -55,6 +59,7 @@ export const emptyStorage: LocalStore = { likes: [], feeds: [], msgContacts: { profiles: { other: {}, follows: {}, any: {} }, counts: {} }, + dmConversations: { profiles: {}, counts: {} }, theme: 'sunrise', homeSidebarSelection: undefined, userProfile: undefined, @@ -400,7 +405,6 @@ export const saveMsgContacts = (pubkey: string | undefined, contacts: Record { const store = getStorage(pubkey) @@ -408,6 +412,30 @@ export const loadMsgContacts = (pubkey: string) => { }; +export const saveDmConversations = (pubkey: string | undefined, contacts: Record, counts: Record) => { + if (!pubkey) { + return; + } + + const store = getStorage(pubkey); + + if (!store.dmConversations) { + store.dmConversations = { profiles: {}, counts: {} }; + } + + store.dmConversations.profiles = { ...contacts }; + store.dmConversations.counts = { ...counts }; + + setStorage(pubkey, store); +} + +export const loadDmCoversations = (pubkey: string) => { + const store = getStorage(pubkey) + + return store.dmConversations || { profiles: {}, counts: {} }; +}; + + export const fetchStoredFeed = (pubkey: string | undefined) => { if (!pubkey) return undefined;