Skip to content

Commit

Permalink
WIP: Tab Interceptor for AI Chat
Browse files Browse the repository at this point in the history
  • Loading branch information
Bunn committed Dec 20, 2024
1 parent 6b6a223 commit a9c82c4
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 23 deletions.
29 changes: 16 additions & 13 deletions DuckDuckGo/MainViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -1506,27 +1506,30 @@ class MainViewController: UIViewController {
}
.store(in: &emailCancellables)
}

private func subscribeToURLInterceptorNotifications() {
NotificationCenter.default.publisher(for: .urlInterceptPrivacyPro)
.receive(on: DispatchQueue.main)
.sink { [weak self] notification in
switch notification.name {
case .urlInterceptPrivacyPro:
let deepLinkTarget: SettingsViewModel.SettingsDeepLinkSection
if let origin = notification.userInfo?[AttributionParameter.origin] as? String {
deepLinkTarget = .subscriptionFlow(origin: origin)
} else {
deepLinkTarget = .subscriptionFlow()
}
self?.launchSettings(deepLinkTarget: deepLinkTarget)
default:
return
let deepLinkTarget: SettingsViewModel.SettingsDeepLinkSection
if let origin = notification.userInfo?[AttributionParameter.origin] as? String {
deepLinkTarget = .subscriptionFlow(origin: origin)
} else {
deepLinkTarget = .subscriptionFlow()
}
self?.launchSettings(deepLinkTarget: deepLinkTarget)

}
.store(in: &urlInterceptorCancellables)

NotificationCenter.default.publisher(for: .urlInterceptAIChat)
.receive(on: DispatchQueue.main)
.sink { [weak self] _ in
self?.openAIChat()
}
.store(in: &urlInterceptorCancellables)
}

private func subscribeToSettingsDeeplinkNotifications() {
NotificationCenter.default.publisher(for: .settingsDeepLinkNotification)
.receive(on: DispatchQueue.main)
Expand Down
26 changes: 17 additions & 9 deletions DuckDuckGo/TabURLInterceptor.swift
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,11 @@ import Foundation
import BrowserServicesKit
import Common
import Subscription
import AIChat

enum InterceptedURL: String {
case privacyPro
case aiChat
}

struct InterceptedURLInfo {
Expand All @@ -49,21 +51,19 @@ final class TabURLInterceptorDefault: TabURLInterceptor {
]

func allowsNavigatingTo(url: URL) -> Bool {

if !url.isPart(ofDomain: "duckduckgo.com") {
return true
}

guard let components = normalizeScheme(url.absoluteString) else {
return true
if url.isDuckAIURL {
return handleURLInterception(interceptedURL: .aiChat, queryItems: nil)
}

guard let matchingURL = urlToIntercept(path: components.path) else {

guard url.isPart(ofDomain: "duckduckgo.com"),
let components = normalizeScheme(url.absoluteString),
let matchingURL = urlToIntercept(path: components.path) else {
return true
}

return handleURLInterception(interceptedURL: matchingURL.id, queryItems: components.percentEncodedQueryItems)
}

}

extension TabURLInterceptorDefault {
Expand Down Expand Up @@ -99,11 +99,19 @@ extension TabURLInterceptorDefault {
)
return false
}
case .aiChat:
NotificationCenter.default.post(
name: .urlInterceptAIChat,
object: nil,
userInfo: nil
)
return false
}
return true
}
}

extension NSNotification.Name {
static let urlInterceptPrivacyPro: NSNotification.Name = Notification.Name(rawValue: "com.duckduckgo.notification.urlInterceptPrivacyPro")
static let urlInterceptAIChat: NSNotification.Name = Notification.Name(rawValue: "com.duckduckgo.notification.urlInterceptAIChat")
}
2 changes: 1 addition & 1 deletion LocalPackages/AIChat/Sources/AIChat/URL+Extension.swift
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ extension URL {
return urlComponents.url ?? self
}

var isDuckAIURL: Bool {
public var isDuckAIURL: Bool {
guard let host = self.host, host == Constants.duckDuckGoHost else {
return false
}
Expand Down

0 comments on commit a9c82c4

Please sign in to comment.