1
0
mirror of https://github.com/danog/Telegram.git synced 2024-12-12 09:29:55 +01:00
Telegram/Telegraph/TGExtendedChatDataRequestActor.m

168 lines
6.4 KiB
Mathematica
Raw Normal View History

2014-07-10 16:11:09 +02:00
#import "TGExtendedChatDataRequestActor.h"
#import "ActionStage.h"
#import "SGraphObjectNode.h"
#import "TGUpdateStateRequestBuilder.h"
#import "TGUserDataRequestBuilder.h"
#import "TGTelegraph.h"
#import "TGDatabase.h"
#import "TGConversation+Telegraph.h"
#import "TGConversationAddMessagesActor.h"
2015-10-01 18:19:52 +02:00
#import "TGTelegramNetworking.h"
#import "TGBotSignals.h"
2014-07-10 16:11:09 +02:00
static NSMutableDictionary *extendedChatDataDictionary()
{
static NSMutableDictionary *dict = nil;
static int updateStateVersion = 0;
if (dict == nil)
{
dict = [[NSMutableDictionary alloc] init];
updateStateVersion = [TGUpdateStateRequestBuilder stateVersion];
}
else if (updateStateVersion != [TGUpdateStateRequestBuilder stateVersion])
{
[dict removeAllObjects];
updateStateVersion = [TGUpdateStateRequestBuilder stateVersion];
}
return dict;
}
@interface TGExtendedChatDataRequestActor ()
@property (nonatomic) int64_t conversationId;
@end
@implementation TGExtendedChatDataRequestActor
@synthesize conversationId = _conversationId;
+ (NSString *)genericPath
{
return @"/tg/conversationExtended/@";
}
- (void)execute:(NSDictionary *)options
{
_conversationId = [[options objectForKey:@"conversationId"] longLongValue];
2015-10-01 18:19:52 +02:00
TGConversation *conversation = [TGDatabaseInstance() loadConversationWithId:_conversationId];
2014-07-10 16:11:09 +02:00
NSDictionary *resultDict = [extendedChatDataDictionary() objectForKey:[[NSNumber alloc] initWithLongLong:_conversationId]];
2015-10-01 18:19:52 +02:00
if (resultDict != nil && conversation.chatVersion == [resultDict[@"version"] intValue])
2014-07-10 16:11:09 +02:00
{
[ActionStageInstance() nodeRetrieved:self.path node:[[SGraphObjectNode alloc] initWithObject:resultDict]];
}
else
{
self.cancelToken = [TGTelegraphInstance doRequestConversationData:_conversationId actor:self];
}
}
- (void)chatFullRequestSuccess:(TLmessages_ChatFull *)chatFull
{
[TGUserDataRequestBuilder executeUserDataUpdate:chatFull.users];
TLPeerNotifySettings *settings = chatFull.full_chat.notify_settings;
int peerSoundId = 0;
int peerMuteUntil = 0;
bool peerPreviewText = true;
bool photoNotificationsEnabled = true;
if ([settings isKindOfClass:[TLPeerNotifySettings$peerNotifySettings class]])
{
TLPeerNotifySettings$peerNotifySettings *concreteSettings = (TLPeerNotifySettings$peerNotifySettings *)settings;
peerMuteUntil = concreteSettings.mute_until;
2015-10-01 18:19:52 +02:00
if (peerMuteUntil <= [[TGTelegramNetworking instance] approximateRemoteTime])
peerMuteUntil = 0;
2014-07-10 16:11:09 +02:00
if (concreteSettings.sound.length == 0)
peerSoundId = 0;
else if ([concreteSettings.sound isEqualToString:@"default"])
peerSoundId = 1;
else
peerSoundId = [concreteSettings.sound intValue];
peerPreviewText = concreteSettings.show_previews;
photoNotificationsEnabled = concreteSettings.events_mask & 1;
}
int64_t conversationId = _conversationId;
[TGDatabaseInstance() storePeerNotificationSettings:_conversationId soundId:peerSoundId muteUntil:peerMuteUntil previewText:peerPreviewText photoNotificationsEnabled:photoNotificationsEnabled writeToActionQueue:false completion:^(bool changed)
{
if (changed)
{
[ActionStageInstance() dispatchOnStageQueue:^
{
NSDictionary *dict = [[NSDictionary alloc] initWithObjectsAndKeys:[NSNumber numberWithInt:peerMuteUntil], @"muteUntil", [NSNumber numberWithInt:peerSoundId], @"soundId", [[NSNumber alloc] initWithBool:peerPreviewText], @"previewText", [[NSNumber alloc] initWithBool:photoNotificationsEnabled], @"photoNotificationsEnabled", nil];
[extendedChatDataDictionary() setObject:dict forKey:[[NSNumber alloc] initWithLongLong:conversationId]];
[ActionStageInstance() dispatchResource:[NSString stringWithFormat:@"/tg/peerSettings/(%lld)", conversationId] resource:[[SGraphObjectNode alloc] initWithObject:dict]];
}];
}
}];
2015-10-01 18:19:52 +02:00
TGConversationParticipantsData *participantsData = nil;
if ([chatFull.full_chat isKindOfClass:[TLChatFull$chatFull class]])
{
TLChatFull$chatFull *concreteChatFull = (TLChatFull$chatFull *)chatFull.full_chat;
participantsData = [[TGConversationParticipantsData alloc] initWithTelegraphParticipantsDesc:concreteChatFull.participants];
}
NSDictionary *dict = [[NSDictionary alloc] initWithObjectsAndKeys:[NSNumber numberWithInt:peerMuteUntil], @"muteUntil", [NSNumber numberWithInt:peerSoundId], @"soundId", @(participantsData.version), @"version", nil];
2014-07-10 16:11:09 +02:00
[extendedChatDataDictionary() setObject:dict forKey:[[NSNumber alloc] initWithLongLong:_conversationId]];
2015-10-01 18:19:52 +02:00
if ([chatFull.full_chat.exported_invite isKindOfClass:[TLExportedChatInvite$chatInviteExported class]])
{
participantsData.exportedChatInviteString = ((TLExportedChatInvite$chatInviteExported *)chatFull.full_chat.exported_invite).link;
}
2014-07-10 16:11:09 +02:00
[TGDatabaseInstance() storeConversationParticipantData:-chatFull.full_chat.n_id participantData:participantsData];
if (chatFull.chats.count != 0)
{
TGConversation *conversation = [[TGConversation alloc] initWithTelegraphChatDesc:[chatFull.chats lastObject]];
static int actionId = 0;
[[[TGConversationAddMessagesActor alloc] initWithPath:[[NSString alloc] initWithFormat:@"/tg/addmessage/(chatData%d)", actionId++]] execute:[[NSDictionary alloc] initWithObjectsAndKeys:[[NSArray alloc] initWithObjects:conversation, nil], @"chats", nil]];
}
2015-10-01 18:19:52 +02:00
if ([chatFull.full_chat isKindOfClass:[TLChatFull$chatFull class]])
{
TLChatFull$chatFull *concreteChatFull = (TLChatFull$chatFull *)chatFull.full_chat;
if (concreteChatFull.bot_info.count != 0)
{
for (TLBotInfo *info in concreteChatFull.bot_info)
{
if ([info isKindOfClass:[TLBotInfo$botInfo class]])
{
TGBotInfo *botInfo = [TGBotSignals botInfoForInfo:info];
if (botInfo != nil)
[TGDatabaseInstance() storeBotInfo:botInfo forUserId:((TLBotInfo$botInfo *)info).user_id];
}
}
}
}
2014-07-10 16:11:09 +02:00
[ActionStageInstance() nodeRetrieved:self.path node:[[SGraphObjectNode alloc] initWithObject:dict]];
}
- (void)chatFullRequestFailed
{
[ActionStageInstance() nodeRetrieveFailed:self.path];
}
@end