2015-10-01 18:19:52 +02:00
|
|
|
#import "TGBridgeContextService.h"
|
|
|
|
#import "TGChatListSignals.h"
|
2016-02-25 01:03:51 +01:00
|
|
|
|
|
|
|
#import "PGCamera.h"
|
2015-10-01 18:19:52 +02:00
|
|
|
|
|
|
|
#import "TGBridgeChat+TGConversation.h"
|
|
|
|
#import "TGBridgeUser+TGUser.h"
|
|
|
|
|
|
|
|
#import "TGDatabase.h"
|
|
|
|
|
|
|
|
const NSUInteger TGBridgeContextChatsCount = 4;
|
|
|
|
|
|
|
|
@interface TGBridgeContextService ()
|
|
|
|
{
|
|
|
|
SSignal *_chatListSignal;
|
|
|
|
SMetaDisposable *_disposable;
|
|
|
|
}
|
|
|
|
@end
|
|
|
|
|
|
|
|
|
|
|
|
@implementation TGBridgeContextService
|
|
|
|
|
|
|
|
- (instancetype)initWithServer:(TGBridgeServer *)server
|
|
|
|
{
|
2016-02-25 01:03:51 +01:00
|
|
|
self = [super initWithServer:server];
|
2015-10-01 18:19:52 +02:00
|
|
|
if (self != nil)
|
|
|
|
{
|
|
|
|
_chatListSignal = [server serviceSignalForKey:@"chatList" producer:^SSignal *
|
|
|
|
{
|
|
|
|
return [TGChatListSignals chatListWithLimit:24];
|
|
|
|
}];
|
|
|
|
|
|
|
|
__weak TGBridgeContextService *weakSelf = self;
|
|
|
|
_disposable = [[SMetaDisposable alloc] init];
|
2016-02-25 01:03:51 +01:00
|
|
|
[_disposable setDisposable:[[_chatListSignal mapToSignal:^SSignal *(id next)
|
|
|
|
{
|
|
|
|
return [[SSignal single:next] delay:2.0 onQueue:[SQueue concurrentDefaultQueue]];
|
|
|
|
}] startWithNext:^(NSArray *next)
|
2015-10-01 18:19:52 +02:00
|
|
|
{
|
|
|
|
__strong TGBridgeContextService *strongSelf = weakSelf;
|
|
|
|
if (strongSelf == nil)
|
|
|
|
return;
|
|
|
|
|
2016-02-25 01:03:51 +01:00
|
|
|
bool micAccessAllowed = ([PGCamera microphoneAuthorizationStatus] == PGMicrophoneAuthorizationStatusAuthorized);
|
|
|
|
|
2015-10-01 18:19:52 +02:00
|
|
|
if (next.count > 0)
|
|
|
|
{
|
|
|
|
NSUInteger limit = MIN(next.count, TGBridgeContextChatsCount);
|
|
|
|
|
|
|
|
NSMutableArray *bridgeChats = [[NSMutableArray alloc] init];
|
|
|
|
NSMutableDictionary *bridgeUsers = [[NSMutableDictionary alloc] init];
|
|
|
|
|
|
|
|
NSMutableIndexSet *userIds = [[NSMutableIndexSet alloc] init];
|
|
|
|
|
|
|
|
NSUInteger i = 0;
|
|
|
|
for (TGConversation *chat in next)
|
|
|
|
{
|
|
|
|
if (chat.isBroadcast || chat.isEncrypted)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if (i >= limit)
|
|
|
|
break;
|
|
|
|
|
|
|
|
TGBridgeChat *bridgeChat = [TGBridgeChat chatWithTGConversation:chat];
|
|
|
|
if (bridgeChat != nil)
|
|
|
|
{
|
|
|
|
[bridgeChats addObject:bridgeChat];
|
|
|
|
[userIds addIndexes:[bridgeChat involvedUserIds]];
|
|
|
|
}
|
|
|
|
|
|
|
|
i++;
|
|
|
|
}
|
|
|
|
|
|
|
|
[userIds enumerateIndexesUsingBlock:^(NSUInteger userId, __unused BOOL *stop)
|
|
|
|
{
|
|
|
|
TGBridgeUser *bridgeUser = [TGBridgeUser userWithTGUser:[[TGDatabase instance] loadUser:(int)userId]];
|
|
|
|
if (bridgeUser != nil)
|
|
|
|
bridgeUsers[@(userId)] = bridgeUser;
|
|
|
|
}];
|
|
|
|
|
2016-02-25 01:03:51 +01:00
|
|
|
[strongSelf.server setStartupData:@{ TGBridgeChatsArrayKey: bridgeChats, TGBridgeUsersDictionaryKey: bridgeUsers } micAccessAllowed:micAccessAllowed];
|
2015-10-01 18:19:52 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2016-02-25 01:03:51 +01:00
|
|
|
[strongSelf.server setStartupData:nil micAccessAllowed:micAccessAllowed];
|
2015-10-01 18:19:52 +02:00
|
|
|
}
|
|
|
|
}]];
|
|
|
|
}
|
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)dealloc
|
|
|
|
{
|
|
|
|
[_disposable dispose];
|
|
|
|
}
|
|
|
|
|
|
|
|
@end
|