Skip to content

Commit

Permalink
Merge pull request #1202 from planetary-social/handle-njump-me-links
Browse files Browse the repository at this point in the history
Handle njump me links
  • Loading branch information
mplorentz authored Jun 5, 2024
2 parents b8722c9 + 5b70c31 commit 9edd6b8
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 3 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased]

- Added support for opening njump.me content in Nos.
- Fixed a crash on logout
- Fixed a bug where some profiles wouldn't load old notes.
- Fixed an issue where NIP-05 could appear as invalid.

Expand Down
45 changes: 42 additions & 3 deletions Nos/Service/DeepLinkService.swift
Original file line number Diff line number Diff line change
@@ -1,26 +1,65 @@
import Foundation
import Logger
import Dependencies

enum DeepLinkService {

static let nosURLScheme = "nos"
/// Returns the URL scheme for Nos, which varies by build (dev, staging, production).
static var nosURLScheme: String? = {
if let urlTypes = Bundle.main.infoDictionary?["CFBundleURLTypes"] as? [[String: Any]] {
for urlTypeDictionary in urlTypes {
guard let urlSchemes = urlTypeDictionary["CFBundleURLSchemes"] as? [String] else { continue }
guard let externalURLScheme = urlSchemes.first else { continue }
return externalURLScheme
}
}

return nil
}()

@MainActor static func handle(_ url: URL, router: Router) {
@Dependency(\.persistenceController) var persistenceController
@Dependency(\.router) var router
Log.info("handling link \(url.absoluteString)")

let components = URLComponents(url: url, resolvingAgainstBaseURL: true)

guard let components else {
guard let components, let nosURLScheme, components.scheme == nosURLScheme else {
return
}

if components.scheme == nosURLScheme, components.host == "note", components.path == "/new" {
if components.host == "note", components.path == "/new" {
let noteContents = components
.queryItems?
.first(where: { $0.name == "contents" })?
.value

router.showNewNoteView(contents: noteContents)
} else {
/// Check for links like nos:nevent123174
let firstPathComponent = components.path
// swiftlint:disable:next opening_brace
let unformattedRegex = /(?:nostr:)?(?<entity>((npub1|note1|nprofile1|nevent1)[a-zA-Z0-9]{58,255}))/
do {
if let match = try unformattedRegex.firstMatch(in: firstPathComponent) {
let entity = match.1
let string = String(entity)

let (humanReadablePart, checksum) = try Bech32.decode(string)

if humanReadablePart == Nostr.publicKeyPrefix, let hex = SHA256Key.decode(base5: checksum) {
router.push(try Author.findOrCreate(by: hex, context: persistenceController.viewContext))
} else if humanReadablePart == Nostr.notePrefix, let hex = SHA256Key.decode(base5: checksum) {
router.push(try Event.findOrCreateStubBy(id: hex, context: persistenceController.viewContext))
} else if humanReadablePart == Nostr.profilePrefix, let hex = TLV.decode(checksum: checksum) {
router.push(try Author.findOrCreate(by: hex, context: persistenceController.viewContext))
} else if humanReadablePart == Nostr.eventPrefix, let hex = TLV.decode(checksum: checksum) {
router.push(try Event.findOrCreateStubBy(id: hex, context: persistenceController.viewContext))
}
}
} catch {
Log.optional(error)
}
}
}
}

0 comments on commit 9edd6b8

Please sign in to comment.