1
0
mirror of https://github.com/danog/Telegram.git synced 2024-12-04 02:17:51 +01:00
Telegram/Telegraph/TGBridgeConversationHandler.m

73 lines
2.8 KiB
Mathematica
Raw Normal View History

2015-10-01 18:19:52 +02:00
#import "TGBridgeConversationHandler.h"
#import "TGBridgeConversationSubscription.h"
2016-02-25 01:03:51 +01:00
#import "TGDatabase.h"
2015-10-01 18:19:52 +02:00
#import "TGConversationSignals.h"
#import "TGUserSignal.h"
#import "TGBridgeChat+TGConversation.h"
#import "TGBridgeUser+TGUser.h"
@implementation TGBridgeConversationHandler
+ (SSignal *)handlingSignalForSubscription:(TGBridgeSubscription *)subscription server:(TGBridgeServer *)__unused server
{
if ([subscription isKindOfClass:[TGBridgeConversationSubscription class]])
{
TGBridgeConversationSubscription *conversationSubscription = (TGBridgeConversationSubscription *)subscription;
2016-02-25 01:03:51 +01:00
2015-10-01 18:19:52 +02:00
return [[TGConversationSignals conversationWithPeerId:conversationSubscription.peerId] mapToSignal:^SSignal *(TGConversation *conversation)
{
2016-02-25 01:03:51 +01:00
SSignal *(^signal)(TGConversation *) = ^SSignal *(TGConversation *conversation)
2015-10-01 18:19:52 +02:00
{
2016-02-25 01:03:51 +01:00
TGBridgeChat *bridgeChat = [TGBridgeChat chatWithTGConversation:conversation];
NSMutableIndexSet *userIds = [[NSMutableIndexSet alloc] init];
[userIds addIndexes:[bridgeChat involvedUserIds]];
[userIds addIndexes:[bridgeChat participantsUserIds]];
NSMutableArray *userSignals = [[NSMutableArray alloc] init];
[userIds enumerateIndexesUsingBlock:^(NSUInteger index, __unused BOOL *stop)
{
if (index != 0)
[userSignals addObject:[TGUserSignal userWithUserId:(int32_t)index]];
}];
return [[SSignal combineSignals:userSignals] map:^id(NSArray *users)
{
NSMutableDictionary *bridgeUsers = [[NSMutableDictionary alloc] init];
for (TGUser *user in users)
{
TGBridgeUser *bridgeUser = [TGBridgeUser userWithTGUser:user];
if (bridgeUser != nil)
bridgeUsers[@(bridgeUser.identifier)] = bridgeUser;
}
return @{ TGBridgeChatKey: bridgeChat, TGBridgeUsersDictionaryKey: bridgeUsers };
}];
};
2015-10-01 18:19:52 +02:00
2016-02-25 01:03:51 +01:00
if (conversation.isChannelGroup)
2015-10-01 18:19:52 +02:00
{
2016-02-25 01:03:51 +01:00
return [[TGDatabaseInstance() channelCachedData:conversation.conversationId] mapToSignal:^SSignal *(TGCachedConversationData *conversationData)
2015-10-01 18:19:52 +02:00
{
2016-02-25 01:03:51 +01:00
conversation.chatParticipantCount = conversationData.memberCount;
return signal(conversation);
}];
}
else
{
return signal(conversation);
}
2015-10-01 18:19:52 +02:00
}];
}
return [SSignal fail:nil];
}
+ (NSArray *)handledSubscriptions
{
return @[ [TGBridgeConversationSubscription class] ];
}
@end