15 changed files with 113 additions and 19 deletions
@ -0,0 +1,52 @@ |
|||||||
|
import { describe, expect, it } from 'vitest' |
||||||
|
import { MEDIA_AUTO_LOAD_POLICY } from '@/constants' |
||||||
|
import { resolveAutoLoadMediaForAuthor } from '@/lib/media-auto-load-policy' |
||||||
|
import type { Event } from 'nostr-tools' |
||||||
|
|
||||||
|
const author = 'aa'.repeat(32) |
||||||
|
|
||||||
|
function warnedNote(): Event { |
||||||
|
return { |
||||||
|
id: 'id', |
||||||
|
sig: 'sig', |
||||||
|
kind: 1, |
||||||
|
tags: [['content-warning', 'Sensitive content']], |
||||||
|
content: 'hello', |
||||||
|
created_at: 1, |
||||||
|
pubkey: author |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
describe('resolveAutoLoadMediaForAuthor', () => { |
||||||
|
it('blocks autoload for content-warning notes even when author is followed', () => { |
||||||
|
expect( |
||||||
|
resolveAutoLoadMediaForAuthor({ |
||||||
|
policy: MEDIA_AUTO_LOAD_POLICY.FOLLOWS_ONLY, |
||||||
|
authorPubkey: author, |
||||||
|
followings: [author], |
||||||
|
sourceEvent: warnedNote() |
||||||
|
}) |
||||||
|
).toBe(false) |
||||||
|
}) |
||||||
|
|
||||||
|
it('allows autoload for followed authors on notes without content warnings', () => { |
||||||
|
expect( |
||||||
|
resolveAutoLoadMediaForAuthor({ |
||||||
|
policy: MEDIA_AUTO_LOAD_POLICY.FOLLOWS_ONLY, |
||||||
|
authorPubkey: author, |
||||||
|
followings: [author], |
||||||
|
sourceEvent: { ...warnedNote(), tags: [] } |
||||||
|
}) |
||||||
|
).toBe(true) |
||||||
|
}) |
||||||
|
|
||||||
|
it('blocks autoload for content warnings under ALWAYS policy', () => { |
||||||
|
expect( |
||||||
|
resolveAutoLoadMediaForAuthor({ |
||||||
|
policy: MEDIA_AUTO_LOAD_POLICY.ALWAYS, |
||||||
|
authorPubkey: author, |
||||||
|
sourceEvent: warnedNote() |
||||||
|
}) |
||||||
|
).toBe(false) |
||||||
|
}) |
||||||
|
}) |
||||||
@ -0,0 +1,21 @@ |
|||||||
|
import type { Event } from 'nostr-tools' |
||||||
|
import { createContext, useContext } from 'react' |
||||||
|
|
||||||
|
const MediaAutoLoadEventContext = createContext<Event | null>(null) |
||||||
|
|
||||||
|
/** Supplies the note/event being rendered so media policy can respect content warnings. */ |
||||||
|
export function MediaAutoLoadEventProvider({ |
||||||
|
event, |
||||||
|
children |
||||||
|
}: { |
||||||
|
event: Event |
||||||
|
children: React.ReactNode |
||||||
|
}) { |
||||||
|
return ( |
||||||
|
<MediaAutoLoadEventContext.Provider value={event}>{children}</MediaAutoLoadEventContext.Provider> |
||||||
|
) |
||||||
|
} |
||||||
|
|
||||||
|
export function useMediaAutoLoadSourceEvent(): Event | null { |
||||||
|
return useContext(MediaAutoLoadEventContext) |
||||||
|
} |
||||||
Loading…
Reference in new issue