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); }
void ApiWrap::gotWebPages(const MTPmessages_Messages &msgs, mtpRequestId req) { const QVector<MTPMessage> *v = 0; switch (msgs.type()) { case mtpc_messages_messages: App::feedUsers(msgs.c_messages_messages().vusers); App::feedChats(msgs.c_messages_messages().vchats); v = &msgs.c_messages_messages().vmessages.c_vector().v; break; case mtpc_messages_messagesSlice: App::feedUsers(msgs.c_messages_messagesSlice().vusers); App::feedChats(msgs.c_messages_messagesSlice().vchats); v = &msgs.c_messages_messagesSlice().vmessages.c_vector().v; break; } QMap<int32, int32> msgsIds; // copied from feedMsgs for (int32 i = 0, l = v->size(); i < l; ++i) { const MTPMessage &msg(v->at(i)); switch (msg.type()) { case mtpc_message: msgsIds.insert(msg.c_message().vid.v, i); break; case mtpc_messageEmpty: msgsIds.insert(msg.c_messageEmpty().vid.v, i); break; case mtpc_messageService: msgsIds.insert(msg.c_messageService().vid.v, i); break; } } MainWidget *m = App::main(); for (QMap<int32, int32>::const_iterator i = msgsIds.cbegin(), e = msgsIds.cend(); i != e; ++i) { HistoryItem *item = App::histories().addToBack(v->at(*i), -1); if (item) { item->initDimensions(); if (m) m->itemResized(item); } } const WebPageItems &items(App::webPageItems()); for (WebPagesPending::iterator i = _webPagesPending.begin(); i != _webPagesPending.cend();) { if (i.value() == req) { if (i.key()->pendingTill > 0) { i.key()->pendingTill = -1; WebPageItems::const_iterator j = items.constFind(i.key()); if (j != items.cend()) { for (HistoryItemsMap::const_iterator k = j.value().cbegin(), e = j.value().cend(); k != e; ++k) { k.key()->initDimensions(); if (m) m->itemResized(k.key()); } } } i = _webPagesPending.erase(i); } else { ++i; } } }
void ApiWrap::resolveWebPages() { QVector<MTPint> ids; const WebPageItems &items(App::webPageItems()); ids.reserve(_webPagesPending.size()); int32 t = unixtime(), m = INT_MAX; for (WebPagesPending::const_iterator i = _webPagesPending.cbegin(), e = _webPagesPending.cend(); i != e; ++i) { if (i.value()) continue; if (i.key()->pendingTill <= t) { WebPageItems::const_iterator j = items.constFind(i.key()); if (j != items.cend() && !j.value().isEmpty()) { ids.push_back(MTP_int(j.value().begin().key()->id)); } } else { m = qMin(m, i.key()->pendingTill - t); } } if (!ids.isEmpty()) { mtpRequestId req = MTP::send(MTPmessages_GetMessages(MTP_vector<MTPint>(ids)), rpcDone(&ApiWrap::gotWebPages)); for (WebPagesPending::iterator i = _webPagesPending.begin(); i != _webPagesPending.cend(); ++i) { if (i.value()) continue; if (i.key()->pendingTill <= t) { i.value() = req; } } } if (m < INT_MAX) _webPagesTimer.start(m * 1000); }
void ApiWrap::gotWebPages(ChannelData *channel, const MTPmessages_Messages &msgs, mtpRequestId req) { const QVector<MTPMessage> *v = 0; switch (msgs.type()) { case mtpc_messages_messages: { const MTPDmessages_messages &d(msgs.c_messages_messages()); App::feedUsers(d.vusers); App::feedChats(d.vchats); v = &d.vmessages.c_vector().v; } break; case mtpc_messages_messagesSlice: { const MTPDmessages_messagesSlice &d(msgs.c_messages_messagesSlice()); App::feedUsers(d.vusers); App::feedChats(d.vchats); v = &d.vmessages.c_vector().v; } break; case mtpc_messages_channelMessages: { const MTPDmessages_channelMessages &d(msgs.c_messages_channelMessages()); if (channel) { channel->ptsReceived(d.vpts.v); } else { LOG(("API Error: received messages.channelMessages when no channel was passed! (ApiWrap::gotWebPages)")); } if (d.has_collapsed()) { // should not be returned LOG(("API Error: channels.getMessages and messages.getMessages should not return collapsed groups! (ApiWrap::gotWebPages)")); } App::feedUsers(d.vusers); App::feedChats(d.vchats); v = &d.vmessages.c_vector().v; } break; } if (!v) return; QMap<uint64, int32> msgsIds; // copied from feedMsgs for (int32 i = 0, l = v->size(); i < l; ++i) { const MTPMessage &msg(v->at(i)); switch (msg.type()) { case mtpc_message: msgsIds.insert((uint64(uint32(msg.c_message().vid.v)) << 32) | uint64(i), i); break; case mtpc_messageEmpty: msgsIds.insert((uint64(uint32(msg.c_messageEmpty().vid.v)) << 32) | uint64(i), i); break; case mtpc_messageService: msgsIds.insert((uint64(uint32(msg.c_messageService().vid.v)) << 32) | uint64(i), i); break; } } for (QMap<uint64, int32>::const_iterator i = msgsIds.cbegin(), e = msgsIds.cend(); i != e; ++i) { HistoryItem *item = App::histories().addNewMessage(v->at(i.value()), NewMessageExisting); if (item) { item->initDimensions(); Notify::historyItemResized(item); } } const WebPageItems &items(App::webPageItems()); for (WebPagesPending::iterator i = _webPagesPending.begin(); i != _webPagesPending.cend();) { if (i.value() == req) { if (i.key()->pendingTill > 0) { i.key()->pendingTill = -1; WebPageItems::const_iterator j = items.constFind(i.key()); if (j != items.cend()) { for (HistoryItemsMap::const_iterator k = j.value().cbegin(), e = j.value().cend(); k != e; ++k) { k.key()->initDimensions(); Notify::historyItemResized(k.key()); } } } i = _webPagesPending.erase(i); } else { ++i; } } }