Skip to content

Commit c892185

Browse files
Copilotgraycreate
andcommitted
Fix reportLink nil handling to prevent crashes
Co-authored-by: graycreate <5203798+graycreate@users.noreply.github.com>
1 parent 5613d57 commit c892185

File tree

4 files changed

+22
-3
lines changed

4 files changed

+22
-3
lines changed

V2er/State/DataFlow/Actions/FeedDetailActions.swift

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -143,9 +143,15 @@ struct FeedDetailActions {
143143
func execute(in store: Store) async {
144144
Toast.show("举报中")
145145
let state = store.appState.feedDetailStates[id]!
146+
147+
guard let reportLink = state.model.reportLink, !reportLink.isEmpty else {
148+
Toast.show("无法举报此主题")
149+
dispatch(ReportTopicDone(id: id, reported: false))
150+
return
151+
}
146152

147153
let result: APIResult<DailyInfo> = await APIService.shared
148-
.htmlGet(endpoint: .general(url: state.model.reportLink!),
154+
.htmlGet(endpoint: .general(url: reportLink),
149155
requestHeaders: Headers.TINY_REFERER)
150156
var reported = false
151157
if case let .success(result) = result {

V2er/State/DataFlow/Model/FeedDetailInfo.swift

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -258,6 +258,8 @@ struct FeedDetailInfo: BaseModel {
258258
let sIndex = rawReportUrl.index(of: "/report/topic/")!
259259
let eIndex = rawReportUrl.lastIndex(of: "'")!
260260
self.reportLink = String(rawReportUrl[sIndex..<eIndex])
261+
} else {
262+
self.reportLink = .empty
261263
}
262264

263265
self.hasReported = root.pick("div.content div.box div.inner span.fade")

V2er/View/FeedDetail/FeedDetailPage.swift

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -232,13 +232,14 @@ struct FeedDetailPage: StateView, KeyboardReadable, InstanceIdentifiable {
232232
Label("忽略", systemImage: "exclamationmark.octagon")
233233
}
234234
let reported = state.model.hasReported ?? false
235+
let canReport = !(state.model.reportLink?.isEmpty ?? true)
235236
Button {
236237
replyIsFocused = false
237238
dispatch(FeedDetailActions.ReportTopic(id: id))
238239
} label: {
239240
Label(reported ? "已举报" : "举报", systemImage: "person.crop.circle.badge.exclamationmark")
240241
}
241-
.disabled(reported)
242+
.disabled(reported || !canReport)
242243

243244
Divider()
244245

V2erTests/V2erTests.swift

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ class V2erTests: XCTestCase {
1313

1414
override func setUpWithError() throws {
1515
// Put setup code here. This method is called before the invocation of each test method in the class.
16-
13.5}
16+
}
1717

1818
override func tearDownWithError() throws {
1919
// Put teardown code here. This method is called after the invocation of each test method in the class.
@@ -23,6 +23,16 @@ class V2erTests: XCTestCase {
2323
// This is an example of a functional test case.
2424
// Use XCTAssert and related functions to verify your tests produce the correct results.
2525
}
26+
27+
func testReportLinkHandling() throws {
28+
// Test that FeedDetailInfo correctly handles empty reportLink
29+
var feedDetailInfo = FeedDetailInfo()
30+
31+
// When reportLink is set to empty, it should not be nil
32+
feedDetailInfo.reportLink = .empty
33+
XCTAssertNotNil(feedDetailInfo.reportLink)
34+
XCTAssertTrue(feedDetailInfo.reportLink!.isEmpty)
35+
}
2636

2737
func testPerformanceExample() throws {
2838
// This is an example of a performance test case.

0 commit comments

Comments
 (0)