32 changed files with 991 additions and 225 deletions
@ -0,0 +1,298 @@
@@ -0,0 +1,298 @@
|
||||
import NoteCard from '@/components/NoteCard' |
||||
import { Skeleton } from '@/components/ui/skeleton' |
||||
import { ExtendedKind } from '@/constants' |
||||
import { useDeletedEvent } from '@/providers/DeletedEventProvider' |
||||
import client from '@/services/client.service' |
||||
import indexedDb from '@/services/indexed-db.service' |
||||
import { generateBech32IdFromATag, getFirstHexEventIdFromETags } from '@/lib/tag' |
||||
import { relayHintsFromEventTags } from '@/lib/relay-list-builder' |
||||
import { hexPubkeysEqual, normalizeHexPubkey } from '@/lib/pubkey' |
||||
import { RefreshCw } from 'lucide-react' |
||||
import { kinds, type Event } from 'nostr-tools' |
||||
import { |
||||
forwardRef, |
||||
useEffect, |
||||
useImperativeHandle, |
||||
useMemo, |
||||
useRef, |
||||
useState |
||||
} from 'react' |
||||
import { useTranslation } from 'react-i18next' |
||||
import { useProfileTimeline } from '@/hooks/useProfileTimeline' |
||||
|
||||
const INITIAL_SHOW_COUNT = 25 |
||||
const LOAD_MORE_COUNT = 25 |
||||
const LIKED_REACTION_KINDS = [kinds.Reaction, ExtendedKind.EXTERNAL_REACTION] |
||||
|
||||
type ReactionTargetRef = { |
||||
key: string |
||||
fetchId: string |
||||
hexId?: string |
||||
relayHints: string[] |
||||
} |
||||
|
||||
type LikedTarget = { |
||||
reaction: Event |
||||
target: Event |
||||
} |
||||
|
||||
function isPositiveReaction(event: Event): boolean { |
||||
return event.content.trim() !== '-' |
||||
} |
||||
|
||||
function relayHintsForReactionTarget(event: Event, tag?: string[]): string[] { |
||||
const hints = new Set(relayHintsFromEventTags(event)) |
||||
const tagHint = tag?.[2]?.trim() |
||||
if (tagHint) hints.add(tagHint) |
||||
return [...hints] |
||||
} |
||||
|
||||
function reactionTargetRef(event: Event): ReactionTargetRef | null { |
||||
const hexId = getFirstHexEventIdFromETags(event.tags) |
||||
if (hexId) { |
||||
return { |
||||
key: `e:${hexId.toLowerCase()}`, |
||||
fetchId: hexId, |
||||
hexId, |
||||
relayHints: relayHintsForReactionTarget(event) |
||||
} |
||||
} |
||||
|
||||
const addressTag = event.tags.find((tag) => (tag[0] === 'a' || tag[0] === 'A') && tag[1]) |
||||
if (!addressTag) return null |
||||
const bech32Id = generateBech32IdFromATag(addressTag) |
||||
if (!bech32Id) return null |
||||
return { |
||||
key: `a:${addressTag[1]}`, |
||||
fetchId: bech32Id, |
||||
relayHints: relayHintsForReactionTarget(event, addressTag) |
||||
} |
||||
} |
||||
|
||||
function samePubkey(a: string, b: string): boolean { |
||||
try { |
||||
return hexPubkeysEqual(normalizeHexPubkey(a), normalizeHexPubkey(b)) |
||||
} catch { |
||||
return a === b |
||||
} |
||||
} |
||||
|
||||
function newestReactionTargets(reactions: Event[]): Array<{ reaction: Event; targetRef: ReactionTargetRef }> { |
||||
const byTarget = new Map<string, { reaction: Event; targetRef: ReactionTargetRef }>() |
||||
for (const reaction of reactions) { |
||||
if (!isPositiveReaction(reaction)) continue |
||||
const targetRef = reactionTargetRef(reaction) |
||||
if (!targetRef) continue |
||||
const existing = byTarget.get(targetRef.key) |
||||
if ( |
||||
!existing || |
||||
reaction.created_at > existing.reaction.created_at || |
||||
(reaction.created_at === existing.reaction.created_at && reaction.id > existing.reaction.id) |
||||
) { |
||||
byTarget.set(targetRef.key, { reaction, targetRef }) |
||||
} |
||||
} |
||||
return [...byTarget.values()].sort((a, b) => b.reaction.created_at - a.reaction.created_at) |
||||
} |
||||
|
||||
const ProfileLikedFeed = forwardRef<{ refresh: () => void }, { pubkey: string }>(({ pubkey }, ref) => { |
||||
const { t } = useTranslation() |
||||
const { isEventDeleted } = useDeletedEvent() |
||||
const [isRefreshing, setIsRefreshing] = useState(false) |
||||
const [isResolvingTargets, setIsResolvingTargets] = useState(false) |
||||
const [likedTargets, setLikedTargets] = useState<LikedTarget[]>([]) |
||||
const [showCount, setShowCount] = useState(INITIAL_SHOW_COUNT) |
||||
const bottomRef = useRef<HTMLDivElement>(null) |
||||
|
||||
const reactionKinds = useMemo(() => [...LIKED_REACTION_KINDS], []) |
||||
const cacheKey = useMemo(() => `${pubkey}-profile-liked-v1`, [pubkey]) |
||||
const { events: reactionEvents, isLoading, refresh } = useProfileTimeline({ |
||||
pubkey, |
||||
cacheKey, |
||||
kinds: reactionKinds, |
||||
limit: 200 |
||||
}) |
||||
|
||||
const targetRefs = useMemo(() => newestReactionTargets(reactionEvents), [reactionEvents]) |
||||
|
||||
useEffect(() => { |
||||
setShowCount(INITIAL_SHOW_COUNT) |
||||
}, [pubkey]) |
||||
|
||||
useEffect(() => { |
||||
if (!isLoading && !isResolvingTargets) { |
||||
setIsRefreshing(false) |
||||
} |
||||
}, [isLoading, isResolvingTargets]) |
||||
|
||||
useImperativeHandle( |
||||
ref, |
||||
() => ({ |
||||
refresh: () => { |
||||
setIsRefreshing(true) |
||||
refresh() |
||||
} |
||||
}), |
||||
[refresh] |
||||
) |
||||
|
||||
useEffect(() => { |
||||
let cancelled = false |
||||
const viewerPubkey = pubkey |
||||
const toLikedTarget = (row: { reaction: Event; target: Event }): LikedTarget | null => { |
||||
if (samePubkey(row.target.pubkey, viewerPubkey)) return null |
||||
if (isEventDeleted(row.target)) return null |
||||
return row |
||||
} |
||||
|
||||
const cachedRows = targetRefs |
||||
.map(({ reaction, targetRef }) => { |
||||
const cached = targetRef.hexId ? client.peekSessionCachedEvent(targetRef.hexId) : undefined |
||||
return cached ? toLikedTarget({ reaction, target: cached }) : null |
||||
}) |
||||
.filter((row): row is LikedTarget => !!row) |
||||
|
||||
setLikedTargets(cachedRows) |
||||
if (targetRefs.length === 0) { |
||||
setIsResolvingTargets(false) |
||||
return () => { |
||||
cancelled = true |
||||
} |
||||
} |
||||
|
||||
setIsResolvingTargets(true) |
||||
void (async () => { |
||||
try { |
||||
const hexIds = targetRefs.map(({ targetRef }) => targetRef.hexId).filter((id): id is string => !!id) |
||||
if (hexIds.length > 0) { |
||||
const [archived, publications] = await Promise.all([ |
||||
indexedDb.getArchivedEventsByIds(hexIds), |
||||
Promise.all(hexIds.map((id) => indexedDb.getEventFromPublicationStore(id))) |
||||
]) |
||||
if (cancelled) return |
||||
const localById = new Map<string, Event>() |
||||
for (const event of archived) localById.set(event.id, event) |
||||
for (const event of publications) { |
||||
if (event) localById.set(event.id, event) |
||||
} |
||||
const localResolved = targetRefs |
||||
.map(({ reaction, targetRef }) => { |
||||
const target = targetRef.hexId ? localById.get(targetRef.hexId) : undefined |
||||
return target ? toLikedTarget({ reaction, target }) : null |
||||
}) |
||||
.filter((row): row is LikedTarget => !!row) |
||||
if (localResolved.length > 0) { |
||||
setLikedTargets((prev) => { |
||||
const byTargetId = new Map(prev.map((row) => [row.target.id, row])) |
||||
for (const row of localResolved) byTargetId.set(row.target.id, row) |
||||
return [...byTargetId.values()].sort((a, b) => b.reaction.created_at - a.reaction.created_at) |
||||
}) |
||||
} |
||||
} |
||||
|
||||
const missingHexIds = targetRefs |
||||
.map(({ targetRef }) => targetRef.hexId) |
||||
.filter((id): id is string => !!id && !client.peekSessionCachedEvent(id)) |
||||
if (missingHexIds.length > 0) { |
||||
await client.prefetchHexEventIds(missingHexIds) |
||||
} |
||||
|
||||
const resolved = await Promise.all( |
||||
targetRefs.map(async ({ reaction, targetRef }) => { |
||||
const target = |
||||
(targetRef.hexId ? client.peekSessionCachedEvent(targetRef.hexId) : undefined) ?? |
||||
await client.fetchEvent(targetRef.fetchId, { relayHints: targetRef.relayHints }) |
||||
if (!target) return null |
||||
return toLikedTarget({ reaction, target }) |
||||
}) |
||||
) |
||||
if (cancelled) return |
||||
const byTargetId = new Map<string, LikedTarget>() |
||||
for (const row of resolved) { |
||||
if (!row) continue |
||||
const existing = byTargetId.get(row.target.id) |
||||
if ( |
||||
!existing || |
||||
row.reaction.created_at > existing.reaction.created_at || |
||||
(row.reaction.created_at === existing.reaction.created_at && row.reaction.id > existing.reaction.id) |
||||
) { |
||||
byTargetId.set(row.target.id, row) |
||||
} |
||||
} |
||||
setLikedTargets([...byTargetId.values()].sort((a, b) => b.reaction.created_at - a.reaction.created_at)) |
||||
} finally { |
||||
if (!cancelled) setIsResolvingTargets(false) |
||||
} |
||||
})() |
||||
|
||||
return () => { |
||||
cancelled = true |
||||
} |
||||
}, [targetRefs, pubkey, isEventDeleted]) |
||||
|
||||
const displayedTargets = useMemo( |
||||
() => likedTargets.slice(0, showCount), |
||||
[likedTargets, showCount] |
||||
) |
||||
|
||||
useEffect(() => { |
||||
if (!bottomRef.current || displayedTargets.length >= likedTargets.length) return |
||||
const observer = new IntersectionObserver( |
||||
(entries) => { |
||||
if (entries[0].isIntersecting && displayedTargets.length < likedTargets.length) { |
||||
setShowCount((prev) => Math.min(prev + LOAD_MORE_COUNT, likedTargets.length)) |
||||
} |
||||
}, |
||||
{ threshold: 0.1 } |
||||
) |
||||
observer.observe(bottomRef.current) |
||||
return () => observer.disconnect() |
||||
}, [displayedTargets.length, likedTargets.length]) |
||||
|
||||
if ((isLoading || isResolvingTargets) && likedTargets.length === 0) { |
||||
return ( |
||||
<div className="mt-4 space-y-2"> |
||||
{Array.from({ length: 3 }).map((_, i) => ( |
||||
<Skeleton key={i} className="h-32 w-full" /> |
||||
))} |
||||
</div> |
||||
) |
||||
} |
||||
|
||||
if (likedTargets.length === 0) { |
||||
return ( |
||||
<div className="px-4 py-8 text-center text-sm text-muted-foreground"> |
||||
{t('No liked posts yet')} |
||||
</div> |
||||
) |
||||
} |
||||
|
||||
return ( |
||||
<div className="mt-4 min-w-0"> |
||||
{isRefreshing && ( |
||||
<div |
||||
className="flex items-center justify-center gap-2 px-4 py-2 text-center text-sm text-green-500" |
||||
role="status" |
||||
aria-live="polite" |
||||
> |
||||
<RefreshCw className="h-4 w-4 shrink-0 animate-spin" aria-hidden /> |
||||
{t('Refreshing liked posts...')} |
||||
</div> |
||||
)} |
||||
<div className="space-y-2"> |
||||
{displayedTargets.map(({ target }) => ( |
||||
<NoteCard key={target.id} className="w-full" event={target} filterMutedNotes={false} bottomNoteLabel={t('Liked by you')} /> |
||||
))} |
||||
</div> |
||||
{displayedTargets.length < likedTargets.length && ( |
||||
<div ref={bottomRef} className="h-10 flex items-center justify-center"> |
||||
<div className="text-sm text-muted-foreground">{t('Loading more...')}</div> |
||||
</div> |
||||
)} |
||||
</div> |
||||
) |
||||
}) |
||||
|
||||
ProfileLikedFeed.displayName = 'ProfileLikedFeed' |
||||
|
||||
export default ProfileLikedFeed |
||||
@ -0,0 +1,44 @@
@@ -0,0 +1,44 @@
|
||||
import { describe, expect, it } from 'vitest' |
||||
import type { Event } from 'nostr-tools' |
||||
import { eventMatchesLocalFeedFilter } from './feed-local-event-match' |
||||
|
||||
function event(overrides: Partial<Event> = {}): Event { |
||||
return { |
||||
id: 'a'.repeat(64), |
||||
pubkey: 'b'.repeat(64), |
||||
created_at: 1000, |
||||
kind: 1, |
||||
tags: [ |
||||
['p', 'c'.repeat(64)], |
||||
['t', 'Nostr'], |
||||
['d', 'article-1'] |
||||
], |
||||
content: 'hello local cache', |
||||
sig: 'd'.repeat(128), |
||||
...overrides |
||||
} |
||||
} |
||||
|
||||
describe('eventMatchesLocalFeedFilter', () => { |
||||
it('matches ids, authors, kinds, time windows, tags, and search', () => { |
||||
expect( |
||||
eventMatchesLocalFeedFilter(event(), { |
||||
ids: ['a'.repeat(64)], |
||||
authors: ['b'.repeat(64)], |
||||
kinds: [1], |
||||
since: 900, |
||||
until: 1100, |
||||
'#p': ['c'.repeat(64)], |
||||
'#t': ['nostr'], |
||||
search: 'local' |
||||
}) |
||||
).toBe(true) |
||||
}) |
||||
|
||||
it('rejects events outside any filter constraint', () => { |
||||
expect(eventMatchesLocalFeedFilter(event({ kind: 6 }), { kinds: [1] })).toBe(false) |
||||
expect(eventMatchesLocalFeedFilter(event(), { since: 1001 })).toBe(false) |
||||
expect(eventMatchesLocalFeedFilter(event(), { '#e': ['e'.repeat(64)] })).toBe(false) |
||||
expect(eventMatchesLocalFeedFilter(event(), { search: 'relay-only' })).toBe(false) |
||||
}) |
||||
}) |
||||
@ -0,0 +1,43 @@
@@ -0,0 +1,43 @@
|
||||
import type { Event, Filter } from 'nostr-tools' |
||||
|
||||
function valuesMatchTag(tagName: string, eventValues: string[], filterValues: unknown[]): boolean { |
||||
if (tagName.toLowerCase() === 't') { |
||||
const allowed = new Set(filterValues.map((v) => String(v).toLowerCase())) |
||||
return eventValues.some((v) => allowed.has(v.toLowerCase())) |
||||
} |
||||
const allowed = new Set(filterValues.map((v) => String(v))) |
||||
return eventValues.some((v) => allowed.has(v)) |
||||
} |
||||
|
||||
export function eventMatchesLocalFeedFilter(event: Event, filter: Filter): boolean { |
||||
if (Array.isArray(filter.ids) && filter.ids.length > 0 && !filter.ids.includes(event.id)) return false |
||||
if (Array.isArray(filter.authors) && filter.authors.length > 0 && !filter.authors.includes(event.pubkey)) { |
||||
return false |
||||
} |
||||
if (Array.isArray(filter.kinds) && filter.kinds.length > 0 && !filter.kinds.includes(event.kind)) return false |
||||
if (typeof filter.since === 'number' && event.created_at < filter.since) return false |
||||
if (typeof filter.until === 'number' && event.created_at > filter.until) return false |
||||
|
||||
const search = typeof filter.search === 'string' ? filter.search.trim().toLowerCase() : '' |
||||
if (search) { |
||||
const haystack = `${event.content ?? ''} ${(event.tags ?? []).flat().join(' ')}`.toLowerCase() |
||||
if (!haystack.includes(search)) return false |
||||
} |
||||
|
||||
for (const [key, values] of Object.entries(filter)) { |
||||
if (!key.startsWith('#')) continue |
||||
if (!Array.isArray(values) || values.length === 0) continue |
||||
const tagName = key.slice(1) |
||||
const eventValues = event.tags |
||||
.filter((tag) => tag[0] === tagName && typeof tag[1] === 'string') |
||||
.map((tag) => tag[1] as string) |
||||
if (eventValues.length === 0) return false |
||||
if (!valuesMatchTag(tagName, eventValues, values)) return false |
||||
} |
||||
|
||||
return true |
||||
} |
||||
|
||||
export function eventMatchesAnyLocalFeedFilter(event: Event, filters: readonly Filter[]): boolean { |
||||
return filters.some((filter) => eventMatchesLocalFeedFilter(event, filter)) |
||||
} |
||||
@ -0,0 +1,27 @@
@@ -0,0 +1,27 @@
|
||||
import { feedRelayPolicyUrls } from '@/features/feed/relay-policy' |
||||
import { getFavoritesFeedRelayUrls } from '@/lib/favorites-feed-relays' |
||||
import { AGGR_NOSTR_LAND_WSS } from '@/lib/nostr-land-aggr' |
||||
import { normalizeAnyRelayUrl } from '@/lib/url' |
||||
|
||||
function relayUrlIsNostrLandAggr(url: string): boolean { |
||||
const normalized = (normalizeAnyRelayUrl(url) || url.trim()).toLowerCase() |
||||
const aggr = (normalizeAnyRelayUrl(AGGR_NOSTR_LAND_WSS) || AGGR_NOSTR_LAND_WSS).toLowerCase() |
||||
return normalized === aggr |
||||
} |
||||
|
||||
export function buildAllFavoritesFeedRelayUrls( |
||||
favoriteRelays: string[], |
||||
blockedRelays: string[], |
||||
extraFeedRelayUrls: string[] |
||||
): string[] { |
||||
return feedRelayPolicyUrls([ |
||||
{ source: 'favorites', urls: getFavoritesFeedRelayUrls(favoriteRelays, blockedRelays) }, |
||||
{ source: 'fallback', urls: extraFeedRelayUrls } |
||||
], { |
||||
operation: 'favorites-feed', |
||||
blockedRelays, |
||||
nostrLandAggr: 'never', |
||||
applySocialKindBlockedFilter: false, |
||||
allowThirdPartyLocalRelays: true |
||||
}).filter((url) => !relayUrlIsNostrLandAggr(url)) |
||||
} |
||||
@ -0,0 +1,19 @@
@@ -0,0 +1,19 @@
|
||||
import { describe, expect, it } from 'vitest' |
||||
import { AGGR_NOSTR_LAND_WSS } from '@/lib/nostr-land-aggr' |
||||
import { buildWispTrendingNotesRelayUrl } from '@/lib/wisp-trending-relay' |
||||
import { buildAllFavoritesFeedRelayUrls } from '@/lib/home-feed-relays' |
||||
|
||||
describe('home feed relay policy', () => { |
||||
it('keeps aggr.nostr.land out of the main home feed', () => { |
||||
const urls = buildAllFavoritesFeedRelayUrls( |
||||
['wss://relay.example.com/', AGGR_NOSTR_LAND_WSS], |
||||
[], |
||||
[buildWispTrendingNotesRelayUrl(), AGGR_NOSTR_LAND_WSS] |
||||
) |
||||
|
||||
expect(urls).toContain('wss://relay.example.com/') |
||||
expect(urls).toContain(buildWispTrendingNotesRelayUrl()) |
||||
expect(urls).not.toContain('wss://aggr.nostr.land/') |
||||
expect(urls).not.toContain(AGGR_NOSTR_LAND_WSS) |
||||
}) |
||||
}) |
||||
Loading…
Reference in new issue