Skip to content

Commit ba15125

Browse files
committed
feat(IM): padding raw data of conversation after creation
1 parent 0f45668 commit ba15125

File tree

2 files changed

+42
-43
lines changed

2 files changed

+42
-43
lines changed

Sources/RTM/IMClient.swift

Lines changed: 40 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -537,14 +537,14 @@ extension IMClient {
537537
extension IMClient {
538538
// MARK: Create Conversation
539539

540-
/// Create a Normal Conversation. Default is a Unique Conversation.
540+
/// Create a Normal Conversation. Default is a Normal Unique Conversation.
541541
///
542542
/// - Parameters:
543543
/// - clientIDs: The set of client ID. it's the members of the conversation which will be created. the initialized members always contains current client's ID. if the created conversation is unique, and server has one unique conversation with the same members, that unique conversation will be returned.
544544
/// - name: The name of the conversation.
545545
/// - attributes: The attributes of the conversation.
546546
/// - isUnique: True means create or get a unique conversation, default is true.
547-
/// - completion: callback.
547+
/// - completion: Result callback.
548548
public func createConversation(
549549
clientIDs: Set<String>,
550550
name: String? = nil,
@@ -558,16 +558,15 @@ extension IMClient {
558558
name: name,
559559
attributes: attributes,
560560
option: (isUnique ? .normalAndUnique : .normal),
561-
completion: completion
562-
)
561+
completion: completion)
563562
}
564563

565564
/// Create a Chat Room.
566565
///
567566
/// - Parameters:
568567
/// - name: The name of the chat room.
569568
/// - attributes: The attributes of the chat room.
570-
/// - completion: callback.
569+
/// - completion: Result callback.
571570
public func createChatRoom(
572571
name: String? = nil,
573572
attributes: [String: Any]? = nil,
@@ -579,16 +578,15 @@ extension IMClient {
579578
name: name,
580579
attributes: attributes,
581580
option: .transient,
582-
completion: completion
583-
)
581+
completion: completion)
584582
}
585583

586584
/// Create a Temporary Conversation. Temporary Conversation is unique in it's Life Cycle.
587585
///
588586
/// - Parameters:
589587
/// - clientIDs: The set of client ID. it's the members of the conversation which will be created. the initialized members always contains this client's ID.
590588
/// - timeToLive: The time interval for the life of the temporary conversation.
591-
/// - completion: callback.
589+
/// - completion: Result callback.
592590
public func createTemporaryConversation(
593591
clientIDs: Set<String>,
594592
timeToLive: Int32,
@@ -598,8 +596,7 @@ extension IMClient {
598596
try self.createConversation(
599597
clientIDs: clientIDs,
600598
option: .temporary(ttl: timeToLive),
601-
completion: completion
602-
)
599+
completion: completion)
603600
}
604601

605602
enum ConversationCreationOption {
@@ -654,8 +651,7 @@ extension IMClient {
654651
name: String?,
655652
attributes: [String: Any]?,
656653
option: ConversationCreationOption)
657-
throws
658-
-> ConversationCreationTuple
654+
throws -> ConversationCreationTuple
659655
{
660656
var members: [String]
661657
if option.isTransient {
@@ -671,20 +667,18 @@ extension IMClient {
671667
members.append(self.ID)
672668
}
673669
}
674-
675670
var attr: [String: Any] = [:]
676-
if let name: String = name {
671+
if let name = name {
677672
attr[IMConversation.Key.name.rawValue] = name
678673
}
679-
if let attributes: [String: Any] = attributes {
674+
if let attributes = attributes {
680675
attr[IMConversation.Key.attributes.rawValue] = attributes
681676
}
682-
var attrString: String? = nil
677+
var attrString: String?
683678
if !attr.isEmpty {
684679
let data = try JSONSerialization.data(withJSONObject: attr)
685680
attrString = String(data: data, encoding: .utf8)
686681
}
687-
688682
return (members, attrString, option)
689683
}
690684

@@ -764,7 +758,6 @@ extension IMClient {
764758
name: name,
765759
attributes: attributes,
766760
option: option)
767-
768761
let sendingClosure: (IMClient, IMGenericCommand) -> Void = { (client, outCommand) in
769762
client.sendCommand(constructor: { outCommand }) { (client, result) in
770763
switch result {
@@ -790,7 +783,6 @@ extension IMClient {
790783
}
791784
}
792785
}
793-
794786
if option.isTemporary {
795787
sendingClosure(self, self.newConvStartCommand(
796788
tuple: tuple))
@@ -808,63 +800,68 @@ extension IMClient {
808800
{
809801
assert(self.specificAssertion)
810802
guard let convMessage = (inCommand.hasConvMessage ? inCommand.convMessage : nil),
811-
let convID = (convMessage.hasCid ? convMessage.cid : nil) else {
803+
let conversationID = (convMessage.hasCid ? convMessage.cid : nil) else {
812804
throw LCError(
813805
code: .commandInvalid,
814806
userInfo: ["command": "\(inCommand)"])
815807
}
816-
var attr: [String: Any] = [:]
817-
if let json: [String: Any] = try tuple
818-
.attrString?.jsonObject() {
819-
attr = json
808+
var attributes: [String: Any] = [:]
809+
if let attrObject: [String: Any] = try tuple.attrString?.jsonObject() {
810+
attributes = attrObject
820811
}
821812
let conversation: IMConversation
822-
if let conv = self.convCollection[convID] {
823-
conv.safeExecuting(
813+
if let existConversation = self.convCollection[conversationID] {
814+
existConversation.safeExecuting(
824815
operation: .rawDataMerging(
825-
data: attr),
816+
data: attributes),
826817
client: self)
827818
#if canImport(GRDB)
828-
if conv.isUnique {
829-
conv.tryUpdateLocalStorageData(
819+
if existConversation.isUnique {
820+
existConversation.tryUpdateLocalStorageData(
830821
client: self,
831-
rawData: conv.rawData)
822+
rawData: existConversation.rawData)
832823
}
833824
#endif
834-
conversation = conv
825+
conversation = existConversation
835826
} else {
836827
let key = IMConversation.Key.self
837-
attr[key.convType.rawValue] = tuple.option.convType.rawValue
838-
attr[key.creator.rawValue] = self.ID
839-
if !tuple.option.isTransient {
840-
attr[key.members.rawValue] = tuple.members
828+
attributes[key.objectId.rawValue] = conversationID
829+
attributes[key.convType.rawValue] = tuple.option.convType.rawValue
830+
attributes[key.creator.rawValue] = self.ID
831+
if tuple.option.isTransient {
832+
attributes[key.transient.rawValue] = true
833+
} else {
834+
attributes[key.members.rawValue] = tuple.members
841835
}
842836
if tuple.option.isUnique {
843-
attr[key.unique.rawValue] = true
837+
attributes[key.unique.rawValue] = true
844838
}
845839
if convMessage.hasCdate {
846-
attr[key.createdAt.rawValue] = convMessage.cdate
840+
attributes[key.createdAt.rawValue] = convMessage.cdate
847841
}
848842
if convMessage.hasUniqueID {
849-
attr[key.uniqueId.rawValue] = convMessage.uniqueID
843+
attributes[key.uniqueId.rawValue] = convMessage.uniqueID
844+
}
845+
if tuple.option.isTemporary {
846+
attributes[key.temporary.rawValue] = true
850847
}
851848
if convMessage.hasTempConvTtl {
852-
attr[key.temporaryTTL.rawValue] = convMessage.tempConvTtl
849+
attributes[key.temporaryTTL.rawValue] = convMessage.tempConvTtl
853850
}
854-
if let rawData: IMConversation.RawData = try attr.jsonObject() {
851+
if let rawData: IMConversation.RawData = try attributes.jsonObject() {
855852
conversation = IMConversation.instance(
856-
ID: convID,
853+
ID: conversationID,
857854
rawData: rawData,
858855
client: self,
859856
caching: true)
860857
} else {
861858
throw LCError(
862859
code: .malformedData,
863-
userInfo: ["data": attr])
860+
userInfo: ["data": attributes])
864861
}
865862
}
866863
if let conversation = conversation as? T {
867-
self.convCollection[convID] = conversation
864+
self.convCollection[conversationID] = conversation
868865
return conversation
869866
} else {
870867
throw LCError(

Sources/RTM/IMConversation.swift

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -200,6 +200,8 @@ public class IMConversation {
200200
convType = .temporary
201201
}
202202
}
203+
var rawData = rawData
204+
rawData[Key.convType.rawValue] = convType.rawValue
203205
switch convType {
204206
case .normal:
205207
return IMConversation(

0 commit comments

Comments
 (0)