feat: sync public chats
Some checks failed
continuous-integration/drone/push Build is failing

This commit is contained in:
Kieran 2023-11-22 16:11:58 +00:00
parent 6fd2741cc0
commit 5d9ca5ee39
Signed by: Kieran
GPG Key ID: DE71CEB3925BE941
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 useLogin from "@/Hooks/useLogin";
import {
LoginStore,
SnortAppData,
addSubscription,
setAppData,
@ -23,6 +24,7 @@ import { FollowLists, FollowsFeed, GiftsCache, Notifications, UserRelays } from
import { Nip28Chats, Nip4Chats } from "@/chat";
import { useRefreshFeedCache } from "@/Hooks/useRefreshFeedcache";
import { usePrevious } from "@uidotdev/usehooks";
import { Nip28ChatSystem } from "@/chat/nip28";
/**
* 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(() => {
if (loginFeed.data) {
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);
handleBookmarkFeed(bookmarkFeed);
const publicChatsFeed = loginFeed.data.filter(a => a.kind === EventKind.PublicChatsList);
handlePublicChatsListFeed(publicChatsFeed);
}
}, [loginFeed]);

View File

@ -3,7 +3,7 @@ import "./MessagesPage.css";
import React, { useEffect, useMemo, useState } from "react";
import { FormattedMessage, useIntl } from "react-intl";
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 UnreadCount from "@/Element/UnreadCount";
@ -25,6 +25,7 @@ import { LoginSession, LoginStore } from "@/Login";
import { Nip28ChatSystem } from "@/chat/nip28";
import { ChatParticipantProfile } from "@/Element/Chat/ChatParticipant";
import classNames from "classnames";
import useEventPublisher from "@/Hooks/useEventPublisher";
const TwoCol = 768;
const ThreeCol = 1500;
@ -182,6 +183,7 @@ function NewChatWindow() {
const navigate = useNavigate();
const search = useUserSearch();
const login = useLogin();
const { system, publisher } = useEventPublisher();
useEffect(() => {
setNewChat([]);
@ -270,12 +272,25 @@ function NewChatWindow() {
{results.length === 1 && (
<Nip28ChatProfile
id={results[0]}
onClick={id => {
onClick={async id => {
setShow(false);
const chats = appendDedupe(login.extraChats, [Nip28ChatSystem.chatId(id)]);
LoginStore.updateSession({
...login,
extraChats: appendDedupe(login.extraChats, [Nip28ChatSystem.chatId(id)]),
extraChats: chats,
} 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));
}}
/>