Skip to content

Commit 014c952

Browse files
authored
Merge pull request #334 from zapcannon87/developer
feat(IM): last delivered & read date property, fetch receipt timestamp function for conversation
2 parents 336b738 + 9a58489 commit 014c952

File tree

3 files changed

+212
-132
lines changed

3 files changed

+212
-132
lines changed

LeanCloudTests/IMMessageTestCase.swift

Lines changed: 28 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1144,29 +1144,37 @@ class IMMessageTestCase: RTMBaseTestCase {
11441144
delay()
11451145

11461146
let getReadFlagExp = expectation(description: "get read flag timestamp")
1147-
((try? sendingTuple?.conversation.getMessageReceiptFlag(completion: { (result) in
1148-
XCTAssertTrue(Thread.isMainThread)
1149-
XCTAssertTrue(result.isSuccess)
1150-
XCTAssertNil(result.error)
1151-
XCTAssertNotNil(result.value?.readFlagTimestamp)
1152-
XCTAssertNotNil(result.value?.readFlagTimestamp)
1153-
XCTAssertEqual(result.value?.readFlagTimestamp, result.value?.deliveredFlagTimestamp)
1154-
XCTAssertEqual(result.value?.readFlagDate, result.value?.deliveredFlagDate)
1155-
XCTAssertGreaterThan(result.value?.readFlagTimestamp ?? 0, message.sentTimestamp ?? 0)
1156-
getReadFlagExp.fulfill()
1157-
})) as ()??)
1147+
getReadFlagExp.expectedFulfillmentCount = 2
1148+
sendingTuple?.delegator.conversationEvent = { client, conv, event in
1149+
switch event {
1150+
case .lastDeliveredAtUpdated:
1151+
XCTAssertNotNil(conv.lastDeliveredAt)
1152+
getReadFlagExp.fulfill()
1153+
case .lastReadAtUpdated:
1154+
XCTAssertNotNil(conv.lastReadAt)
1155+
getReadFlagExp.fulfill()
1156+
default:
1157+
break
1158+
}
1159+
}
1160+
try! sendingTuple?.conversation.fetchReceiptTimestamps()
11581161
wait(for: [getReadFlagExp], timeout: timeout)
11591162

11601163
let sendNeedRCPMessageExp = expectation(description: "send need RCP message")
1161-
sendNeedRCPMessageExp.expectedFulfillmentCount = 3
1162-
sendingTuple?.delegator.messageEvent = { client, conv, event in
1163-
if conv === sendingTuple?.conversation {
1164-
switch event {
1164+
sendNeedRCPMessageExp.expectedFulfillmentCount = 4
1165+
sendingTuple?.delegator.conversationEvent = { client, conv, event in
1166+
switch event {
1167+
case .lastDeliveredAtUpdated:
1168+
sendNeedRCPMessageExp.fulfill()
1169+
case .message(event: let messageEvent):
1170+
switch messageEvent {
11651171
case .delivered(toClientID: _, messageID: _, deliveredTimestamp: _):
11661172
sendNeedRCPMessageExp.fulfill()
11671173
default:
11681174
break
11691175
}
1176+
default:
1177+
break
11701178
}
11711179
}
11721180
receivingTuple?.delegator.conversationEvent = { client, conv, event in
@@ -1180,18 +1188,18 @@ class IMMessageTestCase: RTMBaseTestCase {
11801188
}
11811189
}
11821190
let needRCPMessage = IMMessage()
1183-
try? needRCPMessage.set(content: .string("test"))
1184-
((try? sendingTuple?.conversation.send(message: needRCPMessage, options: [.needReceipt], completion: { (result) in
1191+
try! needRCPMessage.set(content: .string("test"))
1192+
try! sendingTuple?.conversation.send(message: needRCPMessage, options: [.needReceipt], completion: { (result) in
11851193
XCTAssertTrue(result.isSuccess)
11861194
XCTAssertNil(result.error)
11871195
sendNeedRCPMessageExp.fulfill()
1188-
})) as ()??)
1196+
})
11891197
wait(for: [sendNeedRCPMessageExp], timeout: timeout)
11901198

11911199
delay()
11921200

11931201
let getDeliveredFlagExp = expectation(description: "get delivered flag timestamp")
1194-
((try? sendingTuple?.conversation.getMessageReceiptFlag(completion: { (result) in
1202+
try! sendingTuple?.conversation.getMessageReceiptFlag(completion: { (result) in
11951203
XCTAssertTrue(Thread.isMainThread)
11961204
XCTAssertTrue(result.isSuccess)
11971205
XCTAssertNil(result.error)
@@ -1201,7 +1209,7 @@ class IMMessageTestCase: RTMBaseTestCase {
12011209
XCTAssertNotEqual(result.value?.deliveredFlagDate, result.value?.readFlagDate)
12021210
XCTAssertGreaterThanOrEqual(result.value?.deliveredFlagTimestamp ?? 0, needRCPMessage.sentTimestamp ?? 0)
12031211
getDeliveredFlagExp.fulfill()
1204-
})) as ()??)
1212+
})
12051213
wait(for: [getDeliveredFlagExp], timeout: timeout)
12061214

12071215
let client = try! IMClient(ID: uuid, options: [])

Sources/RTM/IMClient.swift

Lines changed: 58 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -2202,36 +2202,42 @@ extension IMClient {
22022202

22032203
func process(rcpCommand: IMRcpCommand, serverTimestamp: Int64?) {
22042204
assert(self.specificAssertion)
2205-
guard let conversationID: String = (rcpCommand.hasCid ? rcpCommand.cid : nil) else {
2205+
guard let conversationID = (rcpCommand.hasCid ? rcpCommand.cid : nil) else {
22062206
return
22072207
}
22082208
self.getConversation(by: conversationID) { (client, result) in
22092209
assert(client.specificAssertion)
22102210
switch result {
22112211
case .success(value: let conversation):
2212-
guard
2213-
let messageID: String = (rcpCommand.hasID ? rcpCommand.id : nil),
2214-
let timestamp: Int64 = (rcpCommand.hasT ? rcpCommand.t : nil)
2215-
else
2216-
{ return }
2212+
guard let messageID = (rcpCommand.hasID ? rcpCommand.id : nil),
2213+
let timestamp = (rcpCommand.hasT ? rcpCommand.t : nil) else {
2214+
return
2215+
}
22172216
let fromID = (rcpCommand.hasFrom ? rcpCommand.from : nil)
2218-
let event: IMMessageEvent
2219-
if rcpCommand.hasRead, rcpCommand.read {
2220-
event = .read(
2217+
let isRead = (rcpCommand.hasRead ? rcpCommand.read : false)
2218+
let messageEvent: IMMessageEvent
2219+
if isRead {
2220+
messageEvent = .read(
22212221
byClientID: fromID,
22222222
messageID: messageID,
2223-
readTimestamp: timestamp
2224-
)
2223+
readTimestamp: timestamp)
22252224
} else {
2226-
event = .delivered(
2225+
messageEvent = .delivered(
22272226
toClientID: fromID,
22282227
messageID: messageID,
2229-
deliveredTimestamp: timestamp
2230-
)
2228+
deliveredTimestamp: timestamp)
22312229
}
2232-
client.localRecord.update(lastServerTimestamp: serverTimestamp)
2230+
client.localRecord.update(
2231+
lastServerTimestamp: serverTimestamp)
2232+
conversation.process(
2233+
rcpTimestamp: timestamp,
2234+
isRead: isRead,
2235+
client: client)
22332236
client.eventQueue.async {
2234-
client.delegate?.client(client, conversation: conversation, event: .message(event: event))
2237+
client.delegate?.client(
2238+
client, conversation: conversation,
2239+
event: .message(
2240+
event: messageEvent))
22352241
}
22362242
case .failure(error: let error):
22372243
Logger.shared.error(error)
@@ -2514,104 +2520,86 @@ extension IMClient: RTMConnectionDelegate {
25142520
// MARK: - Event
25152521

25162522
/// The session event about the client.
2517-
///
2518-
/// - sessionDidOpen: Session opened event.
2519-
/// - sessionDidResume: Session in resuming event.
2520-
/// - sessionDidPause: Session paused event.
2521-
/// - sessionDidClose: Session closed event.
25222523
public enum IMClientEvent {
2523-
2524+
/// Session opened event.
25242525
case sessionDidOpen
2525-
2526+
/// Session in resuming event.
25262527
case sessionDidResume
2527-
2528+
/// Session paused event.
25282529
case sessionDidPause(error: LCError)
2529-
2530+
/// Session closed event.
25302531
case sessionDidClose(error: LCError)
25312532
}
25322533

2533-
/// The event about the conversation that belong to the client.
2534-
///
2535-
/// - joined: The client joined the conversation.
2536-
/// - left: The client left the conversation.
2537-
/// - membersJoined: The members joined the conversation.
2538-
/// - membersLeft: The members left the conversation.
2539-
/// - memberInfoChanged: The info of the member in the conversaiton has changed.
2540-
/// - blocked: The client has been blocked in the conversation.
2541-
/// - unblocked: The client has been unblocked int the conversation.
2542-
/// - membersBlocked: The members have been blocked in the conversation.
2543-
/// - membersUnblocked: The members have been unblocked in the conversation.
2544-
/// - muted: The client has been muted in the conversation.
2545-
/// - unmuted: The client has been unmuted in the conversation.
2546-
/// - membersMuted: The members have been muted in the conversation.
2547-
/// - membersUnmuted: The members have been unmuted in the conversation.
2548-
/// - dataUpdated: The data of the conversation updated.
2549-
/// - lastMessageUpdated: The last message of the conversation updated.
2550-
/// - unreadMessageCountUpdated: The unread message count of the conversation updated.
2551-
/// - message: Events about message in the conversation.
2534+
/// The events about conversation that belong to the client.
25522535
public enum IMConversationEvent {
2553-
2536+
/// This client joined this conversation.
25542537
case joined(byClientID: String?, at: Date?)
2538+
/// This client left this conversation.
25552539
case left(byClientID: String?, at: Date?)
2540+
/// The members joined this conversation.
25562541
case membersJoined(members: [String], byClientID: String?, at: Date?)
2542+
/// The members left this conversation.
25572543
case membersLeft(members: [String], byClientID: String?, at: Date?)
2558-
2544+
/// The info of the member in this conversaiton has been changed.
25592545
case memberInfoChanged(info: IMConversation.MemberInfo, byClientID: String?, at: Date?)
2560-
2546+
/// The client in this conversation has been blocked.
25612547
case blocked(byClientID: String?, at: Date?)
2548+
/// The client int this conversation has been unblocked.
25622549
case unblocked(byClientID: String?, at: Date?)
2550+
/// The members in this conversation have been blocked.
25632551
case membersBlocked(members: [String], byClientID: String?, at: Date?)
2552+
/// The members in this conversation have been unblocked.
25642553
case membersUnblocked(members: [String], byClientID: String?, at: Date?)
2565-
2554+
/// The client in this conversation has been muted.
25662555
case muted(byClientID: String?, at: Date?)
2556+
/// The client in this conversation has been unmuted.
25672557
case unmuted(byClientID: String?, at: Date?)
2558+
/// The members in this conversation have been muted.
25682559
case membersMuted(members: [String], byClientID: String?, at: Date?)
2560+
/// The members in this conversation have been unmuted.
25692561
case membersUnmuted(members: [String], byClientID: String?, at: Date?)
2570-
2562+
/// The data of this conversation has been updated.
25712563
case dataUpdated(updatingData: [String: Any]?, updatedData: [String: Any]?, byClientID: String?, at: Date?)
2572-
2564+
/// The last message of this conversation has been updated, if *newMessage* is *false*, means the message has been modified.
25732565
case lastMessageUpdated(newMessage: Bool)
2574-
2566+
/// The last delivered time of message to other in this conversation has been updated.
2567+
case lastDeliveredAtUpdated
2568+
/// The last read time of message by other in this conversation has been updated.
2569+
case lastReadAtUpdated
2570+
/// The unread message count for this client in this conversation has been updated.
25752571
case unreadMessageCountUpdated
2576-
2572+
/// The events about message that belong to this conversation, @see `IMMessageEvent`.
25772573
case message(event: IMMessageEvent)
25782574
}
25792575

2580-
/// The event about the message that belong to the conversation.
2581-
///
2582-
/// - received: The client received message from the conversation.
2583-
/// - updated: The message in the conversation has been updated.
2584-
/// - delivered: The message sent to the conversation by the client has delivered to other.
2585-
/// - read: The message sent to the conversation by the client has been read by other.
2576+
/// The events about message that belong to the conversation.
25862577
public enum IMMessageEvent {
2587-
2578+
/// The new message received from this conversation.
25882579
case received(message: IMMessage)
2589-
2580+
/// The message in this conversation has been updated.
25902581
case updated(updatedMessage: IMMessage, reason: IMMessage.PatchedReason?)
2591-
2582+
/// The message has been delivered to other.
25922583
case delivered(toClientID: String?, messageID: String, deliveredTimestamp: Int64)
2593-
2584+
/// The message sent to other has been read.
25942585
case read(byClientID: String?, messageID: String, readTimestamp: Int64)
25952586
}
25962587

25972588
/// IM Client Delegate
25982589
public protocol IMClientDelegate: class {
25992590

26002591
/// Delegate function of the event about the client.
2601-
///
26022592
/// - Parameters:
2603-
/// - client: Which the event belong to.
2604-
/// - event: @see `IMClientEvent`
2593+
/// - client: Which the *event* belong to.
2594+
/// - event: Belong to the *client*, @see `IMClientEvent`.
26052595
func client(_ client: IMClient, event: IMClientEvent)
26062596

26072597
/// Delegate function of the event about the conversation.
2608-
///
26092598
/// - Parameters:
2610-
/// - client: Which the conversation belong to.
2611-
/// - conversation: Which the event belong to.
2612-
/// - event: @see `IMConversationEvent`
2599+
/// - client: Which the *conversation* belong to.
2600+
/// - conversation: Which the *event* belong to.
2601+
/// - event: Belong to the *conversation*, @see `IMConversationEvent`.
26132602
func client(_ client: IMClient, conversation: IMConversation, event: IMConversationEvent)
2614-
26152603
}
26162604

26172605
// MARK: - Signature
@@ -2626,7 +2614,6 @@ public protocol IMSignatureDelegate: class {
26262614
/// - action: @see `IMSignature.Action`.
26272615
/// - signatureHandler: The handler for the signature.
26282616
func client(_ client: IMClient, action: IMSignature.Action, signatureHandler: @escaping (IMClient, IMSignature?) -> Void)
2629-
26302617
}
26312618

26322619
public struct IMSignature {

0 commit comments

Comments
 (0)