2015-03-19 09:18:19 +00:00
|
|
|
/*
|
|
|
|
This file is part of Telegram Desktop,
|
|
|
|
the official desktop version of Telegram messaging app, see https://telegram.org
|
|
|
|
|
|
|
|
Telegram Desktop is free software: you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU General Public License as published by
|
|
|
|
the Free Software Foundation, either version 3 of the License, or
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
It is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
2015-10-03 13:16:42 +00:00
|
|
|
In addition, as a special exception, the copyright holders give permission
|
|
|
|
to link the code of portions of this program with the OpenSSL library.
|
|
|
|
|
2015-03-19 09:18:19 +00:00
|
|
|
Full license: https://github.com/telegramdesktop/tdesktop/blob/master/LICENSE
|
2016-02-08 10:56:18 +00:00
|
|
|
Copyright (c) 2014-2016 John Preston, https://desktop.telegram.org
|
2015-03-19 09:18:19 +00:00
|
|
|
*/
|
|
|
|
#include "stdafx.h"
|
2016-04-21 17:57:29 +00:00
|
|
|
#include "apiwrap.h"
|
2015-03-19 09:18:19 +00:00
|
|
|
|
2016-06-09 14:31:10 +00:00
|
|
|
#include "data/data_drafts.h"
|
2016-05-25 12:09:05 +00:00
|
|
|
#include "observer_peer.h"
|
2016-04-21 17:57:29 +00:00
|
|
|
#include "lang.h"
|
2015-03-19 09:18:19 +00:00
|
|
|
#include "application.h"
|
2016-04-12 21:31:28 +00:00
|
|
|
#include "mainwindow.h"
|
2015-03-19 09:18:19 +00:00
|
|
|
#include "mainwidget.h"
|
2016-06-07 19:59:39 +00:00
|
|
|
#include "historywidget.h"
|
2015-03-19 09:18:19 +00:00
|
|
|
#include "localstorage.h"
|
2016-06-07 19:59:39 +00:00
|
|
|
#include "boxes/confirmbox.h"
|
2015-03-19 09:18:19 +00:00
|
|
|
|
2016-03-10 10:15:21 +00:00
|
|
|
ApiWrap::ApiWrap(QObject *parent) : QObject(parent)
|
|
|
|
, _messageDataResolveDelayed(new SingleDelayedCall(this, "resolveMessageDatas")) {
|
2015-03-19 09:18:19 +00:00
|
|
|
App::initBackground();
|
|
|
|
|
2015-04-04 20:01:34 +00:00
|
|
|
connect(&_webPagesTimer, SIGNAL(timeout()), this, SLOT(resolveWebPages()));
|
2016-06-07 19:59:39 +00:00
|
|
|
connect(&_draftsSaveTimer, SIGNAL(timeout()), this, SLOT(saveDraftsToCloud()));
|
2015-03-19 09:18:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void ApiWrap::init() {
|
|
|
|
}
|
|
|
|
|
2016-05-09 10:07:25 +00:00
|
|
|
void ApiWrap::requestMessageData(ChannelData *channel, MsgId msgId, std_::unique_ptr<RequestMessageDataCallback> callback) {
|
2016-03-10 10:15:21 +00:00
|
|
|
MessageDataRequest &req(channel ? _channelMessageDataRequests[channel][msgId] : _messageDataRequests[msgId]);
|
2016-05-09 10:07:25 +00:00
|
|
|
if (callback) {
|
|
|
|
MessageDataRequest::CallbackPtr pcallback(callback.release());
|
|
|
|
req.callbacks.append(pcallback);
|
|
|
|
}
|
2016-03-10 10:15:21 +00:00
|
|
|
if (!req.req) _messageDataResolveDelayed->call();
|
2015-03-19 09:18:19 +00:00
|
|
|
}
|
|
|
|
|
2016-03-10 10:15:21 +00:00
|
|
|
ApiWrap::MessageIds ApiWrap::collectMessageIds(const MessageDataRequests &requests) {
|
2015-09-12 11:59:50 +00:00
|
|
|
MessageIds result;
|
|
|
|
result.reserve(requests.size());
|
2016-05-09 10:07:25 +00:00
|
|
|
for (auto i = requests.cbegin(), e = requests.cend(); i != e; ++i) {
|
2015-09-12 11:59:50 +00:00
|
|
|
if (i.value().req > 0) continue;
|
|
|
|
result.push_back(MTP_int(i.key()));
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
2015-06-28 12:37:10 +00:00
|
|
|
|
2016-03-10 10:15:21 +00:00
|
|
|
ApiWrap::MessageDataRequests *ApiWrap::messageDataRequests(ChannelData *channel, bool onlyExisting) {
|
2015-09-12 11:59:50 +00:00
|
|
|
if (channel) {
|
2016-05-09 10:07:25 +00:00
|
|
|
auto i = _channelMessageDataRequests.find(channel);
|
2016-03-10 10:15:21 +00:00
|
|
|
if (i == _channelMessageDataRequests.cend()) {
|
2015-09-12 11:59:50 +00:00
|
|
|
if (onlyExisting) return 0;
|
2016-03-10 10:15:21 +00:00
|
|
|
i = _channelMessageDataRequests.insert(channel, MessageDataRequests());
|
2015-06-28 12:37:10 +00:00
|
|
|
}
|
2015-09-12 11:59:50 +00:00
|
|
|
return &i.value();
|
2015-06-28 12:37:10 +00:00
|
|
|
}
|
2016-03-10 10:15:21 +00:00
|
|
|
return &_messageDataRequests;
|
2015-09-12 11:59:50 +00:00
|
|
|
}
|
|
|
|
|
2016-03-10 10:15:21 +00:00
|
|
|
void ApiWrap::resolveMessageDatas() {
|
|
|
|
if (_messageDataRequests.isEmpty() && _channelMessageDataRequests.isEmpty()) return;
|
2015-09-12 11:59:50 +00:00
|
|
|
|
2016-03-10 10:15:21 +00:00
|
|
|
MessageIds ids = collectMessageIds(_messageDataRequests);
|
2015-06-28 12:37:10 +00:00
|
|
|
if (!ids.isEmpty()) {
|
2016-03-10 10:15:21 +00:00
|
|
|
mtpRequestId req = MTP::send(MTPmessages_GetMessages(MTP_vector<MTPint>(ids)), rpcDone(&ApiWrap::gotMessageDatas, (ChannelData*)nullptr), RPCFailHandlerPtr(), 0, 5);
|
2016-05-09 10:07:25 +00:00
|
|
|
for (auto &request : _messageDataRequests) {
|
|
|
|
if (request.req > 0) continue;
|
|
|
|
request.req = req;
|
2015-06-28 12:37:10 +00:00
|
|
|
}
|
|
|
|
}
|
2016-05-09 10:07:25 +00:00
|
|
|
for (auto j = _channelMessageDataRequests.begin(); j != _channelMessageDataRequests.cend();) {
|
2015-09-12 11:59:50 +00:00
|
|
|
if (j->isEmpty()) {
|
2016-03-10 10:15:21 +00:00
|
|
|
j = _channelMessageDataRequests.erase(j);
|
2015-09-12 11:59:50 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
MessageIds ids = collectMessageIds(j.value());
|
|
|
|
if (!ids.isEmpty()) {
|
2016-03-10 10:15:21 +00:00
|
|
|
mtpRequestId req = MTP::send(MTPchannels_GetMessages(j.key()->inputChannel, MTP_vector<MTPint>(ids)), rpcDone(&ApiWrap::gotMessageDatas, j.key()), RPCFailHandlerPtr(), 0, 5);
|
2016-05-09 10:07:25 +00:00
|
|
|
for (auto &request : *j) {
|
|
|
|
if (request.req > 0) continue;
|
|
|
|
request.req = req;
|
2015-09-12 11:59:50 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
++j;
|
|
|
|
}
|
2015-06-28 12:37:10 +00:00
|
|
|
}
|
|
|
|
|
2016-05-25 17:59:21 +00:00
|
|
|
void ApiWrap::updatesReceived(const MTPUpdates &updates) {
|
|
|
|
App::main()->sentUpdatesReceived(updates);
|
|
|
|
}
|
|
|
|
|
2016-03-10 10:15:21 +00:00
|
|
|
void ApiWrap::gotMessageDatas(ChannelData *channel, const MTPmessages_Messages &msgs, mtpRequestId req) {
|
2015-06-28 12:37:10 +00:00
|
|
|
switch (msgs.type()) {
|
2015-09-04 13:01:31 +00:00
|
|
|
case mtpc_messages_messages: {
|
2016-05-27 16:47:46 +00:00
|
|
|
auto &d(msgs.c_messages_messages());
|
2015-09-04 13:01:31 +00:00
|
|
|
App::feedUsers(d.vusers);
|
|
|
|
App::feedChats(d.vchats);
|
2015-09-20 08:55:41 +00:00
|
|
|
App::feedMsgs(d.vmessages, NewMessageExisting);
|
2015-09-04 13:01:31 +00:00
|
|
|
} break;
|
|
|
|
|
|
|
|
case mtpc_messages_messagesSlice: {
|
2016-05-27 16:47:46 +00:00
|
|
|
auto &d(msgs.c_messages_messagesSlice());
|
2015-09-04 13:01:31 +00:00
|
|
|
App::feedUsers(d.vusers);
|
|
|
|
App::feedChats(d.vchats);
|
2015-09-20 08:55:41 +00:00
|
|
|
App::feedMsgs(d.vmessages, NewMessageExisting);
|
2015-09-04 13:01:31 +00:00
|
|
|
} break;
|
|
|
|
|
|
|
|
case mtpc_messages_channelMessages: {
|
2016-05-27 16:47:46 +00:00
|
|
|
auto &d(msgs.c_messages_channelMessages());
|
2015-09-13 08:41:27 +00:00
|
|
|
if (channel) {
|
|
|
|
channel->ptsReceived(d.vpts.v);
|
|
|
|
} else {
|
2016-03-05 21:12:55 +00:00
|
|
|
LOG(("App Error: received messages.channelMessages when no channel was passed! (ApiWrap::gotDependencyItem)"));
|
2015-09-19 09:13:21 +00:00
|
|
|
}
|
2015-09-04 13:01:31 +00:00
|
|
|
App::feedUsers(d.vusers);
|
|
|
|
App::feedChats(d.vchats);
|
2015-09-20 08:55:41 +00:00
|
|
|
App::feedMsgs(d.vmessages, NewMessageExisting);
|
2015-09-04 13:01:31 +00:00
|
|
|
} break;
|
2015-06-28 12:37:10 +00:00
|
|
|
}
|
2016-03-10 10:15:21 +00:00
|
|
|
MessageDataRequests *requests(messageDataRequests(channel, true));
|
2015-09-12 11:59:50 +00:00
|
|
|
if (requests) {
|
2016-05-09 10:07:25 +00:00
|
|
|
for (auto i = requests->begin(); i != requests->cend();) {
|
2015-09-12 11:59:50 +00:00
|
|
|
if (i.value().req == req) {
|
2016-05-09 10:07:25 +00:00
|
|
|
for_const (auto &callback, i.value().callbacks) {
|
|
|
|
callback->call(channel, i.key());
|
2015-06-28 12:37:10 +00:00
|
|
|
}
|
2015-09-12 11:59:50 +00:00
|
|
|
i = requests->erase(i);
|
|
|
|
} else {
|
|
|
|
++i;
|
2015-06-28 12:37:10 +00:00
|
|
|
}
|
2015-09-12 11:59:50 +00:00
|
|
|
}
|
|
|
|
if (channel && requests->isEmpty()) {
|
2016-03-10 10:15:21 +00:00
|
|
|
_channelMessageDataRequests.remove(channel);
|
2015-06-28 12:37:10 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-03-19 09:18:19 +00:00
|
|
|
void ApiWrap::requestFullPeer(PeerData *peer) {
|
2015-06-29 22:09:23 +00:00
|
|
|
if (!peer || _fullPeerRequests.contains(peer)) return;
|
|
|
|
|
2015-09-03 10:48:40 +00:00
|
|
|
mtpRequestId req = 0;
|
|
|
|
if (peer->isUser()) {
|
2015-06-29 22:09:23 +00:00
|
|
|
req = MTP::send(MTPusers_GetFullUser(peer->asUser()->inputUser), rpcDone(&ApiWrap::gotUserFull, peer), rpcFail(&ApiWrap::gotPeerFullFailed, peer));
|
2015-09-03 10:48:40 +00:00
|
|
|
} else if (peer->isChat()) {
|
|
|
|
req = MTP::send(MTPmessages_GetFullChat(peer->asChat()->inputChat), rpcDone(&ApiWrap::gotChatFull, peer), rpcFail(&ApiWrap::gotPeerFullFailed, peer));
|
|
|
|
} else if (peer->isChannel()) {
|
2015-09-16 21:15:13 +00:00
|
|
|
req = MTP::send(MTPchannels_GetFullChannel(peer->asChannel()->inputChannel), rpcDone(&ApiWrap::gotChatFull, peer), rpcFail(&ApiWrap::gotPeerFullFailed, peer));
|
2015-03-19 09:18:19 +00:00
|
|
|
}
|
2015-09-03 10:48:40 +00:00
|
|
|
if (req) _fullPeerRequests.insert(peer, req);
|
2015-03-19 09:18:19 +00:00
|
|
|
}
|
|
|
|
|
2015-10-30 15:57:22 +00:00
|
|
|
void ApiWrap::processFullPeer(PeerData *peer, const MTPmessages_ChatFull &result) {
|
|
|
|
gotChatFull(peer, result, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
void ApiWrap::processFullPeer(PeerData *peer, const MTPUserFull &result) {
|
|
|
|
gotUserFull(peer, result, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
void ApiWrap::gotChatFull(PeerData *peer, const MTPmessages_ChatFull &result, mtpRequestId req) {
|
2016-04-08 10:44:35 +00:00
|
|
|
const auto &d(result.c_messages_chatFull());
|
|
|
|
const auto &vc(d.vchats.c_vector().v);
|
2015-09-03 10:48:40 +00:00
|
|
|
bool badVersion = false;
|
|
|
|
if (peer->isChat()) {
|
|
|
|
badVersion = (!vc.isEmpty() && vc.at(0).type() == mtpc_chat && vc.at(0).c_chat().vversion.v < peer->asChat()->version);
|
|
|
|
} else if (peer->isChannel()) {
|
|
|
|
badVersion = (!vc.isEmpty() && vc.at(0).type() == mtpc_channel && vc.at(0).c_channel().vversion.v < peer->asChannel()->version);
|
|
|
|
}
|
2015-06-29 22:09:23 +00:00
|
|
|
|
2016-06-02 13:57:49 +00:00
|
|
|
App::feedUsers(d.vusers);
|
|
|
|
App::feedChats(d.vchats);
|
2015-09-03 10:48:40 +00:00
|
|
|
|
|
|
|
if (peer->isChat()) {
|
|
|
|
if (d.vfull_chat.type() != mtpc_chatFull) {
|
|
|
|
LOG(("MTP Error: bad type in gotChatFull for chat: %1").arg(d.vfull_chat.type()));
|
|
|
|
return;
|
|
|
|
}
|
2016-04-08 10:44:35 +00:00
|
|
|
const auto &f(d.vfull_chat.c_chatFull());
|
2015-09-03 10:48:40 +00:00
|
|
|
App::feedParticipants(f.vparticipants, false, false);
|
2016-04-08 10:44:35 +00:00
|
|
|
const auto &v(f.vbot_info.c_vector().v);
|
2015-09-03 10:48:40 +00:00
|
|
|
for (QVector<MTPBotInfo>::const_iterator i = v.cbegin(), e = v.cend(); i < e; ++i) {
|
|
|
|
switch (i->type()) {
|
|
|
|
case mtpc_botInfo: {
|
2016-04-08 10:44:35 +00:00
|
|
|
const auto &b(i->c_botInfo());
|
2015-09-03 10:48:40 +00:00
|
|
|
UserData *user = App::userLoaded(b.vuser_id.v);
|
|
|
|
if (user) {
|
|
|
|
user->setBotInfo(*i);
|
|
|
|
App::clearPeerUpdated(user);
|
|
|
|
emit fullPeerUpdated(user);
|
|
|
|
}
|
|
|
|
} break;
|
2015-06-15 17:19:24 +00:00
|
|
|
}
|
|
|
|
}
|
2015-09-03 10:48:40 +00:00
|
|
|
PhotoData *photo = App::feedPhoto(f.vchat_photo);
|
|
|
|
ChatData *chat = peer->asChat();
|
2015-04-30 13:53:36 +00:00
|
|
|
if (photo) {
|
2015-03-19 09:18:19 +00:00
|
|
|
chat->photoId = photo->id;
|
2015-09-03 10:48:40 +00:00
|
|
|
photo->peer = chat;
|
2015-06-29 22:09:23 +00:00
|
|
|
} else {
|
|
|
|
chat->photoId = 0;
|
2015-03-19 09:18:19 +00:00
|
|
|
}
|
2016-06-01 13:07:03 +00:00
|
|
|
chat->setInviteLink((f.vexported_invite.type() == mtpc_chatInviteExported) ? qs(f.vexported_invite.c_chatInviteExported().vlink) : QString());
|
2015-04-30 13:53:36 +00:00
|
|
|
|
2016-06-01 13:07:03 +00:00
|
|
|
notifySettingReceived(MTP_inputNotifyPeer(peer->input), f.vnotify_settings);
|
2015-09-03 10:48:40 +00:00
|
|
|
} else if (peer->isChannel()) {
|
|
|
|
if (d.vfull_chat.type() != mtpc_channelFull) {
|
|
|
|
LOG(("MTP Error: bad type in gotChatFull for channel: %1").arg(d.vfull_chat.type()));
|
|
|
|
return;
|
|
|
|
}
|
2016-05-20 16:01:06 +00:00
|
|
|
auto &f(d.vfull_chat.c_channelFull());
|
2015-09-03 10:48:40 +00:00
|
|
|
PhotoData *photo = App::feedPhoto(f.vchat_photo);
|
|
|
|
ChannelData *channel = peer->asChannel();
|
2016-06-02 13:02:55 +00:00
|
|
|
|
|
|
|
auto canViewAdmins = channel->canViewAdmins();
|
|
|
|
auto canViewMembers = channel->canViewMembers();
|
|
|
|
|
2015-10-12 21:02:10 +00:00
|
|
|
channel->flagsFull = f.vflags.v;
|
2015-09-03 10:48:40 +00:00
|
|
|
if (photo) {
|
|
|
|
channel->photoId = photo->id;
|
|
|
|
photo->peer = channel;
|
|
|
|
} else {
|
|
|
|
channel->photoId = 0;
|
|
|
|
}
|
2015-11-13 15:14:33 +00:00
|
|
|
if (f.has_migrated_from_chat_id()) {
|
|
|
|
if (!channel->mgInfo) {
|
2016-03-19 16:55:15 +00:00
|
|
|
channel->flags |= MTPDchannel::Flag::f_megagroup;
|
2015-11-13 15:14:33 +00:00
|
|
|
channel->flagsUpdated();
|
|
|
|
}
|
|
|
|
ChatData *cfrom = App::chat(peerFromChat(f.vmigrated_from_chat_id));
|
2015-11-20 18:24:44 +00:00
|
|
|
bool updatedTo = (cfrom->migrateToPtr != channel), updatedFrom = (channel->mgInfo->migrateFromPtr != cfrom);
|
|
|
|
if (updatedTo) {
|
2015-11-13 15:14:33 +00:00
|
|
|
cfrom->migrateToPtr = channel;
|
|
|
|
}
|
2015-11-20 18:24:44 +00:00
|
|
|
if (updatedFrom) {
|
2015-11-13 15:14:33 +00:00
|
|
|
channel->mgInfo->migrateFromPtr = cfrom;
|
|
|
|
if (History *h = App::historyLoaded(cfrom->id)) {
|
|
|
|
if (History *hto = App::historyLoaded(channel->id)) {
|
|
|
|
if (!h->isEmpty()) {
|
|
|
|
h->clear(true);
|
|
|
|
}
|
2016-04-11 10:59:01 +00:00
|
|
|
if (hto->inChatList(Dialogs::Mode::All) && h->inChatList(Dialogs::Mode::All)) {
|
2015-11-13 15:14:33 +00:00
|
|
|
App::removeDialog(h);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2015-11-20 18:24:44 +00:00
|
|
|
Notify::migrateUpdated(channel);
|
2015-11-13 15:14:33 +00:00
|
|
|
}
|
2015-11-20 18:24:44 +00:00
|
|
|
if (updatedTo) {
|
|
|
|
Notify::migrateUpdated(cfrom);
|
2015-11-13 15:14:33 +00:00
|
|
|
App::main()->peerUpdated(cfrom);
|
2015-11-09 09:51:22 +00:00
|
|
|
}
|
|
|
|
}
|
2016-05-20 16:01:06 +00:00
|
|
|
auto &v(f.vbot_info.c_vector().v);
|
2015-11-20 13:34:37 +00:00
|
|
|
for (QVector<MTPBotInfo>::const_iterator i = v.cbegin(), e = v.cend(); i < e; ++i) {
|
|
|
|
switch (i->type()) {
|
|
|
|
case mtpc_botInfo: {
|
2016-04-08 10:44:35 +00:00
|
|
|
const auto &b(i->c_botInfo());
|
2015-11-20 13:34:37 +00:00
|
|
|
UserData *user = App::userLoaded(b.vuser_id.v);
|
|
|
|
if (user) {
|
|
|
|
user->setBotInfo(*i);
|
|
|
|
App::clearPeerUpdated(user);
|
|
|
|
emit fullPeerUpdated(user);
|
|
|
|
}
|
|
|
|
} break;
|
|
|
|
}
|
|
|
|
}
|
2016-05-31 19:27:11 +00:00
|
|
|
channel->setAbout(qs(f.vabout));
|
2016-06-02 13:02:55 +00:00
|
|
|
channel->setMembersCount(f.has_participants_count() ? f.vparticipants_count.v : 0);
|
|
|
|
channel->setAdminsCount(f.has_admins_count() ? f.vadmins_count.v : 0);
|
2016-06-01 13:07:03 +00:00
|
|
|
channel->setInviteLink((f.vexported_invite.type() == mtpc_chatInviteExported) ? qs(f.vexported_invite.c_chatInviteExported().vlink) : QString());
|
2015-09-21 20:57:42 +00:00
|
|
|
if (History *h = App::historyLoaded(channel->id)) {
|
|
|
|
if (h->inboxReadBefore < f.vread_inbox_max_id.v + 1) {
|
2016-05-27 16:47:46 +00:00
|
|
|
h->setUnreadCount(f.vunread_count.v);
|
2015-09-21 20:57:42 +00:00
|
|
|
h->inboxReadBefore = f.vread_inbox_max_id.v + 1;
|
|
|
|
}
|
2016-05-27 16:47:46 +00:00
|
|
|
accumulate_max(h->outboxReadBefore, f.vread_outbox_max_id.v + 1);
|
2015-09-21 20:57:42 +00:00
|
|
|
}
|
2016-03-04 15:34:46 +00:00
|
|
|
if (channel->isMegagroup()) {
|
|
|
|
if (f.has_pinned_msg_id()) {
|
|
|
|
channel->mgInfo->pinnedMsgId = f.vpinned_msg_id.v;
|
|
|
|
} else {
|
|
|
|
channel->mgInfo->pinnedMsgId = 0;
|
|
|
|
}
|
|
|
|
}
|
2015-09-21 20:57:42 +00:00
|
|
|
channel->fullUpdated();
|
2015-09-03 10:48:40 +00:00
|
|
|
|
2016-06-02 13:02:55 +00:00
|
|
|
if (canViewAdmins != channel->canViewAdmins()) Notify::peerUpdatedDelayed(channel, Notify::PeerUpdate::Flag::ChannelCanViewAdmins);
|
|
|
|
if (canViewMembers != channel->canViewMembers()) Notify::peerUpdatedDelayed(channel, Notify::PeerUpdate::Flag::ChannelCanViewMembers);
|
|
|
|
|
2016-06-01 13:07:03 +00:00
|
|
|
notifySettingReceived(MTP_inputNotifyPeer(peer->input), f.vnotify_settings);
|
2015-09-03 10:48:40 +00:00
|
|
|
}
|
2015-03-19 09:18:19 +00:00
|
|
|
|
2015-10-30 15:57:22 +00:00
|
|
|
if (req) {
|
|
|
|
QMap<PeerData*, mtpRequestId>::iterator i = _fullPeerRequests.find(peer);
|
|
|
|
if (i != _fullPeerRequests.cend() && i.value() == req) {
|
|
|
|
_fullPeerRequests.erase(i);
|
|
|
|
}
|
|
|
|
}
|
2015-06-29 22:09:23 +00:00
|
|
|
if (badVersion) {
|
2015-09-03 10:48:40 +00:00
|
|
|
if (peer->isChat()) {
|
|
|
|
peer->asChat()->version = vc.at(0).c_chat().vversion.v;
|
|
|
|
} else if (peer->isChannel()) {
|
|
|
|
peer->asChannel()->version = vc.at(0).c_channel().vversion.v;
|
|
|
|
}
|
2015-06-29 22:09:23 +00:00
|
|
|
requestPeer(peer);
|
|
|
|
}
|
|
|
|
App::clearPeerUpdated(peer);
|
2015-06-15 17:19:24 +00:00
|
|
|
emit fullPeerUpdated(peer);
|
2015-03-19 09:18:19 +00:00
|
|
|
}
|
|
|
|
|
2015-10-30 15:57:22 +00:00
|
|
|
void ApiWrap::gotUserFull(PeerData *peer, const MTPUserFull &result, mtpRequestId req) {
|
2016-04-08 10:44:35 +00:00
|
|
|
const auto &d(result.c_userFull());
|
2016-06-02 13:57:49 +00:00
|
|
|
App::feedUsers(MTP_vector<MTPUser>(1, d.vuser));
|
2016-03-04 15:34:46 +00:00
|
|
|
if (d.has_profile_photo()) {
|
|
|
|
App::feedPhoto(d.vprofile_photo);
|
|
|
|
}
|
2016-06-02 13:57:49 +00:00
|
|
|
App::feedUserLink(MTP_int(peerToUser(peer->id)), d.vlink.c_contacts_link().vmy_link, d.vlink.c_contacts_link().vforeign_link);
|
2016-02-17 17:14:09 +00:00
|
|
|
if (App::main()) {
|
2016-06-01 13:07:03 +00:00
|
|
|
notifySettingReceived(MTP_inputNotifyPeer(peer->input), d.vnotify_settings);
|
2016-02-17 17:14:09 +00:00
|
|
|
}
|
2015-03-19 09:18:19 +00:00
|
|
|
|
2016-03-04 15:34:46 +00:00
|
|
|
if (d.has_bot_info()) {
|
|
|
|
peer->asUser()->setBotInfo(d.vbot_info);
|
|
|
|
} else {
|
|
|
|
peer->asUser()->setBotInfoVersion(-1);
|
|
|
|
}
|
2016-06-01 20:05:37 +00:00
|
|
|
peer->asUser()->setBlockStatus(d.is_blocked() ? UserData::BlockStatus::Blocked : UserData::BlockStatus::NotBlocked);
|
2016-05-31 19:27:11 +00:00
|
|
|
peer->asUser()->setAbout(d.has_about() ? qs(d.vabout) : QString());
|
2015-06-10 15:54:24 +00:00
|
|
|
|
2015-10-30 15:57:22 +00:00
|
|
|
if (req) {
|
|
|
|
QMap<PeerData*, mtpRequestId>::iterator i = _fullPeerRequests.find(peer);
|
|
|
|
if (i != _fullPeerRequests.cend() && i.value() == req) {
|
|
|
|
_fullPeerRequests.erase(i);
|
|
|
|
}
|
|
|
|
}
|
2015-06-29 22:09:23 +00:00
|
|
|
App::clearPeerUpdated(peer);
|
2015-06-15 17:19:24 +00:00
|
|
|
emit fullPeerUpdated(peer);
|
2015-06-29 22:09:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool ApiWrap::gotPeerFullFailed(PeerData *peer, const RPCError &error) {
|
2016-04-08 10:44:35 +00:00
|
|
|
if (MTP::isDefaultHandledError(error)) return false;
|
2015-06-29 22:09:23 +00:00
|
|
|
|
|
|
|
_fullPeerRequests.remove(peer);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void ApiWrap::requestPeer(PeerData *peer) {
|
|
|
|
if (!peer || _fullPeerRequests.contains(peer) || _peerRequests.contains(peer)) return;
|
|
|
|
|
2015-09-03 10:48:40 +00:00
|
|
|
mtpRequestId req = 0;
|
|
|
|
if (peer->isUser()) {
|
2015-06-29 22:09:23 +00:00
|
|
|
req = MTP::send(MTPusers_GetUsers(MTP_vector<MTPInputUser>(1, peer->asUser()->inputUser)), rpcDone(&ApiWrap::gotUser, peer), rpcFail(&ApiWrap::gotPeerFailed, peer));
|
2015-09-03 10:48:40 +00:00
|
|
|
} else if (peer->isChat()) {
|
2015-09-16 21:15:13 +00:00
|
|
|
req = MTP::send(MTPmessages_GetChats(MTP_vector<MTPint>(1, peer->asChat()->inputChat)), rpcDone(&ApiWrap::gotChat, peer), rpcFail(&ApiWrap::gotPeerFailed, peer));
|
2015-09-03 10:48:40 +00:00
|
|
|
} else if (peer->isChannel()) {
|
2015-09-16 21:15:13 +00:00
|
|
|
req = MTP::send(MTPchannels_GetChannels(MTP_vector<MTPInputChannel>(1, peer->asChannel()->inputChannel)), rpcDone(&ApiWrap::gotChat, peer), rpcFail(&ApiWrap::gotPeerFailed, peer));
|
2015-06-29 22:09:23 +00:00
|
|
|
}
|
2015-09-03 10:48:40 +00:00
|
|
|
if (req) _peerRequests.insert(peer, req);
|
2015-06-29 22:09:23 +00:00
|
|
|
}
|
|
|
|
|
2015-08-07 12:11:50 +00:00
|
|
|
void ApiWrap::requestPeers(const QList<PeerData*> &peers) {
|
2015-09-16 21:15:13 +00:00
|
|
|
QVector<MTPint> chats;
|
|
|
|
QVector<MTPInputChannel> channels;
|
2015-08-07 12:11:50 +00:00
|
|
|
QVector<MTPInputUser> users;
|
|
|
|
chats.reserve(peers.size());
|
2015-09-16 21:15:13 +00:00
|
|
|
channels.reserve(peers.size());
|
2015-08-07 12:11:50 +00:00
|
|
|
users.reserve(peers.size());
|
|
|
|
for (QList<PeerData*>::const_iterator i = peers.cbegin(), e = peers.cend(); i != e; ++i) {
|
|
|
|
if (!*i || _fullPeerRequests.contains(*i) || _peerRequests.contains(*i)) continue;
|
2015-09-03 10:48:40 +00:00
|
|
|
if ((*i)->isUser()) {
|
2015-08-07 12:11:50 +00:00
|
|
|
users.push_back((*i)->asUser()->inputUser);
|
2015-09-03 10:48:40 +00:00
|
|
|
} else if ((*i)->isChat()) {
|
|
|
|
chats.push_back((*i)->asChat()->inputChat);
|
|
|
|
} else if ((*i)->isChannel()) {
|
2015-09-16 21:15:13 +00:00
|
|
|
channels.push_back((*i)->asChannel()->inputChannel);
|
2015-08-07 12:11:50 +00:00
|
|
|
}
|
|
|
|
}
|
2015-09-16 21:15:13 +00:00
|
|
|
if (!chats.isEmpty()) MTP::send(MTPmessages_GetChats(MTP_vector<MTPint>(chats)), rpcDone(&ApiWrap::gotChats));
|
|
|
|
if (!channels.isEmpty()) MTP::send(MTPchannels_GetChannels(MTP_vector<MTPInputChannel>(channels)), rpcDone(&ApiWrap::gotChats));
|
2015-08-07 12:11:50 +00:00
|
|
|
if (!users.isEmpty()) MTP::send(MTPusers_GetUsers(MTP_vector<MTPInputUser>(users)), rpcDone(&ApiWrap::gotUsers));
|
|
|
|
}
|
|
|
|
|
2015-11-19 15:56:29 +00:00
|
|
|
void ApiWrap::requestLastParticipants(ChannelData *peer, bool fromStart) {
|
|
|
|
if (!peer || !peer->isMegagroup()) return;
|
2015-11-24 10:40:18 +00:00
|
|
|
bool needAdmins = peer->amEditor(), adminsOutdated = (peer->mgInfo->lastParticipantsStatus & MegagroupInfo::LastParticipantsAdminsOutdated);
|
|
|
|
if ((needAdmins && adminsOutdated) || peer->lastParticipantsCountOutdated()) {
|
2015-11-19 15:56:29 +00:00
|
|
|
fromStart = true;
|
|
|
|
}
|
2016-04-06 11:07:05 +00:00
|
|
|
auto i = _participantsRequests.find(peer);
|
2015-11-19 15:56:29 +00:00
|
|
|
if (i != _participantsRequests.cend()) {
|
|
|
|
if (fromStart && i.value() < 0) { // was not loading from start
|
|
|
|
_participantsRequests.erase(i);
|
|
|
|
} else {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
2016-02-18 16:36:33 +00:00
|
|
|
mtpRequestId req = MTP::send(MTPchannels_GetParticipants(peer->inputChannel, MTP_channelParticipantsRecent(), MTP_int(fromStart ? 0 : peer->mgInfo->lastParticipants.size()), MTP_int(Global::ChatSizeMax())), rpcDone(&ApiWrap::lastParticipantsDone, peer), rpcFail(&ApiWrap::lastParticipantsFail, peer));
|
2015-11-19 15:56:29 +00:00
|
|
|
_participantsRequests.insert(peer, fromStart ? req : -req);
|
2015-11-20 13:34:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void ApiWrap::requestBots(ChannelData *peer) {
|
|
|
|
if (!peer || !peer->isMegagroup() || _botsRequests.contains(peer)) return;
|
2016-02-18 16:36:33 +00:00
|
|
|
_botsRequests.insert(peer, MTP::send(MTPchannels_GetParticipants(peer->inputChannel, MTP_channelParticipantsBots(), MTP_int(0), MTP_int(Global::ChatSizeMax())), rpcDone(&ApiWrap::lastParticipantsDone, peer), rpcFail(&ApiWrap::lastParticipantsFail, peer)));
|
2015-11-09 09:51:22 +00:00
|
|
|
}
|
|
|
|
|
2015-06-29 22:09:23 +00:00
|
|
|
void ApiWrap::gotChat(PeerData *peer, const MTPmessages_Chats &result) {
|
|
|
|
_peerRequests.remove(peer);
|
2016-01-09 11:24:16 +00:00
|
|
|
|
2015-06-29 22:09:23 +00:00
|
|
|
if (result.type() == mtpc_messages_chats) {
|
2016-04-08 10:44:35 +00:00
|
|
|
const auto &v(result.c_messages_chats().vchats.c_vector().v);
|
2015-09-03 10:48:40 +00:00
|
|
|
bool badVersion = false;
|
|
|
|
if (peer->isChat()) {
|
|
|
|
badVersion = (!v.isEmpty() && v.at(0).type() == mtpc_chat && v.at(0).c_chat().vversion.v < peer->asChat()->version);
|
|
|
|
} else if (peer->isChannel()) {
|
|
|
|
badVersion = (!v.isEmpty() && v.at(0).type() == mtpc_channel && v.at(0).c_chat().vversion.v < peer->asChannel()->version);
|
|
|
|
}
|
|
|
|
PeerData *chat = App::feedChats(result.c_messages_chats().vchats);
|
2015-06-29 22:09:23 +00:00
|
|
|
if (chat == peer) {
|
|
|
|
if (badVersion) {
|
2015-09-03 10:48:40 +00:00
|
|
|
if (peer->isChat()) {
|
|
|
|
peer->asChat()->version = v.at(0).c_chat().vversion.v;
|
|
|
|
} else if (peer->isChannel()) {
|
2015-09-06 10:17:09 +00:00
|
|
|
peer->asChannel()->version = v.at(0).c_channel().vversion.v;
|
2015-09-03 10:48:40 +00:00
|
|
|
}
|
2015-06-29 22:09:23 +00:00
|
|
|
requestPeer(peer);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void ApiWrap::gotUser(PeerData *peer, const MTPVector<MTPUser> &result) {
|
|
|
|
_peerRequests.remove(peer);
|
|
|
|
|
|
|
|
UserData *user = App::feedUsers(result);
|
|
|
|
if (user == peer) {
|
|
|
|
}
|
2015-03-19 09:18:19 +00:00
|
|
|
}
|
|
|
|
|
2015-08-07 12:11:50 +00:00
|
|
|
void ApiWrap::gotChats(const MTPmessages_Chats &result) {
|
2016-08-12 15:22:11 +00:00
|
|
|
if (result.type() == mtpc_messages_chats) {
|
|
|
|
auto &d = result.c_messages_chats();
|
|
|
|
App::feedChats(d.vchats);
|
|
|
|
}
|
2015-08-07 12:11:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void ApiWrap::gotUsers(const MTPVector<MTPUser> &result) {
|
|
|
|
App::feedUsers(result);
|
|
|
|
}
|
|
|
|
|
2015-04-04 20:01:34 +00:00
|
|
|
bool ApiWrap::gotPeerFailed(PeerData *peer, const RPCError &error) {
|
2016-04-08 10:44:35 +00:00
|
|
|
if (MTP::isDefaultHandledError(error)) return false;
|
2015-04-04 20:01:34 +00:00
|
|
|
|
2015-06-29 22:09:23 +00:00
|
|
|
_peerRequests.remove(peer);
|
2015-03-19 09:18:19 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2015-11-09 09:51:22 +00:00
|
|
|
void ApiWrap::lastParticipantsDone(ChannelData *peer, const MTPchannels_ChannelParticipants &result, mtpRequestId req) {
|
2015-11-19 15:56:29 +00:00
|
|
|
bool bots = (_botsRequests.value(peer) == req), fromStart = false;
|
|
|
|
if (bots) {
|
|
|
|
_botsRequests.remove(peer);
|
|
|
|
} else {
|
|
|
|
int32 was = _participantsRequests.value(peer);
|
|
|
|
if (was == req) {
|
|
|
|
fromStart = true;
|
|
|
|
} else if (was != -req) {
|
|
|
|
return;
|
|
|
|
}
|
2015-11-09 09:51:22 +00:00
|
|
|
_participantsRequests.remove(peer);
|
|
|
|
}
|
|
|
|
|
2015-11-19 15:56:29 +00:00
|
|
|
if (!peer->mgInfo || result.type() != mtpc_channels_channelParticipants) return;
|
2015-11-09 09:51:22 +00:00
|
|
|
|
2015-11-24 16:19:18 +00:00
|
|
|
History *h = 0;
|
2015-11-19 15:56:29 +00:00
|
|
|
if (bots) {
|
2015-11-24 16:19:18 +00:00
|
|
|
h = App::historyLoaded(peer->id);
|
2015-11-19 15:56:29 +00:00
|
|
|
peer->mgInfo->bots.clear();
|
|
|
|
peer->mgInfo->botStatus = -1;
|
|
|
|
} else if (fromStart) {
|
|
|
|
peer->mgInfo->lastAdmins.clear();
|
|
|
|
peer->mgInfo->lastParticipants.clear();
|
|
|
|
peer->mgInfo->lastParticipantsStatus = MegagroupInfo::LastParticipantsUpToDate;
|
|
|
|
}
|
|
|
|
|
2016-04-08 10:44:35 +00:00
|
|
|
const auto &d(result.c_channels_channelParticipants());
|
|
|
|
const auto &v(d.vparticipants.c_vector().v);
|
2015-11-19 15:56:29 +00:00
|
|
|
App::feedUsers(d.vusers);
|
2015-11-20 13:34:37 +00:00
|
|
|
bool added = false, needBotsInfos = false;
|
2015-11-19 15:56:29 +00:00
|
|
|
int32 botStatus = peer->mgInfo->botStatus;
|
2015-11-24 16:19:18 +00:00
|
|
|
bool keyboardBotFound = !h || !h->lastKeyboardFrom;
|
2015-11-19 15:56:29 +00:00
|
|
|
for (QVector<MTPChannelParticipant>::const_iterator i = v.cbegin(), e = v.cend(); i != e; ++i) {
|
|
|
|
int32 userId = 0;
|
|
|
|
bool admin = false;
|
|
|
|
|
|
|
|
switch (i->type()) {
|
|
|
|
case mtpc_channelParticipant: userId = i->c_channelParticipant().vuser_id.v; break;
|
|
|
|
case mtpc_channelParticipantSelf: userId = i->c_channelParticipantSelf().vuser_id.v; break;
|
|
|
|
case mtpc_channelParticipantModerator: userId = i->c_channelParticipantModerator().vuser_id.v; break;
|
|
|
|
case mtpc_channelParticipantEditor: userId = i->c_channelParticipantEditor().vuser_id.v; admin = true; break;
|
|
|
|
case mtpc_channelParticipantKicked: userId = i->c_channelParticipantKicked().vuser_id.v; break;
|
|
|
|
case mtpc_channelParticipantCreator: userId = i->c_channelParticipantCreator().vuser_id.v; admin = true; break;
|
|
|
|
}
|
|
|
|
UserData *u = App::user(userId);
|
|
|
|
if (bots) {
|
|
|
|
if (u->botInfo) {
|
2016-03-10 15:42:01 +00:00
|
|
|
peer->mgInfo->bots.insert(u);
|
2015-11-20 13:34:37 +00:00
|
|
|
botStatus = 2;// (botStatus > 0/* || !i.key()->botInfo->readsAllHistory*/) ? 2 : 1;
|
|
|
|
if (!u->botInfo->inited) {
|
|
|
|
needBotsInfos = true;
|
|
|
|
}
|
2015-11-09 09:51:22 +00:00
|
|
|
}
|
2015-11-24 16:19:18 +00:00
|
|
|
if (!keyboardBotFound && u->id == h->lastKeyboardFrom) {
|
|
|
|
keyboardBotFound = true;
|
|
|
|
}
|
2015-11-19 15:56:29 +00:00
|
|
|
} else {
|
|
|
|
if (peer->mgInfo->lastParticipants.indexOf(u) < 0) {
|
2015-11-09 09:51:22 +00:00
|
|
|
peer->mgInfo->lastParticipants.push_back(u);
|
2016-03-10 15:42:01 +00:00
|
|
|
if (admin) peer->mgInfo->lastAdmins.insert(u);
|
2015-11-20 13:34:37 +00:00
|
|
|
if (u->botInfo) {
|
2016-03-10 15:42:01 +00:00
|
|
|
peer->mgInfo->bots.insert(u);
|
2015-11-20 13:34:37 +00:00
|
|
|
if (peer->mgInfo->botStatus != 0 && peer->mgInfo->botStatus < 2) {
|
|
|
|
peer->mgInfo->botStatus = 2;
|
|
|
|
}
|
|
|
|
}
|
2015-11-19 15:56:29 +00:00
|
|
|
added = true;
|
2015-11-09 09:51:22 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2015-11-20 13:34:37 +00:00
|
|
|
if (needBotsInfos) {
|
|
|
|
requestFullPeer(peer);
|
|
|
|
}
|
2015-11-24 16:19:18 +00:00
|
|
|
if (!keyboardBotFound) {
|
|
|
|
h->clearLastKeyboard();
|
|
|
|
}
|
2016-06-02 13:02:55 +00:00
|
|
|
int newMembersCount = qMax(d.vcount.v, v.count());
|
|
|
|
if (newMembersCount > peer->membersCount()) {
|
|
|
|
peer->setMembersCount(newMembersCount);
|
2015-11-19 15:56:29 +00:00
|
|
|
}
|
2016-06-03 07:20:24 +00:00
|
|
|
if (!bots) {
|
|
|
|
if (v.isEmpty()) {
|
|
|
|
peer->setMembersCount(peer->mgInfo->lastParticipants.size());
|
|
|
|
}
|
|
|
|
Notify::PeerUpdate update(peer);
|
|
|
|
update.flags |= Notify::PeerUpdate::Flag::MembersChanged | Notify::PeerUpdate::Flag::AdminsChanged;
|
|
|
|
Notify::peerUpdatedDelayed(update);
|
2015-11-19 15:56:29 +00:00
|
|
|
}
|
2016-06-03 07:20:24 +00:00
|
|
|
|
2015-11-19 15:56:29 +00:00
|
|
|
peer->mgInfo->botStatus = botStatus;
|
|
|
|
if (App::main()) emit fullPeerUpdated(peer);
|
2015-11-09 09:51:22 +00:00
|
|
|
}
|
|
|
|
|
2015-11-19 15:56:29 +00:00
|
|
|
bool ApiWrap::lastParticipantsFail(ChannelData *peer, const RPCError &error, mtpRequestId req) {
|
2016-04-08 10:44:35 +00:00
|
|
|
if (MTP::isDefaultHandledError(error)) return false;
|
2015-11-19 15:56:29 +00:00
|
|
|
if (_participantsRequests.value(peer) == req || _participantsRequests.value(peer) == -req) {
|
|
|
|
_participantsRequests.remove(peer);
|
|
|
|
} else if (_botsRequests.value(peer) == req) {
|
|
|
|
_botsRequests.remove(peer);
|
|
|
|
}
|
2015-11-09 09:51:22 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2015-09-21 20:57:42 +00:00
|
|
|
void ApiWrap::requestSelfParticipant(ChannelData *channel) {
|
|
|
|
if (_selfParticipantRequests.contains(channel)) return;
|
|
|
|
_selfParticipantRequests.insert(channel, MTP::send(MTPchannels_GetParticipant(channel->inputChannel, MTP_inputUserSelf()), rpcDone(&ApiWrap::gotSelfParticipant, channel), rpcFail(&ApiWrap::gotSelfParticipantFail, channel), 0, 5));
|
|
|
|
}
|
|
|
|
|
|
|
|
void ApiWrap::gotSelfParticipant(ChannelData *channel, const MTPchannels_ChannelParticipant &result) {
|
|
|
|
_selfParticipantRequests.remove(channel);
|
|
|
|
if (result.type() != mtpc_channels_channelParticipant) {
|
|
|
|
LOG(("API Error: unknown type in gotSelfParticipant (%1)").arg(result.type()));
|
|
|
|
channel->inviter = -1;
|
|
|
|
if (App::main()) App::main()->onSelfParticipantUpdated(channel);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-04-08 10:44:35 +00:00
|
|
|
const auto &p(result.c_channels_channelParticipant());
|
2015-09-21 20:57:42 +00:00
|
|
|
App::feedUsers(p.vusers);
|
|
|
|
|
|
|
|
switch (p.vparticipant.type()) {
|
|
|
|
case mtpc_channelParticipantSelf: {
|
2016-04-08 10:44:35 +00:00
|
|
|
const auto &d(p.vparticipant.c_channelParticipantSelf());
|
2015-09-21 20:57:42 +00:00
|
|
|
channel->inviter = d.vinviter_id.v;
|
|
|
|
channel->inviteDate = date(d.vdate);
|
|
|
|
} break;
|
|
|
|
case mtpc_channelParticipantCreator: {
|
2016-04-08 10:44:35 +00:00
|
|
|
const auto &d(p.vparticipant.c_channelParticipantCreator());
|
2015-09-21 20:57:42 +00:00
|
|
|
channel->inviter = MTP::authedId();
|
|
|
|
channel->inviteDate = date(MTP_int(channel->date));
|
|
|
|
} break;
|
|
|
|
case mtpc_channelParticipantModerator: {
|
2016-04-08 10:44:35 +00:00
|
|
|
const auto &d(p.vparticipant.c_channelParticipantModerator());
|
2015-09-21 20:57:42 +00:00
|
|
|
channel->inviter = d.vinviter_id.v;
|
|
|
|
channel->inviteDate = date(d.vdate);
|
|
|
|
} break;
|
|
|
|
case mtpc_channelParticipantEditor: {
|
2016-04-08 10:44:35 +00:00
|
|
|
const auto &d(p.vparticipant.c_channelParticipantEditor());
|
2015-09-21 20:57:42 +00:00
|
|
|
channel->inviter = d.vinviter_id.v;
|
|
|
|
channel->inviteDate = date(d.vdate);
|
|
|
|
} break;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
if (App::main()) App::main()->onSelfParticipantUpdated(channel);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool ApiWrap::gotSelfParticipantFail(ChannelData *channel, const RPCError &error) {
|
2016-04-08 10:44:35 +00:00
|
|
|
if (MTP::isDefaultHandledError(error)) return false;
|
2015-09-21 20:57:42 +00:00
|
|
|
|
|
|
|
if (error.type() == qstr("USER_NOT_PARTICIPANT")) {
|
|
|
|
channel->inviter = -1;
|
|
|
|
}
|
|
|
|
_selfParticipantRequests.remove(channel);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2015-11-09 09:51:22 +00:00
|
|
|
void ApiWrap::kickParticipant(PeerData *peer, UserData *user) {
|
|
|
|
KickRequest req(peer, user);
|
2015-11-20 19:03:31 +00:00
|
|
|
if (_kickRequests.contains(req)) return;
|
|
|
|
|
2015-11-09 09:51:22 +00:00
|
|
|
if (peer->isChannel()) {
|
|
|
|
_kickRequests.insert(req, MTP::send(MTPchannels_KickFromChannel(peer->asChannel()->inputChannel, user->inputUser, MTP_bool(true)), rpcDone(&ApiWrap::kickParticipantDone, req), rpcFail(&ApiWrap::kickParticipantFail, req)));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void ApiWrap::kickParticipantDone(KickRequest kick, const MTPUpdates &result, mtpRequestId req) {
|
|
|
|
_kickRequests.remove(kick);
|
|
|
|
if (kick.first->isMegagroup()) {
|
2016-06-02 13:02:55 +00:00
|
|
|
auto channel = kick.first->asChannel();
|
|
|
|
auto megagroupInfo = channel->mgInfo;
|
|
|
|
|
|
|
|
int32 i = megagroupInfo->lastParticipants.indexOf(kick.second);
|
2015-11-09 09:51:22 +00:00
|
|
|
if (i >= 0) {
|
2016-06-02 13:02:55 +00:00
|
|
|
megagroupInfo->lastParticipants.removeAt(i);
|
2015-11-09 09:51:22 +00:00
|
|
|
}
|
2016-06-02 13:02:55 +00:00
|
|
|
|
|
|
|
if (channel->membersCount() > 1) {
|
|
|
|
channel->setMembersCount(channel->membersCount() - 1);
|
2015-11-19 15:56:29 +00:00
|
|
|
} else {
|
2016-06-02 13:02:55 +00:00
|
|
|
megagroupInfo->lastParticipantsStatus |= MegagroupInfo::LastParticipantsCountOutdated;
|
|
|
|
megagroupInfo->lastParticipantsCount = 0;
|
2015-11-09 09:51:22 +00:00
|
|
|
}
|
2016-06-02 13:02:55 +00:00
|
|
|
if (megagroupInfo->lastAdmins.contains(kick.second)) {
|
|
|
|
megagroupInfo->lastAdmins.remove(kick.second);
|
|
|
|
if (channel->adminsCount() > 1) {
|
|
|
|
channel->setAdminsCount(channel->adminsCount() - 1);
|
2016-03-14 06:10:24 +00:00
|
|
|
}
|
2016-06-03 07:20:24 +00:00
|
|
|
Notify::peerUpdatedDelayed(channel, Notify::PeerUpdate::Flag::AdminsChanged);
|
2016-03-14 06:10:24 +00:00
|
|
|
}
|
2016-06-02 13:02:55 +00:00
|
|
|
megagroupInfo->bots.remove(kick.second);
|
|
|
|
if (megagroupInfo->bots.isEmpty() && megagroupInfo->botStatus > 0) {
|
|
|
|
megagroupInfo->botStatus = -1;
|
2016-03-14 06:10:24 +00:00
|
|
|
}
|
2015-11-09 09:51:22 +00:00
|
|
|
}
|
2016-06-03 07:20:24 +00:00
|
|
|
Notify::peerUpdatedDelayed(kick.first, Notify::PeerUpdate::Flag::MembersChanged);
|
2015-11-09 09:51:22 +00:00
|
|
|
emit fullPeerUpdated(kick.first);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool ApiWrap::kickParticipantFail(KickRequest kick, const RPCError &error, mtpRequestId req) {
|
2016-04-08 10:44:35 +00:00
|
|
|
if (MTP::isDefaultHandledError(error)) return false;
|
2015-11-09 09:51:22 +00:00
|
|
|
_kickRequests.remove(kick);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2015-06-28 12:37:10 +00:00
|
|
|
void ApiWrap::scheduleStickerSetRequest(uint64 setId, uint64 access) {
|
|
|
|
if (!_stickerSetRequests.contains(setId)) {
|
|
|
|
_stickerSetRequests.insert(setId, qMakePair(access, 0));
|
|
|
|
}
|
|
|
|
}
|
2015-03-19 09:18:19 +00:00
|
|
|
|
2015-06-28 12:37:10 +00:00
|
|
|
void ApiWrap::requestStickerSets() {
|
|
|
|
for (QMap<uint64, QPair<uint64, mtpRequestId> >::iterator i = _stickerSetRequests.begin(), j = i, e = _stickerSetRequests.end(); i != e; i = j) {
|
2016-01-09 12:51:42 +00:00
|
|
|
++j;
|
2015-06-28 12:37:10 +00:00
|
|
|
if (i.value().second) continue;
|
|
|
|
|
|
|
|
int32 wait = (j == e) ? 0 : 10;
|
|
|
|
i.value().second = MTP::send(MTPmessages_GetStickerSet(MTP_inputStickerSetID(MTP_long(i.key()), MTP_long(i.value().first))), rpcDone(&ApiWrap::gotStickerSet, i.key()), rpcFail(&ApiWrap::gotStickerSetFail, i.key()), 0, wait);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-05-25 17:59:21 +00:00
|
|
|
void ApiWrap::joinChannel(ChannelData *channel) {
|
|
|
|
if (channel->amIn()) {
|
|
|
|
channelAmInUpdated(channel);
|
|
|
|
} else if (!_channelAmInRequests.contains(channel)) {
|
|
|
|
auto requestId = MTP::send(MTPchannels_JoinChannel(channel->inputChannel), rpcDone(&ApiWrap::channelAmInDone, channel), rpcFail(&ApiWrap::channelAmInFail, channel));
|
|
|
|
_channelAmInRequests.insert(channel, requestId);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void ApiWrap::leaveChannel(ChannelData *channel) {
|
|
|
|
if (!channel->amIn()) {
|
|
|
|
channelAmInUpdated(channel);
|
|
|
|
} else if (!_channelAmInRequests.contains(channel)) {
|
|
|
|
auto requestId = MTP::send(MTPchannels_LeaveChannel(channel->inputChannel), rpcDone(&ApiWrap::channelAmInDone, channel), rpcFail(&ApiWrap::channelAmInFail, channel));
|
|
|
|
_channelAmInRequests.insert(channel, requestId);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void ApiWrap::channelAmInUpdated(ChannelData *channel) {
|
2016-06-01 20:05:37 +00:00
|
|
|
Notify::peerUpdatedDelayed(channel, Notify::PeerUpdate::Flag::ChannelAmIn);
|
2016-05-25 17:59:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void ApiWrap::channelAmInDone(ChannelData *channel, const MTPUpdates &updates) {
|
|
|
|
_channelAmInRequests.remove(channel);
|
|
|
|
|
|
|
|
updatesReceived(updates);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool ApiWrap::channelAmInFail(ChannelData *channel, const RPCError &error) {
|
|
|
|
if (MTP::isDefaultHandledError(error)) return false;
|
|
|
|
|
2016-06-07 19:59:39 +00:00
|
|
|
if (error.type() == qstr("CHANNELS_TOO_MUCH")) {
|
|
|
|
Ui::showLayer(new InformBox(lang(lng_join_channel_error)));
|
|
|
|
}
|
2016-05-25 17:59:21 +00:00
|
|
|
_channelAmInRequests.remove(channel);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2016-06-01 20:05:37 +00:00
|
|
|
void ApiWrap::blockUser(UserData *user) {
|
|
|
|
if (user->isBlocked()) {
|
|
|
|
Notify::peerUpdatedDelayed(user, Notify::PeerUpdate::Flag::UserIsBlocked);
|
|
|
|
} else if (!_blockRequests.contains(user)) {
|
|
|
|
auto requestId = MTP::send(MTPcontacts_Block(user->inputUser), rpcDone(&ApiWrap::blockDone, user), rpcFail(&ApiWrap::blockFail, user));
|
|
|
|
_blockRequests.insert(user, requestId);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void ApiWrap::unblockUser(UserData *user) {
|
|
|
|
if (!user->isBlocked()) {
|
|
|
|
Notify::peerUpdatedDelayed(user, Notify::PeerUpdate::Flag::UserIsBlocked);
|
|
|
|
} else if (!_blockRequests.contains(user)) {
|
|
|
|
auto requestId = MTP::send(MTPcontacts_Unblock(user->inputUser), rpcDone(&ApiWrap::unblockDone, user), rpcFail(&ApiWrap::blockFail, user));
|
|
|
|
_blockRequests.insert(user, requestId);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void ApiWrap::blockDone(UserData *user, const MTPBool &result) {
|
|
|
|
_blockRequests.remove(user);
|
|
|
|
user->setBlockStatus(UserData::BlockStatus::Blocked);
|
|
|
|
emit App::main()->peerUpdated(user);
|
|
|
|
}
|
|
|
|
|
|
|
|
void ApiWrap::unblockDone(UserData *user, const MTPBool &result) {
|
|
|
|
_blockRequests.remove(user);
|
|
|
|
user->setBlockStatus(UserData::BlockStatus::NotBlocked);
|
|
|
|
emit App::main()->peerUpdated(user);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool ApiWrap::blockFail(UserData *user, const RPCError &error) {
|
|
|
|
if (MTP::isDefaultHandledError(error)) return false;
|
|
|
|
|
|
|
|
_blockRequests.remove(user);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2016-06-01 13:07:03 +00:00
|
|
|
void ApiWrap::exportInviteLink(PeerData *peer) {
|
|
|
|
if (_exportInviteRequests.contains(peer)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
mtpRequestId request = 0;
|
|
|
|
if (auto chat = peer->asChat()) {
|
|
|
|
request = MTP::send(MTPmessages_ExportChatInvite(chat->inputChat), rpcDone(&ApiWrap::exportInviteDone, peer), rpcFail(&ApiWrap::exportInviteFail, peer));
|
|
|
|
} else if (auto channel = peer->asChannel()) {
|
|
|
|
request = MTP::send(MTPchannels_ExportInvite(channel->inputChannel), rpcDone(&ApiWrap::exportInviteDone, peer), rpcFail(&ApiWrap::exportInviteFail, peer));
|
|
|
|
}
|
|
|
|
if (request) {
|
|
|
|
_exportInviteRequests.insert(peer, request);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void ApiWrap::exportInviteDone(PeerData *peer, const MTPExportedChatInvite &result) {
|
|
|
|
_exportInviteRequests.remove(peer);
|
|
|
|
if (auto chat = peer->asChat()) {
|
|
|
|
chat->setInviteLink((result.type() == mtpc_chatInviteExported) ? qs(result.c_chatInviteExported().vlink) : QString());
|
|
|
|
} else if (auto channel = peer->asChannel()) {
|
|
|
|
channel->setInviteLink((result.type() == mtpc_chatInviteExported) ? qs(result.c_chatInviteExported().vlink) : QString());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool ApiWrap::exportInviteFail(PeerData *peer, const RPCError &error) {
|
|
|
|
if (MTP::isDefaultHandledError(error)) return false;
|
|
|
|
|
|
|
|
_exportInviteRequests.remove(peer);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void ApiWrap::requestNotifySetting(PeerData *peer) {
|
|
|
|
if (_notifySettingRequests.contains(peer)) return;
|
|
|
|
|
|
|
|
MTPInputNotifyPeer notifyPeer = MTP_inputNotifyPeer(peer->input);
|
|
|
|
auto requestId = MTP::send(MTPaccount_GetNotifySettings(notifyPeer), rpcDone(&ApiWrap::notifySettingDone, notifyPeer), rpcFail(&ApiWrap::notifySettingFail, peer));
|
|
|
|
_notifySettingRequests.insert(peer, requestId);
|
|
|
|
}
|
|
|
|
|
2016-06-07 19:59:39 +00:00
|
|
|
void ApiWrap::saveDraftToCloudDelayed(History *history) {
|
|
|
|
_draftsSaveRequestIds.insert(history, 0);
|
|
|
|
if (!_draftsSaveTimer.isActive()) {
|
|
|
|
_draftsSaveTimer.start(SaveCloudDraftTimeout);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool ApiWrap::hasUnsavedDrafts() const {
|
|
|
|
return !_draftsSaveRequestIds.isEmpty();
|
|
|
|
}
|
|
|
|
|
|
|
|
void ApiWrap::saveDraftsToCloud() {
|
|
|
|
for (auto i = _draftsSaveRequestIds.begin(), e = _draftsSaveRequestIds.end(); i != e; ++i) {
|
|
|
|
if (i.value()) continue; // sent already
|
|
|
|
|
|
|
|
auto history = i.key();
|
|
|
|
auto cloudDraft = history->cloudDraft();
|
|
|
|
auto localDraft = history->localDraft();
|
|
|
|
if (cloudDraft && cloudDraft->saveRequestId) {
|
|
|
|
MTP::cancel(cloudDraft->saveRequestId);
|
|
|
|
}
|
|
|
|
cloudDraft = history->createCloudDraft(localDraft);
|
|
|
|
|
|
|
|
MTPmessages_SaveDraft::Flags flags = 0;
|
|
|
|
auto &textWithTags = cloudDraft->textWithTags;
|
|
|
|
if (cloudDraft->previewCancelled) {
|
|
|
|
flags |= MTPmessages_SaveDraft::Flag::f_no_webpage;
|
|
|
|
}
|
|
|
|
if (cloudDraft->msgId) {
|
|
|
|
flags |= MTPmessages_SaveDraft::Flag::f_reply_to_msg_id;
|
|
|
|
}
|
|
|
|
if (!textWithTags.tags.isEmpty()) {
|
|
|
|
flags |= MTPmessages_SaveDraft::Flag::f_entities;
|
|
|
|
}
|
|
|
|
auto entities = linksToMTP(entitiesFromTextTags(textWithTags.tags), true);
|
|
|
|
cloudDraft->saveRequestId = MTP::send(MTPmessages_SaveDraft(MTP_flags(flags), MTP_int(cloudDraft->msgId), history->peer->input, MTP_string(textWithTags.text), entities), rpcDone(&ApiWrap::saveCloudDraftDone, history), rpcFail(&ApiWrap::saveCloudDraftFail, history));
|
|
|
|
i.value() = cloudDraft->saveRequestId;
|
|
|
|
}
|
|
|
|
if (_draftsSaveRequestIds.isEmpty()) {
|
|
|
|
App::allDraftsSaved(); // can quit the application
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void ApiWrap::saveCloudDraftDone(History *history, const MTPBool &result, mtpRequestId requestId) {
|
|
|
|
if (auto cloudDraft = history->cloudDraft()) {
|
|
|
|
if (cloudDraft->saveRequestId == requestId) {
|
|
|
|
cloudDraft->saveRequestId = 0;
|
2016-06-09 14:31:10 +00:00
|
|
|
history->draftSavedToCloud();
|
2016-06-07 19:59:39 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
auto i = _draftsSaveRequestIds.find(history);
|
|
|
|
if (i != _draftsSaveRequestIds.cend() && i.value() == requestId) {
|
|
|
|
_draftsSaveRequestIds.remove(history);
|
|
|
|
if (_draftsSaveRequestIds.isEmpty()) {
|
|
|
|
App::allDraftsSaved(); // can quit the application
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool ApiWrap::saveCloudDraftFail(History *history, const RPCError &error, mtpRequestId requestId) {
|
|
|
|
if (MTP::isDefaultHandledError(error)) return false;
|
|
|
|
|
|
|
|
if (auto cloudDraft = history->cloudDraft()) {
|
|
|
|
if (cloudDraft->saveRequestId == requestId) {
|
|
|
|
history->clearCloudDraft();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
auto i = _draftsSaveRequestIds.find(history);
|
|
|
|
if (i != _draftsSaveRequestIds.cend() && i.value() == requestId) {
|
|
|
|
_draftsSaveRequestIds.remove(history);
|
|
|
|
if (_draftsSaveRequestIds.isEmpty()) {
|
|
|
|
App::allDraftsSaved(); // can quit the application
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2016-06-01 13:07:03 +00:00
|
|
|
void ApiWrap::notifySettingDone(MTPInputNotifyPeer notifyPeer, const MTPPeerNotifySettings &result) {
|
|
|
|
if (auto requestedPeer = notifySettingReceived(notifyPeer, result)) {
|
|
|
|
_notifySettingRequests.remove(requestedPeer);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
PeerData *ApiWrap::notifySettingReceived(MTPInputNotifyPeer notifyPeer, const MTPPeerNotifySettings &settings) {
|
|
|
|
PeerData *requestedPeer = nullptr;
|
|
|
|
switch (notifyPeer.type()) {
|
|
|
|
case mtpc_inputNotifyAll: App::main()->applyNotifySetting(MTP_notifyAll(), settings); break;
|
|
|
|
case mtpc_inputNotifyUsers: App::main()->applyNotifySetting(MTP_notifyUsers(), settings); break;
|
|
|
|
case mtpc_inputNotifyChats: App::main()->applyNotifySetting(MTP_notifyChats(), settings); break;
|
|
|
|
case mtpc_inputNotifyPeer: {
|
|
|
|
auto &peer = notifyPeer.c_inputNotifyPeer().vpeer;
|
|
|
|
switch (peer.type()) {
|
|
|
|
case mtpc_inputPeerEmpty: App::main()->applyNotifySetting(MTP_notifyPeer(MTP_peerUser(MTP_int(0))), settings); break;
|
|
|
|
case mtpc_inputPeerSelf: requestedPeer = App::self(); break;
|
|
|
|
case mtpc_inputPeerUser: requestedPeer = App::user(peerFromUser(peer.c_inputPeerUser().vuser_id)); break;
|
|
|
|
case mtpc_inputPeerChat: requestedPeer = App::chat(peerFromChat(peer.c_inputPeerChat().vchat_id)); break;
|
|
|
|
case mtpc_inputPeerChannel: requestedPeer = App::channel(peerFromChannel(peer.c_inputPeerChannel().vchannel_id)); break;
|
|
|
|
}
|
|
|
|
if (requestedPeer) {
|
|
|
|
App::main()->applyNotifySetting(MTP_notifyPeer(peerToMTP(requestedPeer->id)), settings);
|
|
|
|
}
|
|
|
|
} break;
|
|
|
|
}
|
|
|
|
App::wnd()->notifySettingGot();
|
|
|
|
return requestedPeer;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool ApiWrap::notifySettingFail(PeerData *peer, const RPCError &error) {
|
|
|
|
if (MTP::isDefaultHandledError(error)) return false;
|
|
|
|
|
|
|
|
notifySettingReceived(MTP_inputNotifyPeer(peer->input), MTP_peerNotifySettingsEmpty());
|
|
|
|
_notifySettingRequests.remove(peer);
|
|
|
|
return true;
|
|
|
|
}
|
2016-05-25 17:59:21 +00:00
|
|
|
|
2015-06-28 12:37:10 +00:00
|
|
|
void ApiWrap::gotStickerSet(uint64 setId, const MTPmessages_StickerSet &result) {
|
|
|
|
_stickerSetRequests.remove(setId);
|
2016-01-09 11:24:16 +00:00
|
|
|
|
2015-06-28 12:37:10 +00:00
|
|
|
if (result.type() != mtpc_messages_stickerSet) return;
|
2016-06-28 18:05:38 +00:00
|
|
|
auto &d(result.c_messages_stickerSet());
|
2016-01-09 11:24:16 +00:00
|
|
|
|
2015-06-28 12:37:10 +00:00
|
|
|
if (d.vset.type() != mtpc_stickerSet) return;
|
2016-06-28 18:05:38 +00:00
|
|
|
auto &s(d.vset.c_stickerSet());
|
2015-06-28 12:37:10 +00:00
|
|
|
|
2016-06-27 16:25:21 +00:00
|
|
|
auto &sets = Global::RefStickerSets();
|
2016-03-19 16:55:15 +00:00
|
|
|
auto it = sets.find(setId);
|
2015-06-28 12:37:10 +00:00
|
|
|
if (it == sets.cend()) return;
|
|
|
|
|
|
|
|
it->access = s.vaccess_hash.v;
|
|
|
|
it->hash = s.vhash.v;
|
|
|
|
it->shortName = qs(s.vshort_name);
|
2015-12-02 17:17:53 +00:00
|
|
|
it->title = stickerSetTitle(s);
|
2016-07-15 15:58:52 +00:00
|
|
|
auto clientFlags = it->flags & (MTPDstickerSet_ClientFlag::f_featured | MTPDstickerSet_ClientFlag::f_unread | MTPDstickerSet_ClientFlag::f_not_loaded | MTPDstickerSet_ClientFlag::f_special);
|
2016-06-27 16:25:21 +00:00
|
|
|
it->flags = s.vflags.v | clientFlags;
|
|
|
|
it->flags &= ~MTPDstickerSet_ClientFlag::f_not_loaded;
|
2015-06-28 12:37:10 +00:00
|
|
|
|
2016-04-08 10:44:35 +00:00
|
|
|
const auto &d_docs(d.vdocuments.c_vector().v);
|
2016-03-19 16:55:15 +00:00
|
|
|
auto custom = sets.find(Stickers::CustomSetId);
|
2015-06-28 12:37:10 +00:00
|
|
|
|
2015-12-02 17:17:53 +00:00
|
|
|
StickerPack pack;
|
|
|
|
pack.reserve(d_docs.size());
|
2015-06-28 12:37:10 +00:00
|
|
|
for (int32 i = 0, l = d_docs.size(); i != l; ++i) {
|
|
|
|
DocumentData *doc = App::feedDocument(d_docs.at(i));
|
2015-06-30 21:07:05 +00:00
|
|
|
if (!doc || !doc->sticker()) continue;
|
2015-06-28 12:37:10 +00:00
|
|
|
|
2015-12-02 17:17:53 +00:00
|
|
|
pack.push_back(doc);
|
2015-06-28 12:37:10 +00:00
|
|
|
if (custom != sets.cend()) {
|
|
|
|
int32 index = custom->stickers.indexOf(doc);
|
|
|
|
if (index >= 0) {
|
|
|
|
custom->stickers.removeAt(index);
|
|
|
|
}
|
2015-03-19 09:18:19 +00:00
|
|
|
}
|
|
|
|
}
|
2015-06-28 12:37:10 +00:00
|
|
|
if (custom != sets.cend() && custom->stickers.isEmpty()) {
|
|
|
|
sets.erase(custom);
|
|
|
|
custom = sets.end();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool writeRecent = false;
|
|
|
|
RecentStickerPack &recent(cGetRecentStickers());
|
2015-12-02 17:17:53 +00:00
|
|
|
for (RecentStickerPack::iterator i = recent.begin(); i != recent.cend();) {
|
|
|
|
if (it->stickers.indexOf(i->first) >= 0 && pack.indexOf(i->first) < 0) {
|
|
|
|
i = recent.erase(i);
|
|
|
|
writeRecent = true;
|
|
|
|
} else {
|
|
|
|
++i;
|
2015-03-19 09:18:19 +00:00
|
|
|
}
|
2015-12-02 17:17:53 +00:00
|
|
|
}
|
2016-01-09 11:24:16 +00:00
|
|
|
|
2015-12-02 17:17:53 +00:00
|
|
|
if (pack.isEmpty()) {
|
2016-03-19 16:55:15 +00:00
|
|
|
int removeIndex = Global::StickerSetsOrder().indexOf(setId);
|
|
|
|
if (removeIndex >= 0) Global::RefStickerSetsOrder().removeAt(removeIndex);
|
2015-06-28 12:37:10 +00:00
|
|
|
sets.erase(it);
|
|
|
|
} else {
|
2015-12-02 17:17:53 +00:00
|
|
|
it->stickers = pack;
|
2016-01-09 12:51:42 +00:00
|
|
|
it->emoji.clear();
|
2016-04-08 10:44:35 +00:00
|
|
|
const auto &v(d.vpacks.c_vector().v);
|
2016-01-09 12:51:42 +00:00
|
|
|
for (int32 i = 0, l = v.size(); i < l; ++i) {
|
|
|
|
if (v.at(i).type() != mtpc_stickerPack) continue;
|
|
|
|
|
2016-04-08 10:44:35 +00:00
|
|
|
const auto &pack(v.at(i).c_stickerPack());
|
2016-01-09 12:51:42 +00:00
|
|
|
if (EmojiPtr e = emojiGetNoColor(emojiFromText(qs(pack.vemoticon)))) {
|
2016-04-08 10:44:35 +00:00
|
|
|
const auto &stickers(pack.vdocuments.c_vector().v);
|
2016-01-09 12:51:42 +00:00
|
|
|
StickerPack p;
|
|
|
|
p.reserve(stickers.size());
|
|
|
|
for (int32 j = 0, c = stickers.size(); j < c; ++j) {
|
|
|
|
DocumentData *doc = App::document(stickers.at(j).v);
|
|
|
|
if (!doc || !doc->sticker()) continue;
|
|
|
|
|
|
|
|
p.push_back(doc);
|
|
|
|
}
|
|
|
|
it->emoji.insert(e, p);
|
|
|
|
}
|
|
|
|
}
|
2015-06-28 12:37:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (writeRecent) {
|
|
|
|
Local::writeUserSettings();
|
|
|
|
}
|
|
|
|
|
2016-07-21 10:09:47 +00:00
|
|
|
if (it->flags & MTPDstickerSet::Flag::f_installed) {
|
|
|
|
if (!(it->flags & MTPDstickerSet::Flag::f_archived)) {
|
|
|
|
Local::writeInstalledStickers();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (it->flags & MTPDstickerSet_ClientFlag::f_featured) {
|
|
|
|
Local::writeFeaturedStickers();
|
|
|
|
}
|
2015-06-28 12:37:10 +00:00
|
|
|
|
|
|
|
if (App::main()) emit App::main()->stickersUpdated();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool ApiWrap::gotStickerSetFail(uint64 setId, const RPCError &error) {
|
2016-04-08 10:44:35 +00:00
|
|
|
if (MTP::isDefaultHandledError(error)) return false;
|
2015-06-28 12:37:10 +00:00
|
|
|
|
|
|
|
_stickerSetRequests.remove(setId);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void ApiWrap::requestWebPageDelayed(WebPageData *page) {
|
|
|
|
if (page->pendingTill <= 0) return;
|
|
|
|
_webPagesPending.insert(page, 0);
|
|
|
|
int32 left = (page->pendingTill - unixtime()) * 1000;
|
|
|
|
if (!_webPagesTimer.isActive() || left <= _webPagesTimer.remainingTime()) {
|
|
|
|
_webPagesTimer.start((left < 0 ? 0 : left) + 1);
|
2015-03-19 09:18:19 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-06-28 12:37:10 +00:00
|
|
|
void ApiWrap::clearWebPageRequest(WebPageData *page) {
|
|
|
|
_webPagesPending.remove(page);
|
|
|
|
if (_webPagesPending.isEmpty() && _webPagesTimer.isActive()) _webPagesTimer.stop();
|
|
|
|
}
|
|
|
|
|
|
|
|
void ApiWrap::clearWebPageRequests() {
|
|
|
|
_webPagesPending.clear();
|
|
|
|
_webPagesTimer.stop();
|
|
|
|
}
|
|
|
|
|
2015-04-04 20:01:34 +00:00
|
|
|
void ApiWrap::resolveWebPages() {
|
2015-09-12 11:59:50 +00:00
|
|
|
MessageIds ids; // temp_req_id = -1
|
|
|
|
typedef QPair<int32, MessageIds> IndexAndMessageIds;
|
|
|
|
typedef QMap<ChannelData*, IndexAndMessageIds> MessageIdsByChannel;
|
|
|
|
MessageIdsByChannel idsByChannel; // temp_req_id = -index - 2
|
|
|
|
|
2015-04-04 20:01:34 +00:00
|
|
|
const WebPageItems &items(App::webPageItems());
|
|
|
|
ids.reserve(_webPagesPending.size());
|
|
|
|
int32 t = unixtime(), m = INT_MAX;
|
2015-09-12 11:59:50 +00:00
|
|
|
for (WebPagesPending::iterator i = _webPagesPending.begin(); i != _webPagesPending.cend(); ++i) {
|
|
|
|
if (i.value() > 0) continue;
|
2015-04-04 20:01:34 +00:00
|
|
|
if (i.key()->pendingTill <= t) {
|
|
|
|
WebPageItems::const_iterator j = items.constFind(i.key());
|
|
|
|
if (j != items.cend() && !j.value().isEmpty()) {
|
2015-09-12 11:59:50 +00:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
2015-04-04 20:01:34 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
m = qMin(m, i.key()->pendingTill - t);
|
|
|
|
}
|
|
|
|
}
|
2015-09-12 11:59:50 +00:00
|
|
|
|
2015-09-13 08:41:27 +00:00
|
|
|
mtpRequestId req = ids.isEmpty() ? 0 : MTP::send(MTPmessages_GetMessages(MTP_vector<MTPint>(ids)), rpcDone(&ApiWrap::gotWebPages, (ChannelData*)0), RPCFailHandlerPtr(), 0, 5);
|
2015-09-12 11:59:50 +00:00
|
|
|
typedef QVector<mtpRequestId> RequestIds;
|
|
|
|
RequestIds reqsByIndex(idsByChannel.size(), 0);
|
|
|
|
for (MessageIdsByChannel::const_iterator i = idsByChannel.cbegin(), e = idsByChannel.cend(); i != e; ++i) {
|
2015-09-16 21:15:13 +00:00
|
|
|
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);
|
2015-09-12 11:59:50 +00:00
|
|
|
}
|
|
|
|
if (req || !reqsByIndex.isEmpty()) {
|
2015-04-04 20:01:34 +00:00
|
|
|
for (WebPagesPending::iterator i = _webPagesPending.begin(); i != _webPagesPending.cend(); ++i) {
|
2015-09-12 11:59:50 +00:00
|
|
|
if (i.value() > 0) continue;
|
|
|
|
if (i.value() < 0) {
|
|
|
|
if (i.value() == -1) {
|
|
|
|
i.value() = req;
|
|
|
|
} else {
|
|
|
|
i.value() = reqsByIndex[-i.value() - 2];
|
|
|
|
}
|
2015-04-04 20:01:34 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2015-09-12 11:59:50 +00:00
|
|
|
|
2015-04-04 20:01:34 +00:00
|
|
|
if (m < INT_MAX) _webPagesTimer.start(m * 1000);
|
|
|
|
}
|
|
|
|
|
2015-11-13 15:14:33 +00:00
|
|
|
void ApiWrap::delayedRequestParticipantsCount() {
|
|
|
|
if (App::main() && App::main()->peer() && App::main()->peer()->isChannel()) {
|
|
|
|
requestFullPeer(App::main()->peer());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-09-13 08:41:27 +00:00
|
|
|
void ApiWrap::gotWebPages(ChannelData *channel, const MTPmessages_Messages &msgs, mtpRequestId req) {
|
2015-04-04 20:01:34 +00:00
|
|
|
const QVector<MTPMessage> *v = 0;
|
|
|
|
switch (msgs.type()) {
|
2015-09-04 13:01:31 +00:00
|
|
|
case mtpc_messages_messages: {
|
2016-04-08 10:44:35 +00:00
|
|
|
const auto &d(msgs.c_messages_messages());
|
2015-09-04 13:01:31 +00:00
|
|
|
App::feedUsers(d.vusers);
|
|
|
|
App::feedChats(d.vchats);
|
|
|
|
v = &d.vmessages.c_vector().v;
|
|
|
|
} break;
|
|
|
|
|
|
|
|
case mtpc_messages_messagesSlice: {
|
2016-04-08 10:44:35 +00:00
|
|
|
const auto &d(msgs.c_messages_messagesSlice());
|
2015-09-04 13:01:31 +00:00
|
|
|
App::feedUsers(d.vusers);
|
|
|
|
App::feedChats(d.vchats);
|
|
|
|
v = &d.vmessages.c_vector().v;
|
|
|
|
} break;
|
|
|
|
|
|
|
|
case mtpc_messages_channelMessages: {
|
2016-05-27 16:47:46 +00:00
|
|
|
auto &d(msgs.c_messages_channelMessages());
|
2015-09-13 08:41:27 +00:00
|
|
|
if (channel) {
|
|
|
|
channel->ptsReceived(d.vpts.v);
|
|
|
|
} else {
|
2015-09-19 09:13:21 +00:00
|
|
|
LOG(("API Error: received messages.channelMessages when no channel was passed! (ApiWrap::gotWebPages)"));
|
|
|
|
}
|
2015-09-04 13:01:31 +00:00
|
|
|
App::feedUsers(d.vusers);
|
|
|
|
App::feedChats(d.vchats);
|
|
|
|
v = &d.vmessages.c_vector().v;
|
|
|
|
} break;
|
2015-04-04 20:01:34 +00:00
|
|
|
}
|
|
|
|
|
2015-09-04 13:01:31 +00:00
|
|
|
if (!v) return;
|
|
|
|
QMap<uint64, int32> msgsIds; // copied from feedMsgs
|
2015-04-04 20:01:34 +00:00
|
|
|
for (int32 i = 0, l = v->size(); i < l; ++i) {
|
2016-04-08 10:44:35 +00:00
|
|
|
const auto &msg(v->at(i));
|
2015-04-04 20:01:34 +00:00
|
|
|
switch (msg.type()) {
|
2015-09-04 13:01:31 +00:00
|
|
|
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;
|
2015-04-04 20:01:34 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-09-04 13:01:31 +00:00
|
|
|
for (QMap<uint64, int32>::const_iterator i = msgsIds.cbegin(), e = msgsIds.cend(); i != e; ++i) {
|
2016-03-19 16:55:15 +00:00
|
|
|
if (HistoryItem *item = App::histories().addNewMessage(v->at(i.value()), NewMessageExisting)) {
|
|
|
|
item->setPendingInitDimensions();
|
2015-04-04 20:01:34 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const WebPageItems &items(App::webPageItems());
|
2015-04-07 23:03:32 +00:00
|
|
|
for (WebPagesPending::iterator i = _webPagesPending.begin(); i != _webPagesPending.cend();) {
|
2015-04-04 20:01:34 +00:00
|
|
|
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) {
|
2016-03-19 16:55:15 +00:00
|
|
|
k.key()->setPendingInitDimensions();
|
2015-04-04 20:01:34 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
i = _webPagesPending.erase(i);
|
|
|
|
} else {
|
|
|
|
++i;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-03-19 09:18:19 +00:00
|
|
|
ApiWrap::~ApiWrap() {
|
2015-12-27 21:37:48 +00:00
|
|
|
App::clearHistories();
|
2015-03-19 09:18:19 +00:00
|
|
|
}
|