-
Notifications
You must be signed in to change notification settings - Fork 14
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
Handle njump me links #1202
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
90c8097
Handle nos:nevent and similar links
mplorentz 7445c15
Merge remote-tracking branch 'origin/main' into handle-njump-me-links
mplorentz b1d51fc
Update changelog
mplorentz 1906924
Merge branch 'main' into handle-njump-me-links
mplorentz 2cdc8a1
fix swiftlint error
mplorentz 5f16eee
Merge branch 'handle-njump-me-links' of github.com:planetary-social/n…
mplorentz 5b70c31
Merge branch 'main' into handle-njump-me-links
mplorentz File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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) | ||
} | ||
} | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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 toNSRegularExpression
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.
There was a problem hiding this comment.
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.