mirror of
https://github.com/irislib/iris-messenger.git
synced 2024-10-18 14:13:21 +00:00
save group latest msgs
This commit is contained in:
parent
0162c675c7
commit
3f6d444755
@ -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) => {
|
||||
setChats((prevChats) => {
|
||||
prevChats.set(key, { ...value });
|
||||
return prevChats;
|
||||
});
|
||||
setRenderCount((prevCount) => prevCount + 1);
|
||||
}),
|
||||
);
|
||||
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(() => {
|
||||
|
@ -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) => {
|
||||
|
@ -245,22 +245,30 @@ function ChatMessages({ id }) {
|
||||
});
|
||||
|
||||
const subscribeGroup = () => {
|
||||
localState
|
||||
.get('groups')
|
||||
.get(id)
|
||||
.on((group) => {
|
||||
const privKey = group.key;
|
||||
const pubKey = getPublicKey(privKey);
|
||||
setKeyPair({ privKey, pubKey });
|
||||
unsub = PubSub.subscribe(
|
||||
{ kinds: [4], '#p': [pubKey], authors: [pubKey] },
|
||||
async (event) => {
|
||||
const decrypted = await nip04.decrypt(privKey, pubKey, event.content);
|
||||
messagesById.set(event.id, { ...event, text: decrypted });
|
||||
setSortedMessages(Array.from(messagesById.values()));
|
||||
},
|
||||
);
|
||||
});
|
||||
const node = localState.get('groups').get(id);
|
||||
node.on((group) => {
|
||||
const privKey = group.key;
|
||||
const pubKey = getPublicKey(privKey);
|
||||
setKeyPair({ privKey, pubKey });
|
||||
unsub = PubSub.subscribe(
|
||||
{ kinds: [4], '#p': [pubKey], authors: [pubKey] },
|
||||
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()));
|
||||
},
|
||||
);
|
||||
});
|
||||
};
|
||||
|
||||
if (hexId) {
|
||||
|
Loading…
Reference in New Issue
Block a user