Skip to content

Commit ed4371c

Browse files
authored
Merge pull request #209 from zapcannon87/master
release: 16.2.0
2 parents acba07e + d704cad commit ed4371c

File tree

7 files changed

+402
-52
lines changed

7 files changed

+402
-52
lines changed

LeanCloud.podspec

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
Pod::Spec.new do |s|
22
s.name = 'LeanCloud'
3-
s.version = '16.1.0'
3+
s.version = '16.2.0'
44
s.license = { :type => 'Apache License, Version 2.0', :file => 'LICENSE' }
55
s.summary = 'LeanCloud Swift SDK'
66
s.homepage = 'https://leancloud.cn/'

LeanCloudTests/LCUserTestCase.swift

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,5 +64,75 @@ class LCUserTestCase: BaseTestCase {
6464
let verificationCode = "375586"
6565
XCTAssertTrue(LCUser.signUpOrLogIn(mobilePhoneNumber: mobilePhoneNumber, verificationCode: verificationCode).isSuccess)
6666
}
67+
68+
func testAuthDataLogin() {
69+
let user = LCUser()
70+
let authData: [String: Any] = [
71+
"access_token": UUID().uuidString,
72+
"openid": UUID().uuidString
73+
]
74+
XCTAssertTrue(user.logIn(authData: authData, platform: .weixin).isSuccess)
75+
XCTAssertNotNil(user.authData)
76+
XCTAssertTrue(user.application.currentUser === user)
77+
}
78+
79+
func testAuthDataLoginWithUnionID() {
80+
let user = LCUser()
81+
let authData: [String: Any] = [
82+
"access_token": UUID().uuidString,
83+
"openid": UUID().uuidString
84+
]
85+
let unionID: String = UUID().uuidString
86+
XCTAssertTrue(user.logIn(authData: authData, platform: .custom(UUID().uuidString), unionID: unionID, unionIDPlatform: .weixin, options: [.mainAccount]).isSuccess)
87+
XCTAssertNotNil(user.authData)
88+
XCTAssertTrue(user.application.currentUser === user)
89+
}
90+
91+
func testAuthDataLoginFailOnNotExist() {
92+
let user = LCUser()
93+
let authData: [String: Any] = [
94+
"access_token": UUID().uuidString,
95+
"openid": UUID().uuidString
96+
]
97+
XCTAssertTrue(user.logIn(authData: authData, platform: .weixin, options: [.failOnNotExist]).isFailure)
98+
}
99+
100+
func testAuthDataAssociate() {
101+
let user = LCUser()
102+
user.username = UUID().uuidString.lcString
103+
user.password = UUID().uuidString.lcString
104+
XCTAssertTrue(user.signUp().isSuccess)
105+
106+
let authData: [String: Any] = [
107+
"access_token": UUID().uuidString,
108+
"openid": UUID().uuidString
109+
]
110+
do {
111+
let result = try user.associate(authData: authData, platform: .weixin)
112+
XCTAssertTrue(result.isSuccess)
113+
XCTAssertNotNil(user.authData)
114+
} catch {
115+
XCTFail("\(error)")
116+
}
117+
}
118+
119+
func testAuthDataDisassociate() {
120+
let user = LCUser()
121+
let authData: [String: Any] = [
122+
"access_token": UUID().uuidString,
123+
"openid": UUID().uuidString
124+
]
125+
XCTAssertTrue(user.logIn(authData: authData, platform: .weixin).isSuccess)
126+
XCTAssertNotNil(user.authData)
127+
XCTAssertTrue(user.application.currentUser === user)
128+
129+
do {
130+
let result = try user.disassociate(authData: .weixin)
131+
XCTAssertTrue(result.isSuccess)
132+
XCTAssertTrue((user.authData ?? [:]).isEmpty)
133+
} catch {
134+
XCTFail("\(error)")
135+
}
136+
}
67137

68138
}

Sources/IM/IMConversation.swift

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1035,7 +1035,7 @@ extension IMConversation {
10351035
)
10361036
}
10371037
var underlyingPolicy: MessageQueryPolicy = policy
1038-
if let _ = type {
1038+
if [ConvType.transient, ConvType.temporary].contains(self.convType) || type != nil {
10391039
underlyingPolicy = .onlyNetwork
10401040
} else {
10411041
if underlyingPolicy == .default {
@@ -1136,7 +1136,10 @@ extension IMConversation {
11361136
assert(client.specificAssertion)
11371137
do {
11381138
let messages = try self.handleMessageQueryResult(command: inCommand, client: client)
1139-
if let localStorage = client.localStorage {
1139+
if
1140+
[ConvType.normal, ConvType.system].contains(self.convType),
1141+
let localStorage = client.localStorage
1142+
{
11401143
localStorage.insertOrReplace(messages: messages)
11411144
}
11421145
completion(client, .success(value: messages))

Sources/IM/IMConversationQuery.swift

Lines changed: 36 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -186,14 +186,11 @@ public class IMConversationQuery: LCQuery {
186186
/// - Parameters:
187187
/// - ID: The ID of the conversation.
188188
/// - completion: callback.
189-
public func getConversation<T: IMConversation>(
189+
public func getConversation(
190190
by ID: String,
191-
completion: @escaping (LCGenericResult<T>) -> Void)
191+
completion: @escaping (LCGenericResult<IMConversation>) -> Void)
192192
throws
193193
{
194-
if T.self == IMTemporaryConversation.self {
195-
throw LCError.conversationQueryTypeInvalid
196-
}
197194
try self.where(IMConversation.Key.objectId.rawValue, .equalTo(ID))
198195
let tuple = try self.whereAndSort()
199196
self.queryConversations(
@@ -203,7 +200,7 @@ public class IMConversationQuery: LCQuery {
203200
{ (result) in
204201
switch result {
205202
case .success(value: let conversations):
206-
if let conversation: T = conversations.first as? T {
203+
if let conversation = conversations.first {
207204
completion(.success(value: conversation))
208205
} else {
209206
let error = LCError(code: .conversationNotFound)
@@ -220,14 +217,11 @@ public class IMConversationQuery: LCQuery {
220217
/// - Parameters:
221218
/// - IDs: The set of ID string.
222219
/// - completion: callback.
223-
public func getConversations<T: IMConversation>(
220+
public func getConversations(
224221
by IDs: Set<String>,
225-
completion: @escaping (LCGenericResult<[T]>) -> Void)
222+
completion: @escaping (LCGenericResult<[IMConversation]>) -> Void)
226223
throws
227224
{
228-
if T.self == IMTemporaryConversation.self {
229-
throw LCError.conversationQueryTypeInvalid
230-
}
231225
guard IMConversationQuery.limitRangeOfQueryResult.contains(IDs.count) else {
232226
throw LCError.conversationQueryLimitInvalid
233227
}
@@ -257,9 +251,19 @@ public class IMConversationQuery: LCQuery {
257251
}
258252
self.queryConversations(
259253
limit: IDs.count,
260-
tempConvIDs: Array(IDs),
261-
completion: completion
262-
)
254+
tempConvIDs: Array(IDs))
255+
{ result in
256+
switch result {
257+
case .success(value: let conversations):
258+
if let tmpConversations = conversations as? [IMTemporaryConversation] {
259+
completion(.success(value: tmpConversations))
260+
} else {
261+
completion(.failure(error: LCError.conversationQueryTypeInvalid))
262+
}
263+
case .failure(error: let error):
264+
completion(.failure(error: error))
265+
}
266+
}
263267
}
264268

265269
/// General conversation query (not support temporary conversation query).
@@ -270,10 +274,7 @@ public class IMConversationQuery: LCQuery {
270274
/// The default skip is 0.
271275
///
272276
/// - Parameter completion: callback
273-
public func findConversations<T: IMConversation>(completion: @escaping (LCGenericResult<[T]>) -> Void) throws {
274-
if T.self == IMTemporaryConversation.self {
275-
throw LCError.conversationQueryTypeInvalid
276-
}
277+
public func findConversations(completion: @escaping (LCGenericResult<[IMConversation]>) -> Void) throws {
277278
if let limit: Int = self.limit {
278279
guard IMConversationQuery.limitRangeOfQueryResult.contains(limit) else {
279280
throw LCError.conversationQueryLimitInvalid
@@ -294,14 +295,14 @@ public class IMConversationQuery: LCQuery {
294295

295296
private extension IMConversationQuery {
296297

297-
func queryConversations<T: IMConversation>(
298+
func queryConversations(
298299
whereString: String? = nil,
299300
sortString: String? = nil,
300301
limit: Int? = nil,
301302
skip: Int? = nil,
302303
options: Options? = nil,
303304
tempConvIDs: [String]? = nil,
304-
completion: @escaping (LCGenericResult<[T]>) -> Void)
305+
completion: @escaping (LCGenericResult<[IMConversation]>) -> Void)
305306
{
306307
self.client?.sendCommand(constructor: { () -> IMGenericCommand in
307308
var outCommand = IMGenericCommand()
@@ -332,7 +333,7 @@ private extension IMConversationQuery {
332333
outCommand.convMessage = convCommand
333334
return outCommand
334335
}, completion: { (client, commandCallbackResult) in
335-
let callback: (LCGenericResult<[T]>) -> Void = { result in
336+
let callback: (LCGenericResult<[IMConversation]>) -> Void = { result in
336337
if let queue = self.eventQueue {
337338
queue.async { completion(result) }
338339
} else {
@@ -343,17 +344,14 @@ private extension IMConversationQuery {
343344
case .inCommand(let inCommand):
344345
assert(client.specificAssertion)
345346
do {
346-
let conversations: [T] = try self.conversations(command: inCommand, client: client)
347-
let result = LCGenericResult<[T]>.success(value: conversations)
348-
callback(result)
347+
let conversations = try self.conversations(command: inCommand, client: client)
348+
callback(.success(value: conversations))
349349
} catch {
350350
let error = LCError(error: error)
351-
let result = LCGenericResult<[T]>.failure(error: error)
352-
callback(result)
351+
callback(.failure(error: error))
353352
}
354353
case .error(let error):
355-
let result = LCGenericResult<[T]>.failure(error: error)
356-
callback(result)
354+
callback(.failure(error: error))
357355
}
358356
})
359357
}
@@ -372,17 +370,17 @@ private extension IMConversationQuery {
372370
return (whereString, sortString)
373371
}
374372

375-
func conversations<T: IMConversation>(command: IMGenericCommand, client: IMClient) throws -> [T] {
373+
func conversations(command: IMGenericCommand, client: IMClient) throws -> [IMConversation] {
376374
assert(client.specificAssertion)
377-
let convMessage: IMConvCommand? = (command.hasConvMessage ? command.convMessage : nil)
378-
let jsonMessage: IMJsonObjectMessage? = ((convMessage?.hasResults ?? false) ? convMessage?.results : nil)
379-
guard let jsonString: String = ((jsonMessage?.hasData ?? false) ? jsonMessage?.data : nil) else {
375+
guard
376+
let convMessage: IMConvCommand = (command.hasConvMessage ? command.convMessage : nil),
377+
let jsonMessage: IMJsonObjectMessage = (convMessage.hasResults ? convMessage.results : nil),
378+
let jsonString: String = (jsonMessage.hasData ? jsonMessage.data : nil),
379+
let rawDatas: [IMConversation.RawData] = try jsonString.jsonObject() else
380+
{
380381
throw LCError(code: .commandInvalid)
381382
}
382-
guard let rawDatas: [IMConversation.RawData] = try jsonString.jsonObject(), !rawDatas.isEmpty else {
383-
throw LCError(code: .conversationNotFound)
384-
}
385-
var conversations: [T] = []
383+
var conversations: [IMConversation] = []
386384
for rawData in rawDatas {
387385
guard let objectId: String = rawData[IMConversation.Key.objectId.rawValue] as? String else {
388386
throw LCError.conversationQueryObjectIDNotFound
@@ -395,13 +393,7 @@ private extension IMConversationQuery {
395393
instance = IMConversation.instance(ID: objectId, rawData: rawData, client: client, caching: true)
396394
client.convCollection[objectId] = instance
397395
}
398-
guard let conversation: T = instance as? T else {
399-
throw LCError(
400-
code: .invalidType,
401-
reason: "conversation<T: \(type(of: instance))> can't cast to type: \(T.self)"
402-
)
403-
}
404-
conversations.append(conversation)
396+
conversations.append(instance)
405397
}
406398
return conversations
407399
}
@@ -427,7 +419,7 @@ fileprivate extension LCError {
427419
static var conversationQueryTypeInvalid: LCError {
428420
return LCError(
429421
code: .invalidType,
430-
reason: "if result type is \(IMTemporaryConversation.self), should use Get-Temporary-Conversations API"
422+
reason: "The type of query result is invalid"
431423
)
432424
}
433425

0 commit comments

Comments
 (0)