Skip to content

Commit

Permalink
Merge pull request #209 from zapcannon87/master
Browse files Browse the repository at this point in the history
release: 16.2.0
  • Loading branch information
zapcannon87 authored Jul 1, 2019
2 parents acba07e + d704cad commit ed4371c
Show file tree
Hide file tree
Showing 7 changed files with 402 additions and 52 deletions.
2 changes: 1 addition & 1 deletion LeanCloud.podspec
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Pod::Spec.new do |s|
s.name = 'LeanCloud'
s.version = '16.1.0'
s.version = '16.2.0'
s.license = { :type => 'Apache License, Version 2.0', :file => 'LICENSE' }
s.summary = 'LeanCloud Swift SDK'
s.homepage = 'https://leancloud.cn/'
Expand Down
70 changes: 70 additions & 0 deletions LeanCloudTests/LCUserTestCase.swift
Original file line number Diff line number Diff line change
Expand Up @@ -64,5 +64,75 @@ class LCUserTestCase: BaseTestCase {
let verificationCode = "375586"
XCTAssertTrue(LCUser.signUpOrLogIn(mobilePhoneNumber: mobilePhoneNumber, verificationCode: verificationCode).isSuccess)
}

func testAuthDataLogin() {
let user = LCUser()
let authData: [String: Any] = [
"access_token": UUID().uuidString,
"openid": UUID().uuidString
]
XCTAssertTrue(user.logIn(authData: authData, platform: .weixin).isSuccess)
XCTAssertNotNil(user.authData)
XCTAssertTrue(user.application.currentUser === user)
}

func testAuthDataLoginWithUnionID() {
let user = LCUser()
let authData: [String: Any] = [
"access_token": UUID().uuidString,
"openid": UUID().uuidString
]
let unionID: String = UUID().uuidString
XCTAssertTrue(user.logIn(authData: authData, platform: .custom(UUID().uuidString), unionID: unionID, unionIDPlatform: .weixin, options: [.mainAccount]).isSuccess)
XCTAssertNotNil(user.authData)
XCTAssertTrue(user.application.currentUser === user)
}

func testAuthDataLoginFailOnNotExist() {
let user = LCUser()
let authData: [String: Any] = [
"access_token": UUID().uuidString,
"openid": UUID().uuidString
]
XCTAssertTrue(user.logIn(authData: authData, platform: .weixin, options: [.failOnNotExist]).isFailure)
}

func testAuthDataAssociate() {
let user = LCUser()
user.username = UUID().uuidString.lcString
user.password = UUID().uuidString.lcString
XCTAssertTrue(user.signUp().isSuccess)

let authData: [String: Any] = [
"access_token": UUID().uuidString,
"openid": UUID().uuidString
]
do {
let result = try user.associate(authData: authData, platform: .weixin)
XCTAssertTrue(result.isSuccess)
XCTAssertNotNil(user.authData)
} catch {
XCTFail("\(error)")
}
}

func testAuthDataDisassociate() {
let user = LCUser()
let authData: [String: Any] = [
"access_token": UUID().uuidString,
"openid": UUID().uuidString
]
XCTAssertTrue(user.logIn(authData: authData, platform: .weixin).isSuccess)
XCTAssertNotNil(user.authData)
XCTAssertTrue(user.application.currentUser === user)

do {
let result = try user.disassociate(authData: .weixin)
XCTAssertTrue(result.isSuccess)
XCTAssertTrue((user.authData ?? [:]).isEmpty)
} catch {
XCTFail("\(error)")
}
}

}
7 changes: 5 additions & 2 deletions Sources/IM/IMConversation.swift
Original file line number Diff line number Diff line change
Expand Up @@ -1035,7 +1035,7 @@ extension IMConversation {
)
}
var underlyingPolicy: MessageQueryPolicy = policy
if let _ = type {
if [ConvType.transient, ConvType.temporary].contains(self.convType) || type != nil {
underlyingPolicy = .onlyNetwork
} else {
if underlyingPolicy == .default {
Expand Down Expand Up @@ -1136,7 +1136,10 @@ extension IMConversation {
assert(client.specificAssertion)
do {
let messages = try self.handleMessageQueryResult(command: inCommand, client: client)
if let localStorage = client.localStorage {
if
[ConvType.normal, ConvType.system].contains(self.convType),
let localStorage = client.localStorage
{
localStorage.insertOrReplace(messages: messages)
}
completion(client, .success(value: messages))
Expand Down
80 changes: 36 additions & 44 deletions Sources/IM/IMConversationQuery.swift
Original file line number Diff line number Diff line change
Expand Up @@ -186,14 +186,11 @@ public class IMConversationQuery: LCQuery {
/// - Parameters:
/// - ID: The ID of the conversation.
/// - completion: callback.
public func getConversation<T: IMConversation>(
public func getConversation(
by ID: String,
completion: @escaping (LCGenericResult<T>) -> Void)
completion: @escaping (LCGenericResult<IMConversation>) -> Void)
throws
{
if T.self == IMTemporaryConversation.self {
throw LCError.conversationQueryTypeInvalid
}
try self.where(IMConversation.Key.objectId.rawValue, .equalTo(ID))
let tuple = try self.whereAndSort()
self.queryConversations(
Expand All @@ -203,7 +200,7 @@ public class IMConversationQuery: LCQuery {
{ (result) in
switch result {
case .success(value: let conversations):
if let conversation: T = conversations.first as? T {
if let conversation = conversations.first {
completion(.success(value: conversation))
} else {
let error = LCError(code: .conversationNotFound)
Expand All @@ -220,14 +217,11 @@ public class IMConversationQuery: LCQuery {
/// - Parameters:
/// - IDs: The set of ID string.
/// - completion: callback.
public func getConversations<T: IMConversation>(
public func getConversations(
by IDs: Set<String>,
completion: @escaping (LCGenericResult<[T]>) -> Void)
completion: @escaping (LCGenericResult<[IMConversation]>) -> Void)
throws
{
if T.self == IMTemporaryConversation.self {
throw LCError.conversationQueryTypeInvalid
}
guard IMConversationQuery.limitRangeOfQueryResult.contains(IDs.count) else {
throw LCError.conversationQueryLimitInvalid
}
Expand Down Expand Up @@ -257,9 +251,19 @@ public class IMConversationQuery: LCQuery {
}
self.queryConversations(
limit: IDs.count,
tempConvIDs: Array(IDs),
completion: completion
)
tempConvIDs: Array(IDs))
{ result in
switch result {
case .success(value: let conversations):
if let tmpConversations = conversations as? [IMTemporaryConversation] {
completion(.success(value: tmpConversations))
} else {
completion(.failure(error: LCError.conversationQueryTypeInvalid))
}
case .failure(error: let error):
completion(.failure(error: error))
}
}
}

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

private extension IMConversationQuery {

func queryConversations<T: IMConversation>(
func queryConversations(
whereString: String? = nil,
sortString: String? = nil,
limit: Int? = nil,
skip: Int? = nil,
options: Options? = nil,
tempConvIDs: [String]? = nil,
completion: @escaping (LCGenericResult<[T]>) -> Void)
completion: @escaping (LCGenericResult<[IMConversation]>) -> Void)
{
self.client?.sendCommand(constructor: { () -> IMGenericCommand in
var outCommand = IMGenericCommand()
Expand Down Expand Up @@ -332,7 +333,7 @@ private extension IMConversationQuery {
outCommand.convMessage = convCommand
return outCommand
}, completion: { (client, commandCallbackResult) in
let callback: (LCGenericResult<[T]>) -> Void = { result in
let callback: (LCGenericResult<[IMConversation]>) -> Void = { result in
if let queue = self.eventQueue {
queue.async { completion(result) }
} else {
Expand All @@ -343,17 +344,14 @@ private extension IMConversationQuery {
case .inCommand(let inCommand):
assert(client.specificAssertion)
do {
let conversations: [T] = try self.conversations(command: inCommand, client: client)
let result = LCGenericResult<[T]>.success(value: conversations)
callback(result)
let conversations = try self.conversations(command: inCommand, client: client)
callback(.success(value: conversations))
} catch {
let error = LCError(error: error)
let result = LCGenericResult<[T]>.failure(error: error)
callback(result)
callback(.failure(error: error))
}
case .error(let error):
let result = LCGenericResult<[T]>.failure(error: error)
callback(result)
callback(.failure(error: error))
}
})
}
Expand All @@ -372,17 +370,17 @@ private extension IMConversationQuery {
return (whereString, sortString)
}

func conversations<T: IMConversation>(command: IMGenericCommand, client: IMClient) throws -> [T] {
func conversations(command: IMGenericCommand, client: IMClient) throws -> [IMConversation] {
assert(client.specificAssertion)
let convMessage: IMConvCommand? = (command.hasConvMessage ? command.convMessage : nil)
let jsonMessage: IMJsonObjectMessage? = ((convMessage?.hasResults ?? false) ? convMessage?.results : nil)
guard let jsonString: String = ((jsonMessage?.hasData ?? false) ? jsonMessage?.data : nil) else {
guard
let convMessage: IMConvCommand = (command.hasConvMessage ? command.convMessage : nil),
let jsonMessage: IMJsonObjectMessage = (convMessage.hasResults ? convMessage.results : nil),
let jsonString: String = (jsonMessage.hasData ? jsonMessage.data : nil),
let rawDatas: [IMConversation.RawData] = try jsonString.jsonObject() else
{
throw LCError(code: .commandInvalid)
}
guard let rawDatas: [IMConversation.RawData] = try jsonString.jsonObject(), !rawDatas.isEmpty else {
throw LCError(code: .conversationNotFound)
}
var conversations: [T] = []
var conversations: [IMConversation] = []
for rawData in rawDatas {
guard let objectId: String = rawData[IMConversation.Key.objectId.rawValue] as? String else {
throw LCError.conversationQueryObjectIDNotFound
Expand All @@ -395,13 +393,7 @@ private extension IMConversationQuery {
instance = IMConversation.instance(ID: objectId, rawData: rawData, client: client, caching: true)
client.convCollection[objectId] = instance
}
guard let conversation: T = instance as? T else {
throw LCError(
code: .invalidType,
reason: "conversation<T: \(type(of: instance))> can't cast to type: \(T.self)"
)
}
conversations.append(conversation)
conversations.append(instance)
}
return conversations
}
Expand All @@ -427,7 +419,7 @@ fileprivate extension LCError {
static var conversationQueryTypeInvalid: LCError {
return LCError(
code: .invalidType,
reason: "if result type is \(IMTemporaryConversation.self), should use Get-Temporary-Conversations API"
reason: "The type of query result is invalid"
)
}

Expand Down
Loading

0 comments on commit ed4371c

Please sign in to comment.