Skip to content
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
14 changes: 14 additions & 0 deletions scripts/test_feishu_browser.py
Original file line number Diff line number Diff line change
Expand Up @@ -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])
Expand Down
16 changes: 2 additions & 14 deletions serverbox/lib/view/page/home.dart
Original file line number Diff line number Diff line change
Expand Up @@ -217,10 +217,8 @@ class _HomePageState extends ConsumerState<HomePage>
// 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();
Expand Down Expand Up @@ -603,16 +601,6 @@ class _HomePageState extends ConsumerState<HomePage>

@override
Future<void> 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();
Expand Down
46 changes: 42 additions & 4 deletions serverbox/macos/Runner/FeishuTaskView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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] ?? [:]
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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 }
Expand Down Expand Up @@ -175,6 +212,7 @@ final class FeishuTaskView: NSView, WKNavigationDelegate, WKUIDelegate, WKDownlo
needsRecovery = false
recoveryAttempted = false
sendState()
checkTaskLanding()
}
}
func webView(_ webView: WKWebView, didStartProvisionalNavigation navigation: WKNavigation!) {
Expand Down
Loading