Skip to content

Commit 36910f1

Browse files
Expose the reason parameter in the flag endpoint (#3173)
1 parent 5fea36d commit 36910f1

File tree

8 files changed

+67
-25
lines changed

8 files changed

+67
-25
lines changed

CHANGELOG.md

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,12 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/).
44
# Upcoming
55

66
## StreamChat
7-
### 🔄 Changed
8-
- Replace message update request to partial update on message pinning [#3166](https://github.com/GetStream/stream-chat-swift/pull/3166)
7+
### ✅ Added
8+
- Add `reason` parameter to `flagMessage()` [#3173](https://github.com/GetStream/stream-chat-swift/issues/3173)
99
### 🐞 Fixed
1010
- Reset managed object contexts before removing all the data on logout [#3170](https://github.com/GetStream/stream-chat-swift/pull/3170)
11+
### 🔄 Changed
12+
- Replace message update request to partial update on message pinning [#3166](https://github.com/GetStream/stream-chat-swift/pull/3166)
1113

1214
## StreamChatUI
1315
### ✅ Added

Sources/StreamChat/APIClient/Endpoints/ModerationEndpoints.swift

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -71,14 +71,21 @@ extension Endpoint {
7171
extension Endpoint {
7272
static func flagMessage(
7373
_ flag: Bool,
74-
with messageId: MessageId
74+
with messageId: MessageId,
75+
reason: String? = nil
7576
) -> Endpoint<FlagMessagePayload> {
76-
.init(
77+
var body: [String: AnyEncodable] = ["target_message_id": AnyEncodable(messageId)]
78+
79+
if let reason = reason {
80+
body["reason"] = AnyEncodable(reason)
81+
}
82+
83+
return .init(
7784
path: .flagMessage(flag),
7885
method: .post,
7986
queryItems: nil,
8087
requiresConnectionId: false,
81-
body: ["target_message_id": messageId]
88+
body: body
8289
)
8390
}
8491
}

Sources/StreamChat/Controllers/MessageController/MessageController.swift

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -569,10 +569,12 @@ public class ChatMessageController: DataController, DelegateCallable, DataStoreP
569569

570570
/// Flags the message this controller manages.
571571
///
572-
/// - Parameter completion: The completion. Will be called on a **callbackQueue** when the network request is finished.
572+
/// - Parameters:
573+
/// - reason: The flag reason.
574+
/// - completion: The completion. Will be called on a **callbackQueue** when the network request is finished.
573575
///
574-
public func flag(completion: ((Error?) -> Void)? = nil) {
575-
messageUpdater.flagMessage(true, with: messageId, in: cid) { error in
576+
public func flag(reason: String? = nil, completion: ((Error?) -> Void)? = nil) {
577+
messageUpdater.flagMessage(true, with: messageId, in: cid, reason: reason) { error in
576578
self.callback {
577579
completion?(error)
578580
}

Sources/StreamChat/Workers/MessageUpdater.swift

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -331,18 +331,26 @@ class MessageUpdater: Worker {
331331
/// If the message doesn't exist locally it will be fetched and saved locally first first.
332332
///
333333
/// - Parameters:
334-
/// - flag: The indicator saying whether the messageId should be flagged or unflagged.
335-
/// - messageId: The identifier of a messageId that should be flagged or unflagged.
334+
/// - flag: The indicator saying whether the message should be flagged or unflagged.
335+
/// - messageId: The identifier of a message that should be flagged or unflagged.
336+
/// - cid: The identifier of the channel the message belongs to.
337+
/// - reason: The flag reason.
336338
/// - completion: Called when the API call is finished. Called with `Error` if the remote update fails.
337339
///
338-
func flagMessage(_ flag: Bool, with messageId: MessageId, in cid: ChannelId, completion: ((Error?) -> Void)? = nil) {
340+
func flagMessage(
341+
_ flag: Bool,
342+
with messageId: MessageId,
343+
in cid: ChannelId,
344+
reason: String? = nil,
345+
completion: ((Error?) -> Void)? = nil
346+
) {
339347
fetchAndSaveMessageIfNeeded(messageId, cid: cid) { error in
340348
guard error == nil else {
341349
completion?(error)
342350
return
343351
}
344352

345-
let endpoint: Endpoint<FlagMessagePayload> = .flagMessage(flag, with: messageId)
353+
let endpoint: Endpoint<FlagMessagePayload> = .flagMessage(flag, with: messageId, reason: reason)
346354
self.apiClient.request(endpoint: endpoint) { result in
347355
switch result {
348356
case let .success(payload):

TestTools/StreamChatTestTools/Mocks/StreamChat/Workers/MessageUpdater_Mock.swift

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ final class MessageUpdater_Mock: MessageUpdater {
5353
@Atomic var flagMessage_flag: Bool?
5454
@Atomic var flagMessage_messageId: MessageId?
5555
@Atomic var flagMessage_cid: ChannelId?
56+
@Atomic var flagMessage_reason: String?
5657
@Atomic var flagMessage_completion: ((Error?) -> Void)?
5758

5859
@Atomic var addReaction_type: MessageReactionType?
@@ -141,6 +142,7 @@ final class MessageUpdater_Mock: MessageUpdater {
141142
flagMessage_flag = nil
142143
flagMessage_messageId = nil
143144
flagMessage_cid = nil
145+
flagMessage_reason = nil
144146
flagMessage_completion = nil
145147

146148
addReaction_type = nil
@@ -280,10 +282,17 @@ final class MessageUpdater_Mock: MessageUpdater {
280282
}
281283
}
282284

283-
override func flagMessage(_ flag: Bool, with messageId: MessageId, in cid: ChannelId, completion: ((Error?) -> Void)? = nil) {
285+
override func flagMessage(
286+
_ flag: Bool,
287+
with messageId: MessageId,
288+
in cid: ChannelId,
289+
reason: String? = nil,
290+
completion: ((Error?) -> Void)? = nil
291+
) {
284292
flagMessage_flag = flag
285293
flagMessage_messageId = messageId
286294
flagMessage_cid = cid
295+
flagMessage_reason = reason
287296
flagMessage_completion = completion
288297
}
289298

Tests/StreamChatTests/APIClient/Endpoints/ModerationEndpoints_Tests.swift

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -136,17 +136,21 @@ final class ModerationEndpoints_Tests: XCTestCase {
136136

137137
for (flag, path) in testCases {
138138
let messageId: MessageId = .unique
139+
let reason = "Test"
139140

140141
let expectedEndpoint = Endpoint<FlagMessagePayload>(
141142
path: path,
142143
method: .post,
143144
queryItems: nil,
144145
requiresConnectionId: false,
145-
body: ["target_message_id": messageId]
146+
body: [
147+
"target_message_id": messageId,
148+
"reason": reason
149+
]
146150
)
147151

148152
// Build endpoint.
149-
let endpoint: Endpoint<FlagMessagePayload> = .flagMessage(flag, with: messageId)
153+
let endpoint: Endpoint<FlagMessagePayload> = .flagMessage(flag, with: messageId, reason: reason)
150154

151155
// Assert endpoint is built correctly.
152156
XCTAssertEqual(AnyEndpoint(expectedEndpoint), AnyEndpoint(endpoint))

Tests/StreamChatTests/Controllers/MessageController/MessageController_Tests.swift

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1011,14 +1011,17 @@ final class MessageController_Tests: XCTestCase {
10111011

10121012
func test_flag_callsUpdater_withCorrectValues() {
10131013
// Simulate `flag` call.
1014-
controller.flag()
1014+
let reason = "Test"
1015+
controller.flag(reason: reason)
10151016

10161017
// Assert updater is called with correct `flag`.
10171018
XCTAssertEqual(env.messageUpdater.flagMessage_flag, true)
10181019
// Assert updater is called with correct `messageId`.
10191020
XCTAssertEqual(env.messageUpdater.flagMessage_messageId, controller.messageId)
10201021
// Assert updater is called with correct `cid`.
10211022
XCTAssertEqual(env.messageUpdater.flagMessage_cid, controller.cid)
1023+
// Assert updater is called with correct `reason`.
1024+
XCTAssertEqual(env.messageUpdater.flagMessage_reason, reason)
10221025
}
10231026

10241027
func test_flag_keepsControllerAlive() {
@@ -1090,6 +1093,8 @@ final class MessageController_Tests: XCTestCase {
10901093
XCTAssertEqual(env.messageUpdater.flagMessage_messageId, controller.messageId)
10911094
// Assert updater is called with correct `cid`.
10921095
XCTAssertEqual(env.messageUpdater.flagMessage_cid, controller.cid)
1096+
// Assert updater is called with correct `reason`.
1097+
XCTAssertEqual(env.messageUpdater.flagMessage_reason, nil)
10931098
}
10941099

10951100
// MARK: - Create new reply

Tests/StreamChatTests/Workers/MessageUpdater_Tests.swift

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1207,6 +1207,7 @@ final class MessageUpdater_Tests: XCTestCase {
12071207
let currentUserId: UserId = .unique
12081208
let messageId: MessageId = .unique
12091209
let cid: ChannelId = .unique
1210+
let reason = "Test"
12101211

12111212
// Create current user in the database
12121213
try database.createCurrentUser(id: currentUserId)
@@ -1222,13 +1223,13 @@ final class MessageUpdater_Tests: XCTestCase {
12221223

12231224
// Simulate `flagMessage` call.
12241225
let expectation = self.expectation(description: "Flag message completion")
1225-
messageUpdater.flagMessage(true, with: messageId, in: cid) { error in
1226+
messageUpdater.flagMessage(true, with: messageId, in: cid, reason: reason) { error in
12261227
XCTAssertNil(error)
12271228
expectation.fulfill()
12281229
}
12291230

12301231
// Assert flag endpoint is called.
1231-
let flagEndpoint: Endpoint<FlagMessagePayload> = .flagMessage(true, with: messageId)
1232+
let flagEndpoint: Endpoint<FlagMessagePayload> = .flagMessage(true, with: messageId, reason: reason)
12321233
AssertAsync.willBeEqual(apiClient.request_endpoint, AnyEndpoint(flagEndpoint))
12331234

12341235
// Add it to DB as it is as expected after a successful getMessage call
@@ -1290,13 +1291,14 @@ final class MessageUpdater_Tests: XCTestCase {
12901291
func test_flagMessage_propagatesError() {
12911292
let messageId: MessageId = .unique
12921293
let cid: ChannelId = .unique
1294+
let reason = "Test"
12931295

12941296
let networkError = TestError()
12951297
messageRepository.getMessageResult = .failure(networkError)
12961298

12971299
// Simulate `flagMessage` call and catch the error.
12981300
var completionCalledError: Error?
1299-
messageUpdater.flagMessage(true, with: messageId, in: cid) {
1301+
messageUpdater.flagMessage(true, with: messageId, in: cid, reason: reason) {
13001302
completionCalledError = $0
13011303
}
13021304

@@ -1307,18 +1309,19 @@ final class MessageUpdater_Tests: XCTestCase {
13071309
func test_flagMessage_propagatesFlagNetworkError() throws {
13081310
let messageId: MessageId = .unique
13091311
let cid: ChannelId = .unique
1312+
let reason = "Test"
13101313

13111314
// Save message to the database.
13121315
try database.createMessage(id: messageId)
13131316

13141317
// Simulate `flagMessage` call and catch the error.
13151318
var completionCalledError: Error?
1316-
messageUpdater.flagMessage(true, with: messageId, in: cid) {
1319+
messageUpdater.flagMessage(true, with: messageId, in: cid, reason: reason) {
13171320
completionCalledError = $0
13181321
}
13191322

13201323
// Assert flag endpoint is called.
1321-
let flagEndpoint: Endpoint<FlagMessagePayload> = .flagMessage(true, with: messageId)
1324+
let flagEndpoint: Endpoint<FlagMessagePayload> = .flagMessage(true, with: messageId, reason: reason)
13221325
AssertAsync.willBeEqual(apiClient.request_endpoint, AnyEndpoint(flagEndpoint))
13231326

13241327
// Simulate flag API response with failure.
@@ -1333,6 +1336,7 @@ final class MessageUpdater_Tests: XCTestCase {
13331336
let currentUserId: UserId = .unique
13341337
let messageId: MessageId = .unique
13351338
let cid: ChannelId = .unique
1339+
let reason = "Test"
13361340

13371341
// Save message to the database.
13381342
try database.createMessage(id: messageId)
@@ -1343,12 +1347,12 @@ final class MessageUpdater_Tests: XCTestCase {
13431347

13441348
// Simulate `flagMessage` call and catch the error.
13451349
var completionCalledError: Error?
1346-
messageUpdater.flagMessage(true, with: messageId, in: cid) {
1350+
messageUpdater.flagMessage(true, with: messageId, in: cid, reason: reason) {
13471351
completionCalledError = $0
13481352
}
13491353

13501354
// Assert flag endpoint is called.
1351-
let flagEndpoint: Endpoint<FlagMessagePayload> = .flagMessage(true, with: messageId)
1355+
let flagEndpoint: Endpoint<FlagMessagePayload> = .flagMessage(true, with: messageId, reason: reason)
13521356
AssertAsync.willBeEqual(apiClient.request_endpoint, AnyEndpoint(flagEndpoint))
13531357

13541358
// Simulate flag API response with success.
@@ -1366,18 +1370,19 @@ final class MessageUpdater_Tests: XCTestCase {
13661370
let currentUserId: UserId = .unique
13671371
let messageId: MessageId = .unique
13681372
let cid: ChannelId = .unique
1373+
let reason = "Test"
13691374

13701375
// Save message to the database.
13711376
try database.createMessage(id: messageId)
13721377

13731378
// Simulate `flagMessage` call and catch the error.
13741379
var completionCalledError: Error?
1375-
messageUpdater.flagMessage(true, with: messageId, in: cid) {
1380+
messageUpdater.flagMessage(true, with: messageId, in: cid, reason: reason) {
13761381
completionCalledError = $0
13771382
}
13781383

13791384
// Assert flag endpoint is called.
1380-
let flagEndpoint: Endpoint<FlagMessagePayload> = .flagMessage(true, with: messageId)
1385+
let flagEndpoint: Endpoint<FlagMessagePayload> = .flagMessage(true, with: messageId, reason: reason)
13811386
AssertAsync.willBeEqual(apiClient.request_endpoint, AnyEndpoint(flagEndpoint))
13821387

13831388
// Delete the message from the database.

0 commit comments

Comments
 (0)