feat: sync public chats

This commit is contained in:
2023-11-22 16:11:58 +00:00
parent 6fd2741cc0
commit 5d9ca5ee39
2 changed files with 33 additions and 3 deletions

View File

@ -6,6 +6,7 @@ import { bech32ToHex, debounce, getNewest, getNewestEventTagsByKey, unwrap } fro
import useEventPublisher from "@/Hooks/useEventPublisher"; import useEventPublisher from "@/Hooks/useEventPublisher";
import useLogin from "@/Hooks/useLogin"; import useLogin from "@/Hooks/useLogin";
import { import {
LoginStore,
SnortAppData, SnortAppData,
addSubscription, addSubscription,
setAppData, setAppData,
@ -23,6 +24,7 @@ import { FollowLists, FollowsFeed, GiftsCache, Notifications, UserRelays } from
import { Nip28Chats, Nip4Chats } from "@/chat"; import { Nip28Chats, Nip4Chats } from "@/chat";
import { useRefreshFeedCache } from "@/Hooks/useRefreshFeedcache"; import { useRefreshFeedCache } from "@/Hooks/useRefreshFeedcache";
import { usePrevious } from "@uidotdev/usehooks"; import { usePrevious } from "@uidotdev/usehooks";
import { Nip28ChatSystem } from "@/chat/nip28";
/** /**
* Managed loading data for the current logged in user * Managed loading data for the current logged in user
@ -188,6 +190,16 @@ export default function useLoginFeed() {
} }
} }
function handlePublicChatsListFeed(bookmarkFeed: TaggedNostrEvent[]) {
const newest = getNewestEventTagsByKey(bookmarkFeed, "e");
if (newest) {
LoginStore.updateSession({
...login,
extraChats: newest.keys.map(Nip28ChatSystem.chatId),
});
}
}
useEffect(() => { useEffect(() => {
if (loginFeed.data) { if (loginFeed.data) {
const mutedFeed = loginFeed.data.filter(a => a.kind === EventKind.MuteList); const mutedFeed = loginFeed.data.filter(a => a.kind === EventKind.MuteList);
@ -201,6 +213,9 @@ export default function useLoginFeed() {
const bookmarkFeed = loginFeed.data.filter(a => a.kind === EventKind.BookmarksList); const bookmarkFeed = loginFeed.data.filter(a => a.kind === EventKind.BookmarksList);
handleBookmarkFeed(bookmarkFeed); handleBookmarkFeed(bookmarkFeed);
const publicChatsFeed = loginFeed.data.filter(a => a.kind === EventKind.PublicChatsList);
handlePublicChatsListFeed(publicChatsFeed);
} }
}, [loginFeed]); }, [loginFeed]);

View File

@ -3,7 +3,7 @@ import "./MessagesPage.css";
import React, { useEffect, useMemo, useState } from "react"; import React, { useEffect, useMemo, useState } from "react";
import { FormattedMessage, useIntl } from "react-intl"; import { FormattedMessage, useIntl } from "react-intl";
import { useNavigate, useParams } from "react-router-dom"; import { useNavigate, useParams } from "react-router-dom";
import { NostrLink, TLVEntryType, UserMetadata, decodeTLV } from "@snort/system"; import { EventKind, NostrLink, TLVEntryType, UserMetadata, decodeTLV } from "@snort/system";
import { useEventFeed, useUserProfile, useUserSearch } from "@snort/system-react"; import { useEventFeed, useUserProfile, useUserSearch } from "@snort/system-react";
import UnreadCount from "@/Element/UnreadCount"; import UnreadCount from "@/Element/UnreadCount";
@ -25,6 +25,7 @@ import { LoginSession, LoginStore } from "@/Login";
import { Nip28ChatSystem } from "@/chat/nip28"; import { Nip28ChatSystem } from "@/chat/nip28";
import { ChatParticipantProfile } from "@/Element/Chat/ChatParticipant"; import { ChatParticipantProfile } from "@/Element/Chat/ChatParticipant";
import classNames from "classnames"; import classNames from "classnames";
import useEventPublisher from "@/Hooks/useEventPublisher";
const TwoCol = 768; const TwoCol = 768;
const ThreeCol = 1500; const ThreeCol = 1500;
@ -182,6 +183,7 @@ function NewChatWindow() {
const navigate = useNavigate(); const navigate = useNavigate();
const search = useUserSearch(); const search = useUserSearch();
const login = useLogin(); const login = useLogin();
const { system, publisher } = useEventPublisher();
useEffect(() => { useEffect(() => {
setNewChat([]); setNewChat([]);
@ -270,12 +272,25 @@ function NewChatWindow() {
{results.length === 1 && ( {results.length === 1 && (
<Nip28ChatProfile <Nip28ChatProfile
id={results[0]} id={results[0]}
onClick={id => { onClick={async id => {
setShow(false); setShow(false);
const chats = appendDedupe(login.extraChats, [Nip28ChatSystem.chatId(id)]);
LoginStore.updateSession({ LoginStore.updateSession({
...login, ...login,
extraChats: appendDedupe(login.extraChats, [Nip28ChatSystem.chatId(id)]), extraChats: chats,
} as LoginSession); } as LoginSession);
const evList = await publisher?.generic(eb => {
eb.kind(EventKind.PublicChatsList);
chats.forEach(c => {
if (c.startsWith("chat281")) {
eb.tag(["e", decodeTLV(c)[0].value as string]);
}
});
return eb;
});
if (evList) {
await system.BroadcastEvent(evList);
}
navigate(createChatLink(ChatType.PublicGroupChat, id)); navigate(createChatLink(ChatType.PublicGroupChat, id));
}} }}
/> />