Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handle njump me links #1202

Merged
merged 7 commits into from
Jun 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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}))/
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we move this regex to a new shared place? Even though the regex could be not 100% the same and could not be shared, we are repeating patterns for some things and I found myself sometimes having to fix a problem with one regex and having to search for the other ones to see if the same error could occur there. Just a centralized place where we put all regexes could help to identify potential issues.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I want to add support for nostr: too. I considered refactoring NoteParser and this code to share a regex, but I didn't because this was just a few minutes of work that I didn't want to throw away because it seemed really useful. I didn't want to (probably) quadruple the time I spent on it to do a refactor. I tried to pull out the shared part of the regex into a constant, but you can't interpolate a regex literal inside another regex literal, so I would have to switch to NSRegularExpression or something, which would require more code refactoring... and that would only be for the regex itself. To share the code that extracts the hex from bech32 would require some more.

Given that this is the second place we are using the regex I'm ok with duplicating it. I like the rule of waiting to refactor until you've copied code in three places rather than two.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fair enough. Let's keep the eyes open for the next time we need to add or modify a regex to include that refactor in the scope of the ticket.

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)
}
}
}
}
Loading