void ApiWrap::resolveWebPages() { MessageIds ids; // temp_req_id = -1 typedef QPair<int32, MessageIds> IndexAndMessageIds; typedef QMap<ChannelData*, IndexAndMessageIds> MessageIdsByChannel; MessageIdsByChannel idsByChannel; // temp_req_id = -index - 2 const WebPageItems &items(App::webPageItems()); ids.reserve(_webPagesPending.size()); int32 t = unixtime(), m = INT_MAX; for (WebPagesPending::iterator i = _webPagesPending.begin(); i != _webPagesPending.cend(); ++i) { if (i.value() > 0) continue; if (i.key()->pendingTill <= t) { WebPageItems::const_iterator j = items.constFind(i.key()); if (j != items.cend() && !j.value().isEmpty()) { for (HistoryItemsMap::const_iterator it = j.value().cbegin(); it != j.value().cend(); ++it) { HistoryItem *item = j.value().begin().key(); if (item->id > 0) { if (item->channelId() == NoChannel) { ids.push_back(MTP_int(item->id)); i.value() = -1; } else { ChannelData *channel = item->history()->peer->asChannel(); MessageIdsByChannel::iterator channelMap = idsByChannel.find(channel); if (channelMap == idsByChannel.cend()) { channelMap = idsByChannel.insert(channel, IndexAndMessageIds(idsByChannel.size(), MessageIds(1, MTP_int(item->id)))); } else { channelMap.value().second.push_back(MTP_int(item->id)); } i.value() = -channelMap.value().first - 2; } break; } } } } else { m = qMin(m, i.key()->pendingTill - t); } } mtpRequestId req = ids.isEmpty() ? 0 : MTP::send(MTPmessages_GetMessages(MTP_vector<MTPint>(ids)), rpcDone(&ApiWrap::gotWebPages, (ChannelData*)0), RPCFailHandlerPtr(), 0, 5); typedef QVector<mtpRequestId> RequestIds; RequestIds reqsByIndex(idsByChannel.size(), 0); for (MessageIdsByChannel::const_iterator i = idsByChannel.cbegin(), e = idsByChannel.cend(); i != e; ++i) { reqsByIndex[i.value().first] = MTP::send(MTPchannels_GetMessages(i.key()->inputChannel, MTP_vector<MTPint>(i.value().second)), rpcDone(&ApiWrap::gotWebPages, i.key()), RPCFailHandlerPtr(), 0, 5); } if (req || !reqsByIndex.isEmpty()) { for (WebPagesPending::iterator i = _webPagesPending.begin(); i != _webPagesPending.cend(); ++i) { if (i.value() > 0) continue; if (i.value() < 0) { if (i.value() == -1) { i.value() = req; } else { i.value() = reqsByIndex[-i.value() - 2]; } } } } if (m < INT_MAX) _webPagesTimer.start(m * 1000); }