mirror of
https://github.com/layer-systems/website.git
synced 2026-09-12 13:43:01 +02:00
Merge remote-tracking branch 'origin/main' into feat/notes-local-draft
# Conflicts: # src/apps/feed/index.tsx Co-authored-by: mroxso <24775431+mroxso@users.noreply.github.com>
This commit is contained in:
@@ -12,6 +12,7 @@ import { useCurrentUser } from '@/hooks/useCurrentUser';
|
||||
import { useMyFollows } from '@/hooks/useFollows';
|
||||
import { cn } from '@/lib/utils';
|
||||
import { useWindowManager } from '@/os/useWindowManager';
|
||||
import { isReply } from '@/lib/nostrUtils';
|
||||
import type { AppProps } from '@/os/types';
|
||||
|
||||
type Scope = 'following' | 'global';
|
||||
@@ -21,9 +22,17 @@ const PAGE_SIZE = 50;
|
||||
/**
|
||||
* A kind 1 event is only worth rendering if it has something to render. Relays
|
||||
* happily return blanks and oddities, so the feed validates before it draws.
|
||||
* Replies (NIP-10 `e` tags) are excluded too: without their parent for
|
||||
* context they read as indistinguishable, orphaned root posts — open the
|
||||
* thread from the Note app instead.
|
||||
*/
|
||||
function isRenderableNote(event: NostrEvent): boolean {
|
||||
return event.kind === 1 && typeof event.content === 'string' && event.content.trim().length > 0;
|
||||
return (
|
||||
event.kind === 1 &&
|
||||
typeof event.content === 'string' &&
|
||||
event.content.trim().length > 0 &&
|
||||
!isReply(event)
|
||||
);
|
||||
}
|
||||
|
||||
function useFeed(scope: Scope, authors: string[] | undefined) {
|
||||
|
||||
@@ -14,9 +14,14 @@ import { useCurrentUser } from '@/hooks/useCurrentUser';
|
||||
import { useMyFollows } from '@/hooks/useFollows';
|
||||
import { useNostrPublish } from '@/hooks/useNostrPublish';
|
||||
import { useToast } from '@/hooks/useToast';
|
||||
import { decodeRelayHints, displayName, npubOf, sanitizeUrl } from '@/lib/nostrUtils';
|
||||
import { decodeRelayHints, displayName, isReply, npubOf, sanitizeUrl } from '@/lib/nostrUtils';
|
||||
import type { AppProps } from '@/os/types';
|
||||
|
||||
/**
|
||||
* Replies are excluded here for the same reason as the Feed: without their
|
||||
* parent for context, a reply on a profile's timeline reads as an orphaned
|
||||
* root post rather than what it is.
|
||||
*/
|
||||
function useAuthorNotes(pubkey: string | undefined, relays: string[] | undefined) {
|
||||
const { nostr } = useNostr();
|
||||
|
||||
@@ -29,7 +34,7 @@ function useAuthorNotes(pubkey: string | undefined, relays: string[] | undefined
|
||||
{ signal: AbortSignal.any([signal, AbortSignal.timeout(6000)]), relays },
|
||||
);
|
||||
return events
|
||||
.filter((event) => event.content.trim().length > 0)
|
||||
.filter((event) => event.content.trim().length > 0 && !isReply(event))
|
||||
.sort((a, b) => b.created_at - a.created_at);
|
||||
},
|
||||
staleTime: 60_000,
|
||||
|
||||
78
src/lib/nostrUtils.test.ts
Normal file
78
src/lib/nostrUtils.test.ts
Normal file
@@ -0,0 +1,78 @@
|
||||
import { describe, expect, it } from 'vitest';
|
||||
import type { NostrEvent } from '@nostrify/nostrify';
|
||||
import { isReply, rootReference } from './nostrUtils';
|
||||
|
||||
function note(tags: string[][]): NostrEvent {
|
||||
return {
|
||||
id: 'x',
|
||||
pubkey: 'y',
|
||||
created_at: 0,
|
||||
kind: 1,
|
||||
tags,
|
||||
content: 'hello',
|
||||
sig: '',
|
||||
};
|
||||
}
|
||||
|
||||
describe('isReply', () => {
|
||||
it('is false for a root note with no e tag', () => {
|
||||
expect(isReply(note([]))).toBe(false);
|
||||
});
|
||||
|
||||
it('is true for a note with a marked root e tag', () => {
|
||||
expect(isReply(note([['e', 'root-id', '', 'root']]))).toBe(true);
|
||||
});
|
||||
|
||||
it('is true for a note using the deprecated positional e tag', () => {
|
||||
expect(isReply(note([['e', 'parent-id']]))).toBe(true);
|
||||
});
|
||||
|
||||
it('is false for a note that only mentions another event', () => {
|
||||
expect(isReply(note([['e', 'mentioned-id', '', 'mention']]))).toBe(false);
|
||||
});
|
||||
|
||||
it('is true for a note with a mention alongside a marked reply', () => {
|
||||
expect(
|
||||
isReply(
|
||||
note([
|
||||
['e', 'root-id', '', 'root'],
|
||||
['e', 'mentioned-id', '', 'mention'],
|
||||
]),
|
||||
),
|
||||
).toBe(true);
|
||||
});
|
||||
});
|
||||
|
||||
describe('rootReference', () => {
|
||||
it('prefers the marked root tag over positional ones', () => {
|
||||
const event = note([
|
||||
['e', 'mention-id', '', 'mention'],
|
||||
['e', 'root-id', '', 'root'],
|
||||
]);
|
||||
expect(rootReference(event)).toBe('root-id');
|
||||
});
|
||||
|
||||
it('falls back to the first positional e tag per the deprecated scheme', () => {
|
||||
const event = note([
|
||||
['e', 'root-id'],
|
||||
['e', 'reply-id'],
|
||||
]);
|
||||
expect(rootReference(event)).toBe('root-id');
|
||||
});
|
||||
|
||||
it('is undefined for a root note', () => {
|
||||
expect(rootReference(note([]))).toBeUndefined();
|
||||
});
|
||||
|
||||
it('is undefined for a note that only mentions another event', () => {
|
||||
expect(rootReference(note([['e', 'mentioned-id', '', 'mention']]))).toBeUndefined();
|
||||
});
|
||||
|
||||
it('ignores a mention when falling back to the positional scheme', () => {
|
||||
const event = note([
|
||||
['e', 'mentioned-id', '', 'mention'],
|
||||
['e', 'root-id'],
|
||||
]);
|
||||
expect(rootReference(event)).toBe('root-id');
|
||||
});
|
||||
});
|
||||
@@ -93,15 +93,25 @@ export function tagValues(event: NostrEvent, name: string): string[] {
|
||||
|
||||
/**
|
||||
* The event a reply points at, following NIP-10: prefer an explicit `root`
|
||||
* marker, fall back to the last positional `e` tag.
|
||||
* marker, fall back to the first *unmarked* `e` tag (the deprecated scheme
|
||||
* puts the root id first: `["e", <root-id>], ["e", <reply-id>]`). A `mention`
|
||||
* or `reply`-only marker is never treated as the root — a mention isn't
|
||||
* part of the thread, and a lone `reply` marker without `root` is malformed
|
||||
* per NIP-10 rather than an implicit root.
|
||||
*/
|
||||
export function rootReference(event: NostrEvent): string | undefined {
|
||||
const marked = event.tags.find(([name, , , marker]) => name === 'e' && marker === 'root');
|
||||
if (marked) return marked[1];
|
||||
const positional = event.tags.filter(([name]) => name === 'e');
|
||||
const positional = event.tags.filter(([name, , , marker]) => name === 'e' && !marker);
|
||||
return positional[0]?.[1];
|
||||
}
|
||||
|
||||
/**
|
||||
* True for a reply per NIP-10: a marked `root`/`reply` `e` tag, or an
|
||||
* unmarked one (the deprecated positional scheme). An `e` tag marked
|
||||
* `mention` alone does not make an event a reply — it cites another event
|
||||
* without being part of its thread.
|
||||
*/
|
||||
export function isReply(event: NostrEvent): boolean {
|
||||
return event.tags.some(([name]) => name === 'e');
|
||||
return event.tags.some(([name, , , marker]) => name === 'e' && marker !== 'mention');
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user