save group latest msgs

This commit is contained in:
Martti Malmi 2023-08-03 18:56:19 +03:00
parent 0162c675c7
commit 3f6d444755
3 changed files with 36 additions and 36 deletions

View File

@ -36,25 +36,17 @@ const ChatList = ({ activeChat, className }) => {
useEffect(() => {
const unsubs = [] as any[];
unsubs.push(
localState.get('chats').map((value, key) => {
setChats((prevChats) => {
prevChats.set(key, { ...value });
return prevChats;
});
setRenderCount((prevCount) => prevCount + 1);
}),
);
unsubs.push(
localState.get('groups').map((value, key) => {
const addToChats = (value, key) => {
setChats((prevChats) => {
prevChats.set(key, { ...value });
return prevChats;
});
setRenderCount((prevCount) => prevCount + 1);
}),
);
};
unsubs.push(localState.get('chats').map(addToChats));
unsubs.push(localState.get('groups').map(addToChats));
unsubs.push(
localState.get('scrollUp').on(() => {

View File

@ -12,11 +12,11 @@ import Key from '../../nostr/Key';
import { translate as t } from '../../translations/Translation.mjs';
const ChatListItem = ({ chat, active = false, latestMsg = {} as any }) => {
const [isGroup] = useState(chat.length < 20);
const [name, setName] = useState(null);
const [latestText, setLatestText] = useState(latestMsg.text || '');
useEffect(() => {
const isGroup = chat.length < 20;
if (isGroup) {
localState
.get('groups')
@ -30,7 +30,7 @@ const ChatListItem = ({ chat, active = false, latestMsg = {} as any }) => {
}, [chat]);
useEffect(() => {
if (latestMsg.text || !latestMsg.id) {
if (isGroup || latestMsg.text || !latestMsg.id) {
return;
}
Events.getEventById(latestMsg.id, false, (event) => {

View File

@ -245,10 +245,8 @@ function ChatMessages({ id }) {
});
const subscribeGroup = () => {
localState
.get('groups')
.get(id)
.on((group) => {
const node = localState.get('groups').get(id);
node.on((group) => {
const privKey = group.key;
const pubKey = getPublicKey(privKey);
setKeyPair({ privKey, pubKey });
@ -257,6 +255,16 @@ function ChatMessages({ id }) {
async (event) => {
const decrypted = await nip04.decrypt(privKey, pubKey, event.content);
messagesById.set(event.id, { ...event, text: decrypted });
const latest = node.get('latest');
latest.once((e) => {
if (!e || !e.created_at || e.created_at < event.created_at) {
latest.put({
id: event.id,
created_at: event.created_at,
text: decrypted.slice(0, decrypted.indexOf('{')),
});
}
});
setSortedMessages(Array.from(messagesById.values()));
},
);