1
0
mirror of https://github.com/danog/Telegram.git synced 2024-12-11 17:09:46 +01:00
Telegram/Telegraph/TGContactListSearchActor.m

94 lines
2.8 KiB
Mathematica
Raw Normal View History

2014-07-10 16:11:09 +02:00
#import "TGContactListSearchActor.h"
#import "ActionStage.h"
#import "SGraphObjectNode.h"
#import "TGDatabase.h"
2015-10-01 18:19:52 +02:00
#import "TGTelegraph.h"
#import "TGUserDataRequestBuilder.h"
#import "TGUser+Telegraph.h"
#import "TGTimer.h"
@interface TGContactListSearchActor ()
{
TGTimer *_timer;
}
@end
2014-07-10 16:11:09 +02:00
@implementation TGContactListSearchActor
+ (NSString *)genericPath
{
return @"/tg/contacts/search/@";
}
2015-10-01 18:19:52 +02:00
- (void)dealloc
{
[_timer invalidate];
}
2014-07-10 16:11:09 +02:00
- (void)execute:(NSDictionary *)options
{
NSString *query = [options objectForKey:@"query"];
if (query == nil)
{
[ActionStageInstance() nodeRetrieveFailed:self.path];
return;
}
int ignoreUid = [[options objectForKey:@"ignoreUid"] intValue];
2015-10-01 18:19:52 +02:00
__weak TGContactListSearchActor *weakSelf = self;
NSTimeInterval delayStart = CFAbsoluteTimeGetCurrent();
2014-07-10 16:11:09 +02:00
[TGDatabaseInstance() searchContacts:query ignoreUid:ignoreUid searchPhonebook:[[options objectForKey:@"searchPhonebook"] boolValue] completion:^(NSDictionary *result)
{
2015-10-01 18:19:52 +02:00
[ActionStageInstance() dispatchOnStageQueue:^
{
[ActionStageInstance() dispatchMessageToWatchers:self.path messageType:@"localResults" message:result];
if (query.length < 5)
[ActionStageInstance() actionCompleted:self.path result:nil];
else
{
_timer = [[TGTimer alloc] initWithTimeout:MAX(0.0, CFAbsoluteTimeGetCurrent() - delayStart - 150) repeat:false completion:^{
__strong TGContactListSearchActor *strongSelf = weakSelf;
if (strongSelf != nil)
{
strongSelf.cancelToken = [TGTelegraphInstance doSearchContactsByName:query limit:1 completion:^(TLcontacts_Found *result)
{
__strong TGContactListSearchActor *strongSelf = weakSelf;
[strongSelf _processRemoteResults:result];
}];
}
} queue:[ActionStageInstance() globalStageDispatchQueue]];
[_timer start];
}
}];
2014-07-10 16:11:09 +02:00
}];
}
2015-10-01 18:19:52 +02:00
- (void)_processRemoteResults:(TLcontacts_Found *)result
{
if (result != nil)
{
NSMutableArray *users = [[NSMutableArray alloc] init];
for (id parsedUser in result.users)
{
TGUser *user = [[TGUser alloc] initWithTelegraphUserDesc:parsedUser];
if (user.uid != 0)
[users addObject:user];
}
[TGUserDataRequestBuilder executeUserObjectsUpdate:users];
[ActionStageInstance() dispatchMessageToWatchers:self.path messageType:@"globalResults" message:@{@"users": users}];
}
[ActionStageInstance() actionCompleted:self.path result:nil];
}
2014-07-10 16:11:09 +02:00
@end