Fix missing contacts in DMs

This commit is contained in:
Bojan Mojsilovic 2024-04-10 14:35:55 +02:00
parent 30b5807248
commit e44339562a
2 changed files with 76 additions and 12 deletions

View File

@ -1,6 +1,7 @@
import { createStore, reconcile, unwrap } from "solid-js/store"; import { createStore, reconcile, unwrap } from "solid-js/store";
import { Kind, threadLenghtInMs } from "../constants"; import { Kind, threadLenghtInMs } from "../constants";
import { import {
batch,
createContext, createContext,
createEffect, createEffect,
onCleanup, onCleanup,
@ -36,14 +37,14 @@ import {
import { APP_ID } from "../App"; import { APP_ID } from "../App";
import { getMessageCounts, getNewMessages, getOldMessages, markAllAsRead, resetMessageCount, subscribeToMessagesStats, unsubscribeToMessagesStats } from "../lib/messages"; import { getMessageCounts, getNewMessages, getOldMessages, markAllAsRead, resetMessageCount, subscribeToMessagesStats, unsubscribeToMessagesStats } from "../lib/messages";
import { useAccountContext } from "./AccountContext"; import { useAccountContext } from "./AccountContext";
import { convertToUser } from "../stores/profile"; import { convertToUser, emptyUser } from "../stores/profile";
import { getUserProfiles } from "../lib/profile"; import { getUserProfiles } from "../lib/profile";
import { getEvents } from "../lib/feed"; import { getEvents } from "../lib/feed";
import { nip19 } from "nostr-tools"; import { nip19 } from "nostr-tools";
import { convertToNotes } from "../stores/note"; import { convertToNotes } from "../stores/note";
import { sanitize, sendEvent } from "../lib/notes"; import { sanitize, sendEvent } from "../lib/notes";
import { decrypt, encrypt } from "../lib/nostrAPI"; import { decrypt, encrypt } from "../lib/nostrAPI";
import { loadMsgContacts, saveMsgContacts } from "../lib/localStore"; import { loadDmCoversations, loadMsgContacts, saveDmConversations, saveMsgContacts } from "../lib/localStore";
import { useAppContext } from "./AppContext"; import { useAppContext } from "./AppContext";
@ -155,10 +156,39 @@ export const MessagesProvider = (props: { children: ContextChildren }) => {
updateStore('activePubkey', () => account.publicKey) updateStore('activePubkey', () => account.publicKey)
// @ts-ignore // @ts-ignore
const contacts = loadMsgContacts(account?.publicKey); // const contacts = loadMsgContacts(account?.publicKey);
updateStore('senders', reconcile({ ...contacts.profiles[store.senderRelation]})); const contacts = loadDmCoversations(account?.publicKey);
updateStore('messageCountPerSender', () => ({ ...contacts.counts }));
const ids = Object.keys(contacts.profiles);
let senders: Record<string, PrimalUser> = {};
let counts: Record<string, SenderMessageCount> = {};
const tests = {
follows: (id: string) => account?.following.includes(id),
other: (id: string) => !account?.following.includes(id),
any: () => true,
};
for (let i =0; i<ids.length; i++) {
const id = ids[i];
if (tests[store.senderRelation](id)) {
senders[id] = ({ ...contacts.profiles[id] });
counts[id] = ({ ...contacts.counts[id] });
}
}
batch(() => {
// @ts-ignore
updateStore('senders', () => undefined);
// @ts-ignore
updateStore('messageCountPerSender', () => undefined);
updateStore('senders', () => ({ ...senders}));
updateStore('messageCountPerSender', () => ({ ...counts }));
})
// @ts-ignore // @ts-ignore
getMessageCounts(account.publicKey, store.senderRelation, subidMsgCountPerSender); getMessageCounts(account.publicKey, store.senderRelation, subidMsgCountPerSender);
@ -574,7 +604,7 @@ export const MessagesProvider = (props: { children: ContextChildren }) => {
const orderedSenders = () => { const orderedSenders = () => {
const senders = store.senders; const senders: Record<string, PrimalUser> = store.senders;
if (!senders) { if (!senders) {
return []; return [];
@ -625,14 +655,19 @@ export const MessagesProvider = (props: { children: ContextChildren }) => {
if (content?.kind === Kind.MesagePerSenderStats) { if (content?.kind === Kind.MesagePerSenderStats) {
const senderCount = JSON.parse(content.content); const senderCount = JSON.parse(content.content);
const senders = Object.keys(senderCount).reduce((acc, pk) => {
return { ...acc, [pk]: emptyUser(pk)};
}, {});
updateStore('messageCountPerSender', () => ({ ...senderCount })); updateStore('messageCountPerSender', () => ({ ...senderCount }));
updateStore('senders', () => ({...senders}));
updateMessageTimings(); updateMessageTimings();
} }
if (content?.kind === Kind.Metadata) { if (content?.kind === Kind.Metadata) {
if (store.senders[content.pubkey]) { // if (store.senders[content.pubkey]) {
return; // return;
} // }
const isFollowing = account?.following.includes(content.pubkey); const isFollowing = account?.following.includes(content.pubkey);
@ -644,7 +679,7 @@ export const MessagesProvider = (props: { children: ContextChildren }) => {
const user = convertToUser(content); 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 keys = Object.keys(store.senders);
const cnt = keys.reduce((acc, k) => acc + (store.messageCountPerSender[k]?.cnt || 0) , 0); 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) { if (store.messageCount > cnt) {
updateStore('hasMessagesInDifferentTab', () => true); updateStore('hasMessagesInDifferentTab', () => true);

View File

@ -22,6 +22,10 @@ export type LocalStore = {
profiles: Record<UserRelation, Record<string, PrimalUser>>, profiles: Record<UserRelation, Record<string, PrimalUser>>,
counts: Record<string, SenderMessageCount>, counts: Record<string, SenderMessageCount>,
}, },
dmConversations: {
profiles: Record<string, PrimalUser>,
counts: Record<string, SenderMessageCount>,
},
emojiHistory: EmojiOption[], emojiHistory: EmojiOption[],
noteDraft: Record<string, string>, noteDraft: Record<string, string>,
noteDraftUserRefs: Record<string, Record<string, PrimalUser>>, noteDraftUserRefs: Record<string, Record<string, PrimalUser>>,
@ -55,6 +59,7 @@ export const emptyStorage: LocalStore = {
likes: [], likes: [],
feeds: [], feeds: [],
msgContacts: { profiles: { other: {}, follows: {}, any: {} }, counts: {} }, msgContacts: { profiles: { other: {}, follows: {}, any: {} }, counts: {} },
dmConversations: { profiles: {}, counts: {} },
theme: 'sunrise', theme: 'sunrise',
homeSidebarSelection: undefined, homeSidebarSelection: undefined,
userProfile: undefined, userProfile: undefined,
@ -400,7 +405,6 @@ export const saveMsgContacts = (pubkey: string | undefined, contacts: Record<str
setStorage(pubkey, store); setStorage(pubkey, store);
} }
export const loadMsgContacts = (pubkey: string) => { export const loadMsgContacts = (pubkey: string) => {
const store = getStorage(pubkey) const store = getStorage(pubkey)
@ -408,6 +412,30 @@ export const loadMsgContacts = (pubkey: string) => {
}; };
export const saveDmConversations = (pubkey: string | undefined, contacts: Record<string, PrimalUser>, counts: Record<string, SenderMessageCount>) => {
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) => { export const fetchStoredFeed = (pubkey: string | undefined) => {
if (!pubkey) return undefined; if (!pubkey) return undefined;