Nostr.NIP36 (Nostr Lib v0.2.0) (nip36)

View Source

NIP-36: Sensitive Content / Content Warning

The content-warning tag enables users to specify if event content needs approval before being shown. Clients can hide the content until the user acts on it.

Tag Format

["content-warning", "<optional reason>"]

Examples

# Extract warning from tags
NIP36.from_tags(event.tags)
# => "Spoilers" | true | nil

# Build a warning tag
NIP36.to_tag("NSFW")
# => %Tag{type: :"content-warning", data: "NSFW"}

# Check if event has warning
NIP36.has_warning?(event)
# => true | false

See: https://github.com/nostr-protocol/nips/blob/master/36.md

Summary

Types

Content warning value.

Functions

Adds a content warning tag to an event.

Extracts content warning from a list of tags.

Checks if an event or list of tags has a content warning.

Removes content warning from an event.

Builds a content-warning tag.

Types

warning()

@type warning() :: binary() | true | nil

Content warning value.

  • nil - no content warning
  • true - content warning present but no reason given
  • binary() - content warning with reason string

Functions

add_warning(event, reason \\ true)

@spec add_warning(Nostr.Event.t(), binary() | true) :: Nostr.Event.t()

Adds a content warning tag to an event.

If the event already has a content-warning tag, it is replaced.

Examples

iex> event = %Event{tags: []}
iex> NIP36.add_warning(event, "Spoilers").tags
[%Tag{type: :"content-warning", data: "Spoilers", info: []}]

iex> event = %Event{tags: []}
iex> NIP36.add_warning(event).tags
[%Tag{type: :"content-warning", data: "", info: []}]

from_tags(tags)

@spec from_tags([Nostr.Tag.t()]) :: warning()

Extracts content warning from a list of tags.

Returns:

  • The reason string if present and non-empty
  • true if the tag is present but has no reason
  • nil if no content-warning tag exists

Examples

iex> tags = [%Tag{type: :"content-warning", data: "Spoilers"}]
iex> NIP36.from_tags(tags)
"Spoilers"

iex> tags = [%Tag{type: :"content-warning", data: ""}]
iex> NIP36.from_tags(tags)
true

iex> NIP36.from_tags([])
nil

has_warning?(tags)

@spec has_warning?(Nostr.Event.t() | [Nostr.Tag.t()]) :: boolean()

Checks if an event or list of tags has a content warning.

Examples

iex> NIP36.has_warning?(%Event{tags: [%Tag{type: :"content-warning", data: ""}]})
true

iex> NIP36.has_warning?([%Tag{type: :p, data: "pubkey"}])
false

remove_warning(event)

@spec remove_warning(Nostr.Event.t()) :: Nostr.Event.t()

Removes content warning from an event.

Examples

iex> tags = [%Tag{type: :"content-warning", data: "test"}]
iex> event = %Event{tags: tags}
iex> NIP36.remove_warning(event).tags
[]

to_tag(reason)

@spec to_tag(warning()) :: Nostr.Tag.t() | nil

Builds a content-warning tag.

Examples

iex> NIP36.to_tag("NSFW")
%Tag{type: :"content-warning", data: "NSFW", info: []}

iex> NIP36.to_tag(true)
%Tag{type: :"content-warning", data: "", info: []}

iex> NIP36.to_tag(nil)
nil