private uint GetMessagePtsForTime(BubbleGroup group, long fromTime, out bool queryChat, out bool lastMessageIsExtendedParty) { VisualBubble bubbleToSyncFrom = null; bool localQueryChat = false; bool localLastMessageIsExtendedParty = false; foreach (var bubble in BubbleGroupSync.ReadBubblesFromDatabase(group)) { if (bubble is PartyInformationBubble) { if ((bubble as PartyInformationBubble).Type == PartyInformationBubble.InformationType.UpgradedToExtendedParty) { localQueryChat = true; if (bubble.Time <= fromTime) { localLastMessageIsExtendedParty = true; bubbleToSyncFrom = bubble; break; } } } if (bubble.Time <= fromTime) { bubbleToSyncFrom = bubble; break; } } if (bubbleToSyncFrom != null) { if (bubbleToSyncFrom.IdService == null) { queryChat = localQueryChat; lastMessageIsExtendedParty = localLastMessageIsExtendedParty; return(1); } queryChat = localQueryChat; lastMessageIsExtendedParty = localLastMessageIsExtendedParty; return(uint.Parse(bubbleToSyncFrom.IdService)); } queryChat = localQueryChat; lastMessageIsExtendedParty = localLastMessageIsExtendedParty; return(1); }
private IMessagesMessages GetMessageHistory(BubbleGroup group, long fromTime, int max) { using (var client = new FullClientDisposable(this)) { if (group.IsExtendedParty) { IMessagesMessages finalMessages = MakeMessagesMessages(new List <IChat>(), new List <IUser>(), new List <IMessage>()); Func <BubbleGroup, bool> isNewBubbleGroup = bubbleGroup => { var bubbles = BubbleGroupSync.ReadBubblesFromDatabase(group); if (bubbles.Count() > 1) { return(false); } foreach (var bubble in bubbles) { if (bubble is NewBubble) { return(true); } } return(false); }; Func <IMessagesMessages, bool> hasSuperGroupCreatedMessage = messages => { var messagesChannelMessages = messages as MessagesChannelMessages; if (messagesChannelMessages != null) { foreach (var message in messagesChannelMessages.Messages) { var messageService = message as MessageService; if (messageService == null) { continue; } if (messageService.Action is MessageActionChannelMigrateFrom) { return(true); } } } return(false); }; Func <IMessagesMessages, int> getMessagesCount = messages => { var messagesChats = messages as MessagesMessages; var messagesChatsSlice = messages as MessagesMessagesSlice; var messagesChatsChannel = messages as MessagesChannelMessages; if (messagesChats != null) { return(messagesChats.Messages.Count); } if (messagesChatsSlice != null) { return(messagesChats.Messages.Count); } if (messagesChatsChannel != null) { return(messagesChatsChannel.Messages.Count); } return(0); }; Action <List <IMessage>, string> changeAddress = (messages, address) => { foreach (var message in messages) { var normalMessage = message as Message; var serviceMessage = message as MessageService; if (normalMessage != null) { normalMessage.ToId = new PeerChannel { ChannelId = uint.Parse(address) }; } if (serviceMessage != null) { serviceMessage.ToId = new PeerChannel { ChannelId = uint.Parse(address) }; } } }; Action <IMessagesMessages, string> updateChatMessageAddresses = (chatMessages, address) => { var messagesChats = chatMessages as MessagesMessages; var messagesChatsSlice = chatMessages as MessagesMessagesSlice; if (messagesChats != null) { changeAddress(messagesChats.Messages, address); } if (messagesChatsSlice != null) { changeAddress(messagesChatsSlice.Messages, address); } }; do { if (isNewBubbleGroup(group)) { var newOffsetId = GetLastPtsForChannel(client.Client, group.Address); SaveChannelState(uint.Parse(group.Address), newOffsetId); //save the state for this channel since it wount have any as its a new bubblegroup finalMessages = GetChannelMessages(group, newOffsetId, max, client); if (hasSuperGroupCreatedMessage(finalMessages)) { var remainingCount = getMessagesCount(finalMessages); if (max - remainingCount > 0) { var chatMessages = GetChatMessagesForChannel(group, Time.GetNowUnixTimestamp(), max - remainingCount, client); updateChatMessageAddresses(chatMessages, group.Address); MergeMessagesMessages(finalMessages, chatMessages); } } break; } bool queryChat; bool lastMessageIsExtendedParty; var offsetId = GetMessagePtsForTime(group, fromTime, out queryChat, out lastMessageIsExtendedParty); if (queryChat) { if (lastMessageIsExtendedParty) { finalMessages = GetChannelMessages(group, 1, max, client); SaveChannelState(uint.Parse(group.Address), 1); //this group was just upgraded to an extended party, we need to explicity save its state otherwise itll be zero } var chatMessages = GetChatMessagesForChannel(group, fromTime, max, client); updateChatMessageAddresses(chatMessages, group.Address); MergeMessagesMessages(finalMessages, chatMessages); break; } finalMessages = GetChannelMessages(group, offsetId, max, client); var finalMessagesCount = getMessagesCount(finalMessages); if (max - finalMessagesCount > 0) { var chatMessages = GetChatMessagesForChannel(group, fromTime, max - finalMessagesCount, client); updateChatMessageAddresses(chatMessages, group.Address); MergeMessagesMessages(finalMessages, chatMessages); } } while (false); return(finalMessages); } else { var peer = GetInputPeer(group.Address, group.IsParty, group.IsExtendedParty); bool queryChat; bool lastMessageIsExtendedParty; var offsetId = GetMessagePtsForTime(group, fromTime, out queryChat, out lastMessageIsExtendedParty); if (offsetId != 1) { var response = TelegramUtils.RunSynchronously( client.Client.Methods.MessagesGetHistoryAsync(new MessagesGetHistoryArgs { Peer = peer, OffsetId = offsetId + 1, Limit = (uint)max })); return(response); } else { var response = TelegramUtils.RunSynchronously( client.Client.Methods.MessagesGetHistoryAsync(new MessagesGetHistoryArgs { Peer = peer, OffsetDate = (uint)fromTime, Limit = (uint)max })); return(response); } } } }