-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add support for NIP-36 Sensitive Content / Content Warning (#196)
- Loading branch information
Showing
4 changed files
with
63 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
// | ||
// ContentWarningTag.swift | ||
// NostrSDK | ||
// | ||
// Created by Terry Yiu on 11/3/24. | ||
// | ||
|
||
import Foundation | ||
|
||
/// Interprets the content-warning tag. | ||
/// | ||
/// See [NIP-36 Sensitive Content / Content Warning](https://github.com/nostr-protocol/nips/blob/master/36.md). | ||
public protocol ContentWarningTagInterpreting: NostrEvent {} | ||
public extension ContentWarningTagInterpreting { | ||
/// Content warning to indicate that the event's content needs to be approved by readers to be shown. Clients can hide the content until the user acts on it. | ||
var contentWarning: String? { | ||
firstValueForRawTagName("content-warning") | ||
} | ||
} | ||
|
||
/// Builder that adds a content warning to an event. | ||
/// | ||
/// See [NIP-36 Sensitive Content / Content Warning](https://github.com/nostr-protocol/nips/blob/master/36.md). | ||
public protocol ContentWarningTagBuilding: NostrEventBuilding {} | ||
public extension ContentWarningTagBuilding { | ||
/// Adds a content warning to indicate that the event's content needs to be approved by readers to be shown. Clients can hide the content until the user acts on it. | ||
@discardableResult | ||
func contentWarning(_ contentWarning: String) -> Self { | ||
appendTags(Tag(name: "content-warning", value: contentWarning)) | ||
} | ||
} |
29 changes: 29 additions & 0 deletions
29
Tests/NostrSDKTests/Events/Tags/ContentWarningTagTests.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
// | ||
// ContentWarningTagTests.swift | ||
// NostrSDK | ||
// | ||
// Created by Terry Yiu on 11/3/24. | ||
// | ||
|
||
@testable import NostrSDK | ||
import XCTest | ||
|
||
final class ContentWarningTagTests: XCTestCase, EventVerifying { | ||
|
||
func testCreateContentWarningTaggedEvent() throws { | ||
let event = try NostrEvent.Builder(kind: .textNote) | ||
.contentWarning("Trigger warning.") | ||
.content("Pineapple goes great on pizza.") | ||
.build(signedBy: .test) | ||
|
||
XCTAssertEqual(event.contentWarning, "Trigger warning.") | ||
|
||
XCTAssertEqual(event.tags.count, 1) | ||
let tag = try XCTUnwrap(event.tags.first) | ||
XCTAssertEqual(tag.name, "content-warning") | ||
XCTAssertEqual(tag.value, "Trigger warning.") | ||
|
||
try verifyEvent(event) | ||
} | ||
|
||
} |