Skip to content

Commit 189534d

Browse files
committed
feat: strictly find user
1 parent 3ba1cb1 commit 189534d

File tree

6 files changed

+93
-22
lines changed

6 files changed

+93
-22
lines changed

AVOS/LeanCloudObjcTests/BaseTestCase.swift

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,11 @@ class BaseTestCase: XCTestCase {
6969
super.tearDown()
7070
}
7171

72+
override func setUp() {
73+
super.setUp()
74+
LCUser.logOut()
75+
}
76+
7277
override func tearDown() {
7378
LCUser.logOut()
7479
super.tearDown()

AVOS/LeanCloudObjcTests/LCUserTestCase.swift

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -330,4 +330,46 @@ class LCUserTestCase: BaseTestCase {
330330
}
331331
}
332332
}
333+
334+
func testStrictlyFind() {
335+
expecting { exp in
336+
LCUser.loginAnonymously { user, error in
337+
XCTAssertNotNil(user)
338+
XCTAssertNil(error)
339+
exp.fulfill()
340+
}
341+
}
342+
guard let user = LCUser.current() else {
343+
XCTFail()
344+
return
345+
}
346+
let hiddenField = "hiddenField"
347+
let exposedField = "exposedField"
348+
user[hiddenField] = uuid
349+
user[exposedField] = uuid
350+
XCTAssertTrue(user.save())
351+
352+
expecting { exp in
353+
let query = LCQuery()
354+
query.whereKey(hiddenField, equalTo: user[hiddenField] ?? "")
355+
LCUser.strictlyFind(with: query) { users, error in
356+
XCTAssertTrue(Thread.isMainThread)
357+
XCTAssertNil(users)
358+
XCTAssertNotNil(error)
359+
exp.fulfill()
360+
}
361+
}
362+
363+
expecting { exp in
364+
let query = LCQuery()
365+
query.whereKey(exposedField, equalTo: user[exposedField] ?? "")
366+
LCUser.strictlyFind(with: query) { users, error in
367+
XCTAssertTrue(Thread.isMainThread)
368+
XCTAssertEqual(users?.count, 1)
369+
XCTAssertEqual(users?.first?.objectId, user.objectId)
370+
XCTAssertNil(error)
371+
exp.fulfill()
372+
}
373+
}
374+
}
333375
}

AVOS/Sources/Foundation/Query/LCQuery.m

Lines changed: 11 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1012,39 +1012,30 @@ + (NSDictionary *)dictionaryFromIncludeKeys:(NSArray *)array {
10121012

10131013
- (NSMutableDictionary *)assembleParameters {
10141014
[self.parameters removeAllObjects];
1015-
1016-
if ([self.where allKeys].count > 0)
1017-
{
1015+
if (self.where.count > 0) {
10181016
[self.parameters setObject:[self whereString] forKey:@"where"];
10191017
}
1020-
1021-
if (self.limit > 0)
1022-
{
1018+
if (self.limit > 0) {
10231019
[self.parameters setObject:@(self.limit) forKey:@"limit"];
10241020
}
1025-
if (self.skip > 0)
1026-
{
1021+
if (self.skip > 0) {
10271022
[self.parameters setObject:@(self.skip) forKey:@"skip"];
10281023
}
1029-
if (self.order.length > 0)
1030-
{
1024+
if (self.order.length > 0) {
10311025
[self.parameters setObject:self.order forKey:@"order"];
10321026
}
1033-
if (self.include.count > 0)
1034-
{
1035-
NSString * myIncludes = [[self.include allObjects] componentsJoinedByString:@","];
1036-
[self.parameters setObject:myIncludes forKey:@"include"];
1027+
if (self.include.count > 0) {
1028+
NSString *includes = [[self.include allObjects] componentsJoinedByString:@","];
1029+
[self.parameters setObject:includes forKey:@"include"];
10371030
}
1038-
if (self.selectedKeys.count > 0)
1039-
{
1040-
NSString * keys = [[self.selectedKeys allObjects] componentsJoinedByString:@","];
1031+
if (self.selectedKeys.count > 0) {
1032+
NSString *keys = [[self.selectedKeys allObjects] componentsJoinedByString:@","];
10411033
[self.parameters setObject:keys forKey:@"keys"];
10421034
}
1043-
if (self.includeACL)
1044-
{
1035+
if (self.includeACL) {
10451036
[self.parameters setObject:@"true" forKey:@"returnACL"];
10461037
}
1047-
if ([self.extraParameters allKeys].count > 0) {
1038+
if (self.extraParameters.count > 0) {
10481039
[self.parameters addEntriesFromDictionary:self.extraParameters];
10491040
}
10501041
return self.parameters;

AVOS/Sources/Foundation/Query/LCQuery_Internal.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
@property (nonatomic) NSMutableSet<NSString *> *selectedKeys;
1515
@property (nonatomic, strong) NSMutableDictionary *extraParameters;
1616

17-
- (NSDictionary *)assembleParameters;
17+
- (NSMutableDictionary *)assembleParameters;
1818
+ (NSDictionary *)dictionaryFromIncludeKeys:(NSArray *)array;
1919
- (NSString *)queryPath;
2020
-(void)queryWithBlock:(NSString *)path

AVOS/Sources/Foundation/User/LCUser.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -500,6 +500,15 @@ FOUNDATION_EXPORT LeanCloudSocialPlatform const LeanCloudSocialPlatformWeiXin;
500500
*/
501501
- (BOOL)isAnonymous;
502502

503+
// MARK: Strictly Find
504+
505+
/// More restrictive on query conditions to find user.
506+
/// Constraints: NOT support `skip`; NOT support the protected fields; NOT support `inQuery` ...
507+
/// @param query The query conditions.
508+
/// @param callback Result callback.
509+
+ (void)strictlyFindWithQuery:(LCQuery *)query
510+
callback:(void (^)(NSArray<LCUser *> * _Nullable users, NSError * _Nullable error))callback;
511+
503512
@end
504513

505514
/**

AVOS/Sources/Foundation/User/LCUser.m

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
#import "LCObject_Internal.h"
77
#import "LCPaasClient.h"
88
#import "LCUtils_Internal.h"
9-
#import "LCQuery.h"
9+
#import "LCQuery_Internal.h"
1010
#import "LCPersistenceUtils.h"
1111
#import "LCObjectUtils.h"
1212
#import "LCPaasClient.h"
@@ -1191,6 +1191,30 @@ - (BOOL)isAnonymous
11911191
return [[self linkedServiceNames] containsObject:anonymousTag];
11921192
}
11931193

1194+
// MARK: Strictly Find
1195+
1196+
+ (void)strictlyFindWithQuery:(LCQuery *)query
1197+
callback:(void (^)(NSArray<LCUser *> * _Nullable, NSError * _Nullable))callback
1198+
{
1199+
[[LCPaasClient sharedInstance] getObject:@"users/strictlyQuery"
1200+
withParameters:[query assembleParameters]
1201+
block:^(id _Nullable object, NSError * _Nullable error) {
1202+
NSMutableArray<LCUser *> *users;
1203+
if (!error && [NSDictionary _lc_isTypeOf:object]) {
1204+
NSArray *results = [NSArray _lc_decoding:object key:@"results"];
1205+
users = [NSMutableArray arrayWithCapacity:results.count];
1206+
for (NSDictionary *dictionary in results) {
1207+
if ([NSDictionary _lc_isTypeOf:dictionary]) {
1208+
LCUser *user = (LCUser *)[LCObjectUtils lcObjectForClass:[LCUser userTag]];
1209+
[LCObjectUtils copyDictionary:dictionary toObject:user];
1210+
[users addObject:user];
1211+
}
1212+
}
1213+
}
1214+
[LCUtils callArrayResultBlock:callback array:users error:error];
1215+
}];
1216+
}
1217+
11941218
#pragma mark - Override from LCObject
11951219

11961220
/**

0 commit comments

Comments
 (0)