Merge pull request #86 from 13x-tech/fix/allow-self-dms

filter for self dms
This commit is contained in:
Kieran 2023-01-19 10:07:51 +00:00 committed by GitHub
commit 3fb1350e39
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 7 additions and 2 deletions

View File

@ -10,7 +10,7 @@ import useEventPublisher from "../feed/EventPublisher";
import DM from "../element/DM"; import DM from "../element/DM";
import { RawEvent } from "../nostr"; import { RawEvent } from "../nostr";
import { dmsInChat } from "./MessagesPage"; import { dmsInChat, isToSelf } from "./MessagesPage";
type RouterParams = { type RouterParams = {
id: string id: string
@ -20,13 +20,14 @@ export default function ChatPage() {
const params = useParams<RouterParams>(); const params = useParams<RouterParams>();
const publisher = useEventPublisher(); const publisher = useEventPublisher();
const id = bech32ToHex(params.id ?? ""); const id = bech32ToHex(params.id ?? "");
const pubKey = useSelector<any>(s => s.login.publicKey);
const dms = useSelector<any, RawEvent[]>(s => filterDms(s.login.dms)); const dms = useSelector<any, RawEvent[]>(s => filterDms(s.login.dms));
const [content, setContent] = useState<string>(); const [content, setContent] = useState<string>();
const { ref, inView, entry } = useInView(); const { ref, inView, entry } = useInView();
const dmListRef = useRef<HTMLDivElement>(null); const dmListRef = useRef<HTMLDivElement>(null);
function filterDms(dms: RawEvent[]) { function filterDms(dms: RawEvent[]) {
return dmsInChat(dms, id); return dmsInChat(id === pubKey ? dms.filter(d => isToSelf(d, pubKey)) : dms, id);
} }
const sortedDms = useMemo<any[]>(() => { const sortedDms = useMemo<any[]>(() => {

View File

@ -92,6 +92,10 @@ function unreadDms(dms: RawEvent[], myPubKey: HexKey, pk: HexKey) {
} }
function newestMessage(dms: RawEvent[], myPubKey: HexKey, pk: HexKey) { function newestMessage(dms: RawEvent[], myPubKey: HexKey, pk: HexKey) {
if(pk === myPubKey) {
return dmsInChat(dms.filter(d => isToSelf(d, myPubKey)), pk).reduce((acc, v) => acc = v.created_at > acc ? v.created_at : acc, 0);
}
return dmsInChat(dms, pk).reduce((acc, v) => acc = v.created_at > acc ? v.created_at : acc, 0); return dmsInChat(dms, pk).reduce((acc, v) => acc = v.created_at > acc ? v.created_at : acc, 0);
} }