diff --git a/scripts/test_feishu_browser.py b/scripts/test_feishu_browser.py index 4f9bf60..b37d1a4 100644 --- a/scripts/test_feishu_browser.py +++ b/scripts/test_feishu_browser.py @@ -46,6 +46,20 @@ import WebKit _ = NSApplication.shared +let tenant = FeishuTaskView.home.host! +for path in ["/next/messenger", "/next/messenger/"] { + precondition(FeishuTaskView.isMessengerLanding(URL(string: "https://\(tenant)\(path)")!), + "The messages landing page must return to tasks") +} +for url in [FeishuTaskView.home, + URL(string: "https://\(tenant)/next/task/watched-tasks")!, + URL(string: "https://\(tenant)/accounts/page/login/")!, + URL(string: "https://\(tenant)/next/messenger/chat")!, + URL(string: "https://\(tenant)/next/messenger?openChatId=fixture")!, + URL(string: "https://example.com/next/messenger")!] { + precondition(!FeishuTaskView.isMessengerLanding(url), + "Task, sign-in, conversation and foreign URLs must retain normal navigation") +} for embedded in [false, true] { let view = FeishuTaskView(viewId: embedded ? 2 : 1, messenger: TestMessenger(), arguments: ["script": "", "embedded": embedded]) diff --git a/serverbox/lib/view/page/home.dart b/serverbox/lib/view/page/home.dart index 1e45751..83fdb6a 100644 --- a/serverbox/lib/view/page/home.dart +++ b/serverbox/lib/view/page/home.dart @@ -217,10 +217,8 @@ class _HomePageState extends ConsumerState // The one way in the lifecycle cannot report: a file or link opened into // an app that never left the foreground. See [MethodChans.onOpened]. MethodChans.onOpened(() => unawaited(_consumePending())); - // avoid index out of range - if (_selectIndex.value >= _tabs.length || _selectIndex.value < 0) { - _selectIndex.value = 0; - } + // Restore before the first frame so launch notices appear over the saved tab. + _selectIndex.value = _savedTabIndex() ?? 0; _pageController = PageController(initialPage: _selectIndex.value); if (Stores.setting.generalWakeLock.fetch()) { WakelockPlus.enable(); @@ -603,16 +601,6 @@ class _HomePageState extends ConsumerState @override Future afterFirstLayout(BuildContext context) async { - // Auth required for first launch - // Where it was left, if that is still a tab: the enabled set is a setting - // and may have shrunk since. - final saved = _savedTabIndex(); - if (saved != null) { - _selectIndex.value = saved; - if (_pageController.hasClients) { - _pageController.jumpToPage(saved); - } - } // Explicitly, because the listener above only fires on a change: the first // tab is usually already the value, and nothing would have announced it. _publishCurrentTab(); diff --git a/serverbox/macos/Runner/FeishuTaskView.swift b/serverbox/macos/Runner/FeishuTaskView.swift index a6a44ab..bad795d 100644 --- a/serverbox/macos/Runner/FeishuTaskView.swift +++ b/serverbox/macos/Runner/FeishuTaskView.swift @@ -25,11 +25,14 @@ final class FeishuTaskView: NSView, WKNavigationDelegate, WKUIDelegate, WKDownlo private var dark: Bool private var popupWindows: [NSWindow] = [] private var loadingObservation: NSKeyValueObservation? + private var urlObservation: NSKeyValueObservation? private var activeObservation: NSObjectProtocol? private var pageError: String? private var needsRecovery = false private var recoveryAttempted = false private var initialNavigationStarted = false + private var taskLandingRetried = false + private var taskLandingCheckPending = false init(viewId: Int64, messenger: FlutterBinaryMessenger, arguments: Any?) { let args = arguments as? [String: Any] ?? [:] @@ -60,11 +63,18 @@ final class FeishuTaskView: NSView, WKNavigationDelegate, WKUIDelegate, WKDownlo loadingObservation = web.observe(\.isLoading, options: [.new]) { [weak self] _, _ in self?.sendState() } + // Feishu can switch its top-level URL via client-side routing. + urlObservation = web.observe(\.url, options: [.new]) { [weak self] _, _ in + self?.checkTaskLanding() + } channel.setMethodCallHandler { [weak self] call, result in guard let self = self else { result(nil); return } switch call.method { case "state": self.sendState(); result(nil) - case "reload": self.reloadPage(); result(nil) + case "reload": + self.taskLandingRetried = false + self.reloadPage() + result(nil) case "refresh": // Do not interrupt sign-in, navigation, or a native attachment dialog. let url = self.web.url ?? Self.home @@ -75,7 +85,10 @@ final class FeishuTaskView: NSView, WKNavigationDelegate, WKUIDelegate, WKDownlo self.reloadPage() } result(nil) - case "home": self.web.load(URLRequest(url: Self.home)); result(nil) + case "home": + self.taskLandingRetried = false + self.web.load(URLRequest(url: Self.home)) + result(nil) case "back": if self.web.canGoBack { self.web.goBack() }; result(nil) case "appearance": self.enabled = call.arguments as? Bool ?? true @@ -119,8 +132,32 @@ final class FeishuTaskView: NSView, WKNavigationDelegate, WKUIDelegate, WKDownlo private func reloadPage() { pageError = nil - // reload() has no navigation to repeat after an initial provisional failure. - if web.reload() == nil { web.load(URLRequest(url: Self.home)) } + // Reloading the messages landing page would keep the task view stranded. + if web.url.map(Self.isMessengerLanding) == true || web.reload() == nil { + web.load(URLRequest(url: Self.home)) + } + } + static func isMessengerLanding(_ url: URL) -> Bool { + guard url.scheme == "https", url.host == home.host, url.port == nil, + url.query == nil, url.fragment == nil else { return false } + return url.path == "/next/messenger" || url.path == "/next/messenger/" + } + private func checkTaskLanding() { + guard !taskLandingCheckPending else { return } + taskLandingCheckPending = true + DispatchQueue.main.async { [weak self] in + guard let self = self else { return } + self.taskLandingCheckPending = false + guard !self.web.isLoading, self.web.url.map(Self.isMessengerLanding) == true, + self.window?.attachedSheet == nil, + !self.popupWindows.contains(where: { $0.isVisible }) else { return } + if self.taskLandingRetried { + self.sendState(error: "飞书仍停在消息首页。请点击刷新重试,或在浏览器中打开任务。") + return + } + self.taskLandingRetried = true + self.web.load(URLRequest(url: Self.home)) + } } private func recoverIfNeeded() { guard needsRecovery, !recoveryAttempted, window != nil, NSApp.isActive else { return } @@ -175,6 +212,7 @@ final class FeishuTaskView: NSView, WKNavigationDelegate, WKUIDelegate, WKDownlo needsRecovery = false recoveryAttempted = false sendState() + checkTaskLanding() } } func webView(_ webView: WKWebView, didStartProvisionalNavigation navigation: WKNavigation!) {