2015-03-19 09:18:19 +00:00
|
|
|
/*
|
|
|
|
This file is part of Telegram Desktop,
|
2018-01-03 10:23:14 +00:00
|
|
|
the official desktop application for the Telegram messaging service.
|
2015-03-19 09:18:19 +00:00
|
|
|
|
2018-01-03 10:23:14 +00:00
|
|
|
For license and copyright information please follow this link:
|
|
|
|
https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL
|
2015-03-19 09:18:19 +00:00
|
|
|
*/
|
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"
|
2017-09-26 11:49:16 +00:00
|
|
|
#include "data/data_photo.h"
|
|
|
|
#include "data/data_web_page.h"
|
2018-12-19 11:20:04 +00:00
|
|
|
#include "data/data_poll.h"
|
2018-01-04 19:54:35 +00:00
|
|
|
#include "data/data_feed.h"
|
2018-01-14 16:02:25 +00:00
|
|
|
#include "data/data_media_types.h"
|
2018-02-21 21:17:36 +00:00
|
|
|
#include "data/data_sparse_ids.h"
|
|
|
|
#include "data/data_search_controller.h"
|
|
|
|
#include "data/data_channel_admins.h"
|
|
|
|
#include "data/data_session.h"
|
2019-01-04 11:09:48 +00:00
|
|
|
#include "data/data_channel.h"
|
|
|
|
#include "data/data_chat.h"
|
|
|
|
#include "data/data_user.h"
|
2018-02-21 21:17:36 +00:00
|
|
|
#include "dialogs/dialogs_key.h"
|
2018-09-09 12:10:54 +00:00
|
|
|
#include "core/core_cloud_password.h"
|
2019-01-21 13:42:21 +00:00
|
|
|
#include "core/application.h"
|
2018-09-09 12:10:54 +00:00
|
|
|
#include "base/openssl_help.h"
|
2016-05-25 12:09:05 +00:00
|
|
|
#include "observer_peer.h"
|
2017-04-13 08:27:10 +00:00
|
|
|
#include "lang/lang_keys.h"
|
2016-04-12 21:31:28 +00:00
|
|
|
#include "mainwindow.h"
|
2015-03-19 09:18:19 +00:00
|
|
|
#include "mainwidget.h"
|
2017-12-07 13:02:24 +00:00
|
|
|
#include "boxes/add_contact_box.h"
|
2018-01-13 12:45:11 +00:00
|
|
|
#include "history/history.h"
|
2017-12-07 13:02:24 +00:00
|
|
|
#include "history/history_message.h"
|
2017-12-18 15:44:50 +00:00
|
|
|
#include "history/history_item_components.h"
|
2018-02-21 21:17:36 +00:00
|
|
|
#include "history/feed/history_feed_section.h"
|
2017-03-04 10:23:56 +00:00
|
|
|
#include "storage/localstorage.h"
|
2017-02-23 09:32:28 +00:00
|
|
|
#include "auth_session.h"
|
2017-04-06 14:38:10 +00:00
|
|
|
#include "boxes/confirm_box.h"
|
2019-03-10 12:55:24 +00:00
|
|
|
#include "boxes/stickers_box.h"
|
|
|
|
#include "boxes/sticker_set_box.h"
|
2017-03-04 19:36:59 +00:00
|
|
|
#include "window/notifications_manager.h"
|
2018-06-03 13:30:40 +00:00
|
|
|
#include "window/window_lock_widgets.h"
|
2018-02-21 21:17:36 +00:00
|
|
|
#include "window/window_controller.h"
|
2019-01-17 08:18:23 +00:00
|
|
|
#include "window/themes/window_theme.h"
|
2018-06-26 15:34:38 +00:00
|
|
|
#include "inline_bots/inline_bot_result.h"
|
2017-04-08 13:27:53 +00:00
|
|
|
#include "chat_helpers/message_field.h"
|
2017-08-02 20:57:49 +00:00
|
|
|
#include "chat_helpers/stickers.h"
|
2018-06-26 15:34:38 +00:00
|
|
|
#include "ui/text_options.h"
|
2018-10-13 17:35:30 +00:00
|
|
|
#include "ui/emoji_config.h"
|
2018-11-20 15:36:36 +00:00
|
|
|
#include "support/support_helper.h"
|
2017-12-19 16:57:42 +00:00
|
|
|
#include "storage/localimageloader.h"
|
2018-07-15 16:36:19 +00:00
|
|
|
#include "storage/file_download.h"
|
2018-09-06 11:13:54 +00:00
|
|
|
#include "storage/file_upload.h"
|
2017-08-18 19:14:31 +00:00
|
|
|
#include "storage/storage_facade.h"
|
|
|
|
#include "storage/storage_shared_media.h"
|
2017-08-29 19:52:52 +00:00
|
|
|
#include "storage/storage_user_photos.h"
|
2017-12-19 16:57:42 +00:00
|
|
|
#include "storage/storage_media_prepare.h"
|
2018-01-09 17:08:31 +00:00
|
|
|
#include "storage/storage_feed_messages.h"
|
2015-03-19 09:18:19 +00:00
|
|
|
|
2017-04-06 16:49:42 +00:00
|
|
|
namespace {
|
|
|
|
|
2018-12-28 11:42:58 +00:00
|
|
|
// 1 second wait before reload members in channel after adding.
|
|
|
|
constexpr auto kReloadChannelMembersTimeout = 1000;
|
|
|
|
|
|
|
|
// Save draft to the cloud with 1 sec extra delay.
|
|
|
|
constexpr auto kSaveCloudDraftTimeout = 1000;
|
|
|
|
|
|
|
|
// Give the app 1.5 secs to save drafts to cloud when quitting.
|
|
|
|
constexpr auto kSaveDraftBeforeQuitTimeout = 1500;
|
|
|
|
|
|
|
|
// Max users in one super group invite request.
|
|
|
|
constexpr auto kMaxUsersPerInvite = 100;
|
|
|
|
|
|
|
|
// How many messages from chat history server should forward to user,
|
|
|
|
// that was added to this chat.
|
|
|
|
constexpr auto kForwardMessagesOnAdd = 100;
|
|
|
|
|
2018-05-11 14:03:53 +00:00
|
|
|
constexpr auto kProxyPromotionInterval = TimeId(60 * 60);
|
|
|
|
constexpr auto kProxyPromotionMinDelay = TimeId(10);
|
2017-04-06 19:02:40 +00:00
|
|
|
constexpr auto kSmallDelayMs = 5;
|
2017-08-11 07:16:07 +00:00
|
|
|
constexpr auto kUnreadMentionsPreloadIfLess = 5;
|
|
|
|
constexpr auto kUnreadMentionsFirstRequestLimit = 10;
|
|
|
|
constexpr auto kUnreadMentionsNextRequestLimit = 100;
|
2017-08-29 19:52:52 +00:00
|
|
|
constexpr auto kSharedMediaLimit = 100;
|
2018-01-09 17:08:31 +00:00
|
|
|
constexpr auto kFeedMessagesLimit = 50;
|
2019-02-19 06:57:53 +00:00
|
|
|
constexpr auto kReadFeaturedSetsTimeout = crl::time(1000);
|
|
|
|
constexpr auto kFileLoaderQueueStopTimeout = crl::time(5000);
|
|
|
|
constexpr auto kFeedReadTimeout = crl::time(1000);
|
|
|
|
constexpr auto kStickersByEmojiInvalidateTimeout = crl::time(60 * 60 * 1000);
|
|
|
|
constexpr auto kNotifySettingSaveTimeout = crl::time(1000);
|
2017-12-19 16:57:42 +00:00
|
|
|
|
2018-07-15 16:36:19 +00:00
|
|
|
using SimpleFileLocationId = Data::SimpleFileLocationId;
|
|
|
|
using DocumentFileLocationId = Data::DocumentFileLocationId;
|
|
|
|
using FileLocationId = Data::FileLocationId;
|
|
|
|
using UpdatedFileReferences = Data::UpdatedFileReferences;
|
|
|
|
|
2017-12-19 16:57:42 +00:00
|
|
|
bool IsSilentPost(not_null<HistoryItem*> item, bool silent) {
|
|
|
|
const auto history = item->history();
|
|
|
|
return silent
|
|
|
|
&& history->peer->isChannel()
|
|
|
|
&& !history->peer->isMegagroup();
|
|
|
|
}
|
|
|
|
|
|
|
|
MTPVector<MTPDocumentAttribute> ComposeSendingDocumentAttributes(
|
|
|
|
not_null<DocumentData*> document) {
|
|
|
|
const auto filenameAttribute = MTP_documentAttributeFilename(
|
|
|
|
MTP_string(document->filename()));
|
|
|
|
const auto dimensions = document->dimensions;
|
|
|
|
auto attributes = QVector<MTPDocumentAttribute>(1, filenameAttribute);
|
|
|
|
if (dimensions.width() > 0 && dimensions.height() > 0) {
|
2019-03-06 08:21:42 +00:00
|
|
|
const auto duration = document->getDuration();
|
2017-12-19 16:57:42 +00:00
|
|
|
if (duration >= 0) {
|
|
|
|
auto flags = MTPDdocumentAttributeVideo::Flags(0);
|
|
|
|
if (document->isVideoMessage()) {
|
|
|
|
flags |= MTPDdocumentAttributeVideo::Flag::f_round_message;
|
|
|
|
}
|
2018-11-07 14:07:20 +00:00
|
|
|
if (document->supportsStreaming()) {
|
|
|
|
flags |= MTPDdocumentAttributeVideo::Flag::f_supports_streaming;
|
|
|
|
}
|
2017-12-19 16:57:42 +00:00
|
|
|
attributes.push_back(MTP_documentAttributeVideo(
|
|
|
|
MTP_flags(flags),
|
|
|
|
MTP_int(duration),
|
|
|
|
MTP_int(dimensions.width()),
|
|
|
|
MTP_int(dimensions.height())));
|
|
|
|
} else {
|
|
|
|
attributes.push_back(MTP_documentAttributeImageSize(
|
|
|
|
MTP_int(dimensions.width()),
|
|
|
|
MTP_int(dimensions.height())));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (document->type == AnimatedDocument) {
|
|
|
|
attributes.push_back(MTP_documentAttributeAnimated());
|
|
|
|
} else if (document->type == StickerDocument && document->sticker()) {
|
|
|
|
attributes.push_back(MTP_documentAttributeSticker(
|
|
|
|
MTP_flags(0),
|
|
|
|
MTP_string(document->sticker()->alt),
|
|
|
|
document->sticker()->set,
|
|
|
|
MTPMaskCoords()));
|
|
|
|
} else if (const auto song = document->song()) {
|
|
|
|
const auto flags = MTPDdocumentAttributeAudio::Flag::f_title
|
|
|
|
| MTPDdocumentAttributeAudio::Flag::f_performer;
|
|
|
|
attributes.push_back(MTP_documentAttributeAudio(
|
|
|
|
MTP_flags(flags),
|
|
|
|
MTP_int(song->duration),
|
|
|
|
MTP_string(song->title),
|
|
|
|
MTP_string(song->performer),
|
|
|
|
MTPstring()));
|
|
|
|
} else if (const auto voice = document->voice()) {
|
|
|
|
const auto flags = MTPDdocumentAttributeAudio::Flag::f_voice
|
|
|
|
| MTPDdocumentAttributeAudio::Flag::f_waveform;
|
|
|
|
attributes.push_back(MTP_documentAttributeAudio(
|
|
|
|
MTP_flags(flags),
|
|
|
|
MTP_int(voice->duration),
|
|
|
|
MTPstring(),
|
|
|
|
MTPstring(),
|
|
|
|
MTP_bytes(documentWaveformEncode5bit(voice->waveform))));
|
|
|
|
}
|
|
|
|
return MTP_vector<MTPDocumentAttribute>(attributes);
|
|
|
|
}
|
|
|
|
|
2018-06-26 15:34:38 +00:00
|
|
|
} // namespace
|
|
|
|
|
|
|
|
ApiWrap::SendOptions::SendOptions(not_null<History*> history)
|
|
|
|
: history(history) {
|
2017-12-19 16:57:42 +00:00
|
|
|
}
|
2017-04-06 16:49:42 +00:00
|
|
|
|
2018-06-26 15:34:38 +00:00
|
|
|
ApiWrap::MessageToSend::MessageToSend(not_null<History*> history)
|
|
|
|
: history(history) {
|
|
|
|
}
|
2017-04-06 16:49:42 +00:00
|
|
|
|
2018-09-11 09:11:52 +00:00
|
|
|
MTPInputPrivacyKey ApiWrap::Privacy::Input(Key key) {
|
|
|
|
switch (key) {
|
|
|
|
case Privacy::Key::Calls: return MTP_inputPrivacyKeyPhoneCall();
|
|
|
|
case Privacy::Key::Invites: return MTP_inputPrivacyKeyChatInvite();
|
|
|
|
case Privacy::Key::LastSeen:
|
|
|
|
return MTP_inputPrivacyKeyStatusTimestamp();
|
2018-10-24 09:07:54 +00:00
|
|
|
case Privacy::Key::CallsPeer2Peer:
|
|
|
|
return MTP_inputPrivacyKeyPhoneP2P();
|
2018-09-11 09:11:52 +00:00
|
|
|
}
|
|
|
|
Unexpected("Key in ApiWrap::Privacy::Input.");
|
|
|
|
}
|
|
|
|
|
2017-08-17 08:31:24 +00:00
|
|
|
ApiWrap::ApiWrap(not_null<AuthSession*> session)
|
2017-06-05 13:33:45 +00:00
|
|
|
: _session(session)
|
2018-05-11 14:03:53 +00:00
|
|
|
, _messageDataResolveDelayed([=] { resolveMessageDatas(); })
|
|
|
|
, _webPagesTimer([=] { resolveWebPages(); })
|
|
|
|
, _draftsSaveTimer([=] { saveDraftsToCloud(); })
|
|
|
|
, _featuredSetsReadTimer([=] { readFeaturedSets(); })
|
2018-02-02 12:51:18 +00:00
|
|
|
, _fileLoader(std::make_unique<TaskQueue>(kFileLoaderQueueStopTimeout))
|
2018-05-11 14:03:53 +00:00
|
|
|
, _feedReadTimer([=] { readFeeds(); })
|
2018-04-09 17:48:29 +00:00
|
|
|
, _proxyPromotionTimer([=] { refreshProxyPromotion(); })
|
|
|
|
, _updateNotifySettingsTimer([=] { sendNotifySettingsUpdates(); }) {
|
2018-09-06 11:13:54 +00:00
|
|
|
crl::on_main([=] {
|
|
|
|
_session->uploader().photoReady(
|
|
|
|
) | rpl::start_with_next([=](const Storage::UploadedPhoto &data) {
|
|
|
|
photoUploadReady(data.fullId, data.file);
|
|
|
|
}, _session->lifetime());
|
|
|
|
});
|
2017-06-05 13:33:45 +00:00
|
|
|
}
|
|
|
|
|
2017-12-29 18:17:07 +00:00
|
|
|
void ApiWrap::requestChangelog(
|
|
|
|
const QString &sinceVersion,
|
2018-06-04 15:35:11 +00:00
|
|
|
Fn<void(const MTPUpdates &result)> callback) {
|
2017-12-29 18:17:07 +00:00
|
|
|
request(MTPhelp_GetAppChangelog(
|
|
|
|
MTP_string(sinceVersion)
|
|
|
|
)).done(
|
|
|
|
callback
|
|
|
|
).send();
|
2017-06-30 10:32:10 +00:00
|
|
|
}
|
|
|
|
|
2018-05-11 14:03:53 +00:00
|
|
|
void ApiWrap::refreshProxyPromotion() {
|
|
|
|
const auto now = unixtime();
|
|
|
|
const auto next = (_proxyPromotionNextRequestTime != 0)
|
|
|
|
? _proxyPromotionNextRequestTime
|
|
|
|
: now;
|
|
|
|
if (_proxyPromotionRequestId) {
|
|
|
|
getProxyPromotionDelayed(now, next);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
const auto key = [&]() -> std::pair<QString, uint32> {
|
2018-11-05 13:58:24 +00:00
|
|
|
if (Global::ProxySettings() != ProxyData::Settings::Enabled) {
|
2018-05-11 14:03:53 +00:00
|
|
|
return {};
|
|
|
|
}
|
|
|
|
const auto &proxy = Global::SelectedProxy();
|
|
|
|
if (proxy.type != ProxyData::Type::Mtproto) {
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
return { proxy.host, proxy.port };
|
|
|
|
}();
|
|
|
|
if (_proxyPromotionKey == key && now < next) {
|
|
|
|
getProxyPromotionDelayed(now, next);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
_proxyPromotionKey = key;
|
|
|
|
if (key.first.isEmpty() || !key.second) {
|
|
|
|
proxyPromotionDone(MTP_help_proxyDataEmpty(
|
|
|
|
MTP_int(unixtime() + kProxyPromotionInterval)));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
_proxyPromotionRequestId = request(MTPhelp_GetProxyData(
|
|
|
|
)).done([=](const MTPhelp_ProxyData &result) {
|
|
|
|
_proxyPromotionRequestId = 0;
|
|
|
|
proxyPromotionDone(result);
|
|
|
|
}).fail([=](const RPCError &error) {
|
|
|
|
_proxyPromotionRequestId = 0;
|
|
|
|
const auto now = unixtime();
|
|
|
|
const auto next = _proxyPromotionNextRequestTime = now
|
|
|
|
+ kProxyPromotionInterval;
|
|
|
|
if (!_proxyPromotionTimer.isActive()) {
|
|
|
|
getProxyPromotionDelayed(now, next);
|
|
|
|
}
|
|
|
|
}).send();
|
|
|
|
}
|
|
|
|
|
|
|
|
void ApiWrap::getProxyPromotionDelayed(TimeId now, TimeId next) {
|
|
|
|
_proxyPromotionTimer.callOnce(std::min(
|
|
|
|
std::max(next - now, kProxyPromotionMinDelay),
|
2019-02-19 06:57:53 +00:00
|
|
|
kProxyPromotionInterval) * crl::time(1000));
|
2018-05-11 14:03:53 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
void ApiWrap::proxyPromotionDone(const MTPhelp_ProxyData &proxy) {
|
2019-01-16 09:01:38 +00:00
|
|
|
_proxyPromotionNextRequestTime = proxy.match([&](const auto &data) {
|
|
|
|
return data.vexpires.v;
|
|
|
|
});
|
|
|
|
getProxyPromotionDelayed(unixtime(), _proxyPromotionNextRequestTime);
|
2018-05-11 14:03:53 +00:00
|
|
|
|
2019-01-16 09:01:38 +00:00
|
|
|
proxy.match([&](const MTPDhelp_proxyDataEmpty &data) {
|
|
|
|
_session->data().setProxyPromoted(nullptr);
|
|
|
|
}, [&](const MTPDhelp_proxyDataPromo &data) {
|
2019-01-18 12:27:37 +00:00
|
|
|
_session->data().processChats(data.vchats);
|
|
|
|
_session->data().processUsers(data.vusers);
|
2019-01-16 09:01:38 +00:00
|
|
|
const auto peerId = peerFromMTP(data.vpeer);
|
|
|
|
const auto peer = _session->data().peer(peerId);
|
|
|
|
_session->data().setProxyPromoted(peer);
|
2019-01-18 12:27:37 +00:00
|
|
|
if (const auto history = _session->data().historyLoaded(peer)) {
|
2019-01-16 09:01:38 +00:00
|
|
|
requestDialogEntry(history);
|
|
|
|
}
|
|
|
|
});
|
2018-05-11 14:03:53 +00:00
|
|
|
}
|
|
|
|
|
2018-04-18 15:42:02 +00:00
|
|
|
void ApiWrap::requestDeepLinkInfo(
|
|
|
|
const QString &path,
|
2018-06-04 15:35:11 +00:00
|
|
|
Fn<void(const MTPDhelp_deepLinkInfo &result)> callback) {
|
2018-04-18 15:42:02 +00:00
|
|
|
request(_deepLinkInfoRequestId).cancel();
|
|
|
|
_deepLinkInfoRequestId = request(MTPhelp_GetDeepLinkInfo(
|
|
|
|
MTP_string(path)
|
|
|
|
)).done([=](const MTPhelp_DeepLinkInfo &result) {
|
|
|
|
_deepLinkInfoRequestId = 0;
|
|
|
|
if (result.type() == mtpc_help_deepLinkInfo) {
|
|
|
|
callback(result.c_help_deepLinkInfo());
|
|
|
|
}
|
|
|
|
}).fail([=](const RPCError &error) {
|
|
|
|
_deepLinkInfoRequestId = 0;
|
|
|
|
}).send();
|
|
|
|
}
|
|
|
|
|
2018-06-03 13:30:40 +00:00
|
|
|
void ApiWrap::requestTermsUpdate() {
|
|
|
|
if (_termsUpdateRequestId) {
|
|
|
|
return;
|
|
|
|
}
|
2019-02-19 06:57:53 +00:00
|
|
|
const auto now = crl::now();
|
2018-06-03 13:30:40 +00:00
|
|
|
if (_termsUpdateSendAt && now < _termsUpdateSendAt) {
|
|
|
|
App::CallDelayed(_termsUpdateSendAt - now, _session, [=] {
|
|
|
|
requestTermsUpdate();
|
|
|
|
});
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-02-19 06:57:53 +00:00
|
|
|
constexpr auto kTermsUpdateTimeoutMin = 10 * crl::time(1000);
|
|
|
|
constexpr auto kTermsUpdateTimeoutMax = 86400 * crl::time(1000);
|
2018-06-03 13:30:40 +00:00
|
|
|
|
|
|
|
_termsUpdateRequestId = request(MTPhelp_GetTermsOfServiceUpdate(
|
|
|
|
)).done([=](const MTPhelp_TermsOfServiceUpdate &result) {
|
|
|
|
_termsUpdateRequestId = 0;
|
|
|
|
|
|
|
|
const auto requestNext = [&](auto &&data) {
|
2018-06-24 14:20:05 +00:00
|
|
|
const auto timeout = (data.vexpires.v - unixtime());
|
2019-02-19 06:57:53 +00:00
|
|
|
_termsUpdateSendAt = crl::now() + snap(
|
|
|
|
timeout * crl::time(1000),
|
2018-06-03 13:30:40 +00:00
|
|
|
kTermsUpdateTimeoutMin,
|
|
|
|
kTermsUpdateTimeoutMax);
|
|
|
|
requestTermsUpdate();
|
|
|
|
};
|
|
|
|
switch (result.type()) {
|
|
|
|
case mtpc_help_termsOfServiceUpdateEmpty: {
|
|
|
|
const auto &data = result.c_help_termsOfServiceUpdateEmpty();
|
|
|
|
requestNext(data);
|
|
|
|
} break;
|
|
|
|
case mtpc_help_termsOfServiceUpdate: {
|
|
|
|
const auto &data = result.c_help_termsOfServiceUpdate();
|
|
|
|
const auto &terms = data.vterms_of_service;
|
|
|
|
const auto &fields = terms.c_help_termsOfService();
|
2019-01-21 13:42:21 +00:00
|
|
|
Core::App().lockByTerms(
|
2018-06-03 13:30:40 +00:00
|
|
|
Window::TermsLock::FromMTP(fields));
|
|
|
|
requestNext(data);
|
|
|
|
} break;
|
|
|
|
default: Unexpected("Type in requestTermsUpdate().");
|
|
|
|
}
|
|
|
|
}).fail([=](const RPCError &error) {
|
|
|
|
_termsUpdateRequestId = 0;
|
2019-02-19 06:57:53 +00:00
|
|
|
_termsUpdateSendAt = crl::now() + kTermsUpdateTimeoutMin;
|
2018-06-03 13:30:40 +00:00
|
|
|
requestTermsUpdate();
|
|
|
|
}).send();
|
|
|
|
}
|
|
|
|
|
|
|
|
void ApiWrap::acceptTerms(bytes::const_span id) {
|
|
|
|
request(MTPhelp_AcceptTermsOfService(
|
|
|
|
MTP_dataJSON(MTP_bytes(id))
|
|
|
|
)).done([=](const MTPBool &result) {
|
|
|
|
requestTermsUpdate();
|
|
|
|
}).send();
|
|
|
|
}
|
|
|
|
|
2018-11-26 11:55:02 +00:00
|
|
|
void ApiWrap::checkChatInvite(
|
|
|
|
const QString &hash,
|
|
|
|
FnMut<void(const MTPChatInvite &)> done,
|
|
|
|
FnMut<void(const RPCError &)> fail) {
|
|
|
|
request(base::take(_checkInviteRequestId)).cancel();
|
|
|
|
_checkInviteRequestId = request(MTPmessages_CheckChatInvite(
|
|
|
|
MTP_string(hash)
|
|
|
|
)).done(std::move(done)).fail(std::move(fail)).send();
|
|
|
|
}
|
|
|
|
|
|
|
|
void ApiWrap::importChatInvite(const QString &hash) {
|
|
|
|
request(MTPmessages_ImportChatInvite(
|
|
|
|
MTP_string(hash)
|
|
|
|
)).done([=](const MTPUpdates &result) {
|
|
|
|
applyUpdates(result);
|
|
|
|
|
|
|
|
Ui::hideLayer();
|
|
|
|
const auto handleChats = [&](const MTPVector<MTPChat> &chats) {
|
|
|
|
if (chats.v.isEmpty()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
const auto peerId = chats.v[0].match([](const MTPDchat &data) {
|
|
|
|
return peerFromChat(data.vid.v);
|
|
|
|
}, [](const MTPDchannel &data) {
|
|
|
|
return peerFromChannel(data.vid.v);
|
|
|
|
}, [](auto&&) {
|
|
|
|
return PeerId(0);
|
|
|
|
});
|
2019-01-18 12:27:37 +00:00
|
|
|
if (const auto peer = _session->data().peerLoaded(peerId)) {
|
2018-11-26 11:55:02 +00:00
|
|
|
App::wnd()->controller()->showPeerHistory(
|
|
|
|
peer,
|
|
|
|
Window::SectionShow::Way::Forward);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
result.match([&](const MTPDupdates &data) {
|
|
|
|
handleChats(data.vchats);
|
|
|
|
}, [&](const MTPDupdatesCombined &data) {
|
|
|
|
handleChats(data.vchats);
|
|
|
|
}, [&](auto &&) {
|
|
|
|
LOG(("API Error: unexpected update cons %1 "
|
|
|
|
"(MainWidget::inviteImportDone)").arg(result.type()));
|
|
|
|
});
|
|
|
|
}).fail([=](const RPCError &error) {
|
2019-01-13 08:03:34 +00:00
|
|
|
const auto &type = error.type();
|
2018-11-26 11:55:02 +00:00
|
|
|
if (type == qstr("CHANNELS_TOO_MUCH")) {
|
|
|
|
Ui::show(Box<InformBox>(lang(lng_join_channel_error)));
|
|
|
|
} else if (error.code() == 400) {
|
|
|
|
Ui::show(Box<InformBox>(lang(type == qstr("USERS_TOO_MUCH")
|
|
|
|
? lng_group_invite_no_room
|
|
|
|
: lng_group_invite_bad_link)));
|
|
|
|
}
|
|
|
|
}).send();
|
|
|
|
}
|
|
|
|
|
2017-12-19 16:57:42 +00:00
|
|
|
void ApiWrap::applyUpdates(
|
|
|
|
const MTPUpdates &updates,
|
|
|
|
uint64 sentMessageRandomId) {
|
2017-04-13 17:59:05 +00:00
|
|
|
App::main()->feedUpdates(updates, sentMessageRandomId);
|
|
|
|
}
|
|
|
|
|
2018-01-04 19:54:35 +00:00
|
|
|
void ApiWrap::savePinnedOrder() {
|
|
|
|
const auto &order = _session->data().pinnedDialogsOrder();
|
|
|
|
auto peers = QVector<MTPInputDialogPeer>();
|
|
|
|
peers.reserve(order.size());
|
2019-01-15 11:57:45 +00:00
|
|
|
for (const auto &pinned : ranges::view::reverse(order)) {
|
2018-01-04 19:54:35 +00:00
|
|
|
if (const auto history = pinned.history()) {
|
|
|
|
peers.push_back(MTP_inputDialogPeer(history->peer->input));
|
|
|
|
} else if (const auto feed = pinned.feed()) {
|
2018-03-06 17:07:42 +00:00
|
|
|
// peers.push_back(MTP_inputDialogPeerFeed(MTP_int(feed->id()))); // #feed
|
2018-01-03 18:00:11 +00:00
|
|
|
}
|
|
|
|
}
|
2018-01-04 19:54:35 +00:00
|
|
|
auto flags = MTPmessages_ReorderPinnedDialogs::Flag::f_force;
|
|
|
|
request(MTPmessages_ReorderPinnedDialogs(
|
|
|
|
MTP_flags(flags),
|
|
|
|
MTP_vector(peers)
|
|
|
|
)).send();
|
2018-01-03 18:00:11 +00:00
|
|
|
}
|
2018-03-06 17:07:42 +00:00
|
|
|
// #feed
|
|
|
|
//void ApiWrap::toggleChannelGrouping(
|
|
|
|
// not_null<ChannelData*> channel,
|
|
|
|
// bool group,
|
2018-06-04 15:35:11 +00:00
|
|
|
// Fn<void()> callback) {
|
2018-03-06 17:07:42 +00:00
|
|
|
// if (const auto already = _channelGroupingRequests.take(channel)) {
|
|
|
|
// request(already->first).cancel();
|
|
|
|
// }
|
|
|
|
// const auto feedId = Data::Feed::kId;
|
|
|
|
// const auto flags = group
|
|
|
|
// ? MTPchannels_ChangeFeedBroadcast::Flag::f_feed_id
|
|
|
|
// : MTPchannels_ChangeFeedBroadcast::Flag(0);
|
|
|
|
// const auto requestId = request(MTPchannels_ChangeFeedBroadcast(
|
|
|
|
// MTP_flags(flags),
|
|
|
|
// channel->inputChannel,
|
|
|
|
// MTP_int(feedId)
|
|
|
|
// )).done([=](const MTPUpdates &result) {
|
|
|
|
// applyUpdates(result);
|
|
|
|
// if (group) {
|
2018-04-09 17:48:29 +00:00
|
|
|
// channel->setFeed(_session->data().feed(feedId));
|
2018-03-06 17:07:42 +00:00
|
|
|
// } else {
|
|
|
|
// channel->clearFeed();
|
|
|
|
// }
|
|
|
|
// if (const auto data = _channelGroupingRequests.take(channel)) {
|
|
|
|
// data->second();
|
|
|
|
// }
|
|
|
|
// }).fail([=](const RPCError &error) {
|
|
|
|
// _channelGroupingRequests.remove(channel);
|
|
|
|
// }).send();
|
|
|
|
// _channelGroupingRequests.emplace(channel, requestId, callback);
|
|
|
|
//}
|
|
|
|
//
|
|
|
|
//void ApiWrap::ungroupAllFromFeed(not_null<Data::Feed*> feed) {
|
|
|
|
// const auto flags = MTPchannels_SetFeedBroadcasts::Flag::f_channels
|
|
|
|
// | MTPchannels_SetFeedBroadcasts::Flag::f_also_newly_joined;
|
|
|
|
// request(MTPchannels_SetFeedBroadcasts(
|
|
|
|
// MTP_flags(flags),
|
|
|
|
// MTP_int(feed->id()),
|
|
|
|
// MTP_vector<MTPInputChannel>(0),
|
|
|
|
// MTP_bool(false)
|
|
|
|
// )).done([=](const MTPUpdates &result) {
|
|
|
|
// applyUpdates(result);
|
|
|
|
// }).send();
|
|
|
|
//}
|
2018-02-18 14:00:14 +00:00
|
|
|
|
2017-12-19 16:57:42 +00:00
|
|
|
void ApiWrap::sendMessageFail(const RPCError &error) {
|
|
|
|
if (error.type() == qstr("PEER_FLOOD")) {
|
|
|
|
Ui::show(Box<InformBox>(
|
|
|
|
PeerFloodErrorText(PeerFloodType::Send)));
|
|
|
|
} else if (error.type() == qstr("USER_BANNED_IN_CHANNEL")) {
|
|
|
|
const auto link = textcmdLink(
|
2019-01-21 13:42:21 +00:00
|
|
|
Core::App().createInternalLinkFull(qsl("spambot")),
|
2017-12-19 16:57:42 +00:00
|
|
|
lang(lng_cant_more_info));
|
|
|
|
Ui::show(Box<InformBox>(lng_error_public_groups_denied(
|
|
|
|
lt_more_info,
|
|
|
|
link)));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-02-26 11:32:13 +00:00
|
|
|
void ApiWrap::requestMessageData(ChannelData *channel, MsgId msgId, RequestMessageDataCallback callback) {
|
2017-04-06 16:49:42 +00:00
|
|
|
auto &req = (channel ? _channelMessageDataRequests[channel][msgId] : _messageDataRequests[msgId]);
|
2016-05-09 10:07:25 +00:00
|
|
|
if (callback) {
|
2016-11-20 12:54:07 +00:00
|
|
|
req.callbacks.append(callback);
|
2016-05-09 10:07:25 +00:00
|
|
|
}
|
2017-04-06 19:02:40 +00:00
|
|
|
if (!req.requestId) _messageDataResolveDelayed.call();
|
2015-03-19 09:18:19 +00:00
|
|
|
}
|
|
|
|
|
2018-02-06 07:24:55 +00:00
|
|
|
QVector<MTPInputMessage> ApiWrap::collectMessageIds(const MessageDataRequests &requests) {
|
|
|
|
auto result = QVector<MTPInputMessage>();
|
2015-09-12 11:59:50 +00:00
|
|
|
result.reserve(requests.size());
|
2016-05-09 10:07:25 +00:00
|
|
|
for (auto i = requests.cbegin(), e = requests.cend(); i != e; ++i) {
|
2017-04-06 19:02:40 +00:00
|
|
|
if (i.value().requestId > 0) continue;
|
2018-02-06 07:24:55 +00:00
|
|
|
result.push_back(MTP_inputMessageID(MTP_int(i.key())));
|
2015-09-12 11:59:50 +00:00
|
|
|
}
|
|
|
|
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()) {
|
2019-01-13 08:03:34 +00:00
|
|
|
if (onlyExisting) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
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
|
|
|
|
2017-04-06 19:02:40 +00:00
|
|
|
auto ids = collectMessageIds(_messageDataRequests);
|
2015-06-28 12:37:10 +00:00
|
|
|
if (!ids.isEmpty()) {
|
2017-11-23 15:41:13 +00:00
|
|
|
auto requestId = request(MTPmessages_GetMessages(
|
2018-02-06 07:24:55 +00:00
|
|
|
MTP_vector<MTPInputMessage>(ids)
|
2017-11-23 15:41:13 +00:00
|
|
|
)).done([this](const MTPmessages_Messages &result, mtpRequestId requestId) {
|
2017-04-06 19:02:40 +00:00
|
|
|
gotMessageDatas(nullptr, result, requestId);
|
2017-11-23 15:41:13 +00:00
|
|
|
}).fail([this](const RPCError &error, mtpRequestId requestId) {
|
|
|
|
finalizeMessageDataRequest(nullptr, requestId);
|
2017-12-07 13:02:24 +00:00
|
|
|
}).afterDelay(kSmallDelayMs).send();
|
2016-05-09 10:07:25 +00:00
|
|
|
for (auto &request : _messageDataRequests) {
|
2017-04-06 19:02:40 +00:00
|
|
|
if (request.requestId > 0) continue;
|
|
|
|
request.requestId = requestId;
|
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;
|
|
|
|
}
|
2017-04-06 19:02:40 +00:00
|
|
|
auto ids = collectMessageIds(j.value());
|
2015-09-12 11:59:50 +00:00
|
|
|
if (!ids.isEmpty()) {
|
2017-11-23 15:41:13 +00:00
|
|
|
auto channel = j.key();
|
|
|
|
auto requestId = request(MTPchannels_GetMessages(
|
|
|
|
j.key()->inputChannel,
|
2018-02-06 07:24:55 +00:00
|
|
|
MTP_vector<MTPInputMessage>(ids)
|
2017-11-23 15:41:13 +00:00
|
|
|
)).done([=](const MTPmessages_Messages &result, mtpRequestId requestId) {
|
2017-04-06 19:02:40 +00:00
|
|
|
gotMessageDatas(channel, result, requestId);
|
2017-11-23 15:41:13 +00:00
|
|
|
}).fail([=](const RPCError &error, mtpRequestId requestId) {
|
|
|
|
finalizeMessageDataRequest(channel, requestId);
|
2017-12-07 13:02:24 +00:00
|
|
|
}).afterDelay(kSmallDelayMs).send();
|
2017-04-06 19:02:40 +00:00
|
|
|
|
2016-05-09 10:07:25 +00:00
|
|
|
for (auto &request : *j) {
|
2017-04-06 19:02:40 +00:00
|
|
|
if (request.requestId > 0) continue;
|
|
|
|
request.requestId = requestId;
|
2015-09-12 11:59:50 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
++j;
|
|
|
|
}
|
2015-06-28 12:37:10 +00:00
|
|
|
}
|
|
|
|
|
2017-04-06 19:02:40 +00:00
|
|
|
void ApiWrap::gotMessageDatas(ChannelData *channel, const MTPmessages_Messages &msgs, mtpRequestId requestId) {
|
2017-11-20 19:54:05 +00:00
|
|
|
auto handleResult = [&](auto &&result) {
|
2019-01-18 12:27:37 +00:00
|
|
|
_session->data().processUsers(result.vusers);
|
|
|
|
_session->data().processChats(result.vchats);
|
2017-11-20 19:54:05 +00:00
|
|
|
App::feedMsgs(result.vmessages, NewMessageExisting);
|
|
|
|
};
|
2015-06-28 12:37:10 +00:00
|
|
|
switch (msgs.type()) {
|
2017-11-20 19:54:05 +00:00
|
|
|
case mtpc_messages_messages:
|
|
|
|
handleResult(msgs.c_messages_messages());
|
|
|
|
break;
|
|
|
|
case mtpc_messages_messagesSlice:
|
|
|
|
handleResult(msgs.c_messages_messagesSlice());
|
|
|
|
break;
|
2015-09-04 13:01:31 +00:00
|
|
|
case mtpc_messages_channelMessages: {
|
2017-11-20 19:54:05 +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
|
|
|
}
|
2017-11-20 19:54:05 +00:00
|
|
|
handleResult(d);
|
2015-09-04 13:01:31 +00:00
|
|
|
} break;
|
2017-11-20 19:54:05 +00:00
|
|
|
case mtpc_messages_messagesNotModified:
|
|
|
|
LOG(("API Error: received messages.messagesNotModified! (ApiWrap::gotDependencyItem)"));
|
|
|
|
break;
|
2015-06-28 12:37:10 +00:00
|
|
|
}
|
2017-11-23 15:41:13 +00:00
|
|
|
finalizeMessageDataRequest(channel, requestId);
|
|
|
|
}
|
|
|
|
|
|
|
|
void ApiWrap::finalizeMessageDataRequest(
|
|
|
|
ChannelData *channel,
|
|
|
|
mtpRequestId requestId) {
|
2017-04-06 19:02:40 +00:00
|
|
|
auto 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();) {
|
2017-04-06 19:02:40 +00:00
|
|
|
if (i.value().requestId == requestId) {
|
2016-05-09 10:07:25 +00:00
|
|
|
for_const (auto &callback, i.value().callbacks) {
|
2016-09-26 12:09:59 +00:00
|
|
|
callback(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
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-01-03 17:06:43 +00:00
|
|
|
void ApiWrap::requestContacts() {
|
|
|
|
if (_session->data().contactsLoaded().value() || _contactsRequestId) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
_contactsRequestId = request(MTPcontacts_GetContacts(
|
|
|
|
MTP_int(0)
|
|
|
|
)).done([=](const MTPcontacts_Contacts &result) {
|
|
|
|
_contactsRequestId = 0;
|
|
|
|
if (result.type() == mtpc_contacts_contactsNotModified) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
Assert(result.type() == mtpc_contacts_contacts);
|
|
|
|
const auto &d = result.c_contacts_contacts();
|
2019-01-18 12:27:37 +00:00
|
|
|
_session->data().processUsers(d.vusers);
|
2018-01-03 17:06:43 +00:00
|
|
|
for (const auto &contact : d.vcontacts.v) {
|
|
|
|
if (contact.type() != mtpc_contact) continue;
|
|
|
|
|
|
|
|
const auto userId = contact.c_contact().vuser_id.v;
|
2018-09-11 12:50:40 +00:00
|
|
|
if (userId == _session->userId()) {
|
2019-01-03 12:36:01 +00:00
|
|
|
_session->user()->setContactStatus(
|
2018-01-04 09:21:06 +00:00
|
|
|
UserData::ContactStatus::Contact);
|
2018-01-03 17:06:43 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
_session->data().contactsLoaded().set(true);
|
|
|
|
}).fail([=](const RPCError &error) {
|
|
|
|
_contactsRequestId = 0;
|
|
|
|
}).send();
|
|
|
|
}
|
|
|
|
|
2018-01-31 17:10:29 +00:00
|
|
|
void ApiWrap::requestDialogEntry(not_null<Data::Feed*> feed) {
|
|
|
|
if (_dialogFeedRequests.contains(feed)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
_dialogFeedRequests.emplace(feed);
|
|
|
|
|
2018-03-06 17:07:42 +00:00
|
|
|
//auto peers = QVector<MTPInputDialogPeer>( // #feed
|
|
|
|
// 1,
|
|
|
|
// MTP_inputDialogPeerFeed(MTP_int(feed->id())));
|
|
|
|
//request(MTPmessages_GetPeerDialogs(
|
|
|
|
// MTP_vector(std::move(peers))
|
|
|
|
//)).done([=](const MTPmessages_PeerDialogs &result) {
|
|
|
|
// applyPeerDialogs(result);
|
|
|
|
// _dialogFeedRequests.remove(feed);
|
|
|
|
//}).fail([=](const RPCError &error) {
|
|
|
|
// _dialogFeedRequests.remove(feed);
|
|
|
|
//}).send();
|
2018-01-31 17:10:29 +00:00
|
|
|
}
|
|
|
|
|
2018-02-03 19:52:35 +00:00
|
|
|
//void ApiWrap::requestFeedDialogsEntries(not_null<Data::Feed*> feed) {
|
|
|
|
// if (_dialogFeedRequests.contains(feed)) {
|
|
|
|
// return;
|
|
|
|
// }
|
|
|
|
// _dialogFeedRequests.emplace(feed);
|
|
|
|
//
|
2018-06-26 13:58:29 +00:00
|
|
|
// const auto hash = 0;
|
2018-02-03 19:52:35 +00:00
|
|
|
// request(MTPmessages_GetDialogs(
|
|
|
|
// MTP_flags(MTPmessages_GetDialogs::Flag::f_feed_id),
|
|
|
|
// MTP_int(feed->id()),
|
|
|
|
// MTP_int(0), // offset_date
|
|
|
|
// MTP_int(0), // offset_id
|
|
|
|
// MTP_inputPeerEmpty(), // offset_peer
|
2018-06-26 13:58:29 +00:00
|
|
|
// MTP_int(Data::Feed::kChannelsLimit),
|
|
|
|
// MTP_int(hash)
|
2018-02-03 19:52:35 +00:00
|
|
|
// )).done([=](const MTPmessages_Dialogs &result) {
|
|
|
|
// applyFeedDialogs(feed, result);
|
|
|
|
// _dialogFeedRequests.remove(feed);
|
|
|
|
// }).fail([=](const RPCError &error) {
|
|
|
|
// _dialogFeedRequests.remove(feed);
|
|
|
|
// }).send();
|
|
|
|
//}
|
|
|
|
|
2018-09-27 20:31:48 +00:00
|
|
|
void ApiWrap::requestDialogEntry(
|
|
|
|
not_null<History*> history,
|
|
|
|
Fn<void()> callback) {
|
2019-01-16 09:01:38 +00:00
|
|
|
const auto i = _dialogRequests.find(history);
|
|
|
|
if (i != end(_dialogRequests)) {
|
|
|
|
if (callback) {
|
|
|
|
i->second.push_back(std::move(callback));
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const auto [j, ok] = _dialogRequestsPending.try_emplace(history);
|
2018-09-27 20:31:48 +00:00
|
|
|
if (callback) {
|
2019-01-16 09:01:38 +00:00
|
|
|
j->second.push_back(std::move(callback));
|
2018-09-27 20:31:48 +00:00
|
|
|
}
|
|
|
|
if (!ok) {
|
2018-01-31 17:10:29 +00:00
|
|
|
return;
|
|
|
|
}
|
2019-01-16 09:01:38 +00:00
|
|
|
if (_dialogRequestsPending.size() > 1) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
Core::App().postponeCall(crl::guard(_session, [=] {
|
|
|
|
sendDialogRequests();
|
|
|
|
}));
|
2018-01-31 17:10:29 +00:00
|
|
|
}
|
|
|
|
|
2019-01-16 09:01:38 +00:00
|
|
|
void ApiWrap::sendDialogRequests() {
|
|
|
|
if (_dialogRequestsPending.empty()) {
|
2018-10-09 15:36:06 +00:00
|
|
|
return;
|
|
|
|
}
|
2019-01-16 09:01:38 +00:00
|
|
|
auto histories = std::vector<not_null<History*>>();
|
|
|
|
ranges::transform(
|
|
|
|
_dialogRequestsPending,
|
|
|
|
ranges::back_inserter(histories),
|
|
|
|
[](const auto &pair) { return pair.first; });
|
2018-10-09 15:36:06 +00:00
|
|
|
auto peers = QVector<MTPInputDialogPeer>();
|
2019-01-16 09:01:38 +00:00
|
|
|
const auto dialogPeer = [](not_null<History*> history) {
|
|
|
|
return MTP_inputDialogPeer(history->peer->input);
|
|
|
|
};
|
|
|
|
ranges::transform(
|
|
|
|
histories,
|
|
|
|
ranges::back_inserter(peers),
|
|
|
|
dialogPeer);
|
|
|
|
for (auto &[history, callbacks] : base::take(_dialogRequestsPending)) {
|
|
|
|
_dialogRequests.emplace(history, std::move(callbacks));
|
2018-10-09 15:36:06 +00:00
|
|
|
}
|
2019-01-16 09:01:38 +00:00
|
|
|
|
|
|
|
const auto finalize = [=] {
|
2018-10-09 15:36:06 +00:00
|
|
|
for (const auto history : histories) {
|
2019-01-16 09:01:38 +00:00
|
|
|
dialogEntryApplied(history);
|
|
|
|
history->updateChatListExistence();
|
2018-10-09 15:36:06 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
request(MTPmessages_GetPeerDialogs(
|
|
|
|
MTP_vector(std::move(peers))
|
|
|
|
)).done([=](const MTPmessages_PeerDialogs &result) {
|
|
|
|
applyPeerDialogs(result);
|
2019-01-16 09:01:38 +00:00
|
|
|
finalize();
|
2018-10-09 15:36:06 +00:00
|
|
|
}).fail([=](const RPCError &error) {
|
2019-01-16 09:01:38 +00:00
|
|
|
finalize();
|
2018-10-09 15:36:06 +00:00
|
|
|
}).send();
|
|
|
|
}
|
|
|
|
|
2019-01-16 09:01:38 +00:00
|
|
|
void ApiWrap::dialogEntryApplied(not_null<History*> history) {
|
|
|
|
history->dialogEntryApplied();
|
|
|
|
if (const auto callbacks = _dialogRequestsPending.take(history)) {
|
|
|
|
for (const auto &callback : *callbacks) {
|
|
|
|
callback();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (const auto callbacks = _dialogRequests.take(history)) {
|
|
|
|
for (const auto &callback : *callbacks) {
|
|
|
|
callback();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-01-31 17:10:29 +00:00
|
|
|
void ApiWrap::applyPeerDialogs(const MTPmessages_PeerDialogs &dialogs) {
|
|
|
|
Expects(dialogs.type() == mtpc_messages_peerDialogs);
|
|
|
|
|
|
|
|
const auto &data = dialogs.c_messages_peerDialogs();
|
2019-01-18 12:27:37 +00:00
|
|
|
_session->data().processUsers(data.vusers);
|
|
|
|
_session->data().processChats(data.vchats);
|
2018-01-31 17:10:29 +00:00
|
|
|
App::feedMsgs(data.vmessages, NewMessageLast);
|
|
|
|
for (const auto &dialog : data.vdialogs.v) {
|
|
|
|
switch (dialog.type()) {
|
|
|
|
case mtpc_dialog: {
|
|
|
|
const auto &fields = dialog.c_dialog();
|
|
|
|
if (const auto peerId = peerFromMTP(fields.vpeer)) {
|
2019-01-18 12:27:37 +00:00
|
|
|
_session->data().history(peerId)->applyDialog(fields);
|
2018-01-31 17:10:29 +00:00
|
|
|
}
|
|
|
|
} break;
|
|
|
|
|
2018-03-06 17:07:42 +00:00
|
|
|
//case mtpc_dialogFeed: { // #feed
|
|
|
|
// const auto &fields = dialog.c_dialogFeed();
|
2018-04-09 17:48:29 +00:00
|
|
|
// const auto feed = _session->data().feed(fields.vfeed_id.v);
|
2018-03-06 17:07:42 +00:00
|
|
|
// feed->applyDialog(fields);
|
|
|
|
//} break;
|
2018-01-31 17:10:29 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
_session->data().sendHistoryChangeNotifications();
|
|
|
|
}
|
|
|
|
|
2018-02-03 19:52:35 +00:00
|
|
|
void ApiWrap::applyFeedDialogs(
|
|
|
|
not_null<Data::Feed*> feed,
|
|
|
|
const MTPmessages_Dialogs &dialogs) {
|
2019-01-15 11:57:45 +00:00
|
|
|
if (dialogs.type() == mtpc_messages_dialogsNotModified) {
|
|
|
|
LOG(("API Error: "
|
|
|
|
"messages.dialogsNotModified in ApiWrap::applyFeedDialogs."));
|
|
|
|
return;
|
|
|
|
}
|
2018-02-03 19:52:35 +00:00
|
|
|
|
|
|
|
auto channels = std::vector<not_null<ChannelData*>>();
|
2019-01-15 11:57:45 +00:00
|
|
|
dialogs.match([&](const MTPDmessages_dialogsNotModified &) {
|
|
|
|
Unexpected("Type in ApiWrap::applyFeedDialogs.");
|
|
|
|
}, [&](const auto &data) {
|
2019-01-18 12:27:37 +00:00
|
|
|
_session->data().processUsers(data.vusers);
|
|
|
|
_session->data().processChats(data.vchats);
|
2019-01-15 11:57:45 +00:00
|
|
|
App::feedMsgs(data.vmessages.v, NewMessageLast);
|
|
|
|
channels.reserve(data.vdialogs.v.size());
|
|
|
|
for (const auto &dialog : data.vdialogs.v) {
|
|
|
|
dialog.match([&](const MTPDdialog &data) {
|
|
|
|
if (const auto peerId = peerFromMTP(data.vpeer)) {
|
|
|
|
if (peerIsChannel(peerId)) {
|
2019-01-18 12:27:37 +00:00
|
|
|
const auto history = _session->data().history(peerId);
|
2019-01-15 11:57:45 +00:00
|
|
|
history->applyDialog(dialog.c_dialog());
|
|
|
|
channels.emplace_back(history->peer->asChannel());
|
|
|
|
} else {
|
|
|
|
LOG(("API Error: "
|
|
|
|
"Unexpected peer in feed dialogs list."));
|
|
|
|
}
|
2018-02-03 19:52:35 +00:00
|
|
|
}
|
2019-01-15 11:57:45 +00:00
|
|
|
//}, [&](const MTPDdialogFeed &) { // #feed
|
|
|
|
// LOG(("API Error: "
|
|
|
|
// "Unexpected dialogFeed in feed dialogs list."));
|
|
|
|
});
|
2018-02-03 19:52:35 +00:00
|
|
|
}
|
2019-01-15 11:57:45 +00:00
|
|
|
});
|
2018-02-03 19:52:35 +00:00
|
|
|
|
|
|
|
feed->setChannels(channels);
|
|
|
|
_session->data().sendHistoryChangeNotifications();
|
|
|
|
}
|
|
|
|
|
2018-06-26 18:03:45 +00:00
|
|
|
void ApiWrap::changeDialogUnreadMark(
|
|
|
|
not_null<History*> history,
|
|
|
|
bool unread) {
|
|
|
|
history->setUnreadMark(unread);
|
|
|
|
|
|
|
|
using Flag = MTPmessages_MarkDialogUnread::Flag;
|
|
|
|
request(MTPmessages_MarkDialogUnread(
|
|
|
|
MTP_flags(unread ? Flag::f_unread : Flag(0)),
|
|
|
|
MTP_inputDialogPeer(history->peer->input)
|
|
|
|
)).send();
|
|
|
|
}
|
|
|
|
|
2019-01-15 11:57:45 +00:00
|
|
|
void ApiWrap::requestFakeChatListMessage(
|
|
|
|
not_null<History*> history) {
|
|
|
|
if (_fakeChatListRequests.contains(history)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
_fakeChatListRequests.emplace(history);
|
|
|
|
request(MTPmessages_GetHistory(
|
|
|
|
history->peer->input,
|
|
|
|
MTP_int(0), // offset_id
|
|
|
|
MTP_int(0), // offset_date
|
|
|
|
MTP_int(0), // add_offset
|
|
|
|
MTP_int(2), // limit
|
|
|
|
MTP_int(0), // max_id
|
|
|
|
MTP_int(0), // min_id
|
|
|
|
MTP_int(0)
|
|
|
|
)).done([=](const MTPmessages_Messages &result) {
|
|
|
|
_fakeChatListRequests.erase(history);
|
|
|
|
history->setFakeChatListMessageFrom(result);
|
|
|
|
}).fail([=](const RPCError &error) {
|
|
|
|
_fakeChatListRequests.erase(history);
|
|
|
|
history->setFakeChatListMessageFrom(MTP_messages_messages(
|
|
|
|
MTP_vector<MTPMessage>(0),
|
|
|
|
MTP_vector<MTPChat>(0),
|
|
|
|
MTP_vector<MTPUser>(0)));
|
|
|
|
}).send();
|
|
|
|
}
|
|
|
|
|
2019-01-17 08:18:23 +00:00
|
|
|
void ApiWrap::requestWallPaper(
|
|
|
|
const QString &slug,
|
|
|
|
Fn<void(const Data::WallPaper &)> done,
|
|
|
|
Fn<void(const RPCError &)> fail) {
|
|
|
|
if (_wallPaperSlug != slug) {
|
|
|
|
_wallPaperSlug = slug;
|
|
|
|
if (_wallPaperRequestId) {
|
|
|
|
request(base::take(_wallPaperRequestId)).cancel();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
_wallPaperDone = std::move(done);
|
|
|
|
_wallPaperFail = std::move(fail);
|
|
|
|
if (_wallPaperRequestId) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
_wallPaperRequestId = request(MTPaccount_GetWallPaper(
|
|
|
|
MTP_inputWallPaperSlug(MTP_string(slug))
|
|
|
|
)).done([=](const MTPWallPaper &result) {
|
|
|
|
_wallPaperRequestId = 0;
|
|
|
|
_wallPaperSlug = QString();
|
2019-01-28 13:59:49 +00:00
|
|
|
if (const auto paper = Data::WallPaper::Create(result)) {
|
|
|
|
if (const auto done = base::take(_wallPaperDone)) {
|
|
|
|
done(*paper);
|
2019-01-17 08:18:23 +00:00
|
|
|
}
|
2019-01-28 13:59:49 +00:00
|
|
|
} else if (const auto fail = base::take(_wallPaperFail)) {
|
|
|
|
fail(RPCError::Local("BAD_DOCUMENT", "In a wallpaper."));
|
|
|
|
}
|
2019-01-17 08:18:23 +00:00
|
|
|
}).fail([=](const RPCError &error) {
|
|
|
|
_wallPaperRequestId = 0;
|
|
|
|
_wallPaperSlug = QString();
|
|
|
|
if (const auto fail = base::take(_wallPaperFail)) {
|
|
|
|
fail(error);
|
|
|
|
}
|
|
|
|
}).send();
|
|
|
|
}
|
|
|
|
|
2019-01-08 13:57:22 +00:00
|
|
|
void ApiWrap::requestFullPeer(not_null<PeerData*> peer) {
|
|
|
|
if (_fullPeerRequests.contains(peer)) {
|
|
|
|
return;
|
|
|
|
}
|
2015-06-29 22:09:23 +00:00
|
|
|
|
2019-01-08 13:57:22 +00:00
|
|
|
const auto requestId = [&] {
|
|
|
|
const auto failHandler = [=](const RPCError &error) {
|
2017-04-06 19:02:40 +00:00
|
|
|
_fullPeerRequests.remove(peer);
|
2019-01-14 06:34:51 +00:00
|
|
|
migrateFail(peer, error);
|
2017-04-06 19:02:40 +00:00
|
|
|
};
|
2018-11-20 15:36:36 +00:00
|
|
|
if (const auto user = peer->asUser()) {
|
|
|
|
if (_session->supportMode()) {
|
|
|
|
_session->supportHelper().refreshInfo(user);
|
|
|
|
}
|
2018-09-11 12:50:40 +00:00
|
|
|
return request(MTPusers_GetFullUser(
|
|
|
|
user->inputUser
|
2018-11-20 15:36:36 +00:00
|
|
|
)).done([=](const MTPUserFull &result, mtpRequestId requestId) {
|
2017-04-06 19:02:40 +00:00
|
|
|
gotUserFull(user, result, requestId);
|
|
|
|
}).fail(failHandler).send();
|
2018-11-20 15:36:36 +00:00
|
|
|
} else if (const auto chat = peer->asChat()) {
|
|
|
|
return request(MTPmessages_GetFullChat(
|
|
|
|
chat->inputChat
|
2019-01-08 13:57:22 +00:00
|
|
|
)).done([=](
|
|
|
|
const MTPmessages_ChatFull &result,
|
|
|
|
mtpRequestId requestId) {
|
2017-04-06 19:02:40 +00:00
|
|
|
gotChatFull(peer, result, requestId);
|
|
|
|
}).fail(failHandler).send();
|
2018-11-20 15:36:36 +00:00
|
|
|
} else if (const auto channel = peer->asChannel()) {
|
|
|
|
return request(MTPchannels_GetFullChannel(
|
|
|
|
channel->inputChannel
|
2019-01-08 13:57:22 +00:00
|
|
|
)).done([=](
|
|
|
|
const MTPmessages_ChatFull &result,
|
|
|
|
mtpRequestId requestId) {
|
2017-04-06 19:02:40 +00:00
|
|
|
gotChatFull(peer, result, requestId);
|
2019-01-14 06:34:51 +00:00
|
|
|
migrateDone(channel, channel);
|
2017-04-06 19:02:40 +00:00
|
|
|
}).fail(failHandler).send();
|
|
|
|
}
|
2019-01-08 13:57:22 +00:00
|
|
|
Unexpected("Peer type in requestFullPeer.");
|
|
|
|
}();
|
|
|
|
_fullPeerRequests.insert(peer, requestId);
|
2015-03-19 09:18:19 +00:00
|
|
|
}
|
|
|
|
|
2019-01-08 13:57:22 +00:00
|
|
|
void ApiWrap::processFullPeer(
|
|
|
|
not_null<PeerData*> peer,
|
|
|
|
const MTPmessages_ChatFull &result) {
|
2017-04-06 19:02:40 +00:00
|
|
|
gotChatFull(peer, result, mtpRequestId(0));
|
2015-10-30 15:57:22 +00:00
|
|
|
}
|
|
|
|
|
2019-01-08 13:57:22 +00:00
|
|
|
void ApiWrap::processFullPeer(
|
|
|
|
not_null<UserData*> user,
|
|
|
|
const MTPUserFull &result) {
|
2017-04-06 19:02:40 +00:00
|
|
|
gotUserFull(user, result, mtpRequestId(0));
|
2015-10-30 15:57:22 +00:00
|
|
|
}
|
|
|
|
|
2019-01-08 13:57:22 +00:00
|
|
|
void ApiWrap::gotChatFull(
|
|
|
|
not_null<PeerData*> peer,
|
|
|
|
const MTPmessages_ChatFull &result,
|
|
|
|
mtpRequestId req) {
|
2019-01-13 08:03:34 +00:00
|
|
|
const auto &d = result.c_messages_chatFull();
|
|
|
|
_session->data().applyMaximumChatVersions(d.vchats);
|
2015-06-29 22:09:23 +00:00
|
|
|
|
2019-01-18 12:27:37 +00:00
|
|
|
_session->data().processUsers(d.vusers);
|
|
|
|
_session->data().processChats(d.vchats);
|
2015-09-03 10:48:40 +00:00
|
|
|
|
2017-09-21 19:21:33 +00:00
|
|
|
using UpdateFlag = Notify::PeerUpdate::Flag;
|
2019-01-08 13:57:22 +00:00
|
|
|
if (const auto chat = peer->asChat()) {
|
2015-09-03 10:48:40 +00:00
|
|
|
if (d.vfull_chat.type() != mtpc_chatFull) {
|
2019-01-08 13:57:22 +00:00
|
|
|
LOG(("MTP Error: bad type in gotChatFull for chat: %1"
|
|
|
|
).arg(d.vfull_chat.type()));
|
2015-09-03 10:48:40 +00:00
|
|
|
return;
|
|
|
|
}
|
2017-03-10 19:46:28 +00:00
|
|
|
auto &f = d.vfull_chat.c_chatFull();
|
2019-01-13 08:03:34 +00:00
|
|
|
Data::ApplyChatParticipants(chat, f.vparticipants);
|
2018-11-02 18:35:57 +00:00
|
|
|
if (f.has_bot_info()) {
|
|
|
|
for (const auto &item : f.vbot_info.v) {
|
|
|
|
item.match([&](const MTPDbotInfo &data) {
|
2019-01-18 12:27:37 +00:00
|
|
|
if (const auto bot = _session->data().userLoaded(data.vuser_id.v)) {
|
2018-11-02 18:35:57 +00:00
|
|
|
bot->setBotInfo(item);
|
|
|
|
fullPeerUpdated().notify(bot);
|
|
|
|
}
|
|
|
|
});
|
2015-06-15 17:19:24 +00:00
|
|
|
}
|
|
|
|
}
|
2019-01-08 13:57:22 +00:00
|
|
|
chat->setFullFlags(f.vflags.v);
|
2018-11-02 18:35:57 +00:00
|
|
|
chat->setUserpicPhoto(f.has_chat_photo()
|
|
|
|
? f.vchat_photo
|
|
|
|
: MTPPhoto(MTP_photoEmpty(MTP_long(0))));
|
|
|
|
chat->setInviteLink(
|
|
|
|
(f.vexported_invite.type() == mtpc_chatInviteExported
|
|
|
|
? qs(f.vexported_invite.c_chatInviteExported().vlink)
|
|
|
|
: QString()));
|
2018-10-31 11:29:14 +00:00
|
|
|
if (f.has_pinned_msg_id()) {
|
|
|
|
chat->setPinnedMessageId(f.vpinned_msg_id.v);
|
|
|
|
} else {
|
|
|
|
chat->clearPinnedMessage();
|
|
|
|
}
|
2017-07-26 11:53:49 +00:00
|
|
|
chat->fullUpdated();
|
2015-04-30 13:53:36 +00:00
|
|
|
|
2019-01-08 13:57:22 +00:00
|
|
|
notifySettingReceived(
|
|
|
|
MTP_inputNotifyPeer(peer->input),
|
|
|
|
f.vnotify_settings);
|
|
|
|
} else if (const auto channel = peer->asChannel()) {
|
2015-09-03 10:48:40 +00:00
|
|
|
if (d.vfull_chat.type() != mtpc_channelFull) {
|
2019-01-08 13:57:22 +00:00
|
|
|
LOG(("MTP Error: bad type in gotChatFull for channel: %1"
|
|
|
|
).arg(d.vfull_chat.type()));
|
2015-09-03 10:48:40 +00:00
|
|
|
return;
|
|
|
|
}
|
2017-04-06 19:02:40 +00:00
|
|
|
auto &f = d.vfull_chat.c_channelFull();
|
2017-11-20 19:54:05 +00:00
|
|
|
channel->setAvailableMinId(f.vavailable_min_id.v);
|
2016-06-02 13:02:55 +00:00
|
|
|
auto canViewAdmins = channel->canViewAdmins();
|
|
|
|
auto canViewMembers = channel->canViewMembers();
|
2017-08-03 12:52:56 +00:00
|
|
|
auto canEditStickers = channel->canEditStickers();
|
2016-06-02 13:02:55 +00:00
|
|
|
|
2017-09-26 11:49:16 +00:00
|
|
|
channel->setFullFlags(f.vflags.v);
|
2017-12-05 06:41:43 +00:00
|
|
|
channel->setUserpicPhoto(f.vchat_photo);
|
2015-11-13 15:14:33 +00:00
|
|
|
if (f.has_migrated_from_chat_id()) {
|
2017-09-26 11:49:16 +00:00
|
|
|
channel->addFlags(MTPDchannel::Flag::f_megagroup);
|
2019-01-14 06:34:51 +00:00
|
|
|
const auto chat = channel->owner().chat(
|
2019-01-18 12:27:37 +00:00
|
|
|
f.vmigrated_from_chat_id.v);
|
2019-01-14 06:34:51 +00:00
|
|
|
Data::ApplyMigration(chat, channel);
|
|
|
|
}
|
|
|
|
for (const auto &item : f.vbot_info.v) {
|
|
|
|
auto &owner = channel->owner();
|
|
|
|
item.match([&](const MTPDbotInfo &info) {
|
|
|
|
if (const auto user = owner.userLoaded(info.vuser_id.v)) {
|
2017-03-10 19:46:28 +00:00
|
|
|
user->setBotInfo(item);
|
2017-04-06 16:49:42 +00:00
|
|
|
fullPeerUpdated().notify(user);
|
2015-11-20 13:34:37 +00:00
|
|
|
}
|
2019-01-14 06:34:51 +00:00
|
|
|
});
|
2015-11-20 13:34:37 +00:00
|
|
|
}
|
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);
|
2017-06-09 16:12:02 +00:00
|
|
|
channel->setRestrictedCount(f.has_banned_count() ? f.vbanned_count.v : 0);
|
2017-04-07 15:07:26 +00:00
|
|
|
channel->setKickedCount(f.has_kicked_count() ? f.vkicked_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());
|
2019-01-18 12:27:37 +00:00
|
|
|
if (const auto history = _session->data().historyLoaded(channel)) {
|
2019-01-03 12:36:01 +00:00
|
|
|
history->clearUpTill(f.vavailable_min_id.v);
|
2018-01-31 17:10:29 +00:00
|
|
|
history->applyDialogFields(
|
|
|
|
f.vunread_count.v,
|
|
|
|
f.vread_inbox_max_id.v,
|
|
|
|
f.vread_outbox_max_id.v);
|
2015-09-21 20:57:42 +00:00
|
|
|
}
|
2017-11-21 13:23:56 +00:00
|
|
|
if (f.has_pinned_msg_id()) {
|
|
|
|
channel->setPinnedMessageId(f.vpinned_msg_id.v);
|
|
|
|
} else {
|
|
|
|
channel->clearPinnedMessage();
|
|
|
|
}
|
2016-03-04 15:34:46 +00:00
|
|
|
if (channel->isMegagroup()) {
|
2017-08-03 12:52:56 +00:00
|
|
|
auto stickersChanged = (canEditStickers != channel->canEditStickers());
|
|
|
|
auto stickerSet = (f.has_stickerset() ? &f.vstickerset.c_stickerSet() : nullptr);
|
|
|
|
auto newSetId = (stickerSet ? stickerSet->vid.v : 0);
|
2018-01-25 12:28:48 +00:00
|
|
|
auto oldSetId = (channel->mgInfo->stickerSet.type() == mtpc_inputStickerSetID)
|
|
|
|
? channel->mgInfo->stickerSet.c_inputStickerSetID().vid.v
|
|
|
|
: 0;
|
2017-08-03 12:52:56 +00:00
|
|
|
if (oldSetId != newSetId) {
|
2018-01-25 12:28:48 +00:00
|
|
|
channel->mgInfo->stickerSet = stickerSet
|
|
|
|
? MTP_inputStickerSetID(stickerSet->vid, stickerSet->vaccess_hash)
|
|
|
|
: MTP_inputStickerSetEmpty();
|
2017-08-03 12:52:56 +00:00
|
|
|
stickersChanged = true;
|
|
|
|
}
|
|
|
|
if (stickersChanged) {
|
2017-09-21 19:21:33 +00:00
|
|
|
Notify::peerUpdatedDelayed(channel, UpdateFlag::ChannelStickersChanged);
|
2017-08-03 12:52:56 +00:00
|
|
|
}
|
2016-03-04 15:34:46 +00:00
|
|
|
}
|
2015-09-21 20:57:42 +00:00
|
|
|
channel->fullUpdated();
|
2015-09-03 10:48:40 +00:00
|
|
|
|
2017-06-09 16:12:02 +00:00
|
|
|
if (canViewAdmins != channel->canViewAdmins()
|
2017-08-03 12:52:56 +00:00
|
|
|
|| canViewMembers != channel->canViewMembers()) {
|
2019-01-05 10:50:04 +00:00
|
|
|
Notify::peerUpdatedDelayed(channel, UpdateFlag::RightsChanged);
|
2017-08-03 12:52:56 +00:00
|
|
|
}
|
2016-06-02 13:02:55 +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
|
|
|
}
|
2015-03-19 09:18:19 +00:00
|
|
|
|
2015-10-30 15:57:22 +00:00
|
|
|
if (req) {
|
2017-06-04 11:09:29 +00:00
|
|
|
auto i = _fullPeerRequests.find(peer);
|
2015-10-30 15:57:22 +00:00
|
|
|
if (i != _fullPeerRequests.cend() && i.value() == req) {
|
|
|
|
_fullPeerRequests.erase(i);
|
|
|
|
}
|
|
|
|
}
|
2017-04-06 16:49:42 +00:00
|
|
|
fullPeerUpdated().notify(peer);
|
2015-03-19 09:18:19 +00:00
|
|
|
}
|
|
|
|
|
2019-01-08 13:57:22 +00:00
|
|
|
void ApiWrap::gotUserFull(
|
|
|
|
not_null<UserData*> user,
|
|
|
|
const MTPUserFull &result,
|
|
|
|
mtpRequestId req) {
|
|
|
|
const auto &d = result.c_userFull();
|
2017-09-26 11:49:16 +00:00
|
|
|
|
2018-09-11 12:50:40 +00:00
|
|
|
if (user == _session->user() && !_session->validateSelf(d.vuser)) {
|
2019-02-19 06:57:53 +00:00
|
|
|
constexpr auto kRequestUserAgainTimeout = crl::time(10000);
|
2018-09-11 12:50:40 +00:00
|
|
|
App::CallDelayed(kRequestUserAgainTimeout, _session, [=] {
|
|
|
|
requestFullPeer(user);
|
|
|
|
});
|
|
|
|
return;
|
|
|
|
}
|
2019-01-18 12:27:37 +00:00
|
|
|
_session->data().processUser(d.vuser);
|
2016-03-04 15:34:46 +00:00
|
|
|
if (d.has_profile_photo()) {
|
2019-01-18 12:27:37 +00:00
|
|
|
_session->data().processPhoto(d.vprofile_photo);
|
2016-03-04 15:34:46 +00:00
|
|
|
}
|
2017-04-06 19:02:40 +00:00
|
|
|
App::feedUserLink(MTP_int(peerToUser(user->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()) {
|
2017-04-06 19:02:40 +00:00
|
|
|
notifySettingReceived(MTP_inputNotifyPeer(user->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()) {
|
2016-10-23 21:03:10 +00:00
|
|
|
user->setBotInfo(d.vbot_info);
|
2016-03-04 15:34:46 +00:00
|
|
|
} else {
|
2016-10-23 21:03:10 +00:00
|
|
|
user->setBotInfoVersion(-1);
|
2016-03-04 15:34:46 +00:00
|
|
|
}
|
2018-10-31 11:29:14 +00:00
|
|
|
if (d.has_pinned_msg_id()) {
|
|
|
|
user->setPinnedMessageId(d.vpinned_msg_id.v);
|
|
|
|
} else {
|
|
|
|
user->clearPinnedMessage();
|
|
|
|
}
|
2018-11-02 18:35:57 +00:00
|
|
|
user->setFullFlags(d.vflags.v);
|
2016-10-23 21:03:10 +00:00
|
|
|
user->setBlockStatus(d.is_blocked() ? UserData::BlockStatus::Blocked : UserData::BlockStatus::NotBlocked);
|
2017-04-28 17:16:14 +00:00
|
|
|
user->setCallsStatus(d.is_phone_calls_private() ? UserData::CallsStatus::Private : d.is_phone_calls_available() ? UserData::CallsStatus::Enabled : UserData::CallsStatus::Disabled);
|
2016-10-23 21:03:10 +00:00
|
|
|
user->setAbout(d.has_about() ? qs(d.vabout) : QString());
|
|
|
|
user->setCommonChatsCount(d.vcommon_chats_count.v);
|
2017-07-26 11:53:49 +00:00
|
|
|
user->fullUpdated();
|
2015-06-10 15:54:24 +00:00
|
|
|
|
2015-10-30 15:57:22 +00:00
|
|
|
if (req) {
|
2017-04-06 19:02:40 +00:00
|
|
|
auto i = _fullPeerRequests.find(user);
|
2015-10-30 15:57:22 +00:00
|
|
|
if (i != _fullPeerRequests.cend() && i.value() == req) {
|
|
|
|
_fullPeerRequests.erase(i);
|
|
|
|
}
|
|
|
|
}
|
2017-04-06 19:02:40 +00:00
|
|
|
fullPeerUpdated().notify(user);
|
2015-06-29 22:09:23 +00:00
|
|
|
}
|
|
|
|
|
2019-01-08 13:57:22 +00:00
|
|
|
void ApiWrap::requestPeer(not_null<PeerData*> peer) {
|
|
|
|
if (_fullPeerRequests.contains(peer) || _peerRequests.contains(peer)) {
|
|
|
|
return;
|
|
|
|
}
|
2015-06-29 22:09:23 +00:00
|
|
|
|
2019-01-08 13:57:22 +00:00
|
|
|
const auto requestId = [&] {
|
|
|
|
const auto failHandler = [=](const RPCError &error) {
|
2017-04-06 19:02:40 +00:00
|
|
|
_peerRequests.remove(peer);
|
|
|
|
};
|
2019-01-08 13:57:22 +00:00
|
|
|
const auto chatHandler = [=](const MTPmessages_Chats &result) {
|
2017-04-06 19:02:40 +00:00
|
|
|
_peerRequests.remove(peer);
|
2019-01-13 08:03:34 +00:00
|
|
|
const auto &chats = result.match([](const auto &data) {
|
|
|
|
return data.vchats;
|
|
|
|
});
|
|
|
|
_session->data().applyMaximumChatVersions(chats);
|
2019-01-18 12:27:37 +00:00
|
|
|
_session->data().processChats(chats);
|
2017-04-06 19:02:40 +00:00
|
|
|
};
|
2019-01-08 13:57:22 +00:00
|
|
|
if (const auto user = peer->asUser()) {
|
|
|
|
return request(MTPusers_GetUsers(
|
|
|
|
MTP_vector<MTPInputUser>(1, user->inputUser)
|
|
|
|
)).done([=](const MTPVector<MTPUser> &result) {
|
2017-04-06 19:02:40 +00:00
|
|
|
_peerRequests.remove(user);
|
2019-01-18 12:27:37 +00:00
|
|
|
_session->data().processUsers(result);
|
2017-04-06 19:02:40 +00:00
|
|
|
}).fail(failHandler).send();
|
2019-01-08 13:57:22 +00:00
|
|
|
} else if (const auto chat = peer->asChat()) {
|
|
|
|
return request(MTPmessages_GetChats(
|
|
|
|
MTP_vector<MTPint>(1, chat->inputChat)
|
|
|
|
)).done(chatHandler).fail(failHandler).send();
|
|
|
|
} else if (const auto channel = peer->asChannel()) {
|
|
|
|
return request(MTPchannels_GetChannels(
|
|
|
|
MTP_vector<MTPInputChannel>(1, channel->inputChannel)
|
|
|
|
)).done(chatHandler).fail(failHandler).send();
|
2017-04-06 19:02:40 +00:00
|
|
|
}
|
2019-01-08 13:57:22 +00:00
|
|
|
Unexpected("Peer type in requestPeer.");
|
|
|
|
}();
|
|
|
|
_peerRequests.insert(peer, requestId);
|
2015-06-29 22:09:23 +00:00
|
|
|
}
|
|
|
|
|
2019-01-13 13:28:05 +00:00
|
|
|
void ApiWrap::migrateChat(
|
|
|
|
not_null<ChatData*> chat,
|
|
|
|
FnMut<void(not_null<ChannelData*>)> done,
|
|
|
|
FnMut<void(const RPCError &)> fail) {
|
|
|
|
const auto callback = [&] {
|
|
|
|
return MigrateCallbacks{ std::move(done), std::move(fail) };
|
|
|
|
};
|
|
|
|
const auto i = _migrateCallbacks.find(chat);
|
|
|
|
if (i != end(_migrateCallbacks)) {
|
|
|
|
i->second.push_back(callback());
|
|
|
|
return;
|
2019-01-14 06:34:51 +00:00
|
|
|
}
|
|
|
|
_migrateCallbacks.emplace(chat).first->second.push_back(callback());
|
|
|
|
if (const auto channel = chat->migrateTo()) {
|
2019-01-13 13:28:05 +00:00
|
|
|
Notify::peerUpdatedDelayed(
|
|
|
|
chat,
|
|
|
|
Notify::PeerUpdate::Flag::MigrationChanged);
|
2019-01-14 06:34:51 +00:00
|
|
|
crl::on_main([=] {
|
|
|
|
migrateDone(chat, channel);
|
2019-01-13 13:28:05 +00:00
|
|
|
});
|
|
|
|
} else if (chat->isDeactivated()) {
|
2019-01-14 06:34:51 +00:00
|
|
|
crl::on_main([=] {
|
|
|
|
migrateFail(
|
|
|
|
chat,
|
|
|
|
RPCError::Local(
|
|
|
|
"BAD_MIGRATION",
|
|
|
|
"Chat is already deactivated"));
|
2019-01-13 13:28:05 +00:00
|
|
|
});
|
|
|
|
return;
|
|
|
|
} else if (!chat->amCreator()) {
|
2019-01-14 06:34:51 +00:00
|
|
|
crl::on_main([=] {
|
|
|
|
migrateFail(
|
|
|
|
chat,
|
|
|
|
RPCError::Local(
|
|
|
|
"BAD_MIGRATION",
|
|
|
|
"Current user is not the creator of that chat"));
|
2019-01-13 13:28:05 +00:00
|
|
|
});
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
request(MTPmessages_MigrateChat(
|
|
|
|
chat->inputChat
|
|
|
|
)).done([=](const MTPUpdates &result) {
|
|
|
|
applyUpdates(result);
|
|
|
|
Notify::peerUpdatedSendDelayed();
|
|
|
|
|
2019-01-14 06:34:51 +00:00
|
|
|
if (const auto channel = chat->migrateTo()) {
|
|
|
|
if (auto handlers = _migrateCallbacks.take(chat)) {
|
|
|
|
_migrateCallbacks.emplace(channel, std::move(*handlers));
|
2019-01-13 13:28:05 +00:00
|
|
|
}
|
2019-01-14 06:34:51 +00:00
|
|
|
requestFullPeer(channel);
|
|
|
|
} else {
|
|
|
|
migrateFail(
|
|
|
|
chat,
|
|
|
|
RPCError::Local("MIGRATION_FAIL", "No channel"));
|
2019-01-13 13:28:05 +00:00
|
|
|
}
|
|
|
|
}).fail([=](const RPCError &error) {
|
2019-01-14 06:34:51 +00:00
|
|
|
migrateFail(chat, error);
|
|
|
|
}).send();
|
|
|
|
}
|
|
|
|
|
|
|
|
void ApiWrap::migrateDone(
|
|
|
|
not_null<PeerData*> peer,
|
|
|
|
not_null<ChannelData*> channel) {
|
|
|
|
Notify::peerUpdatedSendDelayed();
|
|
|
|
if (auto handlers = _migrateCallbacks.take(peer)) {
|
|
|
|
for (auto &handler : *handlers) {
|
|
|
|
if (handler.done) {
|
|
|
|
handler.done(channel);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void ApiWrap::migrateFail(not_null<PeerData*> peer, const RPCError &error) {
|
|
|
|
const auto &type = error.type();
|
|
|
|
if (type == qstr("CHANNELS_TOO_MUCH")) {
|
|
|
|
Ui::show(Box<InformBox>(lang(lng_migrate_error)));
|
|
|
|
}
|
|
|
|
if (auto handlers = _migrateCallbacks.take(peer)) {
|
|
|
|
for (auto &handler : *handlers) {
|
|
|
|
if (handler.fail) {
|
2019-01-13 13:28:05 +00:00
|
|
|
handler.fail(error);
|
|
|
|
}
|
|
|
|
}
|
2019-01-14 06:34:51 +00:00
|
|
|
}
|
2019-01-13 13:28:05 +00:00
|
|
|
}
|
|
|
|
|
2018-01-17 16:21:01 +00:00
|
|
|
void ApiWrap::markMediaRead(
|
|
|
|
const base::flat_set<not_null<HistoryItem*>> &items) {
|
|
|
|
auto markedIds = QVector<MTPint>();
|
|
|
|
auto channelMarkedIds = base::flat_map<
|
|
|
|
not_null<ChannelData*>,
|
|
|
|
QVector<MTPint>>();
|
|
|
|
markedIds.reserve(items.size());
|
|
|
|
for (const auto item : items) {
|
2018-12-26 10:28:24 +00:00
|
|
|
if ((!item->isUnreadMedia() || item->out())
|
|
|
|
&& !item->isUnreadMention()) {
|
2018-01-17 16:21:01 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
item->markMediaRead();
|
|
|
|
if (!IsServerMsgId(item->id)) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (const auto channel = item->history()->peer->asChannel()) {
|
|
|
|
channelMarkedIds[channel].push_back(MTP_int(item->id));
|
|
|
|
} else {
|
|
|
|
markedIds.push_back(MTP_int(item->id));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!markedIds.isEmpty()) {
|
|
|
|
request(MTPmessages_ReadMessageContents(
|
|
|
|
MTP_vector<MTPint>(markedIds)
|
|
|
|
)).done([=](const MTPmessages_AffectedMessages &result) {
|
|
|
|
applyAffectedMessages(result);
|
|
|
|
}).send();
|
|
|
|
}
|
|
|
|
for (const auto &channelIds : channelMarkedIds) {
|
|
|
|
request(MTPchannels_ReadMessageContents(
|
|
|
|
channelIds.first->inputChannel,
|
|
|
|
MTP_vector<MTPint>(channelIds.second)
|
|
|
|
)).send();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void ApiWrap::markMediaRead(not_null<HistoryItem*> item) {
|
2018-12-26 10:28:24 +00:00
|
|
|
if ((!item->isUnreadMedia() || item->out())
|
|
|
|
&& !item->isUnreadMention()) {
|
2018-01-17 16:21:01 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
item->markMediaRead();
|
|
|
|
if (!IsServerMsgId(item->id)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
const auto ids = MTP_vector<MTPint>(1, MTP_int(item->id));
|
|
|
|
if (const auto channel = item->history()->peer->asChannel()) {
|
|
|
|
request(MTPchannels_ReadMessageContents(
|
|
|
|
channel->inputChannel,
|
|
|
|
ids
|
|
|
|
)).send();
|
|
|
|
} else {
|
|
|
|
request(MTPmessages_ReadMessageContents(
|
|
|
|
ids
|
|
|
|
)).done([=](const MTPmessages_AffectedMessages &result) {
|
|
|
|
applyAffectedMessages(result);
|
|
|
|
}).send();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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());
|
2019-01-13 08:03:34 +00:00
|
|
|
for (const auto peer : peers) {
|
|
|
|
if (!peer
|
|
|
|
|| _fullPeerRequests.contains(peer)
|
|
|
|
|| _peerRequests.contains(peer)) {
|
|
|
|
continue;
|
2015-08-07 12:11:50 +00:00
|
|
|
}
|
2019-01-13 08:03:34 +00:00
|
|
|
if (const auto user = peer->asUser()) {
|
|
|
|
users.push_back(user->inputUser);
|
|
|
|
} else if (const auto chat = peer->asChat()) {
|
|
|
|
chats.push_back(chat->inputChat);
|
|
|
|
} else if (const auto channel = peer->asChannel()) {
|
|
|
|
channels.push_back(channel->inputChannel);
|
2017-04-06 19:02:40 +00:00
|
|
|
}
|
2019-01-13 08:03:34 +00:00
|
|
|
}
|
|
|
|
const auto handleChats = [=](const MTPmessages_Chats &result) {
|
2019-01-18 12:27:37 +00:00
|
|
|
_session->data().processChats(result.match([](const auto &data) {
|
2019-01-13 08:03:34 +00:00
|
|
|
return data.vchats;
|
|
|
|
}));
|
2017-04-06 19:02:40 +00:00
|
|
|
};
|
|
|
|
if (!chats.isEmpty()) {
|
2019-01-13 08:03:34 +00:00
|
|
|
request(MTPmessages_GetChats(
|
|
|
|
MTP_vector<MTPint>(chats)
|
|
|
|
)).done(handleChats).send();
|
2017-04-06 19:02:40 +00:00
|
|
|
}
|
|
|
|
if (!channels.isEmpty()) {
|
2019-01-13 08:03:34 +00:00
|
|
|
request(MTPchannels_GetChannels(
|
|
|
|
MTP_vector<MTPInputChannel>(channels)
|
|
|
|
)).done(handleChats).send();
|
2017-04-06 19:02:40 +00:00
|
|
|
}
|
|
|
|
if (!users.isEmpty()) {
|
2019-01-13 08:03:34 +00:00
|
|
|
request(MTPusers_GetUsers(
|
|
|
|
MTP_vector<MTPInputUser>(users)
|
|
|
|
)).done([=](const MTPVector<MTPUser> &result) {
|
2019-01-18 12:27:37 +00:00
|
|
|
_session->data().processUsers(result);
|
2017-04-06 19:02:40 +00:00
|
|
|
}).send();
|
|
|
|
}
|
2015-08-07 12:11:50 +00:00
|
|
|
}
|
|
|
|
|
2017-12-03 16:43:42 +00:00
|
|
|
void ApiWrap::requestLastParticipants(not_null<ChannelData*> channel) {
|
2019-01-13 08:03:34 +00:00
|
|
|
if (!channel->isMegagroup()
|
|
|
|
|| _participantsRequests.contains(channel)) {
|
2017-11-28 18:30:01 +00:00
|
|
|
return;
|
2015-11-19 15:56:29 +00:00
|
|
|
}
|
2016-01-09 11:24:16 +00:00
|
|
|
|
2017-11-28 18:30:01 +00:00
|
|
|
const auto offset = 0;
|
|
|
|
const auto participantsHash = 0;
|
|
|
|
const auto requestId = request(MTPchannels_GetParticipants(
|
2017-11-20 19:54:05 +00:00
|
|
|
channel->inputChannel,
|
|
|
|
MTP_channelParticipantsRecent(),
|
|
|
|
MTP_int(offset),
|
|
|
|
MTP_int(Global::ChatSizeMax()),
|
|
|
|
MTP_int(participantsHash)
|
2019-01-13 08:03:34 +00:00
|
|
|
)).done([=](const MTPchannels_ChannelParticipants &result) {
|
2017-12-03 16:43:42 +00:00
|
|
|
_participantsRequests.remove(channel);
|
|
|
|
parseChannelParticipants(channel, result, [&](
|
|
|
|
int availableCount,
|
|
|
|
const QVector<MTPChannelParticipant> &list) {
|
|
|
|
applyLastParticipantsList(
|
|
|
|
channel,
|
|
|
|
availableCount,
|
|
|
|
list);
|
|
|
|
});
|
|
|
|
}).fail([this, channel](const RPCError &error) {
|
|
|
|
_participantsRequests.remove(channel);
|
2017-04-06 19:02:40 +00:00
|
|
|
}).send();
|
2015-06-29 22:09:23 +00:00
|
|
|
|
2017-11-28 18:30:01 +00:00
|
|
|
_participantsRequests.insert(channel, requestId);
|
2015-03-19 09:18:19 +00:00
|
|
|
}
|
|
|
|
|
2017-12-03 16:43:42 +00:00
|
|
|
void ApiWrap::requestBots(not_null<ChannelData*> channel) {
|
|
|
|
if (!channel->isMegagroup() || _botsRequests.contains(channel)) {
|
2017-04-06 19:02:40 +00:00
|
|
|
return;
|
2016-08-12 15:22:11 +00:00
|
|
|
}
|
2015-08-07 12:11:50 +00:00
|
|
|
|
2017-11-20 19:54:05 +00:00
|
|
|
auto offset = 0;
|
|
|
|
auto participantsHash = 0;
|
|
|
|
auto requestId = request(MTPchannels_GetParticipants(
|
|
|
|
channel->inputChannel,
|
|
|
|
MTP_channelParticipantsBots(),
|
|
|
|
MTP_int(offset),
|
|
|
|
MTP_int(Global::ChatSizeMax()),
|
|
|
|
MTP_int(participantsHash)
|
2017-12-03 16:43:42 +00:00
|
|
|
)).done([this, channel](const MTPchannels_ChannelParticipants &result) {
|
|
|
|
_botsRequests.remove(channel);
|
|
|
|
parseChannelParticipants(channel, result, [&](
|
|
|
|
int availableCount,
|
|
|
|
const QVector<MTPChannelParticipant> &list) {
|
|
|
|
applyBotsList(
|
|
|
|
channel,
|
|
|
|
availableCount,
|
|
|
|
list);
|
|
|
|
});
|
|
|
|
}).fail([this, channel](const RPCError &error) {
|
|
|
|
_botsRequests.remove(channel);
|
2017-04-06 19:02:40 +00:00
|
|
|
}).send();
|
2015-04-04 20:01:34 +00:00
|
|
|
|
2017-04-06 19:02:40 +00:00
|
|
|
_botsRequests.insert(channel, requestId);
|
2015-03-19 09:18:19 +00:00
|
|
|
}
|
|
|
|
|
2017-12-03 16:43:42 +00:00
|
|
|
void ApiWrap::requestAdmins(not_null<ChannelData*> channel) {
|
|
|
|
if (!channel->isMegagroup() || _adminsRequests.contains(channel)) {
|
|
|
|
return;
|
2015-11-09 09:51:22 +00:00
|
|
|
}
|
|
|
|
|
2017-12-03 16:43:42 +00:00
|
|
|
auto offset = 0;
|
|
|
|
auto participantsHash = 0;
|
|
|
|
auto requestId = request(MTPchannels_GetParticipants(
|
|
|
|
channel->inputChannel,
|
|
|
|
MTP_channelParticipantsAdmins(),
|
|
|
|
MTP_int(offset),
|
|
|
|
MTP_int(Global::ChatSizeMax()),
|
|
|
|
MTP_int(participantsHash)
|
|
|
|
)).done([this, channel](const MTPchannels_ChannelParticipants &result) {
|
|
|
|
_adminsRequests.remove(channel);
|
2019-01-10 11:15:13 +00:00
|
|
|
result.match([&](const MTPDchannels_channelParticipants &data) {
|
2019-01-18 12:27:37 +00:00
|
|
|
_session->data().processUsers(data.vusers);
|
2017-12-03 16:43:42 +00:00
|
|
|
applyAdminsList(
|
|
|
|
channel,
|
|
|
|
data.vcount.v,
|
|
|
|
data.vparticipants.v);
|
2019-01-10 11:15:13 +00:00
|
|
|
}, [&](const MTPDchannels_channelParticipantsNotModified &) {
|
2017-12-03 16:43:42 +00:00
|
|
|
LOG(("API Error: channels.channelParticipantsNotModified received!"));
|
2019-01-10 11:15:13 +00:00
|
|
|
});
|
2017-12-03 16:43:42 +00:00
|
|
|
}).fail([this, channel](const RPCError &error) {
|
|
|
|
_adminsRequests.remove(channel);
|
|
|
|
}).send();
|
2017-11-20 19:54:05 +00:00
|
|
|
|
2017-12-03 16:43:42 +00:00
|
|
|
_adminsRequests.insert(channel, requestId);
|
2017-11-20 19:54:05 +00:00
|
|
|
}
|
2015-11-09 09:51:22 +00:00
|
|
|
|
2017-11-20 19:54:05 +00:00
|
|
|
void ApiWrap::applyLastParticipantsList(
|
2017-12-03 16:43:42 +00:00
|
|
|
not_null<ChannelData*> channel,
|
2017-11-28 18:30:01 +00:00
|
|
|
int availableCount,
|
2017-12-03 16:43:42 +00:00
|
|
|
const QVector<MTPChannelParticipant> &list) {
|
|
|
|
channel->mgInfo->lastAdmins.clear();
|
|
|
|
channel->mgInfo->lastRestricted.clear();
|
|
|
|
channel->mgInfo->lastParticipants.clear();
|
|
|
|
channel->mgInfo->lastParticipantsStatus = MegagroupInfo::LastParticipantsUpToDate;
|
|
|
|
|
|
|
|
auto botStatus = channel->mgInfo->botStatus;
|
2019-01-03 14:39:19 +00:00
|
|
|
const auto emptyAdminRights = MTP_chatAdminRights(MTP_flags(0));
|
|
|
|
const auto emptyRestrictedRights = MTP_chatBannedRights(
|
2017-12-03 16:43:42 +00:00
|
|
|
MTP_flags(0),
|
|
|
|
MTP_int(0));
|
|
|
|
for (const auto &p : list) {
|
2019-01-10 11:15:13 +00:00
|
|
|
const auto userId = p.match([](const auto &data) {
|
|
|
|
return data.vuser_id.v;
|
|
|
|
});
|
2017-12-03 16:43:42 +00:00
|
|
|
const auto adminCanEdit = (p.type() == mtpc_channelParticipantAdmin)
|
|
|
|
? p.c_channelParticipantAdmin().is_can_edit()
|
|
|
|
: false;
|
|
|
|
const auto adminRights = (p.type() == mtpc_channelParticipantAdmin)
|
|
|
|
? p.c_channelParticipantAdmin().vadmin_rights
|
|
|
|
: emptyAdminRights;
|
|
|
|
const auto restrictedRights = (p.type() == mtpc_channelParticipantBanned)
|
|
|
|
? p.c_channelParticipantBanned().vbanned_rights
|
|
|
|
: emptyRestrictedRights;
|
2017-06-04 11:09:29 +00:00
|
|
|
if (!userId) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2019-01-18 12:27:37 +00:00
|
|
|
auto user = _session->data().user(userId);
|
2017-12-03 16:43:42 +00:00
|
|
|
if (p.type() == mtpc_channelParticipantCreator) {
|
|
|
|
channel->mgInfo->creator = user;
|
|
|
|
if (!channel->mgInfo->admins.empty()
|
|
|
|
&& !channel->mgInfo->admins.contains(userId)) {
|
|
|
|
Data::ChannelAdminChanges changes(channel);
|
|
|
|
changes.feed(userId, true);
|
2015-11-09 09:51:22 +00:00
|
|
|
}
|
2017-12-03 16:43:42 +00:00
|
|
|
}
|
|
|
|
if (!base::contains(channel->mgInfo->lastParticipants, user)) {
|
|
|
|
channel->mgInfo->lastParticipants.push_back(user);
|
2019-01-03 14:39:19 +00:00
|
|
|
if (adminRights.c_chatAdminRights().vflags.v) {
|
2017-12-03 16:43:42 +00:00
|
|
|
channel->mgInfo->lastAdmins.emplace(
|
|
|
|
user,
|
|
|
|
MegagroupInfo::Admin{ adminRights, adminCanEdit });
|
2019-01-03 14:39:19 +00:00
|
|
|
} else if (restrictedRights.c_chatBannedRights().vflags.v != 0) {
|
2017-12-03 16:43:42 +00:00
|
|
|
channel->mgInfo->lastRestricted.emplace(
|
|
|
|
user,
|
|
|
|
MegagroupInfo::Restricted{ restrictedRights });
|
2015-11-24 16:19:18 +00:00
|
|
|
}
|
2017-12-03 16:43:42 +00:00
|
|
|
if (user->botInfo) {
|
|
|
|
channel->mgInfo->bots.insert(user);
|
|
|
|
if (channel->mgInfo->botStatus != 0 && channel->mgInfo->botStatus < 2) {
|
|
|
|
channel->mgInfo->botStatus = 2;
|
2015-11-20 13:34:37 +00:00
|
|
|
}
|
2015-11-09 09:51:22 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2017-12-03 16:43:42 +00:00
|
|
|
//
|
|
|
|
// getParticipants(Recent) sometimes can't return all members,
|
|
|
|
// only some last subset, size of this subset is availableCount.
|
|
|
|
//
|
|
|
|
// So both list size and availableCount have nothing to do with
|
|
|
|
// the full supergroup members count.
|
|
|
|
//
|
|
|
|
//if (list.isEmpty()) {
|
|
|
|
// channel->setMembersCount(channel->mgInfo->lastParticipants.size());
|
|
|
|
//} else {
|
|
|
|
// channel->setMembersCount(availableCount);
|
|
|
|
//}
|
|
|
|
Notify::PeerUpdate update(channel);
|
|
|
|
update.flags |= Notify::PeerUpdate::Flag::MembersChanged | Notify::PeerUpdate::Flag::AdminsChanged;
|
|
|
|
Notify::peerUpdatedDelayed(update);
|
|
|
|
|
|
|
|
channel->mgInfo->botStatus = botStatus;
|
2019-01-16 09:01:38 +00:00
|
|
|
fullPeerUpdated().notify(channel);
|
2017-12-03 16:43:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void ApiWrap::applyBotsList(
|
|
|
|
not_null<ChannelData*> channel,
|
|
|
|
int availableCount,
|
|
|
|
const QVector<MTPChannelParticipant> &list) {
|
2019-01-18 12:27:37 +00:00
|
|
|
const auto history = _session->data().historyLoaded(channel);
|
2017-12-03 16:43:42 +00:00
|
|
|
channel->mgInfo->bots.clear();
|
|
|
|
channel->mgInfo->botStatus = -1;
|
|
|
|
|
|
|
|
auto needBotsInfos = false;
|
|
|
|
auto botStatus = channel->mgInfo->botStatus;
|
|
|
|
auto keyboardBotFound = !history || !history->lastKeyboardFrom;
|
|
|
|
for (const auto &p : list) {
|
2019-01-10 11:15:13 +00:00
|
|
|
const auto userId = p.match([](const auto &data) {
|
|
|
|
return data.vuser_id.v;
|
|
|
|
});
|
2017-12-03 16:43:42 +00:00
|
|
|
if (!userId) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2019-01-18 12:27:37 +00:00
|
|
|
auto user = _session->data().user(userId);
|
2017-12-03 16:43:42 +00:00
|
|
|
if (user->botInfo) {
|
|
|
|
channel->mgInfo->bots.insert(user);
|
|
|
|
botStatus = 2;// (botStatus > 0/* || !i.key()->botInfo->readsAllHistory*/) ? 2 : 1;
|
|
|
|
if (!user->botInfo->inited) {
|
|
|
|
needBotsInfos = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!keyboardBotFound && user->id == history->lastKeyboardFrom) {
|
|
|
|
keyboardBotFound = true;
|
|
|
|
}
|
|
|
|
}
|
2015-11-20 13:34:37 +00:00
|
|
|
if (needBotsInfos) {
|
2017-12-03 16:43:42 +00:00
|
|
|
requestFullPeer(channel);
|
2015-11-20 13:34:37 +00:00
|
|
|
}
|
2015-11-24 16:19:18 +00:00
|
|
|
if (!keyboardBotFound) {
|
2017-12-03 16:43:42 +00:00
|
|
|
history->clearLastKeyboard();
|
|
|
|
}
|
|
|
|
|
|
|
|
channel->mgInfo->botStatus = botStatus;
|
2019-01-16 09:01:38 +00:00
|
|
|
fullPeerUpdated().notify(channel);
|
2017-12-03 16:43:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void ApiWrap::applyAdminsList(
|
|
|
|
not_null<ChannelData*> channel,
|
|
|
|
int availableCount,
|
|
|
|
const QVector<MTPChannelParticipant> &list) {
|
|
|
|
auto admins = ranges::make_iterator_range(
|
|
|
|
list.begin(), list.end()
|
|
|
|
) | ranges::view::transform([](const MTPChannelParticipant &p) {
|
2019-01-10 11:15:13 +00:00
|
|
|
return p.match([](const auto &data) { return data.vuser_id.v; });
|
2017-12-03 16:43:42 +00:00
|
|
|
});
|
|
|
|
auto adding = base::flat_set<UserId>{ admins.begin(), admins.end() };
|
|
|
|
if (channel->mgInfo->creator) {
|
|
|
|
adding.insert(peerToUser(channel->mgInfo->creator->id));
|
|
|
|
}
|
|
|
|
auto removing = channel->mgInfo->admins;
|
|
|
|
|
|
|
|
if (removing.empty() && adding.empty()) {
|
|
|
|
// Add some admin-placeholder so we don't DDOS
|
|
|
|
// server with admins list requests.
|
|
|
|
LOG(("API Error: Got empty admins list from server."));
|
|
|
|
adding.insert(0);
|
|
|
|
}
|
|
|
|
|
|
|
|
Data::ChannelAdminChanges changes(channel);
|
|
|
|
for (const auto addingId : adding) {
|
|
|
|
if (!removing.remove(addingId)) {
|
|
|
|
changes.feed(addingId, true);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for (const auto removingId : removing) {
|
|
|
|
changes.feed(removingId, false);
|
|
|
|
}
|
2015-11-09 09:51:22 +00:00
|
|
|
}
|
|
|
|
|
2019-01-16 09:01:38 +00:00
|
|
|
void ApiWrap::requestSelfParticipant(not_null<ChannelData*> channel) {
|
2017-04-06 19:02:40 +00:00
|
|
|
if (_selfParticipantRequests.contains(channel)) {
|
2015-09-21 20:57:42 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-01-16 09:01:38 +00:00
|
|
|
const auto finalize = [=](UserId inviter, TimeId inviteDate) {
|
|
|
|
channel->inviter = inviter;
|
|
|
|
channel->inviteDate = inviteDate;
|
2019-01-18 12:27:37 +00:00
|
|
|
if (const auto history = _session->data().historyLoaded(channel)) {
|
2019-01-16 09:01:38 +00:00
|
|
|
if (history->lastMessageKnown()) {
|
|
|
|
history->checkJoinedMessage(true);
|
|
|
|
history->owner().sendHistoryChangeNotifications();
|
|
|
|
} else {
|
|
|
|
requestDialogEntry(history);
|
2017-06-09 16:12:02 +00:00
|
|
|
}
|
2017-04-06 19:02:40 +00:00
|
|
|
}
|
2019-01-16 09:01:38 +00:00
|
|
|
};
|
|
|
|
_selfParticipantRequests.emplace(channel);
|
|
|
|
request(MTPchannels_GetParticipant(
|
|
|
|
channel->inputChannel,
|
|
|
|
MTP_inputUserSelf()
|
|
|
|
)).done([=](const MTPchannels_ChannelParticipant &result) {
|
|
|
|
_selfParticipantRequests.erase(channel);
|
|
|
|
result.match([&](const MTPDchannels_channelParticipant &data) {
|
2019-01-18 12:27:37 +00:00
|
|
|
_session->data().processUsers(data.vusers);
|
2015-09-21 20:57:42 +00:00
|
|
|
|
2019-01-16 09:01:38 +00:00
|
|
|
const auto &participant = data.vparticipant;
|
|
|
|
participant.match([&](const MTPDchannelParticipantSelf &data) {
|
|
|
|
finalize(data.vinviter_id.v, data.vdate.v);
|
|
|
|
}, [&](const MTPDchannelParticipantCreator &) {
|
|
|
|
if (channel->mgInfo) {
|
|
|
|
channel->mgInfo->creator = _session->user();
|
|
|
|
}
|
|
|
|
finalize(_session->userId(), channel->date);
|
|
|
|
}, [&](const MTPDchannelParticipantAdmin &data) {
|
|
|
|
const auto inviter = (data.is_self()
|
|
|
|
&& data.has_inviter_id())
|
|
|
|
? data.vinviter_id.v
|
|
|
|
: -1;
|
|
|
|
finalize(inviter, data.vdate.v);
|
|
|
|
}, [&](const MTPDchannelParticipantBanned &data) {
|
|
|
|
LOG(("API Error: Got self banned participant."));
|
|
|
|
finalize(-1, 0);
|
|
|
|
}, [&](const MTPDchannelParticipant &data) {
|
|
|
|
LOG(("API Error: Got self regular participant."));
|
|
|
|
finalize(-1, 0);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}).fail([=](const RPCError &error) {
|
|
|
|
_selfParticipantRequests.erase(channel);
|
|
|
|
finalize(-1, 0);
|
2017-12-07 13:02:24 +00:00
|
|
|
}).afterDelay(kSmallDelayMs).send();
|
2015-09-21 20:57:42 +00:00
|
|
|
}
|
|
|
|
|
2017-12-26 16:12:10 +00:00
|
|
|
void ApiWrap::kickParticipant(
|
|
|
|
not_null<ChatData*> chat,
|
|
|
|
not_null<UserData*> user) {
|
|
|
|
request(MTPmessages_DeleteChatUser(
|
|
|
|
chat->inputChat,
|
|
|
|
user->inputUser
|
|
|
|
)).done([=](const MTPUpdates &result) {
|
|
|
|
applyUpdates(result);
|
|
|
|
}).send();
|
|
|
|
}
|
2015-11-20 19:03:31 +00:00
|
|
|
|
2017-12-26 16:12:10 +00:00
|
|
|
void ApiWrap::kickParticipant(
|
|
|
|
not_null<ChannelData*> channel,
|
|
|
|
not_null<UserData*> user,
|
2019-01-03 14:39:19 +00:00
|
|
|
const MTPChatBannedRights ¤tRights) {
|
2017-12-26 16:12:10 +00:00
|
|
|
const auto kick = KickRequest(channel, user);
|
|
|
|
if (_kickRequests.contains(kick)) return;
|
2017-06-09 16:12:02 +00:00
|
|
|
|
2017-12-26 16:12:10 +00:00
|
|
|
const auto rights = ChannelData::KickedRestrictedRights();
|
|
|
|
const auto requestId = request(MTPchannels_EditBanned(
|
|
|
|
channel->inputChannel,
|
|
|
|
user->inputUser,
|
|
|
|
rights
|
|
|
|
)).done([=](const MTPUpdates &result) {
|
|
|
|
applyUpdates(result);
|
|
|
|
|
|
|
|
_kickRequests.remove(KickRequest(channel, user));
|
|
|
|
channel->applyEditBanned(user, currentRights, rights);
|
|
|
|
}).fail([this, kick](const RPCError &error) {
|
|
|
|
_kickRequests.remove(kick);
|
|
|
|
}).send();
|
2017-06-09 16:12:02 +00:00
|
|
|
|
2017-12-26 16:12:10 +00:00
|
|
|
_kickRequests.emplace(kick, requestId);
|
2015-11-09 09:51:22 +00:00
|
|
|
}
|
|
|
|
|
2017-12-26 16:12:10 +00:00
|
|
|
void ApiWrap::unblockParticipant(
|
|
|
|
not_null<ChannelData*> channel,
|
|
|
|
not_null<UserData*> user) {
|
|
|
|
const auto kick = KickRequest(channel, user);
|
2018-01-31 17:10:29 +00:00
|
|
|
if (_kickRequests.contains(kick)) {
|
|
|
|
return;
|
|
|
|
}
|
2017-04-07 15:07:26 +00:00
|
|
|
|
2017-12-26 16:12:10 +00:00
|
|
|
const auto requestId = request(MTPchannels_EditBanned(
|
|
|
|
channel->inputChannel,
|
|
|
|
user->inputUser,
|
2019-01-03 14:39:19 +00:00
|
|
|
MTP_chatBannedRights(MTP_flags(0), MTP_int(0))
|
2017-12-26 16:12:10 +00:00
|
|
|
)).done([=](const MTPUpdates &result) {
|
|
|
|
applyUpdates(result);
|
|
|
|
|
|
|
|
_kickRequests.remove(KickRequest(channel, user));
|
|
|
|
if (channel->kickedCount() > 0) {
|
|
|
|
channel->setKickedCount(channel->kickedCount() - 1);
|
|
|
|
} else {
|
|
|
|
channel->updateFullForced();
|
|
|
|
}
|
2019-01-08 13:57:22 +00:00
|
|
|
}).fail([=](const RPCError &error) {
|
2017-12-26 16:12:10 +00:00
|
|
|
_kickRequests.remove(kick);
|
|
|
|
}).send();
|
2017-06-09 16:12:02 +00:00
|
|
|
|
2017-12-26 16:12:10 +00:00
|
|
|
_kickRequests.emplace(kick, requestId);
|
2017-04-07 15:07:26 +00:00
|
|
|
}
|
|
|
|
|
2019-01-08 10:54:18 +00:00
|
|
|
void ApiWrap::saveDefaultRestrictions(
|
|
|
|
not_null<PeerData*> peer,
|
|
|
|
const MTPChatBannedRights &rights,
|
|
|
|
Fn<void(bool)> callback) {
|
|
|
|
if (_defaultRestrictionsRequests.contains(peer)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
const auto requestId = request(MTPmessages_EditChatDefaultBannedRights(
|
|
|
|
peer->input,
|
|
|
|
rights
|
|
|
|
)).done([=](const MTPUpdates &result) {
|
|
|
|
_defaultRestrictionsRequests.erase(peer);
|
|
|
|
applyUpdates(result);
|
|
|
|
if (callback) {
|
|
|
|
callback(true);
|
|
|
|
}
|
|
|
|
}).fail([=](const RPCError &error) {
|
|
|
|
_defaultRestrictionsRequests.erase(peer);
|
|
|
|
if (error.type() == qstr("CHAT_NOT_MODIFIED")) {
|
|
|
|
if (const auto chat = peer->asChat()) {
|
|
|
|
chat->setDefaultRestrictions(rights);
|
2019-01-21 06:26:19 +00:00
|
|
|
} else if (const auto channel = peer->asChannel()) {
|
|
|
|
channel->setDefaultRestrictions(rights);
|
|
|
|
} else {
|
|
|
|
Unexpected("Peer in ApiWrap::saveDefaultRestrictions.");
|
2019-01-08 10:54:18 +00:00
|
|
|
}
|
2019-01-21 06:26:19 +00:00
|
|
|
if (callback) {
|
|
|
|
callback(true);
|
|
|
|
}
|
|
|
|
return;
|
2019-01-08 10:54:18 +00:00
|
|
|
}
|
|
|
|
if (callback) {
|
|
|
|
callback(false);
|
|
|
|
}
|
|
|
|
}).send();
|
|
|
|
}
|
|
|
|
|
2018-01-31 17:10:29 +00:00
|
|
|
void ApiWrap::deleteAllFromUser(
|
|
|
|
not_null<ChannelData*> channel,
|
|
|
|
not_null<UserData*> from) {
|
2019-01-18 12:27:37 +00:00
|
|
|
const auto history = _session->data().historyLoaded(channel);
|
2018-01-31 17:10:29 +00:00
|
|
|
const auto ids = history
|
|
|
|
? history->collectMessagesFromUserToDelete(from)
|
|
|
|
: QVector<MsgId>();
|
|
|
|
const auto channelId = peerToChannel(channel->id);
|
|
|
|
for (const auto msgId : ids) {
|
|
|
|
if (const auto item = App::histItemById(channelId, msgId)) {
|
|
|
|
item->destroy();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-04-09 17:48:29 +00:00
|
|
|
_session->data().sendHistoryChangeNotifications();
|
2018-01-31 17:10:29 +00:00
|
|
|
|
|
|
|
deleteAllFromUserSend(channel, from);
|
|
|
|
}
|
|
|
|
|
|
|
|
void ApiWrap::deleteAllFromUserSend(
|
|
|
|
not_null<ChannelData*> channel,
|
|
|
|
not_null<UserData*> from) {
|
|
|
|
request(MTPchannels_DeleteUserHistory(
|
|
|
|
channel->inputChannel,
|
|
|
|
from->inputUser
|
|
|
|
)).done([=](const MTPmessages_AffectedHistory &result) {
|
|
|
|
const auto offset = applyAffectedHistory(channel, result);
|
|
|
|
if (offset > 0) {
|
|
|
|
deleteAllFromUserSend(channel, from);
|
2019-01-18 12:27:37 +00:00
|
|
|
} else if (const auto history = _session->data().historyLoaded(channel)) {
|
2019-01-15 11:57:45 +00:00
|
|
|
history->requestChatListMessage();
|
2018-01-31 17:10:29 +00:00
|
|
|
}
|
|
|
|
}).send();
|
|
|
|
}
|
|
|
|
|
2017-11-19 14:41:52 +00:00
|
|
|
void ApiWrap::requestChannelMembersForAdd(
|
|
|
|
not_null<ChannelData*> channel,
|
2018-06-04 15:35:11 +00:00
|
|
|
Fn<void(const MTPchannels_ChannelParticipants&)> callback) {
|
2017-11-19 14:41:52 +00:00
|
|
|
_channelMembersForAddCallback = std::move(callback);
|
|
|
|
if (_channelMembersForAdd == channel) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
request(base::take(_channelMembersForAddRequestId)).cancel();
|
|
|
|
|
2017-11-20 19:54:05 +00:00
|
|
|
auto offset = 0;
|
|
|
|
auto participantsHash = 0;
|
2017-11-19 14:41:52 +00:00
|
|
|
|
|
|
|
_channelMembersForAdd = channel;
|
2017-11-20 19:54:05 +00:00
|
|
|
_channelMembersForAddRequestId = request(MTPchannels_GetParticipants(
|
|
|
|
channel->inputChannel,
|
|
|
|
MTP_channelParticipantsRecent(),
|
|
|
|
MTP_int(offset),
|
|
|
|
MTP_int(Global::ChatSizeMax()),
|
|
|
|
MTP_int(participantsHash)
|
|
|
|
)).done([this](const MTPchannels_ChannelParticipants &result) {
|
2017-11-19 14:41:52 +00:00
|
|
|
base::take(_channelMembersForAddRequestId);
|
|
|
|
base::take(_channelMembersForAdd);
|
|
|
|
base::take(_channelMembersForAddCallback)(result);
|
|
|
|
}).fail([this](const RPCError &error) {
|
|
|
|
base::take(_channelMembersForAddRequestId);
|
|
|
|
base::take(_channelMembersForAdd);
|
|
|
|
base::take(_channelMembersForAddCallback);
|
|
|
|
}).send();
|
|
|
|
}
|
|
|
|
|
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() {
|
2017-04-06 19:02:40 +00:00
|
|
|
for (auto 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;
|
|
|
|
|
2017-04-06 19:02:40 +00:00
|
|
|
auto waitMs = (j == e) ? 0 : kSmallDelayMs;
|
|
|
|
i.value().second = request(MTPmessages_GetStickerSet(MTP_inputStickerSetID(MTP_long(i.key()), MTP_long(i.value().first)))).done([this, setId = i.key()](const MTPmessages_StickerSet &result) {
|
|
|
|
gotStickerSet(setId, result);
|
|
|
|
}).fail([this, setId = i.key()](const RPCError &error) {
|
|
|
|
_stickerSetRequests.remove(setId);
|
2017-12-07 13:02:24 +00:00
|
|
|
}).afterDelay(waitMs).send();
|
2015-06-28 12:37:10 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-01-25 12:28:48 +00:00
|
|
|
void ApiWrap::saveStickerSets(
|
|
|
|
const Stickers::Order &localOrder,
|
|
|
|
const Stickers::Order &localRemoved) {
|
2016-11-22 09:48:13 +00:00
|
|
|
for (auto requestId : base::take(_stickerSetDisenableRequests)) {
|
2017-04-06 19:02:40 +00:00
|
|
|
request(requestId).cancel();
|
2016-11-22 09:48:13 +00:00
|
|
|
}
|
2017-04-06 19:02:40 +00:00
|
|
|
request(base::take(_stickersReorderRequestId)).cancel();
|
|
|
|
request(base::take(_stickersClearRecentRequestId)).cancel();
|
2016-11-22 09:48:13 +00:00
|
|
|
|
2017-08-02 15:07:28 +00:00
|
|
|
auto writeInstalled = true, writeRecent = false, writeCloudRecent = false, writeFaved = false, writeArchived = false;
|
2017-12-11 14:45:29 +00:00
|
|
|
auto &recent = Stickers::GetRecentPack();
|
2018-01-03 17:06:43 +00:00
|
|
|
auto &sets = _session->data().stickerSetsRef();
|
2016-11-22 09:48:13 +00:00
|
|
|
|
|
|
|
_stickersOrder = localOrder;
|
|
|
|
for_const (auto removedSetId, localRemoved) {
|
|
|
|
if (removedSetId == Stickers::CloudRecentSetId) {
|
|
|
|
if (sets.remove(Stickers::CloudRecentSetId) != 0) {
|
|
|
|
writeCloudRecent = true;
|
|
|
|
}
|
|
|
|
if (sets.remove(Stickers::CustomSetId)) {
|
|
|
|
writeInstalled = true;
|
|
|
|
}
|
|
|
|
if (!recent.isEmpty()) {
|
|
|
|
recent.clear();
|
|
|
|
writeRecent = true;
|
|
|
|
}
|
|
|
|
|
2018-03-07 17:43:26 +00:00
|
|
|
_stickersClearRecentRequestId = request(MTPmessages_ClearRecentStickers(
|
|
|
|
MTP_flags(0)
|
|
|
|
)).done([this](const MTPBool &result) {
|
2017-04-06 19:02:40 +00:00
|
|
|
_stickersClearRecentRequestId = 0;
|
|
|
|
}).fail([this](const RPCError &error) {
|
|
|
|
_stickersClearRecentRequestId = 0;
|
|
|
|
}).send();
|
2016-11-22 09:48:13 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
auto it = sets.find(removedSetId);
|
|
|
|
if (it != sets.cend()) {
|
|
|
|
for (auto i = recent.begin(); i != recent.cend();) {
|
|
|
|
if (it->stickers.indexOf(i->first) >= 0) {
|
|
|
|
i = recent.erase(i);
|
|
|
|
writeRecent = true;
|
|
|
|
} else {
|
|
|
|
++i;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!(it->flags & MTPDstickerSet::Flag::f_archived)) {
|
|
|
|
MTPInputStickerSet setId = (it->id && it->access) ? MTP_inputStickerSetID(MTP_long(it->id), MTP_long(it->access)) : MTP_inputStickerSetShortName(MTP_string(it->shortName));
|
2017-04-06 19:02:40 +00:00
|
|
|
|
|
|
|
auto requestId = request(MTPmessages_UninstallStickerSet(setId)).done([this](const MTPBool &result, mtpRequestId requestId) {
|
|
|
|
stickerSetDisenabled(requestId);
|
|
|
|
}).fail([this](const RPCError &error, mtpRequestId requestId) {
|
|
|
|
stickerSetDisenabled(requestId);
|
2017-12-07 13:02:24 +00:00
|
|
|
}).afterDelay(kSmallDelayMs).send();
|
2017-04-06 19:02:40 +00:00
|
|
|
|
|
|
|
_stickerSetDisenableRequests.insert(requestId);
|
|
|
|
|
2018-01-03 17:06:43 +00:00
|
|
|
int removeIndex = _session->data().stickerSetsOrder().indexOf(it->id);
|
|
|
|
if (removeIndex >= 0) _session->data().stickerSetsOrderRef().removeAt(removeIndex);
|
2018-01-25 12:28:48 +00:00
|
|
|
if (!(it->flags & MTPDstickerSet_ClientFlag::f_featured)
|
|
|
|
&& !(it->flags & MTPDstickerSet_ClientFlag::f_special)) {
|
2016-11-22 09:48:13 +00:00
|
|
|
sets.erase(it);
|
|
|
|
} else {
|
|
|
|
if (it->flags & MTPDstickerSet::Flag::f_archived) {
|
|
|
|
writeArchived = true;
|
|
|
|
}
|
2018-01-25 12:28:48 +00:00
|
|
|
it->flags &= ~(MTPDstickerSet::Flag::f_installed_date | MTPDstickerSet::Flag::f_archived);
|
|
|
|
it->installDate = TimeId(0);
|
2016-11-22 09:48:13 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Clear all installed flags, set only for sets from order.
|
|
|
|
for (auto &set : sets) {
|
|
|
|
if (!(set.flags & MTPDstickerSet::Flag::f_archived)) {
|
2018-01-25 12:28:48 +00:00
|
|
|
set.flags &= ~MTPDstickerSet::Flag::f_installed_date;
|
2016-11-22 09:48:13 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-01-03 17:06:43 +00:00
|
|
|
auto &order = _session->data().stickerSetsOrderRef();
|
2016-11-22 09:48:13 +00:00
|
|
|
order.clear();
|
|
|
|
for_const (auto setId, _stickersOrder) {
|
|
|
|
auto it = sets.find(setId);
|
|
|
|
if (it != sets.cend()) {
|
|
|
|
if ((it->flags & MTPDstickerSet::Flag::f_archived) && !localRemoved.contains(it->id)) {
|
|
|
|
MTPInputStickerSet mtpSetId = (it->id && it->access) ? MTP_inputStickerSetID(MTP_long(it->id), MTP_long(it->access)) : MTP_inputStickerSetShortName(MTP_string(it->shortName));
|
2017-04-06 19:02:40 +00:00
|
|
|
|
|
|
|
auto requestId = request(MTPmessages_InstallStickerSet(mtpSetId, MTP_boolFalse())).done([this](const MTPmessages_StickerSetInstallResult &result, mtpRequestId requestId) {
|
|
|
|
stickerSetDisenabled(requestId);
|
|
|
|
}).fail([this](const RPCError &error, mtpRequestId requestId) {
|
|
|
|
stickerSetDisenabled(requestId);
|
2017-12-07 13:02:24 +00:00
|
|
|
}).afterDelay(kSmallDelayMs).send();
|
2017-04-06 19:02:40 +00:00
|
|
|
|
|
|
|
_stickerSetDisenableRequests.insert(requestId);
|
|
|
|
|
2016-11-22 09:48:13 +00:00
|
|
|
it->flags &= ~MTPDstickerSet::Flag::f_archived;
|
|
|
|
writeArchived = true;
|
|
|
|
}
|
|
|
|
order.push_back(setId);
|
2018-01-25 12:28:48 +00:00
|
|
|
it->flags |= MTPDstickerSet::Flag::f_installed_date;
|
|
|
|
if (!it->installDate) {
|
|
|
|
it->installDate = unixtime();
|
|
|
|
}
|
2016-11-22 09:48:13 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
for (auto it = sets.begin(); it != sets.cend();) {
|
|
|
|
if ((it->flags & MTPDstickerSet_ClientFlag::f_featured)
|
2018-01-25 12:28:48 +00:00
|
|
|
|| (it->flags & MTPDstickerSet::Flag::f_installed_date)
|
2016-11-22 09:48:13 +00:00
|
|
|
|| (it->flags & MTPDstickerSet::Flag::f_archived)
|
|
|
|
|| (it->flags & MTPDstickerSet_ClientFlag::f_special)) {
|
|
|
|
++it;
|
|
|
|
} else {
|
|
|
|
it = sets.erase(it);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (writeInstalled) Local::writeInstalledStickers();
|
|
|
|
if (writeRecent) Local::writeUserSettings();
|
|
|
|
if (writeArchived) Local::writeArchivedStickers();
|
|
|
|
if (writeCloudRecent) Local::writeRecentStickers();
|
2017-08-02 15:07:28 +00:00
|
|
|
if (writeFaved) Local::writeFavedStickers();
|
2018-01-17 18:20:55 +00:00
|
|
|
_session->data().notifyStickersUpdated();
|
2016-11-22 09:48:13 +00:00
|
|
|
|
2017-11-05 17:07:27 +00:00
|
|
|
if (_stickerSetDisenableRequests.empty()) {
|
2016-11-22 09:48:13 +00:00
|
|
|
stickersSaveOrder();
|
|
|
|
} else {
|
2017-04-06 19:02:40 +00:00
|
|
|
requestSendDelayed();
|
2016-11-22 09:48:13 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-04-06 19:02:40 +00:00
|
|
|
void ApiWrap::stickerSetDisenabled(mtpRequestId requestId) {
|
|
|
|
_stickerSetDisenableRequests.remove(requestId);
|
2017-11-05 17:07:27 +00:00
|
|
|
if (_stickerSetDisenableRequests.empty()) {
|
2017-04-06 19:02:40 +00:00
|
|
|
stickersSaveOrder();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2018-03-16 16:56:07 +00:00
|
|
|
void ApiWrap::joinChannel(not_null<ChannelData*> channel) {
|
2016-05-25 17:59:21 +00:00
|
|
|
if (channel->amIn()) {
|
2018-03-16 16:56:07 +00:00
|
|
|
Notify::peerUpdatedDelayed(
|
|
|
|
channel,
|
|
|
|
Notify::PeerUpdate::Flag::ChannelAmIn);
|
2016-05-25 17:59:21 +00:00
|
|
|
} else if (!_channelAmInRequests.contains(channel)) {
|
2018-03-16 16:56:07 +00:00
|
|
|
auto requestId = request(MTPchannels_JoinChannel(
|
|
|
|
channel->inputChannel
|
|
|
|
)).done([=](const MTPUpdates &result) {
|
2017-04-06 19:02:40 +00:00
|
|
|
_channelAmInRequests.remove(channel);
|
2017-04-13 17:59:05 +00:00
|
|
|
applyUpdates(result);
|
2018-03-16 16:56:07 +00:00
|
|
|
}).fail([=](const RPCError &error) {
|
|
|
|
if (error.type() == qstr("CHANNEL_PRIVATE")
|
|
|
|
|| error.type() == qstr("CHANNEL_PUBLIC_GROUP_NA")
|
|
|
|
|| error.type() == qstr("USER_BANNED_IN_CHANNEL")) {
|
|
|
|
Ui::show(Box<InformBox>(lang(channel->isMegagroup()
|
|
|
|
? lng_group_not_accessible
|
|
|
|
: lng_channel_not_accessible)));
|
|
|
|
} else if (error.type() == qstr("CHANNELS_TOO_MUCH")) {
|
2017-04-06 19:02:40 +00:00
|
|
|
Ui::show(Box<InformBox>(lang(lng_join_channel_error)));
|
2018-03-16 16:56:07 +00:00
|
|
|
} else if (error.type() == qstr("USERS_TOO_MUCH")) {
|
|
|
|
Ui::show(Box<InformBox>(lang(lng_group_full)));
|
2017-04-06 19:02:40 +00:00
|
|
|
}
|
|
|
|
_channelAmInRequests.remove(channel);
|
|
|
|
}).send();
|
|
|
|
|
2016-05-25 17:59:21 +00:00
|
|
|
_channelAmInRequests.insert(channel, requestId);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-03-16 16:56:07 +00:00
|
|
|
void ApiWrap::leaveChannel(not_null<ChannelData*> channel) {
|
2016-05-25 17:59:21 +00:00
|
|
|
if (!channel->amIn()) {
|
2018-03-16 16:56:07 +00:00
|
|
|
Notify::peerUpdatedDelayed(
|
|
|
|
channel,
|
|
|
|
Notify::PeerUpdate::Flag::ChannelAmIn);
|
2016-05-25 17:59:21 +00:00
|
|
|
} else if (!_channelAmInRequests.contains(channel)) {
|
2018-03-16 16:56:07 +00:00
|
|
|
auto requestId = request(MTPchannels_LeaveChannel(
|
|
|
|
channel->inputChannel
|
|
|
|
)).done([=](const MTPUpdates &result) {
|
2017-04-06 19:02:40 +00:00
|
|
|
_channelAmInRequests.remove(channel);
|
2017-04-13 17:59:05 +00:00
|
|
|
applyUpdates(result);
|
2018-03-16 16:56:07 +00:00
|
|
|
}).fail([=](const RPCError &error) {
|
2017-04-06 19:02:40 +00:00
|
|
|
_channelAmInRequests.remove(channel);
|
|
|
|
}).send();
|
2016-05-25 17:59:21 +00:00
|
|
|
|
2017-04-06 19:02:40 +00:00
|
|
|
_channelAmInRequests.insert(channel, requestId);
|
2016-06-07 19:59:39 +00:00
|
|
|
}
|
2016-05-25 17:59:21 +00:00
|
|
|
}
|
|
|
|
|
2018-04-09 17:48:29 +00:00
|
|
|
void ApiWrap::blockUser(not_null<UserData*> user) {
|
2016-06-01 20:05:37 +00:00
|
|
|
if (user->isBlocked()) {
|
|
|
|
Notify::peerUpdatedDelayed(user, Notify::PeerUpdate::Flag::UserIsBlocked);
|
2018-04-09 17:48:29 +00:00
|
|
|
} else if (_blockRequests.find(user) == end(_blockRequests)) {
|
2017-04-06 19:02:40 +00:00
|
|
|
auto requestId = request(MTPcontacts_Block(user->inputUser)).done([this, user](const MTPBool &result) {
|
2018-04-09 17:48:29 +00:00
|
|
|
_blockRequests.erase(user);
|
2017-04-06 19:02:40 +00:00
|
|
|
user->setBlockStatus(UserData::BlockStatus::Blocked);
|
|
|
|
}).fail([this, user](const RPCError &error) {
|
2018-04-09 17:48:29 +00:00
|
|
|
_blockRequests.erase(user);
|
2017-04-06 19:02:40 +00:00
|
|
|
}).send();
|
|
|
|
|
2018-04-09 17:48:29 +00:00
|
|
|
_blockRequests.emplace(user, requestId);
|
2016-06-01 20:05:37 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-04-09 17:48:29 +00:00
|
|
|
void ApiWrap::unblockUser(not_null<UserData*> user) {
|
2016-06-01 20:05:37 +00:00
|
|
|
if (!user->isBlocked()) {
|
2018-12-04 11:46:07 +00:00
|
|
|
Notify::peerUpdatedDelayed(
|
|
|
|
user,
|
|
|
|
Notify::PeerUpdate::Flag::UserIsBlocked);
|
2018-04-09 17:48:29 +00:00
|
|
|
} else if (_blockRequests.find(user) == end(_blockRequests)) {
|
2018-12-04 11:46:07 +00:00
|
|
|
const auto requestId = request(MTPcontacts_Unblock(
|
|
|
|
user->inputUser
|
|
|
|
)).done([=](const MTPBool &result) {
|
2018-04-09 17:48:29 +00:00
|
|
|
_blockRequests.erase(user);
|
2017-04-06 19:02:40 +00:00
|
|
|
user->setBlockStatus(UserData::BlockStatus::NotBlocked);
|
2019-03-12 10:36:33 +00:00
|
|
|
if (user->isBot() && !user->isSupport()) {
|
2018-12-04 11:46:07 +00:00
|
|
|
sendBotStart(user);
|
|
|
|
}
|
|
|
|
}).fail([=](const RPCError &error) {
|
2018-04-09 17:48:29 +00:00
|
|
|
_blockRequests.erase(user);
|
2017-04-06 19:02:40 +00:00
|
|
|
}).send();
|
2018-04-09 17:48:29 +00:00
|
|
|
_blockRequests.emplace(user, requestId);
|
2016-06-01 20:05:37 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-04-09 17:48:29 +00:00
|
|
|
void ApiWrap::exportInviteLink(not_null<PeerData*> peer) {
|
|
|
|
if (_exportInviteRequests.find(peer) != end(_exportInviteRequests)) {
|
2016-06-01 13:07:03 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-01-03 14:39:19 +00:00
|
|
|
const auto requestId = [&] {
|
|
|
|
return request(MTPmessages_ExportChatInvite(
|
|
|
|
peer->input
|
|
|
|
)).done([=](const MTPExportedChatInvite &result) {
|
2018-04-09 17:48:29 +00:00
|
|
|
_exportInviteRequests.erase(peer);
|
2019-01-03 14:39:19 +00:00
|
|
|
const auto link = (result.type() == mtpc_chatInviteExported)
|
|
|
|
? qs(result.c_chatInviteExported().vlink)
|
|
|
|
: QString();
|
|
|
|
if (const auto chat = peer->asChat()) {
|
|
|
|
chat->setInviteLink(link);
|
|
|
|
} else if (const auto channel = peer->asChannel()) {
|
|
|
|
channel->setInviteLink(link);
|
|
|
|
} else {
|
|
|
|
Unexpected("Peer in ApiWrap::exportInviteLink.");
|
|
|
|
}
|
|
|
|
}).fail([=](const RPCError &error) {
|
|
|
|
_exportInviteRequests.erase(peer);
|
|
|
|
}).send();
|
|
|
|
}();
|
|
|
|
_exportInviteRequests.emplace(peer, requestId);
|
2016-06-01 13:07:03 +00:00
|
|
|
}
|
|
|
|
|
2018-04-09 17:48:29 +00:00
|
|
|
void ApiWrap::requestNotifySettings(const MTPInputNotifyPeer &peer) {
|
|
|
|
const auto key = [&] {
|
|
|
|
switch (peer.type()) {
|
|
|
|
case mtpc_inputNotifyUsers: return peerFromUser(0);
|
|
|
|
case mtpc_inputNotifyChats: return peerFromChat(0);
|
2018-11-06 11:10:20 +00:00
|
|
|
case mtpc_inputNotifyBroadcasts: return peerFromChannel(0);
|
2018-04-09 17:48:29 +00:00
|
|
|
case mtpc_inputNotifyPeer: {
|
|
|
|
const auto &inner = peer.c_inputNotifyPeer().vpeer;
|
|
|
|
switch (inner.type()) {
|
|
|
|
case mtpc_inputPeerSelf:
|
|
|
|
return _session->userPeerId();
|
|
|
|
case mtpc_inputPeerEmpty:
|
|
|
|
return PeerId(0);
|
|
|
|
case mtpc_inputPeerChannel:
|
|
|
|
return peerFromChannel(
|
|
|
|
inner.c_inputPeerChannel().vchannel_id);
|
|
|
|
case mtpc_inputPeerChat:
|
|
|
|
return peerFromChat(inner.c_inputPeerChat().vchat_id);
|
|
|
|
case mtpc_inputPeerUser:
|
|
|
|
return peerFromUser(inner.c_inputPeerUser().vuser_id);
|
|
|
|
}
|
|
|
|
Unexpected("Type in ApiRequest::requestNotifySettings peer.");
|
|
|
|
} break;
|
|
|
|
}
|
|
|
|
Unexpected("Type in ApiRequest::requestNotifySettings.");
|
|
|
|
}();
|
|
|
|
if (_notifySettingRequests.find(key) != end(_notifySettingRequests)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
auto requestId = request(MTPaccount_GetNotifySettings(
|
|
|
|
peer
|
|
|
|
)).done([=](const MTPPeerNotifySettings &result) {
|
|
|
|
notifySettingReceived(peer, result);
|
|
|
|
_notifySettingRequests.erase(key);
|
|
|
|
}).fail([=](const RPCError &error) {
|
|
|
|
notifySettingReceived(peer, MTP_peerNotifySettings(
|
|
|
|
MTP_flags(0),
|
|
|
|
MTPBool(),
|
|
|
|
MTPBool(),
|
|
|
|
MTPint(),
|
|
|
|
MTPstring()));
|
|
|
|
_notifySettingRequests.erase(key);
|
2017-04-06 19:02:40 +00:00
|
|
|
}).send();
|
|
|
|
|
2018-04-09 17:48:29 +00:00
|
|
|
_notifySettingRequests.emplace(key, requestId);
|
|
|
|
}
|
|
|
|
|
|
|
|
void ApiWrap::updateNotifySettingsDelayed(not_null<const PeerData*> peer) {
|
|
|
|
_updateNotifySettingsPeers.emplace(peer);
|
|
|
|
_updateNotifySettingsTimer.callOnce(kNotifySettingSaveTimeout);
|
|
|
|
}
|
|
|
|
|
|
|
|
void ApiWrap::sendNotifySettingsUpdates() {
|
|
|
|
while (!_updateNotifySettingsPeers.empty()) {
|
|
|
|
const auto peer = *_updateNotifySettingsPeers.begin();
|
|
|
|
_updateNotifySettingsPeers.erase(_updateNotifySettingsPeers.begin());
|
|
|
|
request(MTPaccount_UpdateNotifySettings(
|
|
|
|
MTP_inputNotifyPeer(peer->input),
|
|
|
|
peer->notifySerialize()
|
|
|
|
)).afterDelay(_updateNotifySettingsPeers.empty() ? 0 : 10).send();
|
|
|
|
}
|
2016-06-01 13:07:03 +00:00
|
|
|
}
|
|
|
|
|
2018-04-09 17:48:29 +00:00
|
|
|
void ApiWrap::saveDraftToCloudDelayed(not_null<History*> history) {
|
|
|
|
_draftsSaveRequestIds.emplace(history, 0);
|
2016-06-07 19:59:39 +00:00
|
|
|
if (!_draftsSaveTimer.isActive()) {
|
2017-04-07 12:10:10 +00:00
|
|
|
_draftsSaveTimer.callOnce(kSaveCloudDraftTimeout);
|
2016-06-07 19:59:39 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-17 17:19:46 +00:00
|
|
|
void ApiWrap::savePrivacy(const MTPInputPrivacyKey &key, QVector<MTPInputPrivacyRule> &&rules) {
|
2018-09-11 09:11:52 +00:00
|
|
|
const auto keyTypeId = key.type();
|
|
|
|
const auto it = _privacySaveRequests.find(keyTypeId);
|
2017-03-17 17:19:46 +00:00
|
|
|
if (it != _privacySaveRequests.cend()) {
|
2018-01-03 17:06:43 +00:00
|
|
|
request(it->second).cancel();
|
2017-03-17 17:19:46 +00:00
|
|
|
_privacySaveRequests.erase(it);
|
|
|
|
}
|
|
|
|
|
2018-09-11 09:11:52 +00:00
|
|
|
const auto requestId = request(MTPaccount_SetPrivacy(
|
|
|
|
key,
|
|
|
|
MTP_vector<MTPInputPrivacyRule>(std::move(rules))
|
|
|
|
)).done([=](const MTPaccount_PrivacyRules &result) {
|
2017-04-06 19:02:40 +00:00
|
|
|
Expects(result.type() == mtpc_account_privacyRules);
|
2018-04-09 17:48:29 +00:00
|
|
|
|
2017-04-06 19:02:40 +00:00
|
|
|
auto &rules = result.c_account_privacyRules();
|
2019-01-18 12:27:37 +00:00
|
|
|
_session->data().processUsers(rules.vusers);
|
2017-04-06 19:02:40 +00:00
|
|
|
_privacySaveRequests.remove(keyTypeId);
|
|
|
|
handlePrivacyChange(keyTypeId, rules.vrules);
|
2018-09-11 09:11:52 +00:00
|
|
|
}).fail([=](const RPCError &error) {
|
2017-04-06 19:02:40 +00:00
|
|
|
_privacySaveRequests.remove(keyTypeId);
|
|
|
|
}).send();
|
2017-03-17 17:19:46 +00:00
|
|
|
|
2018-01-03 17:06:43 +00:00
|
|
|
_privacySaveRequests.emplace(keyTypeId, requestId);
|
2017-03-17 17:19:46 +00:00
|
|
|
}
|
|
|
|
|
2018-09-11 09:11:52 +00:00
|
|
|
void ApiWrap::handlePrivacyChange(
|
|
|
|
mtpTypeId keyTypeId,
|
|
|
|
const MTPVector<MTPPrivacyRule> &rules) {
|
|
|
|
using Key = Privacy::Key;
|
2018-09-21 16:28:46 +00:00
|
|
|
const auto key = [&]() -> std::optional<Key> {
|
2018-09-11 09:11:52 +00:00
|
|
|
switch (keyTypeId) {
|
|
|
|
case mtpc_privacyKeyStatusTimestamp:
|
|
|
|
case mtpc_inputPrivacyKeyStatusTimestamp: return Key::LastSeen;
|
|
|
|
case mtpc_privacyKeyChatInvite:
|
|
|
|
case mtpc_inputPrivacyKeyChatInvite: return Key::Invites;
|
|
|
|
case mtpc_privacyKeyPhoneCall:
|
|
|
|
case mtpc_inputPrivacyKeyPhoneCall: return Key::Calls;
|
2018-10-24 09:07:54 +00:00
|
|
|
case mtpc_privacyKeyPhoneP2P:
|
|
|
|
case mtpc_inputPrivacyKeyPhoneP2P: return Key::CallsPeer2Peer;
|
2018-09-11 09:11:52 +00:00
|
|
|
}
|
2018-09-21 16:28:46 +00:00
|
|
|
return std::nullopt;
|
2018-09-11 09:11:52 +00:00
|
|
|
}();
|
|
|
|
if (!key) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
pushPrivacy(*key, rules.v);
|
|
|
|
if (*key == Key::LastSeen) {
|
|
|
|
updatePrivacyLastSeens(rules.v);
|
|
|
|
}
|
|
|
|
}
|
2017-03-17 17:19:46 +00:00
|
|
|
|
2018-09-11 09:11:52 +00:00
|
|
|
void ApiWrap::updatePrivacyLastSeens(const QVector<MTPPrivacyRule> &rules) {
|
|
|
|
enum class Rule {
|
|
|
|
Unknown,
|
|
|
|
Allow,
|
|
|
|
Disallow,
|
|
|
|
};
|
|
|
|
auto userRules = QMap<UserId, Rule>();
|
|
|
|
auto contactsRule = Rule::Unknown;
|
|
|
|
auto everyoneRule = Rule::Unknown;
|
|
|
|
for (auto &rule : rules) {
|
|
|
|
auto type = rule.type();
|
|
|
|
if (type != mtpc_privacyValueAllowAll
|
|
|
|
&& type != mtpc_privacyValueDisallowAll
|
|
|
|
&& contactsRule != Rule::Unknown) {
|
|
|
|
// This is simplified: we ignore per-user rules that come after a contacts rule.
|
|
|
|
// But none of the official apps provide such complicated rule sets, so its fine.
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
switch (type) {
|
|
|
|
case mtpc_privacyValueAllowAll: everyoneRule = Rule::Allow; break;
|
|
|
|
case mtpc_privacyValueDisallowAll: everyoneRule = Rule::Disallow; break;
|
|
|
|
case mtpc_privacyValueAllowContacts: contactsRule = Rule::Allow; break;
|
|
|
|
case mtpc_privacyValueDisallowContacts: contactsRule = Rule::Disallow; break;
|
|
|
|
case mtpc_privacyValueAllowUsers: {
|
|
|
|
for_const (auto &userId, rule.c_privacyValueAllowUsers().vusers.v) {
|
|
|
|
if (!userRules.contains(userId.v)) {
|
|
|
|
userRules.insert(userId.v, Rule::Allow);
|
2017-03-17 17:19:46 +00:00
|
|
|
}
|
|
|
|
}
|
2018-09-11 09:11:52 +00:00
|
|
|
} break;
|
|
|
|
case mtpc_privacyValueDisallowUsers: {
|
|
|
|
for_const (auto &userId, rule.c_privacyValueDisallowUsers().vusers.v) {
|
|
|
|
if (!userRules.contains(userId.v)) {
|
|
|
|
userRules.insert(userId.v, Rule::Disallow);
|
|
|
|
}
|
2017-03-17 17:19:46 +00:00
|
|
|
}
|
2018-09-11 09:11:52 +00:00
|
|
|
} break;
|
2017-03-17 17:19:46 +00:00
|
|
|
}
|
2018-09-11 09:11:52 +00:00
|
|
|
if (everyoneRule != Rule::Unknown) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2017-03-17 17:19:46 +00:00
|
|
|
|
2018-09-11 09:11:52 +00:00
|
|
|
auto now = unixtime();
|
2019-01-03 12:36:01 +00:00
|
|
|
_session->data().enumerateUsers([&](UserData *user) {
|
2018-09-11 09:11:52 +00:00
|
|
|
if (user->isSelf() || user->loadedStatus != PeerData::FullLoaded) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (user->onlineTill <= 0) {
|
|
|
|
return;
|
|
|
|
}
|
2017-03-17 17:19:46 +00:00
|
|
|
|
2018-09-11 09:11:52 +00:00
|
|
|
if (user->onlineTill + 3 * 86400 >= now) {
|
|
|
|
user->onlineTill = -2; // recently
|
|
|
|
} else if (user->onlineTill + 7 * 86400 >= now) {
|
|
|
|
user->onlineTill = -3; // last week
|
|
|
|
} else if (user->onlineTill + 30 * 86400 >= now) {
|
|
|
|
user->onlineTill = -4; // last month
|
|
|
|
} else {
|
|
|
|
user->onlineTill = 0;
|
|
|
|
}
|
|
|
|
Notify::peerUpdatedDelayed(user, Notify::PeerUpdate::Flag::UserOnlineChanged);
|
|
|
|
});
|
2017-03-17 17:19:46 +00:00
|
|
|
|
2018-09-11 09:11:52 +00:00
|
|
|
if (_contactsStatusesRequestId) {
|
|
|
|
request(_contactsStatusesRequestId).cancel();
|
|
|
|
}
|
2018-09-11 12:50:40 +00:00
|
|
|
_contactsStatusesRequestId = request(MTPcontacts_GetStatuses(
|
|
|
|
)).done([=](const MTPVector<MTPContactStatus> &result) {
|
2018-09-11 09:11:52 +00:00
|
|
|
_contactsStatusesRequestId = 0;
|
|
|
|
for_const (auto &item, result.v) {
|
|
|
|
Assert(item.type() == mtpc_contactStatus);
|
|
|
|
auto &data = item.c_contactStatus();
|
2019-01-18 12:27:37 +00:00
|
|
|
if (auto user = _session->data().userLoaded(data.vuser_id.v)) {
|
2018-09-11 09:11:52 +00:00
|
|
|
auto oldOnlineTill = user->onlineTill;
|
2018-09-11 12:50:40 +00:00
|
|
|
auto newOnlineTill = OnlineTillFromStatus(data.vstatus, oldOnlineTill);
|
2018-09-11 09:11:52 +00:00
|
|
|
if (oldOnlineTill != newOnlineTill) {
|
|
|
|
user->onlineTill = newOnlineTill;
|
|
|
|
Notify::peerUpdatedDelayed(user, Notify::PeerUpdate::Flag::UserOnlineChanged);
|
2017-04-06 19:02:40 +00:00
|
|
|
}
|
|
|
|
}
|
2018-09-11 09:11:52 +00:00
|
|
|
}
|
|
|
|
}).fail([this](const RPCError &error) {
|
|
|
|
_contactsStatusesRequestId = 0;
|
|
|
|
}).send();
|
2017-03-17 17:19:46 +00:00
|
|
|
}
|
|
|
|
|
2018-09-11 12:50:40 +00:00
|
|
|
int ApiWrap::OnlineTillFromStatus(
|
|
|
|
const MTPUserStatus &status,
|
|
|
|
int currentOnlineTill) {
|
2017-03-17 17:19:46 +00:00
|
|
|
switch (status.type()) {
|
|
|
|
case mtpc_userStatusEmpty: return 0;
|
2018-09-11 12:50:40 +00:00
|
|
|
case mtpc_userStatusRecently:
|
|
|
|
// Don't modify pseudo-online.
|
|
|
|
return (currentOnlineTill > -10) ? -2 : currentOnlineTill;
|
2017-03-17 17:19:46 +00:00
|
|
|
case mtpc_userStatusLastWeek: return -3;
|
|
|
|
case mtpc_userStatusLastMonth: return -4;
|
|
|
|
case mtpc_userStatusOffline: return status.c_userStatusOffline().vwas_online.v;
|
|
|
|
case mtpc_userStatusOnline: return status.c_userStatusOnline().vexpires.v;
|
|
|
|
}
|
|
|
|
Unexpected("Bad UserStatus type.");
|
|
|
|
}
|
|
|
|
|
2017-11-21 16:38:17 +00:00
|
|
|
void ApiWrap::clearHistory(not_null<PeerData*> peer) {
|
2018-01-31 17:10:29 +00:00
|
|
|
auto deleteTillId = MsgId(0);
|
2019-01-18 12:27:37 +00:00
|
|
|
if (const auto history = _session->data().historyLoaded(peer)) {
|
2018-01-31 17:10:29 +00:00
|
|
|
if (const auto last = history->lastMessage()) {
|
|
|
|
deleteTillId = last->id;
|
2019-01-15 11:57:45 +00:00
|
|
|
}
|
|
|
|
if (const auto last = history->chatListMessage()) {
|
2018-02-03 19:52:35 +00:00
|
|
|
Local::addSavedPeer(history->peer, ItemDateTime(last));
|
2017-11-21 16:38:17 +00:00
|
|
|
}
|
|
|
|
history->clear();
|
2018-02-05 14:37:22 +00:00
|
|
|
history->markFullyLoaded();
|
2017-11-21 16:38:17 +00:00
|
|
|
}
|
2018-01-31 17:10:29 +00:00
|
|
|
if (const auto channel = peer->asChannel()) {
|
|
|
|
if (const auto migrated = peer->migrateFrom()) {
|
2017-11-21 16:38:17 +00:00
|
|
|
clearHistory(migrated);
|
|
|
|
}
|
2018-01-31 17:10:29 +00:00
|
|
|
if (IsServerMsgId(deleteTillId)) {
|
2017-11-21 16:38:17 +00:00
|
|
|
request(MTPchannels_DeleteHistory(
|
|
|
|
channel->inputChannel,
|
2018-01-31 17:10:29 +00:00
|
|
|
MTP_int(deleteTillId)
|
2017-11-21 16:38:17 +00:00
|
|
|
)).send();
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
request(MTPmessages_DeleteHistory(
|
|
|
|
MTP_flags(MTPmessages_DeleteHistory::Flag::f_just_clear),
|
|
|
|
peer->input,
|
|
|
|
MTP_int(0)
|
|
|
|
)).done([=](const MTPmessages_AffectedHistory &result) {
|
2018-01-31 17:10:29 +00:00
|
|
|
const auto offset = applyAffectedHistory(peer, result);
|
2017-11-21 16:38:17 +00:00
|
|
|
if (offset > 0) {
|
|
|
|
clearHistory(peer);
|
|
|
|
}
|
|
|
|
}).send();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
int ApiWrap::applyAffectedHistory(
|
|
|
|
not_null<PeerData*> peer,
|
|
|
|
const MTPmessages_AffectedHistory &result) {
|
2017-12-07 13:02:24 +00:00
|
|
|
const auto &data = result.c_messages_affectedHistory();
|
|
|
|
if (const auto channel = peer->asChannel()) {
|
|
|
|
channel->ptsUpdateAndApply(data.vpts.v, data.vpts_count.v);
|
2017-11-21 16:38:17 +00:00
|
|
|
} else {
|
2017-12-07 13:02:24 +00:00
|
|
|
App::main()->ptsUpdateAndApply(data.vpts.v, data.vpts_count.v);
|
2017-11-21 16:38:17 +00:00
|
|
|
}
|
2017-12-07 13:02:24 +00:00
|
|
|
return data.voffset.v;
|
|
|
|
}
|
|
|
|
|
|
|
|
void ApiWrap::applyAffectedMessages(
|
|
|
|
not_null<PeerData*> peer,
|
|
|
|
const MTPmessages_AffectedMessages &result) {
|
|
|
|
const auto &data = result.c_messages_affectedMessages();
|
|
|
|
if (const auto channel = peer->asChannel()) {
|
|
|
|
channel->ptsUpdateAndApply(data.vpts.v, data.vpts_count.v);
|
|
|
|
} else {
|
|
|
|
applyAffectedMessages(result);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void ApiWrap::applyAffectedMessages(
|
|
|
|
const MTPmessages_AffectedMessages &result) {
|
|
|
|
const auto &data = result.c_messages_affectedMessages();
|
|
|
|
App::main()->ptsUpdateAndApply(data.vpts.v, data.vpts_count.v);
|
2017-11-21 16:38:17 +00:00
|
|
|
}
|
|
|
|
|
2016-06-07 19:59:39 +00:00
|
|
|
void ApiWrap::saveDraftsToCloud() {
|
|
|
|
for (auto i = _draftsSaveRequestIds.begin(), e = _draftsSaveRequestIds.end(); i != e; ++i) {
|
2018-04-09 17:48:29 +00:00
|
|
|
if (i->second) continue; // sent already
|
2016-06-07 19:59:39 +00:00
|
|
|
|
2018-04-09 17:48:29 +00:00
|
|
|
auto history = i->first;
|
2016-06-07 19:59:39 +00:00
|
|
|
auto cloudDraft = history->cloudDraft();
|
|
|
|
auto localDraft = history->localDraft();
|
|
|
|
if (cloudDraft && cloudDraft->saveRequestId) {
|
2017-04-06 19:02:40 +00:00
|
|
|
request(base::take(cloudDraft->saveRequestId)).cancel();
|
2016-06-07 19:59:39 +00:00
|
|
|
}
|
2019-01-03 12:36:01 +00:00
|
|
|
if (!_session->supportMode()) {
|
2018-11-15 15:36:04 +00:00
|
|
|
cloudDraft = history->createCloudDraft(localDraft);
|
|
|
|
} else if (!cloudDraft) {
|
|
|
|
cloudDraft = history->createCloudDraft(nullptr);
|
|
|
|
}
|
2016-06-07 19:59:39 +00:00
|
|
|
|
2017-03-25 15:42:01 +00:00
|
|
|
auto flags = MTPmessages_SaveDraft::Flags(0);
|
2016-06-07 19:59:39 +00:00
|
|
|
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;
|
|
|
|
}
|
2018-06-08 20:06:26 +00:00
|
|
|
auto entities = TextUtilities::EntitiesToMTP(
|
|
|
|
ConvertTextTagsToEntities(textWithTags.tags),
|
|
|
|
TextUtilities::ConvertOption::SkipLocal);
|
2017-04-06 19:02:40 +00:00
|
|
|
|
2018-06-26 15:59:05 +00:00
|
|
|
const auto draftText = textWithTags.text;
|
|
|
|
history->setSentDraftText(draftText);
|
2018-06-08 20:06:26 +00:00
|
|
|
cloudDraft->saveRequestId = request(MTPmessages_SaveDraft(
|
|
|
|
MTP_flags(flags),
|
|
|
|
MTP_int(cloudDraft->msgId),
|
|
|
|
history->peer->input,
|
|
|
|
MTP_string(textWithTags.text),
|
|
|
|
entities
|
|
|
|
)).done([=](const MTPBool &result, mtpRequestId requestId) {
|
2018-06-26 15:59:05 +00:00
|
|
|
history->clearSentDraftText(draftText);
|
2018-06-08 20:06:26 +00:00
|
|
|
|
|
|
|
if (const auto cloudDraft = history->cloudDraft()) {
|
2017-04-06 19:02:40 +00:00
|
|
|
if (cloudDraft->saveRequestId == requestId) {
|
|
|
|
cloudDraft->saveRequestId = 0;
|
|
|
|
history->draftSavedToCloud();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
auto i = _draftsSaveRequestIds.find(history);
|
2018-04-09 17:48:29 +00:00
|
|
|
if (i != _draftsSaveRequestIds.cend() && i->second == requestId) {
|
|
|
|
_draftsSaveRequestIds.erase(history);
|
2017-04-29 20:06:32 +00:00
|
|
|
checkQuitPreventFinished();
|
2017-04-06 19:02:40 +00:00
|
|
|
}
|
2018-06-08 20:06:26 +00:00
|
|
|
}).fail([=](const RPCError &error, mtpRequestId requestId) {
|
2018-06-26 15:59:05 +00:00
|
|
|
history->clearSentDraftText(draftText);
|
2018-06-08 20:06:26 +00:00
|
|
|
|
|
|
|
if (const auto cloudDraft = history->cloudDraft()) {
|
2017-04-06 19:02:40 +00:00
|
|
|
if (cloudDraft->saveRequestId == requestId) {
|
|
|
|
history->clearCloudDraft();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
auto i = _draftsSaveRequestIds.find(history);
|
2018-04-09 17:48:29 +00:00
|
|
|
if (i != _draftsSaveRequestIds.cend() && i->second == requestId) {
|
|
|
|
_draftsSaveRequestIds.erase(history);
|
2017-04-29 20:06:32 +00:00
|
|
|
checkQuitPreventFinished();
|
2017-04-06 19:02:40 +00:00
|
|
|
}
|
|
|
|
}).send();
|
|
|
|
|
2018-04-09 17:48:29 +00:00
|
|
|
i->second = cloudDraft->saveRequestId;
|
2016-06-07 19:59:39 +00:00
|
|
|
}
|
2017-04-29 20:06:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool ApiWrap::isQuitPrevent() {
|
2018-04-09 17:48:29 +00:00
|
|
|
if (_draftsSaveRequestIds.empty()) {
|
2017-04-29 20:06:32 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
LOG(("ApiWrap prevents quit, saving drafts..."));
|
|
|
|
saveDraftsToCloud();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void ApiWrap::checkQuitPreventFinished() {
|
2018-04-09 17:48:29 +00:00
|
|
|
if (_draftsSaveRequestIds.empty()) {
|
2017-04-29 20:06:32 +00:00
|
|
|
if (App::quitting()) {
|
|
|
|
LOG(("ApiWrap doesn't prevent quit any more."));
|
|
|
|
}
|
2019-01-21 13:42:21 +00:00
|
|
|
Core::App().quitPreventFinished();
|
2016-06-07 19:59:39 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-04-09 17:48:29 +00:00
|
|
|
void ApiWrap::notifySettingReceived(
|
2017-12-04 17:46:03 +00:00
|
|
|
MTPInputNotifyPeer notifyPeer,
|
|
|
|
const MTPPeerNotifySettings &settings) {
|
2016-06-01 13:07:03 +00:00
|
|
|
switch (notifyPeer.type()) {
|
2017-12-04 17:46:03 +00:00
|
|
|
case mtpc_inputNotifyUsers:
|
2018-04-09 17:48:29 +00:00
|
|
|
_session->data().applyNotifySetting(MTP_notifyUsers(), settings);
|
2017-12-04 17:46:03 +00:00
|
|
|
break;
|
|
|
|
case mtpc_inputNotifyChats:
|
2018-04-09 17:48:29 +00:00
|
|
|
_session->data().applyNotifySetting(MTP_notifyChats(), settings);
|
2017-12-04 17:46:03 +00:00
|
|
|
break;
|
2018-11-06 11:10:20 +00:00
|
|
|
case mtpc_inputNotifyBroadcasts:
|
|
|
|
_session->data().applyNotifySetting(
|
|
|
|
MTP_notifyBroadcasts(),
|
|
|
|
settings);
|
|
|
|
break;
|
2016-06-01 13:07:03 +00:00
|
|
|
case mtpc_inputNotifyPeer: {
|
|
|
|
auto &peer = notifyPeer.c_inputNotifyPeer().vpeer;
|
2018-04-09 17:48:29 +00:00
|
|
|
const auto apply = [&](PeerId peerId) {
|
|
|
|
_session->data().applyNotifySetting(
|
|
|
|
MTP_notifyPeer(peerToMTP(peerId)),
|
2017-12-04 17:46:03 +00:00
|
|
|
settings);
|
2018-04-09 17:48:29 +00:00
|
|
|
};
|
|
|
|
switch (peer.type()) {
|
|
|
|
case mtpc_inputPeerEmpty:
|
|
|
|
apply(0);
|
|
|
|
break;
|
|
|
|
case mtpc_inputPeerSelf:
|
|
|
|
apply(_session->userPeerId());
|
|
|
|
break;
|
|
|
|
case mtpc_inputPeerUser:
|
|
|
|
apply(peerFromUser(peer.c_inputPeerUser().vuser_id));
|
|
|
|
break;
|
|
|
|
case mtpc_inputPeerChat:
|
|
|
|
apply(peerFromChat(peer.c_inputPeerChat().vchat_id));
|
|
|
|
break;
|
|
|
|
case mtpc_inputPeerChannel:
|
|
|
|
apply(peerFromChannel(peer.c_inputPeerChannel().vchannel_id));
|
|
|
|
break;
|
2016-06-01 13:07:03 +00:00
|
|
|
}
|
|
|
|
} break;
|
|
|
|
}
|
2017-06-05 13:33:45 +00:00
|
|
|
_session->notifications().checkDelayed();
|
2016-06-01 13:07:03 +00:00
|
|
|
}
|
|
|
|
|
2015-06-28 12:37:10 +00:00
|
|
|
void ApiWrap::gotStickerSet(uint64 setId, const MTPmessages_StickerSet &result) {
|
|
|
|
_stickerSetRequests.remove(setId);
|
2017-08-03 12:52:56 +00:00
|
|
|
Stickers::FeedSetFull(result);
|
2015-06-28 12:37:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void ApiWrap::requestWebPageDelayed(WebPageData *page) {
|
|
|
|
if (page->pendingTill <= 0) return;
|
|
|
|
_webPagesPending.insert(page, 0);
|
2017-04-07 12:10:10 +00:00
|
|
|
auto left = (page->pendingTill - unixtime()) * 1000;
|
2015-06-28 12:37:10 +00:00
|
|
|
if (!_webPagesTimer.isActive() || left <= _webPagesTimer.remainingTime()) {
|
2017-04-07 12:10:10 +00:00
|
|
|
_webPagesTimer.callOnce((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);
|
2017-04-07 12:10:10 +00:00
|
|
|
if (_webPagesPending.isEmpty() && _webPagesTimer.isActive()) {
|
|
|
|
_webPagesTimer.cancel();
|
|
|
|
}
|
2015-06-28 12:37:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void ApiWrap::clearWebPageRequests() {
|
|
|
|
_webPagesPending.clear();
|
2017-04-07 12:10:10 +00:00
|
|
|
_webPagesTimer.cancel();
|
2015-06-28 12:37:10 +00:00
|
|
|
}
|
|
|
|
|
2015-04-04 20:01:34 +00:00
|
|
|
void ApiWrap::resolveWebPages() {
|
2018-02-06 07:24:55 +00:00
|
|
|
auto ids = QVector<MTPInputMessage>(); // temp_req_id = -1
|
|
|
|
using IndexAndMessageIds = QPair<int32, QVector<MTPInputMessage>>;
|
2016-09-27 13:37:18 +00:00
|
|
|
using MessageIdsByChannel = QMap<ChannelData*, IndexAndMessageIds>;
|
2015-09-12 11:59:50 +00:00
|
|
|
MessageIdsByChannel idsByChannel; // temp_req_id = -index - 2
|
|
|
|
|
2015-04-04 20:01:34 +00:00
|
|
|
ids.reserve(_webPagesPending.size());
|
|
|
|
int32 t = unixtime(), m = INT_MAX;
|
2016-09-27 13:37:18 +00:00
|
|
|
for (auto i = _webPagesPending.begin(); i != _webPagesPending.cend(); ++i) {
|
2015-09-12 11:59:50 +00:00
|
|
|
if (i.value() > 0) continue;
|
2015-04-04 20:01:34 +00:00
|
|
|
if (i.key()->pendingTill <= t) {
|
2018-01-17 16:21:01 +00:00
|
|
|
const auto item = _session->data().findWebPageItem(i.key());
|
|
|
|
if (item) {
|
|
|
|
if (item->channelId() == NoChannel) {
|
2018-02-06 07:24:55 +00:00
|
|
|
ids.push_back(MTP_inputMessageID(MTP_int(item->id)));
|
2018-01-17 16:21:01 +00:00
|
|
|
i.value() = -1;
|
|
|
|
} else {
|
|
|
|
auto channel = item->history()->peer->asChannel();
|
|
|
|
auto channelMap = idsByChannel.find(channel);
|
|
|
|
if (channelMap == idsByChannel.cend()) {
|
|
|
|
channelMap = idsByChannel.insert(
|
|
|
|
channel,
|
|
|
|
IndexAndMessageIds(
|
|
|
|
idsByChannel.size(),
|
2018-02-06 07:24:55 +00:00
|
|
|
QVector<MTPInputMessage>(
|
|
|
|
1,
|
|
|
|
MTP_inputMessageID(MTP_int(item->id)))));
|
2018-01-17 16:21:01 +00:00
|
|
|
} else {
|
2018-02-06 07:24:55 +00:00
|
|
|
channelMap.value().second.push_back(
|
|
|
|
MTP_inputMessageID(MTP_int(item->id)));
|
2015-09-12 11:59:50 +00:00
|
|
|
}
|
2018-01-17 16:21:01 +00:00
|
|
|
i.value() = -channelMap.value().first - 2;
|
2015-09-12 11:59:50 +00:00
|
|
|
}
|
2015-04-04 20:01:34 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
m = qMin(m, i.key()->pendingTill - t);
|
|
|
|
}
|
|
|
|
}
|
2015-09-12 11:59:50 +00:00
|
|
|
|
2017-04-06 19:02:40 +00:00
|
|
|
auto requestId = mtpRequestId(0);
|
|
|
|
if (!ids.isEmpty()) {
|
2018-02-06 07:24:55 +00:00
|
|
|
requestId = request(MTPmessages_GetMessages(
|
|
|
|
MTP_vector<MTPInputMessage>(ids)
|
|
|
|
)).done([=](const MTPmessages_Messages &result, mtpRequestId requestId) {
|
2017-04-06 19:02:40 +00:00
|
|
|
gotWebPages(nullptr, result, requestId);
|
2017-12-07 13:02:24 +00:00
|
|
|
}).afterDelay(kSmallDelayMs).send();
|
2017-04-06 19:02:40 +00:00
|
|
|
}
|
|
|
|
QVector<mtpRequestId> reqsByIndex(idsByChannel.size(), 0);
|
2016-09-27 13:37:18 +00:00
|
|
|
for (auto i = idsByChannel.cbegin(), e = idsByChannel.cend(); i != e; ++i) {
|
2018-02-06 07:24:55 +00:00
|
|
|
reqsByIndex[i.value().first] = request(MTPchannels_GetMessages(
|
|
|
|
i.key()->inputChannel,
|
|
|
|
MTP_vector<MTPInputMessage>(i.value().second)
|
|
|
|
)).done([=, channel = i.key()](const MTPmessages_Messages &result, mtpRequestId requestId) {
|
2017-04-06 19:02:40 +00:00
|
|
|
gotWebPages(channel, result, requestId);
|
2017-12-07 13:02:24 +00:00
|
|
|
}).afterDelay(kSmallDelayMs).send();
|
2017-04-06 19:02:40 +00:00
|
|
|
}
|
|
|
|
if (requestId || !reqsByIndex.isEmpty()) {
|
|
|
|
for (auto &pendingRequestId : _webPagesPending) {
|
|
|
|
if (pendingRequestId > 0) continue;
|
|
|
|
if (pendingRequestId < 0) {
|
|
|
|
if (pendingRequestId == -1) {
|
|
|
|
pendingRequestId = requestId;
|
2015-09-12 11:59:50 +00:00
|
|
|
} else {
|
2017-04-06 19:02:40 +00:00
|
|
|
pendingRequestId = reqsByIndex[-pendingRequestId - 2];
|
2015-09-12 11:59:50 +00:00
|
|
|
}
|
2015-04-04 20:01:34 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2015-09-12 11:59:50 +00:00
|
|
|
|
2017-04-07 12:10:10 +00:00
|
|
|
if (m < INT_MAX) {
|
|
|
|
_webPagesTimer.callOnce(m * 1000);
|
|
|
|
}
|
2015-04-04 20:01:34 +00:00
|
|
|
}
|
|
|
|
|
2019-03-10 12:55:24 +00:00
|
|
|
void ApiWrap::requestAttachedStickerSets(not_null<PhotoData*> photo) {
|
|
|
|
request(_attachedStickerSetsRequestId).cancel();
|
|
|
|
_attachedStickerSetsRequestId = request(MTPmessages_GetAttachedStickers(
|
|
|
|
MTP_inputStickeredMediaPhoto(photo->mtpInput())
|
|
|
|
)).done([=](const MTPVector<MTPStickerSetCovered> &result) {
|
|
|
|
if (result.v.isEmpty()) {
|
|
|
|
Ui::show(Box<InformBox>(lang(lng_stickers_not_found)));
|
|
|
|
return;
|
|
|
|
} else if (result.v.size() > 1) {
|
|
|
|
Ui::show(Box<StickersBox>(result));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
// Single attached sticker pack.
|
|
|
|
const auto setData = result.v.front().match([&](const auto &data) {
|
|
|
|
return data.vset.match([&](const MTPDstickerSet &data) {
|
|
|
|
return &data;
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2019-03-12 10:36:33 +00:00
|
|
|
const auto setId = (setData->vid.v && setData->vaccess_hash.v)
|
2019-03-10 12:55:24 +00:00
|
|
|
? MTP_inputStickerSetID(setData->vid, setData->vaccess_hash)
|
|
|
|
: MTP_inputStickerSetShortName(setData->vshort_name);
|
|
|
|
Ui::show(
|
|
|
|
Box<StickerSetBox>(setId),
|
|
|
|
LayerOption::KeepOther);
|
|
|
|
|
|
|
|
}).fail([=](const RPCError &error) {
|
|
|
|
Ui::show(Box<InformBox>(lang(lng_stickers_not_found)));
|
|
|
|
}).send();
|
|
|
|
}
|
|
|
|
|
2017-12-03 16:43:42 +00:00
|
|
|
void ApiWrap::requestParticipantsCountDelayed(
|
|
|
|
not_null<ChannelData*> channel) {
|
|
|
|
_participantsCountRequestTimer.call(
|
|
|
|
kReloadChannelMembersTimeout,
|
2018-04-07 08:47:08 +00:00
|
|
|
[=] { channel->updateFullForced(); });
|
2015-11-13 15:14:33 +00:00
|
|
|
}
|
|
|
|
|
2018-01-31 17:10:29 +00:00
|
|
|
void ApiWrap::requestChannelRangeDifference(not_null<History*> history) {
|
|
|
|
Expects(history->isChannel());
|
|
|
|
|
|
|
|
const auto channel = history->peer->asChannel();
|
|
|
|
if (const auto requestId = _rangeDifferenceRequests.take(channel)) {
|
|
|
|
request(*requestId).cancel();
|
|
|
|
}
|
|
|
|
const auto range = history->rangeForDifferenceRequest();
|
|
|
|
if (!(range.from < range.till) || !channel->pts()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
MTP_LOG(0, ("getChannelDifference { good - "
|
|
|
|
"after channelDifferenceTooLong was received, "
|
|
|
|
"validating history part }%1").arg(cTestMode() ? " TESTMODE" : ""));
|
|
|
|
channelRangeDifferenceSend(channel, range, channel->pts());
|
|
|
|
}
|
|
|
|
|
|
|
|
void ApiWrap::channelRangeDifferenceSend(
|
|
|
|
not_null<ChannelData*> channel,
|
|
|
|
MsgRange range,
|
|
|
|
int32 pts) {
|
|
|
|
Expects(range.from < range.till);
|
|
|
|
|
|
|
|
const auto limit = range.till - range.from;
|
|
|
|
const auto filter = MTP_channelMessagesFilter(
|
|
|
|
MTP_flags(0),
|
|
|
|
MTP_vector<MTPMessageRange>(1, MTP_messageRange(
|
|
|
|
MTP_int(range.from),
|
|
|
|
MTP_int(range.till - 1))));
|
|
|
|
const auto requestId = request(MTPupdates_GetChannelDifference(
|
|
|
|
MTP_flags(MTPupdates_GetChannelDifference::Flag::f_force),
|
|
|
|
channel->inputChannel,
|
|
|
|
filter,
|
|
|
|
MTP_int(pts),
|
|
|
|
MTP_int(limit)
|
|
|
|
)).done([=](const MTPupdates_ChannelDifference &result) {
|
|
|
|
_rangeDifferenceRequests.remove(channel);
|
|
|
|
channelRangeDifferenceDone(channel, range, result);
|
|
|
|
}).fail([=](const RPCError &error) {
|
|
|
|
_rangeDifferenceRequests.remove(channel);
|
|
|
|
}).send();
|
|
|
|
_rangeDifferenceRequests.emplace(channel, requestId);
|
|
|
|
}
|
|
|
|
|
|
|
|
void ApiWrap::channelRangeDifferenceDone(
|
|
|
|
not_null<ChannelData*> channel,
|
|
|
|
MsgRange range,
|
|
|
|
const MTPupdates_ChannelDifference &result) {
|
|
|
|
auto nextRequestPts = int32(0);
|
|
|
|
auto isFinal = true;
|
|
|
|
|
|
|
|
switch (result.type()) {
|
|
|
|
case mtpc_updates_channelDifferenceEmpty: {
|
|
|
|
const auto &d = result.c_updates_channelDifferenceEmpty();
|
|
|
|
nextRequestPts = d.vpts.v;
|
|
|
|
isFinal = d.is_final();
|
|
|
|
} break;
|
|
|
|
|
|
|
|
case mtpc_updates_channelDifferenceTooLong: {
|
|
|
|
const auto &d = result.c_updates_channelDifferenceTooLong();
|
|
|
|
|
2019-01-18 12:27:37 +00:00
|
|
|
_session->data().processUsers(d.vusers);
|
|
|
|
_session->data().processChats(d.vchats);
|
2018-01-31 17:10:29 +00:00
|
|
|
|
|
|
|
nextRequestPts = d.vpts.v;
|
|
|
|
isFinal = d.is_final();
|
|
|
|
} break;
|
|
|
|
|
|
|
|
case mtpc_updates_channelDifference: {
|
|
|
|
const auto &d = result.c_updates_channelDifference();
|
|
|
|
|
|
|
|
App::main()->feedChannelDifference(d);
|
|
|
|
|
|
|
|
nextRequestPts = d.vpts.v;
|
|
|
|
isFinal = d.is_final();
|
|
|
|
} break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!isFinal) {
|
|
|
|
MTP_LOG(0, ("getChannelDifference { "
|
|
|
|
"good - after not final channelDifference was received, "
|
|
|
|
"validating history part }%1"
|
|
|
|
).arg(cTestMode() ? " TESTMODE" : ""));
|
|
|
|
channelRangeDifferenceSend(channel, range, nextRequestPts);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-15 16:36:19 +00:00
|
|
|
template <typename Request>
|
|
|
|
void ApiWrap::requestFileReference(
|
|
|
|
Data::FileOrigin origin,
|
2018-07-16 17:31:07 +00:00
|
|
|
FileReferencesHandler &&handler,
|
2018-07-15 16:36:19 +00:00
|
|
|
Request &&data) {
|
|
|
|
const auto i = _fileReferenceHandlers.find(origin);
|
|
|
|
if (i != end(_fileReferenceHandlers)) {
|
|
|
|
i->second.push_back(std::move(handler));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
auto handlers = std::vector<FileReferencesHandler>();
|
|
|
|
handlers.push_back(std::move(handler));
|
|
|
|
_fileReferenceHandlers.emplace(origin, std::move(handlers));
|
|
|
|
|
|
|
|
request(std::move(data)).done([=](const auto &result) {
|
|
|
|
const auto parsed = Data::GetFileReferences(result);
|
2019-01-04 11:09:48 +00:00
|
|
|
for (const auto &p : parsed.data) {
|
2018-08-30 21:28:27 +00:00
|
|
|
// Unpack here the parsed pair by hand to workaround a GCC bug.
|
|
|
|
// See https://gcc.gnu.org/bugzilla/show_bug.cgi?id=87122
|
|
|
|
const auto &origin = p.first;
|
|
|
|
const auto &reference = p.second;
|
2018-07-16 17:31:07 +00:00
|
|
|
const auto documentId = base::get_if<DocumentFileLocationId>(
|
|
|
|
&origin);
|
|
|
|
if (documentId) {
|
|
|
|
_session->data().document(
|
|
|
|
*documentId
|
|
|
|
)->refreshFileReference(reference);
|
|
|
|
}
|
|
|
|
}
|
2018-07-15 16:36:19 +00:00
|
|
|
const auto i = _fileReferenceHandlers.find(origin);
|
|
|
|
Assert(i != end(_fileReferenceHandlers));
|
|
|
|
auto handlers = std::move(i->second);
|
|
|
|
_fileReferenceHandlers.erase(i);
|
|
|
|
for (auto &handler : handlers) {
|
|
|
|
handler(parsed);
|
|
|
|
}
|
|
|
|
}).fail([=](const RPCError &error) {
|
|
|
|
const auto i = _fileReferenceHandlers.find(origin);
|
|
|
|
Assert(i != end(_fileReferenceHandlers));
|
|
|
|
auto handlers = std::move(i->second);
|
|
|
|
_fileReferenceHandlers.erase(i);
|
|
|
|
for (auto &handler : handlers) {
|
|
|
|
handler(Data::UpdatedFileReferences());
|
|
|
|
}
|
|
|
|
}).send();
|
|
|
|
}
|
|
|
|
|
|
|
|
void ApiWrap::refreshFileReference(
|
|
|
|
Data::FileOrigin origin,
|
|
|
|
not_null<mtpFileLoader*> loader,
|
|
|
|
int requestId,
|
|
|
|
const QByteArray ¤t) {
|
2018-07-16 17:31:07 +00:00
|
|
|
return refreshFileReference(origin, crl::guard(loader, [=](
|
|
|
|
const Data::UpdatedFileReferences &data) {
|
|
|
|
loader->refreshFileReferenceFrom(data, requestId, current);
|
|
|
|
}));
|
|
|
|
}
|
|
|
|
|
|
|
|
void ApiWrap::refreshFileReference(
|
|
|
|
Data::FileOrigin origin,
|
|
|
|
FileReferencesHandler &&handler) {
|
|
|
|
const auto request = [&](
|
|
|
|
auto &&data,
|
|
|
|
Fn<void()> &&additional = nullptr) {
|
2018-07-15 16:36:19 +00:00
|
|
|
requestFileReference(
|
|
|
|
origin,
|
2018-07-16 17:31:07 +00:00
|
|
|
std::move(handler),
|
2018-07-15 16:36:19 +00:00
|
|
|
std::move(data));
|
2018-07-16 17:31:07 +00:00
|
|
|
if (additional) {
|
|
|
|
const auto i = _fileReferenceHandlers.find(origin);
|
|
|
|
Assert(i != end(_fileReferenceHandlers));
|
2018-09-26 18:08:11 +00:00
|
|
|
if (i->second.size() == 1) {
|
|
|
|
i->second.push_back([=](auto&&) {
|
|
|
|
additional();
|
|
|
|
});
|
|
|
|
}
|
2018-07-16 17:31:07 +00:00
|
|
|
}
|
2018-07-15 16:36:19 +00:00
|
|
|
};
|
|
|
|
const auto fail = [&] {
|
2018-07-16 17:31:07 +00:00
|
|
|
handler(Data::UpdatedFileReferences());
|
2018-07-15 16:36:19 +00:00
|
|
|
};
|
2019-01-04 11:09:48 +00:00
|
|
|
origin.data.match([&](Data::FileOriginMessage data) {
|
2018-07-15 16:36:19 +00:00
|
|
|
if (const auto item = App::histItemById(data)) {
|
|
|
|
if (const auto channel = item->history()->peer->asChannel()) {
|
|
|
|
request(MTPchannels_GetMessages(
|
|
|
|
channel->inputChannel,
|
|
|
|
MTP_vector<MTPInputMessage>(
|
|
|
|
1,
|
|
|
|
MTP_inputMessageID(MTP_int(item->id)))));
|
|
|
|
} else {
|
|
|
|
request(MTPmessages_GetMessages(
|
|
|
|
MTP_vector<MTPInputMessage>(
|
|
|
|
1,
|
|
|
|
MTP_inputMessageID(MTP_int(item->id)))));
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
fail();
|
|
|
|
}
|
|
|
|
}, [&](Data::FileOriginUserPhoto data) {
|
2019-01-18 12:27:37 +00:00
|
|
|
if (const auto user = _session->data().user(data.userId)) {
|
2018-07-15 16:36:19 +00:00
|
|
|
request(MTPphotos_GetUserPhotos(
|
|
|
|
user->inputUser,
|
|
|
|
MTP_int(-1),
|
|
|
|
MTP_long(data.photoId),
|
|
|
|
MTP_int(1)));
|
|
|
|
} else {
|
|
|
|
fail();
|
|
|
|
}
|
|
|
|
}, [&](Data::FileOriginPeerPhoto data) {
|
2019-01-18 12:27:37 +00:00
|
|
|
if (const auto peer = _session->data().peer(data.peerId)) {
|
2018-07-15 16:36:19 +00:00
|
|
|
if (const auto user = peer->asUser()) {
|
|
|
|
request(MTPusers_GetUsers(
|
|
|
|
MTP_vector<MTPInputUser>(1, user->inputUser)));
|
|
|
|
} else if (const auto chat = peer->asChat()) {
|
|
|
|
request(MTPmessages_GetChats(
|
|
|
|
MTP_vector<MTPint>(1, chat->inputChat)));
|
|
|
|
} else if (const auto channel = peer->asChannel()) {
|
|
|
|
request(MTPchannels_GetChannels(
|
|
|
|
MTP_vector<MTPInputChannel>(1, channel->inputChannel)));
|
|
|
|
} else {
|
|
|
|
fail();
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
fail();
|
|
|
|
}
|
|
|
|
}, [&](Data::FileOriginStickerSet data) {
|
|
|
|
if (data.setId == Stickers::CloudRecentSetId
|
|
|
|
|| data.setId == Stickers::RecentSetId) {
|
|
|
|
request(MTPmessages_GetRecentStickers(
|
|
|
|
MTP_flags(0),
|
2018-07-16 17:31:07 +00:00
|
|
|
MTP_int(0)),
|
|
|
|
[] { crl::on_main([] { Local::writeRecentStickers(); }); });
|
2018-07-15 16:36:19 +00:00
|
|
|
} else if (data.setId == Stickers::FavedSetId) {
|
2018-07-16 17:31:07 +00:00
|
|
|
request(MTPmessages_GetFavedStickers(MTP_int(0)),
|
|
|
|
[] { crl::on_main([] { Local::writeFavedStickers(); }); });
|
2018-07-15 16:36:19 +00:00
|
|
|
} else {
|
|
|
|
request(MTPmessages_GetStickerSet(
|
|
|
|
MTP_inputStickerSetID(
|
|
|
|
MTP_long(data.setId),
|
2018-07-16 17:31:07 +00:00
|
|
|
MTP_long(data.accessHash))),
|
|
|
|
[] { crl::on_main([] {
|
|
|
|
Local::writeInstalledStickers();
|
|
|
|
Local::writeRecentStickers();
|
|
|
|
Local::writeFavedStickers();
|
|
|
|
}); });
|
2018-07-15 16:36:19 +00:00
|
|
|
}
|
|
|
|
}, [&](Data::FileOriginSavedGifs data) {
|
2018-07-16 19:41:43 +00:00
|
|
|
request(
|
|
|
|
MTPmessages_GetSavedGifs(MTP_int(0)),
|
|
|
|
[] { crl::on_main([] { Local::writeSavedGifs(); }); });
|
2019-01-17 08:18:23 +00:00
|
|
|
}, [&](Data::FileOriginWallpaper data) {
|
|
|
|
request(MTPaccount_GetWallPaper(
|
|
|
|
MTP_inputWallPaper(
|
|
|
|
MTP_long(data.paperId),
|
|
|
|
MTP_long(data.accessHash))));
|
2018-09-21 16:28:46 +00:00
|
|
|
}, [&](std::nullopt_t) {
|
2018-07-15 16:36:19 +00:00
|
|
|
fail();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
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-09-27 13:37:18 +00:00
|
|
|
auto &d = msgs.c_messages_messages();
|
2019-01-18 12:27:37 +00:00
|
|
|
_session->data().processUsers(d.vusers);
|
|
|
|
_session->data().processChats(d.vchats);
|
2017-03-10 19:46:28 +00:00
|
|
|
v = &d.vmessages.v;
|
2015-09-04 13:01:31 +00:00
|
|
|
} break;
|
|
|
|
|
|
|
|
case mtpc_messages_messagesSlice: {
|
2016-09-27 13:37:18 +00:00
|
|
|
auto &d = msgs.c_messages_messagesSlice();
|
2019-01-18 12:27:37 +00:00
|
|
|
_session->data().processUsers(d.vusers);
|
|
|
|
_session->data().processChats(d.vchats);
|
2017-03-10 19:46:28 +00:00
|
|
|
v = &d.vmessages.v;
|
2015-09-04 13:01:31 +00:00
|
|
|
} break;
|
|
|
|
|
|
|
|
case mtpc_messages_channelMessages: {
|
2016-09-27 13:37:18 +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)"));
|
|
|
|
}
|
2019-01-18 12:27:37 +00:00
|
|
|
_session->data().processUsers(d.vusers);
|
|
|
|
_session->data().processChats(d.vchats);
|
2017-03-10 19:46:28 +00:00
|
|
|
v = &d.vmessages.v;
|
2015-09-04 13:01:31 +00:00
|
|
|
} break;
|
2017-11-20 19:54:05 +00:00
|
|
|
|
|
|
|
case mtpc_messages_messagesNotModified: {
|
|
|
|
LOG(("API Error: received messages.messagesNotModified! (ApiWrap::gotWebPages)"));
|
|
|
|
} break;
|
2015-04-04 20:01:34 +00:00
|
|
|
}
|
|
|
|
|
2015-09-04 13:01:31 +00:00
|
|
|
if (!v) return;
|
2017-12-13 18:10:48 +00:00
|
|
|
|
|
|
|
auto indices = base::flat_map<uint64, int>(); // copied from feedMsgs
|
|
|
|
for (auto i = 0, l = v->size(); i != l; ++i) {
|
2018-12-17 08:16:06 +00:00
|
|
|
const auto msgId = IdFromMessage(v->at(i));
|
2017-12-13 18:10:48 +00:00
|
|
|
indices.emplace((uint64(uint32(msgId)) << 32) | uint64(i), i);
|
2015-04-04 20:01:34 +00:00
|
|
|
}
|
|
|
|
|
2017-12-13 18:10:48 +00:00
|
|
|
for (const auto [position, index] : indices) {
|
2019-01-03 12:36:01 +00:00
|
|
|
const auto item = _session->data().addNewMessage(
|
2017-12-13 18:10:48 +00:00
|
|
|
v->at(index),
|
|
|
|
NewMessageExisting);
|
|
|
|
if (item) {
|
2018-01-18 11:46:45 +00:00
|
|
|
_session->data().requestItemResize(item);
|
2015-04-04 20:01:34 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-09-27 13:37:18 +00:00
|
|
|
for (auto 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;
|
2018-01-17 16:21:01 +00:00
|
|
|
_session->data().notifyWebPageUpdateDelayed(i.key());
|
2015-04-04 20:01:34 +00:00
|
|
|
}
|
|
|
|
i = _webPagesPending.erase(i);
|
|
|
|
} else {
|
|
|
|
++i;
|
|
|
|
}
|
|
|
|
}
|
2018-12-18 05:43:11 +00:00
|
|
|
_session->data().sendWebPageGamePollNotifications();
|
2015-04-04 20:01:34 +00:00
|
|
|
}
|
|
|
|
|
2016-11-22 09:48:13 +00:00
|
|
|
void ApiWrap::stickersSaveOrder() {
|
|
|
|
if (_stickersOrder.size() > 1) {
|
|
|
|
QVector<MTPlong> mtpOrder;
|
|
|
|
mtpOrder.reserve(_stickersOrder.size());
|
|
|
|
for_const (auto setId, _stickersOrder) {
|
|
|
|
mtpOrder.push_back(MTP_long(setId));
|
|
|
|
}
|
|
|
|
|
2017-04-06 19:02:40 +00:00
|
|
|
_stickersReorderRequestId = request(MTPmessages_ReorderStickerSets(MTP_flags(0), MTP_vector<MTPlong>(mtpOrder))).done([this](const MTPBool &result) {
|
|
|
|
_stickersReorderRequestId = 0;
|
|
|
|
}).fail([this](const RPCError &error) {
|
|
|
|
_stickersReorderRequestId = 0;
|
2018-01-03 17:06:43 +00:00
|
|
|
_session->data().setLastStickersUpdate(0);
|
2017-08-02 15:07:28 +00:00
|
|
|
updateStickers();
|
2017-04-06 19:02:40 +00:00
|
|
|
}).send();
|
2016-11-22 09:48:13 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-08-02 15:07:28 +00:00
|
|
|
void ApiWrap::updateStickers() {
|
2019-02-19 06:57:53 +00:00
|
|
|
auto now = crl::now();
|
2017-08-02 20:57:49 +00:00
|
|
|
requestStickers(now);
|
|
|
|
requestRecentStickers(now);
|
|
|
|
requestFavedStickers(now);
|
|
|
|
requestFeaturedStickers(now);
|
|
|
|
requestSavedGifs(now);
|
2017-08-02 15:07:28 +00:00
|
|
|
}
|
|
|
|
|
2018-03-07 21:25:03 +00:00
|
|
|
void ApiWrap::requestRecentStickersForce() {
|
|
|
|
requestRecentStickersWithHash(0);
|
|
|
|
}
|
|
|
|
|
2017-08-17 08:31:24 +00:00
|
|
|
void ApiWrap::setGroupStickerSet(not_null<ChannelData*> megagroup, const MTPInputStickerSet &set) {
|
2017-08-05 10:48:21 +00:00
|
|
|
Expects(megagroup->mgInfo != nullptr);
|
2018-03-07 13:53:12 +00:00
|
|
|
|
2017-08-05 10:48:21 +00:00
|
|
|
megagroup->mgInfo->stickerSet = set;
|
|
|
|
request(MTPchannels_SetStickers(megagroup->inputChannel, set)).send();
|
2018-01-17 18:20:55 +00:00
|
|
|
_session->data().notifyStickersUpdated();
|
2017-08-05 10:48:21 +00:00
|
|
|
}
|
|
|
|
|
2018-03-07 13:53:12 +00:00
|
|
|
std::vector<not_null<DocumentData*>> *ApiWrap::stickersByEmoji(
|
|
|
|
not_null<EmojiPtr> emoji) {
|
|
|
|
const auto it = _stickersByEmoji.find(emoji);
|
|
|
|
const auto sendRequest = [&] {
|
|
|
|
if (it == _stickersByEmoji.end()) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
const auto received = it->second.received;
|
2019-02-19 06:57:53 +00:00
|
|
|
const auto now = crl::now();
|
2018-03-07 13:53:12 +00:00
|
|
|
return (received > 0)
|
|
|
|
&& (received + kStickersByEmojiInvalidateTimeout) <= now;
|
|
|
|
}();
|
|
|
|
if (sendRequest) {
|
|
|
|
const auto hash = (it != _stickersByEmoji.end())
|
|
|
|
? it->second.hash
|
2018-04-09 17:48:29 +00:00
|
|
|
: int32(0);
|
2018-03-07 13:53:12 +00:00
|
|
|
request(MTPmessages_GetStickers(
|
|
|
|
MTP_string(emoji->text()),
|
2018-04-09 17:48:29 +00:00
|
|
|
MTP_int(hash)
|
2018-03-07 13:53:12 +00:00
|
|
|
)).done([=](const MTPmessages_Stickers &result) {
|
|
|
|
if (result.type() == mtpc_messages_stickersNotModified) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
Assert(result.type() == mtpc_messages_stickers);
|
|
|
|
const auto &data = result.c_messages_stickers();
|
|
|
|
auto &entry = _stickersByEmoji[emoji];
|
|
|
|
entry.list.clear();
|
|
|
|
entry.list.reserve(data.vstickers.v.size());
|
2018-07-11 19:46:48 +00:00
|
|
|
for (const auto &sticker : data.vstickers.v) {
|
2019-01-18 12:27:37 +00:00
|
|
|
const auto document = _session->data().processDocument(
|
|
|
|
sticker);
|
2018-07-11 19:46:48 +00:00
|
|
|
if (document->sticker()) {
|
|
|
|
entry.list.push_back(document);
|
|
|
|
}
|
|
|
|
}
|
2018-04-09 17:48:29 +00:00
|
|
|
entry.hash = data.vhash.v;
|
2019-02-19 06:57:53 +00:00
|
|
|
entry.received = crl::now();
|
2018-03-07 13:53:12 +00:00
|
|
|
_session->data().notifyStickersUpdated();
|
|
|
|
}).send();
|
|
|
|
}
|
|
|
|
if (it == _stickersByEmoji.end()) {
|
|
|
|
_stickersByEmoji.emplace(emoji, StickersByEmoji());
|
|
|
|
} else if (it->second.received > 0) {
|
|
|
|
return &it->second.list;
|
|
|
|
}
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2018-07-16 19:41:43 +00:00
|
|
|
void ApiWrap::toggleFavedSticker(
|
|
|
|
not_null<DocumentData*> document,
|
|
|
|
Data::FileOrigin origin,
|
|
|
|
bool faved) {
|
|
|
|
if (faved && !document->sticker()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
auto failHandler = std::make_shared<Fn<void(const RPCError&)>>();
|
|
|
|
auto performRequest = [=] {
|
|
|
|
request(MTPmessages_FaveSticker(
|
|
|
|
document->mtpInput(),
|
|
|
|
MTP_bool(!faved)
|
|
|
|
)).done([=](const MTPBool &result) {
|
|
|
|
if (mtpIsTrue(result)) {
|
|
|
|
Stickers::SetFaved(document, faved);
|
|
|
|
}
|
|
|
|
}).fail(
|
|
|
|
base::duplicate(*failHandler)
|
|
|
|
).send();
|
|
|
|
};
|
|
|
|
*failHandler = [=](const RPCError &error) {
|
|
|
|
if (error.code() == 400
|
|
|
|
&& error.type().startsWith(qstr("FILE_REFERENCE_"))) {
|
|
|
|
const auto current = document->fileReference();
|
|
|
|
auto refreshed = [=](const Data::UpdatedFileReferences &data) {
|
|
|
|
if (document->fileReference() != current) {
|
|
|
|
performRequest();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
refreshFileReference(origin, std::move(refreshed));
|
|
|
|
}
|
|
|
|
};
|
|
|
|
performRequest();
|
|
|
|
}
|
|
|
|
|
|
|
|
void ApiWrap::toggleSavedGif(
|
|
|
|
not_null<DocumentData*> document,
|
|
|
|
Data::FileOrigin origin,
|
|
|
|
bool saved) {
|
|
|
|
if (saved && !document->isGifv()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
auto failHandler = std::make_shared<Fn<void(const RPCError&)>>();
|
|
|
|
auto performRequest = [=] {
|
|
|
|
request(MTPmessages_SaveGif(
|
|
|
|
document->mtpInput(),
|
|
|
|
MTP_bool(!saved)
|
|
|
|
)).done([=](const MTPBool &result) {
|
|
|
|
if (mtpIsTrue(result)) {
|
|
|
|
if (saved) {
|
|
|
|
App::addSavedGif(document);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}).fail(
|
|
|
|
base::duplicate(*failHandler)
|
|
|
|
).send();
|
|
|
|
};
|
|
|
|
*failHandler = [=](const RPCError &error) {
|
|
|
|
if (error.code() == 400
|
|
|
|
&& error.type().startsWith(qstr("FILE_REFERENCE_"))) {
|
|
|
|
const auto current = document->fileReference();
|
|
|
|
auto refreshed = [=](const Data::UpdatedFileReferences &data) {
|
|
|
|
if (document->fileReference() != current) {
|
|
|
|
performRequest();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
refreshFileReference(origin, std::move(refreshed));
|
|
|
|
}
|
|
|
|
};
|
|
|
|
performRequest();
|
|
|
|
}
|
|
|
|
|
2017-08-02 20:57:49 +00:00
|
|
|
void ApiWrap::requestStickers(TimeId now) {
|
2018-01-03 17:06:43 +00:00
|
|
|
if (!_session->data().stickersUpdateNeeded(now)
|
2017-11-05 17:07:27 +00:00
|
|
|
|| _stickersUpdateRequest) {
|
2017-08-02 20:57:49 +00:00
|
|
|
return;
|
2017-08-02 15:07:28 +00:00
|
|
|
}
|
2017-08-02 20:57:49 +00:00
|
|
|
auto onDone = [this](const MTPmessages_AllStickers &result) {
|
2019-02-19 06:57:53 +00:00
|
|
|
_session->data().setLastStickersUpdate(crl::now());
|
2017-08-02 20:57:49 +00:00
|
|
|
_stickersUpdateRequest = 0;
|
2017-08-02 15:07:28 +00:00
|
|
|
|
2017-08-02 20:57:49 +00:00
|
|
|
switch (result.type()) {
|
|
|
|
case mtpc_messages_allStickersNotModified: return;
|
|
|
|
case mtpc_messages_allStickers: {
|
|
|
|
auto &d = result.c_messages_allStickers();
|
|
|
|
Stickers::SetsReceived(d.vsets.v, d.vhash.v);
|
|
|
|
} return;
|
|
|
|
default: Unexpected("Type in ApiWrap::stickersDone()");
|
2017-08-02 15:07:28 +00:00
|
|
|
}
|
2017-08-02 20:57:49 +00:00
|
|
|
};
|
2018-01-29 17:13:24 +00:00
|
|
|
_stickersUpdateRequest = request(MTPmessages_GetAllStickers(
|
|
|
|
MTP_int(Local::countStickersHash(true))
|
2018-04-07 08:47:08 +00:00
|
|
|
)).done(onDone).fail([=](const RPCError &error) {
|
2017-08-02 20:57:49 +00:00
|
|
|
LOG(("App Fail: Failed to get stickers!"));
|
|
|
|
onDone(MTP_messages_allStickersNotModified());
|
|
|
|
}).send();
|
2017-08-02 15:07:28 +00:00
|
|
|
}
|
|
|
|
|
2017-08-02 20:57:49 +00:00
|
|
|
void ApiWrap::requestRecentStickers(TimeId now) {
|
2018-03-07 21:25:03 +00:00
|
|
|
if (!_session->data().recentStickersUpdateNeeded(now)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
requestRecentStickersWithHash(Local::countRecentStickersHash());
|
|
|
|
}
|
|
|
|
|
|
|
|
void ApiWrap::requestRecentStickersWithHash(int32 hash) {
|
|
|
|
if (_recentStickersUpdateRequest) {
|
2017-08-02 20:57:49 +00:00
|
|
|
return;
|
2017-08-02 15:07:28 +00:00
|
|
|
}
|
2018-01-25 12:28:48 +00:00
|
|
|
_recentStickersUpdateRequest = request(MTPmessages_GetRecentStickers(
|
|
|
|
MTP_flags(0),
|
2018-03-07 21:25:03 +00:00
|
|
|
MTP_int(hash)
|
2018-01-25 12:28:48 +00:00
|
|
|
)).done([=](const MTPmessages_RecentStickers &result) {
|
2019-02-19 06:57:53 +00:00
|
|
|
_session->data().setLastRecentStickersUpdate(crl::now());
|
2017-08-02 20:57:49 +00:00
|
|
|
_recentStickersUpdateRequest = 0;
|
2017-08-02 15:07:28 +00:00
|
|
|
|
2017-08-02 20:57:49 +00:00
|
|
|
switch (result.type()) {
|
|
|
|
case mtpc_messages_recentStickersNotModified: return;
|
|
|
|
case mtpc_messages_recentStickers: {
|
|
|
|
auto &d = result.c_messages_recentStickers();
|
2018-01-25 12:28:48 +00:00
|
|
|
Stickers::SpecialSetReceived(
|
|
|
|
Stickers::CloudRecentSetId,
|
|
|
|
lang(lng_recent_stickers),
|
|
|
|
d.vstickers.v,
|
|
|
|
d.vhash.v,
|
2018-03-07 17:43:26 +00:00
|
|
|
d.vpacks.v,
|
|
|
|
d.vdates.v);
|
2017-08-02 20:57:49 +00:00
|
|
|
} return;
|
|
|
|
default: Unexpected("Type in ApiWrap::recentStickersDone()");
|
2017-08-02 15:07:28 +00:00
|
|
|
}
|
2018-01-25 12:28:48 +00:00
|
|
|
}).fail([=](const RPCError &error) {
|
2019-02-19 06:57:53 +00:00
|
|
|
_session->data().setLastRecentStickersUpdate(crl::now());
|
2018-01-25 12:28:48 +00:00
|
|
|
_recentStickersUpdateRequest = 0;
|
|
|
|
|
2017-08-02 20:57:49 +00:00
|
|
|
LOG(("App Fail: Failed to get recent stickers!"));
|
|
|
|
}).send();
|
2017-08-02 15:07:28 +00:00
|
|
|
}
|
|
|
|
|
2017-08-02 20:57:49 +00:00
|
|
|
void ApiWrap::requestFavedStickers(TimeId now) {
|
2018-01-03 17:06:43 +00:00
|
|
|
if (!_session->data().favedStickersUpdateNeeded(now)
|
2017-11-05 17:07:27 +00:00
|
|
|
|| _favedStickersUpdateRequest) {
|
2017-08-02 20:57:49 +00:00
|
|
|
return;
|
2017-08-02 15:07:28 +00:00
|
|
|
}
|
2018-01-25 12:28:48 +00:00
|
|
|
_favedStickersUpdateRequest = request(MTPmessages_GetFavedStickers(
|
|
|
|
MTP_int(Local::countFavedStickersHash())
|
|
|
|
)).done([=](const MTPmessages_FavedStickers &result) {
|
2019-02-19 06:57:53 +00:00
|
|
|
_session->data().setLastFavedStickersUpdate(crl::now());
|
2017-08-02 20:57:49 +00:00
|
|
|
_favedStickersUpdateRequest = 0;
|
2017-08-02 15:07:28 +00:00
|
|
|
|
2017-08-02 20:57:49 +00:00
|
|
|
switch (result.type()) {
|
|
|
|
case mtpc_messages_favedStickersNotModified: return;
|
|
|
|
case mtpc_messages_favedStickers: {
|
|
|
|
auto &d = result.c_messages_favedStickers();
|
2018-01-25 12:28:48 +00:00
|
|
|
Stickers::SpecialSetReceived(
|
|
|
|
Stickers::FavedSetId,
|
|
|
|
Lang::Hard::FavedSetTitle(),
|
|
|
|
d.vstickers.v,
|
|
|
|
d.vhash.v,
|
|
|
|
d.vpacks.v);
|
2017-08-02 20:57:49 +00:00
|
|
|
} return;
|
|
|
|
default: Unexpected("Type in ApiWrap::favedStickersDone()");
|
2017-08-02 15:07:28 +00:00
|
|
|
}
|
2018-01-25 12:28:48 +00:00
|
|
|
}).fail([=](const RPCError &error) {
|
2019-02-19 06:57:53 +00:00
|
|
|
_session->data().setLastFavedStickersUpdate(crl::now());
|
2018-01-25 12:28:48 +00:00
|
|
|
_favedStickersUpdateRequest = 0;
|
|
|
|
|
2017-08-02 20:57:49 +00:00
|
|
|
LOG(("App Fail: Failed to get faved stickers!"));
|
|
|
|
}).send();
|
|
|
|
}
|
2017-08-02 15:07:28 +00:00
|
|
|
|
2017-08-02 20:57:49 +00:00
|
|
|
void ApiWrap::requestFeaturedStickers(TimeId now) {
|
2018-01-03 17:06:43 +00:00
|
|
|
if (!_session->data().featuredStickersUpdateNeeded(now)
|
2017-11-05 17:07:27 +00:00
|
|
|
|| _featuredStickersUpdateRequest) {
|
2017-08-02 20:57:49 +00:00
|
|
|
return;
|
2017-08-02 15:07:28 +00:00
|
|
|
}
|
2018-01-25 12:28:48 +00:00
|
|
|
_featuredStickersUpdateRequest = request(MTPmessages_GetFeaturedStickers(
|
|
|
|
MTP_int(Local::countFeaturedStickersHash())
|
|
|
|
)).done([=](const MTPmessages_FeaturedStickers &result) {
|
2019-02-19 06:57:53 +00:00
|
|
|
_session->data().setLastFeaturedStickersUpdate(crl::now());
|
2017-08-02 20:57:49 +00:00
|
|
|
_featuredStickersUpdateRequest = 0;
|
2017-08-02 15:07:28 +00:00
|
|
|
|
2017-08-02 20:57:49 +00:00
|
|
|
switch (result.type()) {
|
|
|
|
case mtpc_messages_featuredStickersNotModified: return;
|
|
|
|
case mtpc_messages_featuredStickers: {
|
|
|
|
auto &d = result.c_messages_featuredStickers();
|
|
|
|
Stickers::FeaturedSetsReceived(d.vsets.v, d.vunread.v, d.vhash.v);
|
|
|
|
} return;
|
|
|
|
default: Unexpected("Type in ApiWrap::featuredStickersDone()");
|
2017-08-02 15:07:28 +00:00
|
|
|
}
|
2018-01-25 12:28:48 +00:00
|
|
|
}).fail([=](const RPCError &error) {
|
2019-02-19 06:57:53 +00:00
|
|
|
_session->data().setLastFeaturedStickersUpdate(crl::now());
|
2018-01-25 12:28:48 +00:00
|
|
|
_featuredStickersUpdateRequest = 0;
|
|
|
|
|
2017-08-02 20:57:49 +00:00
|
|
|
LOG(("App Fail: Failed to get featured stickers!"));
|
|
|
|
}).send();
|
|
|
|
}
|
2017-08-02 15:07:28 +00:00
|
|
|
|
2017-08-02 20:57:49 +00:00
|
|
|
void ApiWrap::requestSavedGifs(TimeId now) {
|
2018-01-03 17:06:43 +00:00
|
|
|
if (!_session->data().savedGifsUpdateNeeded(now)
|
2017-11-05 17:07:27 +00:00
|
|
|
|| _savedGifsUpdateRequest) {
|
2017-08-02 20:57:49 +00:00
|
|
|
return;
|
2017-08-02 15:07:28 +00:00
|
|
|
}
|
2018-01-25 12:28:48 +00:00
|
|
|
_savedGifsUpdateRequest = request(MTPmessages_GetSavedGifs(
|
|
|
|
MTP_int(Local::countSavedGifsHash())
|
|
|
|
)).done([=](const MTPmessages_SavedGifs &result) {
|
2019-02-19 06:57:53 +00:00
|
|
|
_session->data().setLastSavedGifsUpdate(crl::now());
|
2017-08-02 20:57:49 +00:00
|
|
|
_savedGifsUpdateRequest = 0;
|
2017-08-02 15:07:28 +00:00
|
|
|
|
2017-08-02 20:57:49 +00:00
|
|
|
switch (result.type()) {
|
|
|
|
case mtpc_messages_savedGifsNotModified: return;
|
|
|
|
case mtpc_messages_savedGifs: {
|
|
|
|
auto &d = result.c_messages_savedGifs();
|
|
|
|
Stickers::GifsReceived(d.vgifs.v, d.vhash.v);
|
|
|
|
} return;
|
|
|
|
default: Unexpected("Type in ApiWrap::savedGifsDone()");
|
|
|
|
}
|
2018-01-25 12:28:48 +00:00
|
|
|
}).fail([=](const RPCError &error) {
|
2019-02-19 06:57:53 +00:00
|
|
|
_session->data().setLastSavedGifsUpdate(crl::now());
|
2018-01-25 12:28:48 +00:00
|
|
|
_savedGifsUpdateRequest = 0;
|
|
|
|
|
2017-08-02 20:57:49 +00:00
|
|
|
LOG(("App Fail: Failed to get saved gifs!"));
|
|
|
|
}).send();
|
2017-08-02 15:07:28 +00:00
|
|
|
}
|
|
|
|
|
2017-11-05 17:07:27 +00:00
|
|
|
void ApiWrap::readFeaturedSetDelayed(uint64 setId) {
|
|
|
|
if (!_featuredSetsRead.contains(setId)) {
|
|
|
|
_featuredSetsRead.insert(setId);
|
|
|
|
_featuredSetsReadTimer.callOnce(kReadFeaturedSetsTimeout);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void ApiWrap::readFeaturedSets() {
|
2018-01-03 17:06:43 +00:00
|
|
|
auto &sets = _session->data().stickerSetsRef();
|
|
|
|
auto count = _session->data().featuredStickerSetsUnreadCount();
|
2017-11-05 17:07:27 +00:00
|
|
|
QVector<MTPlong> wrappedIds;
|
|
|
|
wrappedIds.reserve(_featuredSetsRead.size());
|
|
|
|
for (auto setId : _featuredSetsRead) {
|
|
|
|
auto it = sets.find(setId);
|
|
|
|
if (it != sets.cend()) {
|
|
|
|
it->flags &= ~MTPDstickerSet_ClientFlag::f_unread;
|
|
|
|
wrappedIds.append(MTP_long(setId));
|
|
|
|
if (count) {
|
|
|
|
--count;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
_featuredSetsRead.clear();
|
|
|
|
|
|
|
|
if (!wrappedIds.empty()) {
|
|
|
|
auto requestData = MTPmessages_ReadFeaturedStickers(
|
|
|
|
MTP_vector<MTPlong>(wrappedIds));
|
2018-01-03 17:06:43 +00:00
|
|
|
request(std::move(requestData)).done([=](const MTPBool &result) {
|
2017-11-05 17:07:27 +00:00
|
|
|
Local::writeFeaturedStickers();
|
2018-01-17 18:20:55 +00:00
|
|
|
_session->data().notifyStickersUpdated();
|
2017-11-05 17:07:27 +00:00
|
|
|
}).send();
|
|
|
|
|
2018-01-03 17:06:43 +00:00
|
|
|
_session->data().setFeaturedStickerSetsUnreadCount(count);
|
2017-11-05 17:07:27 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-11-20 19:54:05 +00:00
|
|
|
void ApiWrap::parseChannelParticipants(
|
2017-12-01 18:38:44 +00:00
|
|
|
not_null<ChannelData*> channel,
|
2017-11-20 19:54:05 +00:00
|
|
|
const MTPchannels_ChannelParticipants &result,
|
2018-06-04 15:35:11 +00:00
|
|
|
Fn<void(
|
2017-11-28 18:30:01 +00:00
|
|
|
int availableCount,
|
|
|
|
const QVector<MTPChannelParticipant> &list)> callbackList,
|
2018-06-04 15:35:11 +00:00
|
|
|
Fn<void()> callbackNotModified) {
|
2019-01-10 11:15:13 +00:00
|
|
|
result.match([&](const MTPDchannels_channelParticipants &data) {
|
2019-01-18 12:27:37 +00:00
|
|
|
_session->data().processUsers(data.vusers);
|
2017-12-01 18:38:44 +00:00
|
|
|
if (channel->mgInfo) {
|
|
|
|
refreshChannelAdmins(channel, data.vparticipants.v);
|
|
|
|
}
|
2017-11-20 19:54:05 +00:00
|
|
|
if (callbackList) {
|
|
|
|
callbackList(data.vcount.v, data.vparticipants.v);
|
|
|
|
}
|
2019-01-10 11:15:13 +00:00
|
|
|
}, [&](const MTPDchannels_channelParticipantsNotModified &) {
|
2017-11-20 19:54:05 +00:00
|
|
|
if (callbackNotModified) {
|
|
|
|
callbackNotModified();
|
|
|
|
} else {
|
2019-01-10 11:15:13 +00:00
|
|
|
LOG(("API Error: "
|
|
|
|
"channels.channelParticipantsNotModified received!"));
|
2017-11-20 19:54:05 +00:00
|
|
|
}
|
2019-01-10 11:15:13 +00:00
|
|
|
});
|
2017-12-01 18:38:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void ApiWrap::refreshChannelAdmins(
|
|
|
|
not_null<ChannelData*> channel,
|
|
|
|
const QVector<MTPChannelParticipant> &participants) {
|
2017-12-02 11:07:27 +00:00
|
|
|
Data::ChannelAdminChanges changes(channel);
|
2019-01-10 11:15:13 +00:00
|
|
|
for (const auto &p : participants) {
|
|
|
|
const auto userId = p.match([](const auto &data) {
|
|
|
|
return data.vuser_id.v;
|
|
|
|
});
|
2017-12-02 11:07:27 +00:00
|
|
|
const auto isAdmin = (p.type() == mtpc_channelParticipantAdmin)
|
|
|
|
|| (p.type() == mtpc_channelParticipantCreator);
|
|
|
|
changes.feed(userId, isAdmin);
|
2017-12-01 18:38:44 +00:00
|
|
|
}
|
|
|
|
}
|
2017-11-20 19:54:05 +00:00
|
|
|
|
2017-11-22 09:56:00 +00:00
|
|
|
void ApiWrap::parseRecentChannelParticipants(
|
|
|
|
not_null<ChannelData*> channel,
|
|
|
|
const MTPchannels_ChannelParticipants &result,
|
2018-06-04 15:35:11 +00:00
|
|
|
Fn<void(
|
2017-11-28 18:30:01 +00:00
|
|
|
int availableCount,
|
|
|
|
const QVector<MTPChannelParticipant> &list)> callbackList,
|
2018-06-04 15:35:11 +00:00
|
|
|
Fn<void()> callbackNotModified) {
|
2017-12-01 18:38:44 +00:00
|
|
|
parseChannelParticipants(channel, result, [&](
|
2017-11-28 18:30:01 +00:00
|
|
|
int availableCount,
|
2017-11-22 09:56:00 +00:00
|
|
|
const QVector<MTPChannelParticipant> &list) {
|
|
|
|
auto applyLast = channel->isMegagroup()
|
|
|
|
&& (channel->mgInfo->lastParticipants.size() <= list.size());
|
|
|
|
if (applyLast) {
|
|
|
|
applyLastParticipantsList(
|
|
|
|
channel,
|
2017-11-28 18:30:01 +00:00
|
|
|
availableCount,
|
2017-12-03 16:43:42 +00:00
|
|
|
list);
|
2017-11-22 09:56:00 +00:00
|
|
|
}
|
2017-11-28 18:30:01 +00:00
|
|
|
callbackList(availableCount, list);
|
2017-11-22 09:56:00 +00:00
|
|
|
}, std::move(callbackNotModified));
|
|
|
|
}
|
|
|
|
|
2017-07-14 11:54:47 +00:00
|
|
|
void ApiWrap::applyUpdatesNoPtsCheck(const MTPUpdates &updates) {
|
|
|
|
switch (updates.type()) {
|
|
|
|
case mtpc_updateShortMessage: {
|
|
|
|
auto &d = updates.c_updateShortMessage();
|
|
|
|
auto flags = mtpCastFlags(d.vflags.v) | MTPDmessage::Flag::f_from_id;
|
2018-01-03 17:06:43 +00:00
|
|
|
const auto peerUserId = d.is_out()
|
|
|
|
? d.vuser_id
|
|
|
|
: MTP_int(_session->userId());
|
2019-01-03 12:36:01 +00:00
|
|
|
_session->data().addNewMessage(
|
2017-12-21 12:11:33 +00:00
|
|
|
MTP_message(
|
|
|
|
MTP_flags(flags),
|
|
|
|
d.vid,
|
2018-01-03 17:06:43 +00:00
|
|
|
d.is_out() ? MTP_int(_session->userId()) : d.vuser_id,
|
|
|
|
MTP_peerUser(peerUserId),
|
2017-12-21 12:11:33 +00:00
|
|
|
d.vfwd_from,
|
|
|
|
d.vvia_bot_id,
|
|
|
|
d.vreply_to_msg_id,
|
|
|
|
d.vdate,
|
|
|
|
d.vmessage,
|
|
|
|
MTP_messageMediaEmpty(),
|
|
|
|
MTPnullMarkup,
|
|
|
|
d.has_entities() ? d.ventities : MTPnullEntities,
|
|
|
|
MTPint(),
|
|
|
|
MTPint(),
|
|
|
|
MTPstring(),
|
|
|
|
MTPlong()),
|
|
|
|
NewMessageUnread);
|
2017-07-14 11:54:47 +00:00
|
|
|
} break;
|
|
|
|
|
|
|
|
case mtpc_updateShortChatMessage: {
|
|
|
|
auto &d = updates.c_updateShortChatMessage();
|
|
|
|
auto flags = mtpCastFlags(d.vflags.v) | MTPDmessage::Flag::f_from_id;
|
2019-01-03 12:36:01 +00:00
|
|
|
_session->data().addNewMessage(
|
2017-12-21 12:11:33 +00:00
|
|
|
MTP_message(
|
|
|
|
MTP_flags(flags),
|
|
|
|
d.vid,
|
|
|
|
d.vfrom_id,
|
|
|
|
MTP_peerChat(d.vchat_id),
|
|
|
|
d.vfwd_from,
|
|
|
|
d.vvia_bot_id,
|
|
|
|
d.vreply_to_msg_id,
|
|
|
|
d.vdate,
|
|
|
|
d.vmessage,
|
|
|
|
MTP_messageMediaEmpty(),
|
|
|
|
MTPnullMarkup,
|
|
|
|
d.has_entities() ? d.ventities : MTPnullEntities,
|
|
|
|
MTPint(),
|
|
|
|
MTPint(),
|
|
|
|
MTPstring(),
|
|
|
|
MTPlong()),
|
|
|
|
NewMessageUnread);
|
2017-07-14 11:54:47 +00:00
|
|
|
} break;
|
|
|
|
|
|
|
|
case mtpc_updateShortSentMessage: {
|
|
|
|
auto &d = updates.c_updateShortSentMessage();
|
|
|
|
Q_UNUSED(d); // Sent message data was applied anyway.
|
|
|
|
} break;
|
|
|
|
|
|
|
|
default: Unexpected("Type in applyUpdatesNoPtsCheck()");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void ApiWrap::applyUpdateNoPtsCheck(const MTPUpdate &update) {
|
|
|
|
switch (update.type()) {
|
|
|
|
case mtpc_updateNewMessage: {
|
|
|
|
auto &d = update.c_updateNewMessage();
|
|
|
|
auto needToAdd = true;
|
|
|
|
if (d.vmessage.type() == mtpc_message) { // index forwarded messages to links _overview
|
|
|
|
if (App::checkEntitiesAndViewsUpdate(d.vmessage.c_message())) { // already in blocks
|
|
|
|
LOG(("Skipping message, because it is already in blocks!"));
|
|
|
|
needToAdd = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (needToAdd) {
|
2019-01-03 12:36:01 +00:00
|
|
|
_session->data().addNewMessage(d.vmessage, NewMessageUnread);
|
2017-07-14 11:54:47 +00:00
|
|
|
}
|
|
|
|
} break;
|
|
|
|
|
|
|
|
case mtpc_updateReadMessagesContents: {
|
|
|
|
auto &d = update.c_updateReadMessagesContents();
|
2017-08-11 07:16:07 +00:00
|
|
|
auto possiblyReadMentions = base::flat_set<MsgId>();
|
2018-01-17 16:21:01 +00:00
|
|
|
for (const auto &msgId : d.vmessages.v) {
|
2017-08-11 07:16:07 +00:00
|
|
|
if (auto item = App::histItemById(NoChannel, msgId.v)) {
|
2018-12-26 10:28:24 +00:00
|
|
|
if (item->isUnreadMedia() || item->isUnreadMention()) {
|
2017-07-14 11:54:47 +00:00
|
|
|
item->markMediaRead();
|
2018-01-18 11:46:45 +00:00
|
|
|
_session->data().requestItemRepaint(item);
|
2017-07-14 11:54:47 +00:00
|
|
|
|
|
|
|
if (item->out() && item->history()->peer->isUser()) {
|
|
|
|
auto when = App::main()->requestingDifference() ? 0 : unixtime();
|
|
|
|
item->history()->peer->asUser()->madeAction(when);
|
|
|
|
}
|
|
|
|
}
|
2017-08-11 07:16:07 +00:00
|
|
|
} else {
|
|
|
|
// Perhaps it was an unread mention!
|
|
|
|
possiblyReadMentions.insert(msgId.v);
|
2017-07-14 11:54:47 +00:00
|
|
|
}
|
|
|
|
}
|
2017-08-11 07:16:07 +00:00
|
|
|
checkForUnreadMentions(possiblyReadMentions);
|
2017-07-14 11:54:47 +00:00
|
|
|
} break;
|
|
|
|
|
|
|
|
case mtpc_updateReadHistoryInbox: {
|
|
|
|
auto &d = update.c_updateReadHistoryInbox();
|
|
|
|
App::feedInboxRead(peerFromMTP(d.vpeer), d.vmax_id.v);
|
|
|
|
} break;
|
|
|
|
|
|
|
|
case mtpc_updateReadHistoryOutbox: {
|
|
|
|
auto &d = update.c_updateReadHistoryOutbox();
|
|
|
|
auto peerId = peerFromMTP(d.vpeer);
|
|
|
|
auto when = App::main()->requestingDifference() ? 0 : unixtime();
|
|
|
|
App::feedOutboxRead(peerId, d.vmax_id.v, when);
|
|
|
|
} break;
|
|
|
|
|
|
|
|
case mtpc_updateWebPage: {
|
|
|
|
auto &d = update.c_updateWebPage();
|
|
|
|
Q_UNUSED(d); // Web page was updated anyway.
|
|
|
|
} break;
|
|
|
|
|
|
|
|
case mtpc_updateDeleteMessages: {
|
|
|
|
auto &d = update.c_updateDeleteMessages();
|
|
|
|
App::feedWereDeleted(NoChannel, d.vmessages.v);
|
|
|
|
} break;
|
|
|
|
|
|
|
|
case mtpc_updateNewChannelMessage: {
|
|
|
|
auto &d = update.c_updateNewChannelMessage();
|
|
|
|
auto needToAdd = true;
|
|
|
|
if (d.vmessage.type() == mtpc_message) { // index forwarded messages to links _overview
|
|
|
|
if (App::checkEntitiesAndViewsUpdate(d.vmessage.c_message())) { // already in blocks
|
|
|
|
LOG(("Skipping message, because it is already in blocks!"));
|
|
|
|
needToAdd = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (needToAdd) {
|
2019-01-03 12:36:01 +00:00
|
|
|
_session->data().addNewMessage(d.vmessage, NewMessageUnread);
|
2017-07-14 11:54:47 +00:00
|
|
|
}
|
|
|
|
} break;
|
|
|
|
|
|
|
|
case mtpc_updateEditChannelMessage: {
|
|
|
|
auto &d = update.c_updateEditChannelMessage();
|
|
|
|
App::updateEditedMessage(d.vmessage);
|
|
|
|
} break;
|
|
|
|
|
|
|
|
case mtpc_updateEditMessage: {
|
|
|
|
auto &d = update.c_updateEditMessage();
|
|
|
|
App::updateEditedMessage(d.vmessage);
|
|
|
|
} break;
|
|
|
|
|
|
|
|
case mtpc_updateChannelWebPage: {
|
|
|
|
auto &d = update.c_updateChannelWebPage();
|
|
|
|
Q_UNUSED(d); // Web page was updated anyway.
|
|
|
|
} break;
|
|
|
|
|
|
|
|
case mtpc_updateDeleteChannelMessages: {
|
|
|
|
auto &d = update.c_updateDeleteChannelMessages();
|
|
|
|
App::feedWereDeleted(d.vchannel_id.v, d.vmessages.v);
|
|
|
|
} break;
|
|
|
|
|
|
|
|
default: Unexpected("Type in applyUpdateNoPtsCheck()");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-02-21 21:17:36 +00:00
|
|
|
void ApiWrap::jumpToDate(Dialogs::Key chat, const QDate &date) {
|
|
|
|
if (const auto peer = chat.peer()) {
|
|
|
|
jumpToHistoryDate(peer, date);
|
|
|
|
} else if (const auto feed = chat.feed()) {
|
|
|
|
jumpToFeedDate(feed, date);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-11-30 12:50:13 +00:00
|
|
|
template <typename Callback>
|
|
|
|
void ApiWrap::requestMessageAfterDate(
|
|
|
|
not_null<PeerData*> peer,
|
|
|
|
const QDate &date,
|
|
|
|
Callback &&callback) {
|
2017-08-01 11:43:50 +00:00
|
|
|
// API returns a message with date <= offset_date.
|
|
|
|
// So we request a message with offset_date = desired_date - 1 and add_offset = -1.
|
|
|
|
// This should give us the first message with date >= desired_date.
|
2017-11-20 19:54:05 +00:00
|
|
|
auto offsetId = 0;
|
|
|
|
auto offsetDate = static_cast<int>(QDateTime(date).toTime_t()) - 1;
|
|
|
|
auto addOffset = -1;
|
2017-08-01 11:43:50 +00:00
|
|
|
auto limit = 1;
|
2017-11-20 19:54:05 +00:00
|
|
|
auto maxId = 0;
|
|
|
|
auto minId = 0;
|
|
|
|
auto historyHash = 0;
|
|
|
|
request(MTPmessages_GetHistory(
|
|
|
|
peer->input,
|
|
|
|
MTP_int(offsetId),
|
|
|
|
MTP_int(offsetDate),
|
|
|
|
MTP_int(addOffset),
|
|
|
|
MTP_int(limit),
|
|
|
|
MTP_int(maxId),
|
|
|
|
MTP_int(minId),
|
|
|
|
MTP_int(historyHash)
|
2017-11-30 12:50:13 +00:00
|
|
|
)).done([
|
2019-01-18 12:27:37 +00:00
|
|
|
=,
|
2017-11-30 12:50:13 +00:00
|
|
|
callback = std::forward<Callback>(callback)
|
|
|
|
](const MTPmessages_Messages &result) {
|
2019-01-18 12:27:37 +00:00
|
|
|
auto getMessagesList = [&]() -> const QVector<MTPMessage>* {
|
|
|
|
auto handleMessages = [&](auto &messages) {
|
|
|
|
_session->data().processUsers(messages.vusers);
|
|
|
|
_session->data().processChats(messages.vchats);
|
2017-08-01 11:43:50 +00:00
|
|
|
return &messages.vmessages.v;
|
|
|
|
};
|
|
|
|
switch (result.type()) {
|
2017-11-20 19:54:05 +00:00
|
|
|
case mtpc_messages_messages:
|
|
|
|
return handleMessages(result.c_messages_messages());
|
|
|
|
case mtpc_messages_messagesSlice:
|
|
|
|
return handleMessages(result.c_messages_messagesSlice());
|
2017-08-01 11:43:50 +00:00
|
|
|
case mtpc_messages_channelMessages: {
|
|
|
|
auto &messages = result.c_messages_channelMessages();
|
|
|
|
if (peer && peer->isChannel()) {
|
|
|
|
peer->asChannel()->ptsReceived(messages.vpts.v);
|
|
|
|
} else {
|
2017-11-20 19:54:05 +00:00
|
|
|
LOG(("API Error: received messages.channelMessages when no channel was passed! (ApiWrap::jumpToDate)"));
|
2017-08-01 11:43:50 +00:00
|
|
|
}
|
|
|
|
return handleMessages(messages);
|
|
|
|
} break;
|
2017-11-20 19:54:05 +00:00
|
|
|
case mtpc_messages_messagesNotModified: {
|
|
|
|
LOG(("API Error: received messages.messagesNotModified! (ApiWrap::jumpToDate)"));
|
|
|
|
} break;
|
2017-08-01 11:43:50 +00:00
|
|
|
}
|
|
|
|
return nullptr;
|
|
|
|
};
|
|
|
|
|
|
|
|
if (auto list = getMessagesList()) {
|
|
|
|
App::feedMsgs(*list, NewMessageExisting);
|
|
|
|
for (auto &message : *list) {
|
2018-12-17 08:16:06 +00:00
|
|
|
if (DateFromMessage(message) >= offsetDate) {
|
|
|
|
callback(IdFromMessage(message));
|
2017-11-30 12:50:13 +00:00
|
|
|
return;
|
|
|
|
}
|
2017-08-01 11:43:50 +00:00
|
|
|
}
|
|
|
|
}
|
2017-11-30 12:50:13 +00:00
|
|
|
callback(ShowAtUnreadMsgId);
|
2017-08-01 11:43:50 +00:00
|
|
|
}).send();
|
|
|
|
}
|
|
|
|
|
2018-02-21 21:17:36 +00:00
|
|
|
void ApiWrap::jumpToHistoryDate(not_null<PeerData*> peer, const QDate &date) {
|
|
|
|
if (const auto channel = peer->migrateTo()) {
|
|
|
|
jumpToHistoryDate(channel, date);
|
2017-11-30 12:50:13 +00:00
|
|
|
return;
|
|
|
|
}
|
2018-02-21 21:17:36 +00:00
|
|
|
const auto jumpToDateInPeer = [=] {
|
|
|
|
requestMessageAfterDate(peer, date, [=](MsgId resultId) {
|
2017-11-30 12:50:13 +00:00
|
|
|
Ui::showPeerHistory(peer, resultId);
|
|
|
|
});
|
|
|
|
};
|
2018-02-21 21:17:36 +00:00
|
|
|
if (const auto chat = peer->migrateFrom()) {
|
2017-11-30 12:50:13 +00:00
|
|
|
requestMessageAfterDate(chat, date, [=](MsgId resultId) {
|
|
|
|
if (resultId) {
|
|
|
|
Ui::showPeerHistory(chat, resultId);
|
|
|
|
} else {
|
|
|
|
jumpToDateInPeer();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
jumpToDateInPeer();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-02-21 21:17:36 +00:00
|
|
|
template <typename Callback>
|
|
|
|
void ApiWrap::requestMessageAfterDate(
|
|
|
|
not_null<Data::Feed*> feed,
|
|
|
|
const QDate &date,
|
|
|
|
Callback &&callback) {
|
|
|
|
const auto offsetId = 0;
|
|
|
|
const auto offsetDate = static_cast<TimeId>(QDateTime(date).toTime_t());
|
|
|
|
const auto addOffset = -2;
|
|
|
|
const auto limit = 1;
|
|
|
|
const auto hash = 0;
|
2018-03-06 17:07:42 +00:00
|
|
|
//request(MTPchannels_GetFeed( // #feed
|
|
|
|
// MTP_flags(MTPchannels_GetFeed::Flag::f_offset_position),
|
|
|
|
// MTP_int(feed->id()),
|
|
|
|
// MTP_feedPosition(
|
|
|
|
// MTP_int(offsetDate),
|
|
|
|
// MTP_peerUser(MTP_int(_session->userId())),
|
|
|
|
// MTP_int(0)),
|
|
|
|
// MTP_int(addOffset),
|
|
|
|
// MTP_int(limit),
|
|
|
|
// MTPfeedPosition(), // max_id
|
|
|
|
// MTPfeedPosition(), // min_id
|
|
|
|
// MTP_int(hash)
|
|
|
|
//)).done([
|
|
|
|
// =,
|
|
|
|
// callback = std::forward<Callback>(callback)
|
|
|
|
//](const MTPmessages_FeedMessages &result) {
|
|
|
|
// if (result.type() == mtpc_messages_feedMessagesNotModified) {
|
|
|
|
// LOG(("API Error: "
|
|
|
|
// "Unexpected messages.feedMessagesNotModified."));
|
|
|
|
// callback(Data::UnreadMessagePosition);
|
|
|
|
// return;
|
|
|
|
// }
|
|
|
|
// Assert(result.type() == mtpc_messages_feedMessages);
|
|
|
|
// const auto &data = result.c_messages_feedMessages();
|
|
|
|
// const auto &messages = data.vmessages.v;
|
|
|
|
// const auto type = NewMessageExisting;
|
2019-01-18 12:27:37 +00:00
|
|
|
// _session->data().processUsers(data.vusers);
|
|
|
|
// _session->data().processChats(data.vchats);
|
2018-03-06 17:07:42 +00:00
|
|
|
// for (const auto &msg : messages) {
|
2019-01-03 12:36:01 +00:00
|
|
|
// if (const auto item = _session->data().addNewMessage(msg, type)) {
|
2018-03-06 17:07:42 +00:00
|
|
|
// if (item->date() >= offsetDate || true) {
|
|
|
|
// callback(item->position());
|
|
|
|
// return;
|
|
|
|
// }
|
|
|
|
// }
|
|
|
|
// }
|
|
|
|
// callback(Data::UnreadMessagePosition);
|
|
|
|
//}).send();
|
2018-02-21 21:17:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void ApiWrap::jumpToFeedDate(not_null<Data::Feed*> feed, const QDate &date) {
|
|
|
|
requestMessageAfterDate(feed, date, [=](Data::MessagePosition result) {
|
2018-02-21 23:59:56 +00:00
|
|
|
Ui::hideLayer();
|
2018-02-21 21:17:36 +00:00
|
|
|
App::wnd()->controller()->showSection(
|
|
|
|
HistoryFeed::Memento(feed, result));
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2017-08-17 08:31:24 +00:00
|
|
|
void ApiWrap::preloadEnoughUnreadMentions(not_null<History*> history) {
|
2017-08-11 07:16:07 +00:00
|
|
|
auto fullCount = history->getUnreadMentionsCount();
|
|
|
|
auto loadedCount = history->getUnreadMentionsLoadedCount();
|
|
|
|
auto allLoaded = (fullCount >= 0) ? (loadedCount >= fullCount) : false;
|
|
|
|
if (fullCount < 0 || loadedCount >= kUnreadMentionsPreloadIfLess || allLoaded) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (_unreadMentionsRequests.contains(history)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
auto offsetId = loadedCount ? history->getMaxLoadedUnreadMention() : 1;
|
|
|
|
auto limit = loadedCount ? kUnreadMentionsNextRequestLimit : kUnreadMentionsFirstRequestLimit;
|
|
|
|
auto addOffset = loadedCount ? -(limit + 1) : -limit;
|
|
|
|
auto maxId = 0;
|
|
|
|
auto minId = 0;
|
|
|
|
auto requestId = request(MTPmessages_GetUnreadMentions(history->peer->input, MTP_int(offsetId), MTP_int(addOffset), MTP_int(limit), MTP_int(maxId), MTP_int(minId))).done([this, history](const MTPmessages_Messages &result) {
|
|
|
|
_unreadMentionsRequests.remove(history);
|
|
|
|
history->addUnreadMentionsSlice(result);
|
|
|
|
}).fail([this, history](const RPCError &error) {
|
|
|
|
_unreadMentionsRequests.remove(history);
|
|
|
|
}).send();
|
|
|
|
_unreadMentionsRequests.emplace(history, requestId);
|
|
|
|
}
|
|
|
|
|
2018-01-17 16:21:01 +00:00
|
|
|
void ApiWrap::checkForUnreadMentions(
|
|
|
|
const base::flat_set<MsgId> &possiblyReadMentions,
|
|
|
|
ChannelData *channel) {
|
2017-08-11 07:16:07 +00:00
|
|
|
for (auto msgId : possiblyReadMentions) {
|
|
|
|
requestMessageData(channel, msgId, [](ChannelData *channel, MsgId msgId) {
|
|
|
|
if (auto item = App::histItemById(channel, msgId)) {
|
|
|
|
if (item->mentionsMe()) {
|
|
|
|
item->markMediaRead();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-12-28 11:42:58 +00:00
|
|
|
void ApiWrap::addChatParticipants(
|
|
|
|
not_null<PeerData*> peer,
|
|
|
|
const std::vector<not_null<UserData*>> &users) {
|
|
|
|
if (const auto chat = peer->asChat()) {
|
|
|
|
for (const auto user : users) {
|
|
|
|
request(MTPmessages_AddChatUser(
|
|
|
|
chat->inputChat,
|
|
|
|
user->inputUser,
|
|
|
|
MTP_int(kForwardMessagesOnAdd)
|
|
|
|
)).done([=](const MTPUpdates &result) {
|
|
|
|
applyUpdates(result);
|
|
|
|
}).fail([=](const RPCError &error) {
|
|
|
|
ShowAddParticipantsError(error.type(), peer, { 1, user });
|
2019-02-19 06:57:53 +00:00
|
|
|
}).afterDelay(crl::time(5)).send();
|
2018-12-28 11:42:58 +00:00
|
|
|
}
|
|
|
|
} else if (const auto channel = peer->asChannel()) {
|
|
|
|
const auto bot = ranges::find_if(users, [](not_null<UserData*> user) {
|
|
|
|
return user->botInfo != nullptr;
|
|
|
|
});
|
|
|
|
if (!peer->isMegagroup() && bot != end(users)) {
|
|
|
|
ShowAddParticipantsError("USER_BOT", peer, users);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
auto list = QVector<MTPInputUser>();
|
|
|
|
list.reserve(qMin(int(users.size()), int(kMaxUsersPerInvite)));
|
|
|
|
const auto send = [&] {
|
|
|
|
request(MTPchannels_InviteToChannel(
|
|
|
|
channel->inputChannel,
|
|
|
|
MTP_vector<MTPInputUser>(list)
|
|
|
|
)).done([=](const MTPUpdates &result) {
|
|
|
|
applyUpdates(result);
|
|
|
|
requestParticipantsCountDelayed(channel);
|
|
|
|
}).fail([=](const RPCError &error) {
|
|
|
|
ShowAddParticipantsError(error.type(), peer, users);
|
2019-02-19 06:57:53 +00:00
|
|
|
}).afterDelay(crl::time(5)).send();
|
2018-12-28 11:42:58 +00:00
|
|
|
};
|
|
|
|
for (const auto user : users) {
|
|
|
|
list.push_back(user->inputUser);
|
|
|
|
if (list.size() == kMaxUsersPerInvite) {
|
|
|
|
send();
|
|
|
|
list.clear();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!list.empty()) {
|
|
|
|
send();
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
Unexpected("User in ApiWrap::addChatParticipants.");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-10-29 15:32:01 +00:00
|
|
|
void ApiWrap::requestSharedMediaCount(
|
|
|
|
not_null<PeerData*> peer,
|
|
|
|
Storage::SharedMediaType type) {
|
|
|
|
requestSharedMedia(peer, type, 0, SliceType::Before);
|
|
|
|
}
|
|
|
|
|
2017-08-18 19:14:31 +00:00
|
|
|
void ApiWrap::requestSharedMedia(
|
|
|
|
not_null<PeerData*> peer,
|
|
|
|
SharedMediaType type,
|
|
|
|
MsgId messageId,
|
|
|
|
SliceType slice) {
|
|
|
|
auto key = std::make_tuple(peer, type, messageId, slice);
|
|
|
|
if (_sharedMediaRequests.contains(key)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-10-30 19:24:20 +00:00
|
|
|
auto prepared = Api::PrepareSearchRequest(
|
|
|
|
peer,
|
|
|
|
type,
|
|
|
|
QString(),
|
|
|
|
messageId,
|
|
|
|
slice);
|
|
|
|
if (prepared.vfilter.type() == mtpc_inputMessagesFilterEmpty) {
|
2017-08-18 19:14:31 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-10-30 19:24:20 +00:00
|
|
|
auto requestId = request(
|
|
|
|
std::move(prepared)
|
|
|
|
).done([this, peer, type, messageId, slice](
|
|
|
|
const MTPmessages_Messages &result) {
|
|
|
|
auto key = std::make_tuple(peer, type, messageId, slice);
|
|
|
|
_sharedMediaRequests.remove(key);
|
2017-08-18 19:14:31 +00:00
|
|
|
sharedMediaDone(peer, type, messageId, slice, result);
|
|
|
|
}).fail([this, key](const RPCError &error) {
|
|
|
|
_sharedMediaRequests.remove(key);
|
|
|
|
}).send();
|
|
|
|
_sharedMediaRequests.emplace(key, requestId);
|
|
|
|
}
|
|
|
|
|
|
|
|
void ApiWrap::sharedMediaDone(
|
|
|
|
not_null<PeerData*> peer,
|
|
|
|
SharedMediaType type,
|
|
|
|
MsgId messageId,
|
|
|
|
SliceType slice,
|
|
|
|
const MTPmessages_Messages &result) {
|
2017-10-30 19:24:20 +00:00
|
|
|
auto parsed = Api::ParseSearchResult(
|
|
|
|
peer,
|
|
|
|
type,
|
|
|
|
messageId,
|
|
|
|
slice,
|
|
|
|
result);
|
2018-01-03 17:06:43 +00:00
|
|
|
_session->storage().add(Storage::SharedMediaAddSlice(
|
2017-08-18 19:14:31 +00:00
|
|
|
peer->id,
|
|
|
|
type,
|
2017-10-30 19:24:20 +00:00
|
|
|
std::move(parsed.messageIds),
|
|
|
|
parsed.noSkipRange,
|
|
|
|
parsed.fullCount
|
2017-08-18 19:14:31 +00:00
|
|
|
));
|
|
|
|
}
|
|
|
|
|
2017-08-29 19:52:52 +00:00
|
|
|
void ApiWrap::requestUserPhotos(
|
|
|
|
not_null<UserData*> user,
|
|
|
|
PhotoId afterId) {
|
|
|
|
if (_userPhotosRequests.contains(user)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
auto limit = kSharedMediaLimit;
|
|
|
|
|
|
|
|
auto requestId = request(MTPphotos_GetUserPhotos(
|
|
|
|
user->inputUser,
|
|
|
|
MTP_int(0),
|
|
|
|
MTP_long(afterId),
|
|
|
|
MTP_int(limit)
|
|
|
|
)).done([this, user, afterId](const MTPphotos_Photos &result) {
|
|
|
|
_userPhotosRequests.remove(user);
|
|
|
|
userPhotosDone(user, afterId, result);
|
|
|
|
}).fail([this, user](const RPCError &error) {
|
|
|
|
_userPhotosRequests.remove(user);
|
|
|
|
}).send();
|
|
|
|
_userPhotosRequests.emplace(user, requestId);
|
|
|
|
}
|
|
|
|
|
|
|
|
void ApiWrap::userPhotosDone(
|
|
|
|
not_null<UserData*> user,
|
|
|
|
PhotoId photoId,
|
|
|
|
const MTPphotos_Photos &result) {
|
|
|
|
auto fullCount = 0;
|
|
|
|
auto &photos = *[&] {
|
|
|
|
switch (result.type()) {
|
|
|
|
case mtpc_photos_photos: {
|
|
|
|
auto &d = result.c_photos_photos();
|
2019-01-18 12:27:37 +00:00
|
|
|
_session->data().processUsers(d.vusers);
|
2017-08-29 19:52:52 +00:00
|
|
|
fullCount = d.vphotos.v.size();
|
|
|
|
return &d.vphotos.v;
|
|
|
|
} break;
|
|
|
|
|
|
|
|
case mtpc_photos_photosSlice: {
|
|
|
|
auto &d = result.c_photos_photosSlice();
|
2019-01-18 12:27:37 +00:00
|
|
|
_session->data().processUsers(d.vusers);
|
2017-08-29 19:52:52 +00:00
|
|
|
fullCount = d.vcount.v;
|
|
|
|
return &d.vphotos.v;
|
|
|
|
} break;
|
|
|
|
}
|
|
|
|
Unexpected("photos.Photos type in userPhotosDone()");
|
|
|
|
}();
|
|
|
|
|
|
|
|
auto photoIds = std::vector<PhotoId>();
|
|
|
|
photoIds.reserve(photos.size());
|
|
|
|
for (auto &photo : photos) {
|
2019-01-18 12:27:37 +00:00
|
|
|
if (auto photoData = _session->data().processPhoto(photo)) {
|
2017-08-29 19:52:52 +00:00
|
|
|
photoIds.push_back(photoData->id);
|
|
|
|
}
|
|
|
|
}
|
2018-01-03 17:06:43 +00:00
|
|
|
_session->storage().add(Storage::UserPhotosAddSlice(
|
2017-08-29 19:52:52 +00:00
|
|
|
user->id,
|
|
|
|
std::move(photoIds),
|
|
|
|
fullCount
|
|
|
|
));
|
|
|
|
}
|
2018-03-06 17:07:42 +00:00
|
|
|
// #feed
|
|
|
|
//void ApiWrap::requestFeedChannels(not_null<Data::Feed*> feed) {
|
|
|
|
// if (_feedChannelsGetRequests.contains(feed)) {
|
|
|
|
// return;
|
|
|
|
// }
|
|
|
|
// const auto hash = feed->channelsHash();
|
|
|
|
// request(MTPchannels_GetFeedSources(
|
|
|
|
// MTP_flags(MTPchannels_GetFeedSources::Flag::f_feed_id),
|
|
|
|
// MTP_int(feed->id()),
|
|
|
|
// MTP_int(hash)
|
|
|
|
// )).done([=](const MTPchannels_FeedSources &result) {
|
|
|
|
// _feedChannelsGetRequests.remove(feed);
|
|
|
|
//
|
|
|
|
// switch (result.type()) {
|
|
|
|
// case mtpc_channels_feedSourcesNotModified:
|
|
|
|
// if (feed->channelsHash() == hash) {
|
|
|
|
// feedChannelsDone(feed);
|
|
|
|
// } else {
|
|
|
|
// requestFeedChannels(feed);
|
|
|
|
// }
|
|
|
|
// break;
|
|
|
|
//
|
|
|
|
// case mtpc_channels_feedSources: {
|
|
|
|
// const auto &data = result.c_channels_feedSources();
|
|
|
|
// applyFeedSources(data);
|
|
|
|
// if (feed->channelsLoaded()) {
|
|
|
|
// feedChannelsDone(feed);
|
|
|
|
// } else {
|
|
|
|
// LOG(("API Error: feed channels not received for "
|
|
|
|
// ).arg(feed->id()));
|
|
|
|
// }
|
|
|
|
// } break;
|
|
|
|
//
|
|
|
|
// default: Unexpected("Type in channels.getFeedSources response.");
|
|
|
|
// }
|
|
|
|
// }).fail([=](const RPCError &error) {
|
|
|
|
// _feedChannelsGetRequests.remove(feed);
|
|
|
|
// }).send();
|
|
|
|
// _feedChannelsGetRequests.emplace(feed);
|
|
|
|
//}
|
|
|
|
//
|
|
|
|
//void ApiWrap::applyFeedSources(const MTPDchannels_feedSources &data) {
|
|
|
|
// // First we set channels without reading them from data.
|
|
|
|
// // This allows us to apply them all at once without registering
|
|
|
|
// // them one by one.
|
|
|
|
// for (const auto &broadcasts : data.vfeeds.v) {
|
|
|
|
// if (broadcasts.type() == mtpc_feedBroadcasts) {
|
|
|
|
// const auto &list = broadcasts.c_feedBroadcasts();
|
|
|
|
// const auto feedId = list.vfeed_id.v;
|
|
|
|
// const auto feed = _session->data().feed(feedId);
|
|
|
|
// auto channels = std::vector<not_null<ChannelData*>>();
|
|
|
|
// for (const auto &channelId : list.vchannels.v) {
|
2019-01-18 12:27:37 +00:00
|
|
|
// channels.push_back(_session->data().channel(channelId.v));
|
2018-03-06 17:07:42 +00:00
|
|
|
// }
|
|
|
|
// feed->setChannels(std::move(channels));
|
|
|
|
// }
|
|
|
|
// }
|
|
|
|
//
|
2019-01-18 12:27:37 +00:00
|
|
|
// _session->data().processUsers(data.vusers);
|
|
|
|
// _session->data().processChats(data.vchats);
|
2018-03-06 17:07:42 +00:00
|
|
|
//
|
|
|
|
// if (data.has_newly_joined_feed()) {
|
|
|
|
// _session->data().setDefaultFeedId(
|
|
|
|
// data.vnewly_joined_feed.v);
|
|
|
|
// }
|
|
|
|
//}
|
|
|
|
//
|
|
|
|
//void ApiWrap::setFeedChannels(
|
|
|
|
// not_null<Data::Feed*> feed,
|
|
|
|
// const std::vector<not_null<ChannelData*>> &channels) {
|
|
|
|
// if (const auto already = _feedChannelsSetRequests.take(feed)) {
|
|
|
|
// request(*already).cancel();
|
|
|
|
// }
|
|
|
|
// auto inputs = QVector<MTPInputChannel>();
|
|
|
|
// inputs.reserve(channels.size());
|
|
|
|
// for (const auto channel : channels) {
|
|
|
|
// inputs.push_back(channel->inputChannel);
|
|
|
|
// }
|
|
|
|
// const auto requestId = request(MTPchannels_SetFeedBroadcasts(
|
|
|
|
// MTP_flags(MTPchannels_SetFeedBroadcasts::Flag::f_channels),
|
|
|
|
// MTP_int(feed->id()),
|
|
|
|
// MTP_vector<MTPInputChannel>(inputs),
|
|
|
|
// MTPbool()
|
|
|
|
// )).done([=](const MTPUpdates &result) {
|
|
|
|
// applyUpdates(result);
|
|
|
|
//
|
|
|
|
// _feedChannelsSetRequests.remove(feed);
|
|
|
|
// }).fail([=](const RPCError &error) {
|
|
|
|
// _feedChannelsSetRequests.remove(feed);
|
|
|
|
// }).send();
|
|
|
|
//
|
|
|
|
//}
|
|
|
|
//
|
|
|
|
//void ApiWrap::feedChannelsDone(not_null<Data::Feed*> feed) {
|
|
|
|
// feed->setChannelsLoaded(true);
|
|
|
|
// for (const auto key : base::take(_feedMessagesRequestsPending)) {
|
|
|
|
// std::apply(
|
|
|
|
// [=](auto&&...args) { requestFeedMessages(args...); },
|
|
|
|
// key);
|
|
|
|
// }
|
|
|
|
//}
|
|
|
|
//
|
|
|
|
//void ApiWrap::requestFeedMessages(
|
|
|
|
// not_null<Data::Feed*> feed,
|
|
|
|
// Data::MessagePosition messageId,
|
|
|
|
// SliceType slice) {
|
|
|
|
// const auto key = std::make_tuple(feed, messageId, slice);
|
|
|
|
// if (_feedMessagesRequests.contains(key)
|
|
|
|
// || _feedMessagesRequestsPending.contains(key)) {
|
|
|
|
// return;
|
|
|
|
// }
|
|
|
|
//
|
|
|
|
// if (!feed->channelsLoaded()) {
|
|
|
|
// _feedMessagesRequestsPending.emplace(key);
|
|
|
|
// requestFeedChannels(feed);
|
|
|
|
// return;
|
|
|
|
// }
|
|
|
|
//
|
|
|
|
// // We request messages with overlapping and skip overlapped in response.
|
|
|
|
// const auto limit = kFeedMessagesLimit;
|
|
|
|
// const auto addOffset = [&] {
|
|
|
|
// switch (slice) {
|
|
|
|
// case SliceType::Before: return -2;
|
|
|
|
// case SliceType::Around: return -limit / 2;
|
|
|
|
// case SliceType::After: return 1 - limit;
|
|
|
|
// }
|
|
|
|
// Unexpected("Direction in PrepareSearchRequest");
|
|
|
|
// }();
|
|
|
|
// const auto hash = int32(0);
|
|
|
|
// const auto flags = (messageId && messageId.fullId.channel)
|
|
|
|
// ? MTPchannels_GetFeed::Flag::f_offset_position
|
|
|
|
// : MTPchannels_GetFeed::Flag::f_offset_to_max_read;
|
|
|
|
// const auto requestId = request(MTPchannels_GetFeed(
|
|
|
|
// MTP_flags(flags),
|
|
|
|
// MTP_int(feed->id()),
|
|
|
|
// MTP_feedPosition(
|
|
|
|
// MTP_int(messageId.date),
|
|
|
|
// MTP_peerChannel(MTP_int(messageId.fullId.channel)),
|
|
|
|
// MTP_int(messageId.fullId.msg)),
|
|
|
|
// MTP_int(addOffset),
|
|
|
|
// MTP_int(limit),
|
|
|
|
// MTPFeedPosition(),
|
|
|
|
// MTPFeedPosition(),
|
|
|
|
// MTP_int(hash)
|
|
|
|
// )).done([=](const MTPmessages_FeedMessages &result) {
|
|
|
|
// const auto key = std::make_tuple(feed, messageId, slice);
|
|
|
|
// _feedMessagesRequests.remove(key);
|
|
|
|
// feedMessagesDone(feed, messageId, slice, result);
|
|
|
|
// }).fail([=](const RPCError &error) {
|
|
|
|
// _feedMessagesRequests.remove(key);
|
|
|
|
// if (error.type() == qstr("SOURCES_HASH_INVALID")) {
|
|
|
|
// _feedMessagesRequestsPending.emplace(key);
|
|
|
|
// requestFeedChannels(feed);
|
|
|
|
// }
|
|
|
|
// }).send();
|
|
|
|
// _feedMessagesRequests.emplace(key);
|
|
|
|
//}
|
|
|
|
//
|
|
|
|
//void ApiWrap::feedMessagesDone(
|
|
|
|
// not_null<Data::Feed*> feed,
|
|
|
|
// Data::MessagePosition messageId,
|
|
|
|
// SliceType slice,
|
|
|
|
// const MTPmessages_FeedMessages &result) {
|
|
|
|
// if (result.type() == mtpc_messages_feedMessagesNotModified) {
|
|
|
|
// LOG(("API Error: Unexpected messages.feedMessagesNotModified."));
|
|
|
|
// _session->storage().add(Storage::FeedMessagesAddSlice(
|
|
|
|
// feed->id(),
|
|
|
|
// std::vector<Data::MessagePosition>(),
|
|
|
|
// Data::FullMessagesRange));
|
|
|
|
// return;
|
|
|
|
// }
|
|
|
|
// Assert(result.type() == mtpc_messages_feedMessages);
|
|
|
|
// const auto &data = result.c_messages_feedMessages();
|
|
|
|
// const auto &messages = data.vmessages.v;
|
|
|
|
// const auto type = NewMessageExisting;
|
|
|
|
//
|
|
|
|
// auto ids = std::vector<Data::MessagePosition>();
|
|
|
|
// auto noSkipRange = Data::MessagesRange(messageId, messageId);
|
|
|
|
// const auto accumulateFrom = [](auto &from, const auto &candidate) {
|
|
|
|
// if (!from || from > candidate) {
|
|
|
|
// from = candidate;
|
|
|
|
// }
|
|
|
|
// };
|
|
|
|
// const auto accumulateTill = [](auto &till, const auto &candidate) {
|
|
|
|
// if (!till || till < candidate) {
|
|
|
|
// till = candidate;
|
|
|
|
// }
|
|
|
|
// };
|
|
|
|
// const auto tooLargePosition = [&](const auto &position) {
|
|
|
|
// return (slice == SliceType::Before) && !(position < messageId);
|
|
|
|
// };
|
|
|
|
// const auto tooSmallPosition = [&](const auto &position) {
|
|
|
|
// return (slice == SliceType::After) && !(messageId < position);
|
|
|
|
// };
|
2019-01-18 12:27:37 +00:00
|
|
|
// _session->data().processUsers(data.vusers);
|
|
|
|
// _session->data().processChats(data.vchats);
|
2018-03-06 17:07:42 +00:00
|
|
|
// if (!messages.empty()) {
|
|
|
|
// ids.reserve(messages.size());
|
|
|
|
// for (const auto &msg : messages) {
|
2019-01-03 12:36:01 +00:00
|
|
|
// if (const auto item = _session->data().addNewMessage(msg, type)) {
|
2018-03-06 17:07:42 +00:00
|
|
|
// const auto position = item->position();
|
|
|
|
// if (tooLargePosition(position)) {
|
|
|
|
// accumulateTill(noSkipRange.till, position);
|
|
|
|
// continue;
|
|
|
|
// } else if (tooSmallPosition(position)) {
|
|
|
|
// accumulateFrom(noSkipRange.from, position);
|
|
|
|
// continue;
|
|
|
|
// }
|
|
|
|
// ids.push_back(position);
|
|
|
|
// accumulateFrom(noSkipRange.from, position);
|
|
|
|
// accumulateTill(noSkipRange.till, position);
|
|
|
|
// }
|
|
|
|
// }
|
|
|
|
// ranges::reverse(ids);
|
|
|
|
// }
|
|
|
|
// if (data.has_min_position() && !ids.empty()) {
|
|
|
|
// accumulateFrom(
|
|
|
|
// noSkipRange.from,
|
|
|
|
// Data::FeedPositionFromMTP(data.vmin_position));
|
|
|
|
// } else if (slice == SliceType::Before) {
|
|
|
|
// noSkipRange.from = Data::MinMessagePosition;
|
|
|
|
// }
|
|
|
|
// if (data.has_max_position() && !ids.empty()) {
|
|
|
|
// accumulateTill(
|
|
|
|
// noSkipRange.till,
|
|
|
|
// Data::FeedPositionFromMTP(data.vmax_position));
|
|
|
|
// } else if (slice == SliceType::After) {
|
|
|
|
// noSkipRange.till = Data::MaxMessagePosition;
|
|
|
|
// }
|
|
|
|
//
|
|
|
|
// const auto unreadPosition = [&] {
|
|
|
|
// if (data.has_read_max_position()) {
|
|
|
|
// return Data::FeedPositionFromMTP(data.vread_max_position);
|
|
|
|
// } else if (!messageId) {
|
|
|
|
// const auto result = ids.empty()
|
|
|
|
// ? noSkipRange.till
|
|
|
|
// : ids.back();
|
|
|
|
// return Data::MessagePosition(
|
|
|
|
// result.date,
|
|
|
|
// FullMsgId(result.fullId.channel, result.fullId.msg - 1));
|
|
|
|
// }
|
|
|
|
// return Data::MessagePosition();
|
|
|
|
// }();
|
|
|
|
//
|
|
|
|
// _session->storage().add(Storage::FeedMessagesAddSlice(
|
|
|
|
// feed->id(),
|
|
|
|
// std::move(ids),
|
|
|
|
// noSkipRange));
|
|
|
|
//
|
|
|
|
// if (unreadPosition) {
|
|
|
|
// feed->setUnreadPosition(unreadPosition);
|
|
|
|
// }
|
|
|
|
//}
|
|
|
|
//
|
|
|
|
//void ApiWrap::saveDefaultFeedId(FeedId id, bool isDefaultFeedId) {
|
|
|
|
// if (const auto already = base::take(_saveDefaultFeedIdRequest)) {
|
|
|
|
// request(already).cancel();
|
|
|
|
// }
|
|
|
|
// _saveDefaultFeedIdRequest = request(MTPchannels_SetFeedBroadcasts(
|
|
|
|
// MTP_flags(MTPchannels_SetFeedBroadcasts::Flag::f_also_newly_joined),
|
|
|
|
// MTP_int(id),
|
|
|
|
// MTPVector<MTPInputChannel>(),
|
|
|
|
// MTP_bool(isDefaultFeedId)
|
|
|
|
// )).send();
|
|
|
|
//}
|
2018-02-08 09:20:14 +00:00
|
|
|
|
2017-12-07 13:02:24 +00:00
|
|
|
void ApiWrap::sendAction(const SendOptions &options) {
|
|
|
|
readServerHistory(options.history);
|
|
|
|
options.history->getReadyFor(ShowAtTheEndMsgId);
|
|
|
|
_sendActions.fire_copy(options);
|
|
|
|
}
|
|
|
|
|
2017-12-06 13:56:40 +00:00
|
|
|
void ApiWrap::forwardMessages(
|
|
|
|
HistoryItemsList &&items,
|
|
|
|
const SendOptions &options,
|
2018-06-04 15:35:11 +00:00
|
|
|
FnMut<void()> &&successCallback) {
|
2017-12-06 13:56:40 +00:00
|
|
|
Expects(!items.empty());
|
|
|
|
|
|
|
|
struct SharedCallback {
|
|
|
|
int requestsLeft = 0;
|
2018-06-04 15:35:11 +00:00
|
|
|
FnMut<void()> callback;
|
2017-12-06 13:56:40 +00:00
|
|
|
};
|
|
|
|
const auto shared = successCallback
|
|
|
|
? std::make_shared<SharedCallback>()
|
|
|
|
: std::shared_ptr<SharedCallback>();
|
|
|
|
if (successCallback) {
|
|
|
|
shared->callback = std::move(successCallback);
|
|
|
|
}
|
|
|
|
|
|
|
|
const auto count = int(items.size());
|
|
|
|
const auto genClientSideMessage = options.generateLocal && (count < 2);
|
|
|
|
const auto history = options.history;
|
2017-12-08 11:52:12 +00:00
|
|
|
const auto peer = history->peer;
|
2017-12-06 13:56:40 +00:00
|
|
|
|
2017-12-07 13:02:24 +00:00
|
|
|
readServerHistory(history);
|
2017-12-06 13:56:40 +00:00
|
|
|
|
2017-12-08 11:52:12 +00:00
|
|
|
const auto channelPost = peer->isChannel() && !peer->isMegagroup();
|
2018-04-09 17:48:29 +00:00
|
|
|
const auto silentPost = channelPost
|
|
|
|
&& _session->data().notifySilentPosts(peer);
|
2017-12-06 13:56:40 +00:00
|
|
|
|
|
|
|
auto flags = MTPDmessage::Flags(0);
|
|
|
|
auto sendFlags = MTPmessages_ForwardMessages::Flags(0);
|
|
|
|
if (channelPost) {
|
|
|
|
flags |= MTPDmessage::Flag::f_views;
|
|
|
|
flags |= MTPDmessage::Flag::f_post;
|
|
|
|
}
|
|
|
|
if (!channelPost) {
|
|
|
|
flags |= MTPDmessage::Flag::f_from_id;
|
2017-12-08 11:52:12 +00:00
|
|
|
} else if (peer->asChannel()->addsSignature()) {
|
2017-12-06 13:56:40 +00:00
|
|
|
flags |= MTPDmessage::Flag::f_post_author;
|
|
|
|
}
|
|
|
|
if (silentPost) {
|
|
|
|
sendFlags |= MTPmessages_ForwardMessages::Flag::f_silent;
|
|
|
|
}
|
|
|
|
|
|
|
|
auto forwardFrom = items.front()->history()->peer;
|
2017-12-15 21:23:20 +00:00
|
|
|
auto currentGroupId = items.front()->groupId();
|
2017-12-06 13:56:40 +00:00
|
|
|
auto ids = QVector<MTPint>();
|
|
|
|
auto randomIds = QVector<MTPlong>();
|
|
|
|
|
|
|
|
const auto sendAccumulated = [&] {
|
|
|
|
if (shared) {
|
|
|
|
++shared->requestsLeft;
|
|
|
|
}
|
2017-12-15 21:23:20 +00:00
|
|
|
const auto finalFlags = sendFlags
|
|
|
|
| (currentGroupId == MessageGroupId()
|
|
|
|
? MTPmessages_ForwardMessages::Flag(0)
|
|
|
|
: MTPmessages_ForwardMessages::Flag::f_grouped);
|
2017-12-06 13:56:40 +00:00
|
|
|
history->sendRequestId = request(MTPmessages_ForwardMessages(
|
2017-12-15 21:23:20 +00:00
|
|
|
MTP_flags(finalFlags),
|
2017-12-06 13:56:40 +00:00
|
|
|
forwardFrom->input,
|
|
|
|
MTP_vector<MTPint>(ids),
|
|
|
|
MTP_vector<MTPlong>(randomIds),
|
2017-12-08 11:52:12 +00:00
|
|
|
peer->input
|
2017-12-06 13:56:40 +00:00
|
|
|
)).done([=, callback = std::move(successCallback)](
|
|
|
|
const MTPUpdates &updates) {
|
|
|
|
applyUpdates(updates);
|
|
|
|
if (shared && !--shared->requestsLeft) {
|
|
|
|
shared->callback();
|
|
|
|
}
|
2017-12-07 13:02:24 +00:00
|
|
|
}).afterRequest(
|
2017-12-06 13:56:40 +00:00
|
|
|
history->sendRequestId
|
|
|
|
).send();
|
|
|
|
|
|
|
|
ids.resize(0);
|
|
|
|
randomIds.resize(0);
|
|
|
|
};
|
|
|
|
|
|
|
|
ids.reserve(count);
|
|
|
|
randomIds.reserve(count);
|
|
|
|
for (const auto item : items) {
|
|
|
|
auto randomId = rand_value<uint64>();
|
|
|
|
if (genClientSideMessage) {
|
|
|
|
if (auto message = item->toHistoryMessage()) {
|
|
|
|
const auto newId = FullMsgId(
|
2017-12-08 11:52:12 +00:00
|
|
|
peerToChannel(peer->id),
|
2017-12-06 13:56:40 +00:00
|
|
|
clientMsgId());
|
2018-01-03 17:06:43 +00:00
|
|
|
const auto self = _session->user();
|
2017-12-06 13:56:40 +00:00
|
|
|
const auto messageFromId = channelPost
|
|
|
|
? UserId(0)
|
|
|
|
: peerToUser(self->id);
|
|
|
|
const auto messagePostAuthor = channelPost
|
2019-02-23 18:52:21 +00:00
|
|
|
? App::peerName(self)
|
2017-12-06 13:56:40 +00:00
|
|
|
: QString();
|
|
|
|
history->addNewForwarded(
|
|
|
|
newId.msg,
|
|
|
|
flags,
|
2018-02-03 19:52:35 +00:00
|
|
|
unixtime(),
|
2017-12-06 13:56:40 +00:00
|
|
|
messageFromId,
|
|
|
|
messagePostAuthor,
|
|
|
|
message);
|
|
|
|
App::historyRegRandom(randomId, newId);
|
|
|
|
}
|
|
|
|
}
|
2017-12-15 21:23:20 +00:00
|
|
|
const auto newFrom = item->history()->peer;
|
|
|
|
const auto newGroupId = item->groupId();
|
|
|
|
if (forwardFrom != newFrom
|
|
|
|
|| currentGroupId != newGroupId) {
|
2017-12-06 13:56:40 +00:00
|
|
|
sendAccumulated();
|
2017-12-15 21:23:20 +00:00
|
|
|
forwardFrom = newFrom;
|
|
|
|
currentGroupId = newGroupId;
|
2017-12-06 13:56:40 +00:00
|
|
|
}
|
|
|
|
ids.push_back(MTP_int(item->id));
|
|
|
|
randomIds.push_back(MTP_long(randomId));
|
|
|
|
}
|
|
|
|
sendAccumulated();
|
2018-01-18 09:53:49 +00:00
|
|
|
_session->data().sendHistoryChangeNotifications();
|
2017-12-06 13:56:40 +00:00
|
|
|
}
|
|
|
|
|
2017-12-07 13:02:24 +00:00
|
|
|
void ApiWrap::shareContact(
|
|
|
|
const QString &phone,
|
|
|
|
const QString &firstName,
|
|
|
|
const QString &lastName,
|
|
|
|
const SendOptions &options) {
|
|
|
|
const auto userId = UserId(0);
|
|
|
|
sendSharedContact(phone, firstName, lastName, userId, options);
|
|
|
|
}
|
|
|
|
|
|
|
|
void ApiWrap::shareContact(
|
|
|
|
not_null<UserData*> user,
|
|
|
|
const SendOptions &options) {
|
|
|
|
const auto userId = peerToUser(user->id);
|
2018-01-18 09:53:49 +00:00
|
|
|
const auto phone = _session->data().findContactPhone(user);
|
2017-12-07 13:02:24 +00:00
|
|
|
if (phone.isEmpty()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
sendSharedContact(
|
|
|
|
phone,
|
|
|
|
user->firstName,
|
|
|
|
user->lastName,
|
|
|
|
userId,
|
|
|
|
options);
|
|
|
|
}
|
|
|
|
|
|
|
|
void ApiWrap::sendSharedContact(
|
|
|
|
const QString &phone,
|
|
|
|
const QString &firstName,
|
|
|
|
const QString &lastName,
|
|
|
|
UserId userId,
|
|
|
|
const SendOptions &options) {
|
|
|
|
sendAction(options);
|
|
|
|
|
|
|
|
const auto history = options.history;
|
|
|
|
const auto peer = history->peer;
|
|
|
|
|
|
|
|
const auto newId = FullMsgId(history->channelId(), clientMsgId());
|
|
|
|
const auto channelPost = peer->isChannel() && !peer->isMegagroup();
|
|
|
|
|
|
|
|
auto flags = NewMessageFlags(peer) | MTPDmessage::Flag::f_media;
|
|
|
|
if (options.replyTo) {
|
|
|
|
flags |= MTPDmessage::Flag::f_reply_to_msg_id;
|
|
|
|
}
|
|
|
|
if (channelPost) {
|
|
|
|
flags |= MTPDmessage::Flag::f_views;
|
|
|
|
flags |= MTPDmessage::Flag::f_post;
|
|
|
|
if (peer->asChannel()->addsSignature()) {
|
|
|
|
flags |= MTPDmessage::Flag::f_post_author;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
flags |= MTPDmessage::Flag::f_from_id;
|
|
|
|
}
|
2018-01-03 17:06:43 +00:00
|
|
|
const auto messageFromId = channelPost ? 0 : _session->userId();
|
2017-12-07 13:02:24 +00:00
|
|
|
const auto messagePostAuthor = channelPost
|
2019-02-23 18:52:21 +00:00
|
|
|
? App::peerName(_session->user())
|
2017-12-07 13:02:24 +00:00
|
|
|
: QString();
|
2018-06-26 13:58:29 +00:00
|
|
|
const auto vcard = QString();
|
|
|
|
const auto views = 1;
|
2017-12-19 16:57:42 +00:00
|
|
|
const auto item = history->addNewMessage(
|
2017-12-07 13:02:24 +00:00
|
|
|
MTP_message(
|
|
|
|
MTP_flags(flags),
|
|
|
|
MTP_int(newId.msg),
|
|
|
|
MTP_int(messageFromId),
|
|
|
|
peerToMTP(peer->id),
|
|
|
|
MTPnullFwdHeader,
|
|
|
|
MTPint(),
|
|
|
|
MTP_int(options.replyTo),
|
|
|
|
MTP_int(unixtime()),
|
|
|
|
MTP_string(""),
|
|
|
|
MTP_messageMediaContact(
|
|
|
|
MTP_string(phone),
|
|
|
|
MTP_string(firstName),
|
|
|
|
MTP_string(lastName),
|
2018-06-26 13:58:29 +00:00
|
|
|
MTP_string(vcard),
|
2017-12-07 13:02:24 +00:00
|
|
|
MTP_int(userId)),
|
|
|
|
MTPnullMarkup,
|
|
|
|
MTPnullEntities,
|
2018-06-26 13:58:29 +00:00
|
|
|
MTP_int(views),
|
2017-12-07 13:02:24 +00:00
|
|
|
MTPint(),
|
|
|
|
MTP_string(messagePostAuthor),
|
|
|
|
MTPlong()),
|
|
|
|
NewMessageUnread);
|
|
|
|
|
2017-12-19 16:57:42 +00:00
|
|
|
const auto media = MTP_inputMediaContact(
|
|
|
|
MTP_string(phone),
|
|
|
|
MTP_string(firstName),
|
2018-06-26 13:58:29 +00:00
|
|
|
MTP_string(lastName),
|
|
|
|
MTP_string(vcard));
|
2018-04-09 17:48:29 +00:00
|
|
|
sendMedia(item, media, _session->data().notifySilentPosts(peer));
|
2018-03-25 21:17:32 +00:00
|
|
|
|
|
|
|
if (const auto main = App::main()) {
|
|
|
|
_session->data().sendHistoryChangeNotifications();
|
|
|
|
main->historyToDown(history);
|
|
|
|
main->dialogsToUp();
|
|
|
|
}
|
2017-12-19 16:57:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void ApiWrap::sendVoiceMessage(
|
|
|
|
QByteArray result,
|
|
|
|
VoiceWaveform waveform,
|
|
|
|
int duration,
|
|
|
|
const SendOptions &options) {
|
2018-05-24 13:03:21 +00:00
|
|
|
const auto caption = TextWithTags();
|
2018-06-26 15:34:38 +00:00
|
|
|
const auto to = fileLoadTaskOptions(options);
|
2017-12-19 16:57:42 +00:00
|
|
|
_fileLoader->addTask(std::make_unique<FileLoadTask>(
|
|
|
|
result,
|
|
|
|
duration,
|
|
|
|
waveform,
|
|
|
|
to,
|
|
|
|
caption));
|
|
|
|
}
|
|
|
|
|
|
|
|
void ApiWrap::sendFiles(
|
|
|
|
Storage::PreparedList &&list,
|
|
|
|
SendMediaType type,
|
2018-05-24 13:03:21 +00:00
|
|
|
TextWithTags &&caption,
|
2017-12-19 16:57:42 +00:00
|
|
|
std::shared_ptr<SendingAlbum> album,
|
|
|
|
const SendOptions &options) {
|
2018-12-26 06:47:03 +00:00
|
|
|
const auto haveCaption = !caption.text.isEmpty();
|
|
|
|
const auto isAlbum = (album != nullptr);
|
|
|
|
const auto compressImages = (type == SendMediaType::Photo);
|
|
|
|
if (haveCaption && !list.canAddCaption(isAlbum, compressImages)) {
|
2018-06-26 15:34:38 +00:00
|
|
|
auto message = MessageToSend(options.history);
|
2018-05-24 13:03:21 +00:00
|
|
|
message.textWithTags = std::move(caption);
|
2017-12-19 16:57:42 +00:00
|
|
|
message.replyTo = options.replyTo;
|
|
|
|
message.clearDraft = false;
|
2018-06-26 15:34:38 +00:00
|
|
|
sendMessage(std::move(message));
|
2018-05-24 13:03:21 +00:00
|
|
|
caption = TextWithTags();
|
2017-12-19 16:57:42 +00:00
|
|
|
}
|
|
|
|
|
2018-06-26 15:34:38 +00:00
|
|
|
const auto to = fileLoadTaskOptions(options);
|
2017-12-25 14:17:00 +00:00
|
|
|
if (album) {
|
|
|
|
album->silent = to.silent;
|
|
|
|
}
|
2017-12-19 16:57:42 +00:00
|
|
|
auto tasks = std::vector<std::unique_ptr<Task>>();
|
|
|
|
tasks.reserve(list.files.size());
|
|
|
|
for (auto &file : list.files) {
|
|
|
|
if (album) {
|
|
|
|
switch (file.type) {
|
|
|
|
case Storage::PreparedFile::AlbumType::Photo:
|
|
|
|
type = SendMediaType::Photo;
|
|
|
|
break;
|
|
|
|
case Storage::PreparedFile::AlbumType::Video:
|
|
|
|
type = SendMediaType::File;
|
|
|
|
break;
|
|
|
|
default: Unexpected("AlbumType in uploadFilesAfterConfirmation");
|
|
|
|
}
|
|
|
|
}
|
2017-12-21 16:15:37 +00:00
|
|
|
tasks.push_back(std::make_unique<FileLoadTask>(
|
|
|
|
file.path,
|
|
|
|
file.content,
|
|
|
|
std::move(file.information),
|
|
|
|
type,
|
|
|
|
to,
|
|
|
|
caption,
|
|
|
|
album));
|
2018-09-23 19:55:14 +00:00
|
|
|
caption = TextWithTags();
|
2017-12-19 16:57:42 +00:00
|
|
|
}
|
|
|
|
if (album) {
|
|
|
|
_sendingAlbums.emplace(album->groupId, album);
|
|
|
|
album->items.reserve(tasks.size());
|
|
|
|
for (const auto &task : tasks) {
|
2019-01-13 08:03:34 +00:00
|
|
|
album->items.emplace_back(task->id());
|
2017-12-19 16:57:42 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
_fileLoader->addTasks(std::move(tasks));
|
|
|
|
}
|
|
|
|
|
|
|
|
void ApiWrap::sendFile(
|
|
|
|
const QByteArray &fileContent,
|
|
|
|
SendMediaType type,
|
|
|
|
const SendOptions &options) {
|
2018-06-26 15:34:38 +00:00
|
|
|
const auto to = fileLoadTaskOptions(options);
|
2018-05-24 13:03:21 +00:00
|
|
|
auto caption = TextWithTags();
|
2017-12-19 16:57:42 +00:00
|
|
|
_fileLoader->addTask(std::make_unique<FileLoadTask>(
|
2017-12-21 16:15:37 +00:00
|
|
|
QString(),
|
2017-12-19 16:57:42 +00:00
|
|
|
fileContent,
|
2017-12-21 16:15:37 +00:00
|
|
|
nullptr,
|
2017-12-19 16:57:42 +00:00
|
|
|
type,
|
|
|
|
to,
|
|
|
|
caption));
|
|
|
|
}
|
|
|
|
|
|
|
|
void ApiWrap::sendUploadedPhoto(
|
|
|
|
FullMsgId localId,
|
|
|
|
const MTPInputFile &file,
|
|
|
|
bool silent) {
|
|
|
|
if (const auto item = App::histItemById(localId)) {
|
|
|
|
const auto media = MTP_inputMediaUploadedPhoto(
|
|
|
|
MTP_flags(0),
|
|
|
|
file,
|
|
|
|
MTPVector<MTPInputDocument>(),
|
|
|
|
MTP_int(0));
|
|
|
|
if (const auto groupId = item->groupId()) {
|
2017-12-25 14:17:00 +00:00
|
|
|
uploadAlbumMedia(item, groupId, media);
|
2017-12-19 16:57:42 +00:00
|
|
|
} else {
|
|
|
|
sendMedia(item, media, silent);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void ApiWrap::sendUploadedDocument(
|
|
|
|
FullMsgId localId,
|
|
|
|
const MTPInputFile &file,
|
2018-09-21 16:28:46 +00:00
|
|
|
const std::optional<MTPInputFile> &thumb,
|
2017-12-19 16:57:42 +00:00
|
|
|
bool silent) {
|
|
|
|
if (const auto item = App::histItemById(localId)) {
|
2018-01-14 16:02:25 +00:00
|
|
|
auto media = item->media();
|
|
|
|
if (auto document = media ? media->document() : nullptr) {
|
2017-12-19 16:57:42 +00:00
|
|
|
const auto groupId = item->groupId();
|
|
|
|
const auto flags = MTPDinputMediaUploadedDocument::Flags(0)
|
|
|
|
| (thumb
|
|
|
|
? MTPDinputMediaUploadedDocument::Flag::f_thumb
|
|
|
|
: MTPDinputMediaUploadedDocument::Flag(0))
|
|
|
|
| (groupId
|
|
|
|
? MTPDinputMediaUploadedDocument::Flag::f_nosound_video
|
|
|
|
: MTPDinputMediaUploadedDocument::Flag(0));
|
|
|
|
const auto media = MTP_inputMediaUploadedDocument(
|
|
|
|
MTP_flags(flags),
|
|
|
|
file,
|
|
|
|
thumb ? *thumb : MTPInputFile(),
|
|
|
|
MTP_string(document->mimeString()),
|
|
|
|
ComposeSendingDocumentAttributes(document),
|
|
|
|
MTPVector<MTPInputDocument>(),
|
|
|
|
MTP_int(0));
|
|
|
|
if (groupId) {
|
2017-12-25 14:17:00 +00:00
|
|
|
uploadAlbumMedia(item, groupId, media);
|
2017-12-19 16:57:42 +00:00
|
|
|
} else {
|
|
|
|
sendMedia(item, media, silent);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-12-25 14:17:00 +00:00
|
|
|
void ApiWrap::cancelLocalItem(not_null<HistoryItem*> item) {
|
|
|
|
Expects(!IsServerMsgId(item->id));
|
|
|
|
|
|
|
|
if (const auto groupId = item->groupId()) {
|
|
|
|
sendAlbumWithCancelled(item, groupId);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-06-26 15:34:38 +00:00
|
|
|
void ApiWrap::sendMessage(MessageToSend &&message) {
|
|
|
|
const auto history = message.history;
|
|
|
|
const auto peer = history->peer;
|
|
|
|
auto &textWithTags = message.textWithTags;
|
|
|
|
|
|
|
|
auto options = ApiWrap::SendOptions(history);
|
|
|
|
options.clearDraft = message.clearDraft;
|
|
|
|
options.replyTo = message.replyTo;
|
|
|
|
options.generateLocal = true;
|
|
|
|
options.webPageId = message.webPageId;
|
2018-10-08 14:08:17 +00:00
|
|
|
options.handleSupportSwitch = message.handleSupportSwitch;
|
2018-06-26 15:34:38 +00:00
|
|
|
sendAction(options);
|
|
|
|
|
|
|
|
if (!peer->canWrite()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
Local::saveRecentSentHashtags(textWithTags.text);
|
|
|
|
|
|
|
|
auto sending = TextWithEntities();
|
|
|
|
auto left = TextWithEntities {
|
|
|
|
textWithTags.text,
|
|
|
|
ConvertTextTagsToEntities(textWithTags.tags)
|
|
|
|
};
|
|
|
|
auto prepareFlags = Ui::ItemTextOptions(
|
|
|
|
history,
|
|
|
|
_session->user()).flags;
|
|
|
|
TextUtilities::PrepareForSending(left, prepareFlags);
|
|
|
|
|
|
|
|
HistoryItem *lastMessage = nullptr;
|
|
|
|
|
|
|
|
while (TextUtilities::CutPart(sending, left, MaxMessageSize)) {
|
|
|
|
auto newId = FullMsgId(peerToChannel(peer->id), clientMsgId());
|
|
|
|
auto randomId = rand_value<uint64>();
|
|
|
|
|
|
|
|
TextUtilities::Trim(sending);
|
|
|
|
|
|
|
|
App::historyRegRandom(randomId, newId);
|
|
|
|
App::historyRegSentData(randomId, peer->id, sending.text);
|
|
|
|
|
|
|
|
MTPstring msgText(MTP_string(sending.text));
|
|
|
|
auto flags = NewMessageFlags(peer) | MTPDmessage::Flag::f_entities;
|
|
|
|
auto sendFlags = MTPmessages_SendMessage::Flags(0);
|
|
|
|
if (message.replyTo) {
|
|
|
|
flags |= MTPDmessage::Flag::f_reply_to_msg_id;
|
|
|
|
sendFlags |= MTPmessages_SendMessage::Flag::f_reply_to_msg_id;
|
|
|
|
}
|
|
|
|
MTPMessageMedia media = MTP_messageMediaEmpty();
|
|
|
|
if (message.webPageId == CancelledWebPageId) {
|
|
|
|
sendFlags |= MTPmessages_SendMessage::Flag::f_no_webpage;
|
|
|
|
} else if (message.webPageId) {
|
|
|
|
auto page = _session->data().webpage(message.webPageId);
|
|
|
|
media = MTP_messageMediaWebPage(
|
|
|
|
MTP_webPagePending(
|
|
|
|
MTP_long(page->id),
|
|
|
|
MTP_int(page->pendingTill)));
|
|
|
|
flags |= MTPDmessage::Flag::f_media;
|
|
|
|
}
|
|
|
|
bool channelPost = peer->isChannel() && !peer->isMegagroup();
|
|
|
|
bool silentPost = channelPost
|
|
|
|
&& _session->data().notifySilentPosts(peer);
|
|
|
|
if (channelPost) {
|
|
|
|
flags |= MTPDmessage::Flag::f_views;
|
|
|
|
flags |= MTPDmessage::Flag::f_post;
|
|
|
|
}
|
|
|
|
if (!channelPost) {
|
|
|
|
flags |= MTPDmessage::Flag::f_from_id;
|
|
|
|
} else if (peer->asChannel()->addsSignature()) {
|
|
|
|
flags |= MTPDmessage::Flag::f_post_author;
|
|
|
|
}
|
|
|
|
if (silentPost) {
|
|
|
|
sendFlags |= MTPmessages_SendMessage::Flag::f_silent;
|
|
|
|
}
|
|
|
|
auto localEntities = TextUtilities::EntitiesToMTP(sending.entities);
|
|
|
|
auto sentEntities = TextUtilities::EntitiesToMTP(sending.entities, TextUtilities::ConvertOption::SkipLocal);
|
|
|
|
if (!sentEntities.v.isEmpty()) {
|
|
|
|
sendFlags |= MTPmessages_SendMessage::Flag::f_entities;
|
|
|
|
}
|
|
|
|
if (message.clearDraft) {
|
|
|
|
sendFlags |= MTPmessages_SendMessage::Flag::f_clear_draft;
|
|
|
|
history->clearCloudDraft();
|
2018-06-26 15:59:05 +00:00
|
|
|
history->setSentDraftText(QString());
|
2018-06-26 15:34:38 +00:00
|
|
|
}
|
|
|
|
auto messageFromId = channelPost ? 0 : _session->userId();
|
|
|
|
auto messagePostAuthor = channelPost
|
|
|
|
? App::peerName(_session->user())
|
|
|
|
: QString();
|
|
|
|
lastMessage = history->addNewMessage(
|
|
|
|
MTP_message(
|
|
|
|
MTP_flags(flags),
|
|
|
|
MTP_int(newId.msg),
|
|
|
|
MTP_int(messageFromId),
|
|
|
|
peerToMTP(peer->id),
|
|
|
|
MTPnullFwdHeader,
|
|
|
|
MTPint(),
|
|
|
|
MTP_int(message.replyTo),
|
|
|
|
MTP_int(unixtime()),
|
|
|
|
msgText,
|
|
|
|
media,
|
|
|
|
MTPnullMarkup,
|
|
|
|
localEntities,
|
|
|
|
MTP_int(1),
|
|
|
|
MTPint(),
|
|
|
|
MTP_string(messagePostAuthor),
|
|
|
|
MTPlong()),
|
|
|
|
NewMessageUnread);
|
|
|
|
history->sendRequestId = request(MTPmessages_SendMessage(
|
|
|
|
MTP_flags(sendFlags),
|
|
|
|
peer->input,
|
|
|
|
MTP_int(message.replyTo),
|
|
|
|
msgText,
|
|
|
|
MTP_long(randomId),
|
|
|
|
MTPnullMarkup,
|
|
|
|
sentEntities
|
|
|
|
)).done([=](const MTPUpdates &result) {
|
|
|
|
applyUpdates(result, randomId);
|
2018-06-26 15:59:05 +00:00
|
|
|
history->clearSentDraftText(QString());
|
|
|
|
}).fail([=](const RPCError &error) {
|
2019-03-09 14:56:11 +00:00
|
|
|
if (error.type() == qstr("MESSAGE_EMPTY")) {
|
|
|
|
lastMessage->destroy();
|
|
|
|
} else {
|
|
|
|
sendMessageFail(error);
|
|
|
|
}
|
2018-06-26 15:59:05 +00:00
|
|
|
history->clearSentDraftText(QString());
|
2018-06-26 15:34:38 +00:00
|
|
|
}).afterRequest(history->sendRequestId
|
|
|
|
).send();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (const auto main = App::main()) {
|
|
|
|
main->finishForwarding(history);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-12-28 11:42:58 +00:00
|
|
|
void ApiWrap::sendBotStart(not_null<UserData*> bot, PeerData *chat) {
|
2018-12-04 11:46:07 +00:00
|
|
|
Expects(bot->botInfo != nullptr);
|
2018-12-28 11:42:58 +00:00
|
|
|
Expects(chat == nullptr || !bot->botInfo->startGroupToken.isEmpty());
|
|
|
|
|
|
|
|
if (chat && chat->isChannel() && !chat->isMegagroup()) {
|
|
|
|
ShowAddParticipantsError("USER_BOT", chat, { 1, bot });
|
|
|
|
return;
|
|
|
|
}
|
2018-12-04 11:46:07 +00:00
|
|
|
|
2018-12-28 11:42:58 +00:00
|
|
|
auto &info = bot->botInfo;
|
|
|
|
auto &token = chat ? info->startGroupToken : info->startToken;
|
2018-12-04 11:46:07 +00:00
|
|
|
if (token.isEmpty()) {
|
2019-01-18 12:27:37 +00:00
|
|
|
auto message = ApiWrap::MessageToSend(_session->data().history(bot));
|
2018-12-04 11:46:07 +00:00
|
|
|
message.textWithTags = { qsl("/start"), TextWithTags::Tags() };
|
|
|
|
sendMessage(std::move(message));
|
2018-12-28 11:42:58 +00:00
|
|
|
return;
|
2018-12-04 11:46:07 +00:00
|
|
|
}
|
2018-12-28 11:42:58 +00:00
|
|
|
const auto randomId = rand_value<uint64>();
|
|
|
|
request(MTPmessages_StartBot(
|
|
|
|
bot->inputUser,
|
|
|
|
chat ? chat->input : MTP_inputPeerEmpty(),
|
|
|
|
MTP_long(randomId),
|
|
|
|
MTP_string(base::take(token))
|
|
|
|
)).done([=](const MTPUpdates &result) {
|
|
|
|
applyUpdates(result);
|
|
|
|
}).fail([=](const RPCError &error) {
|
|
|
|
if (chat) {
|
|
|
|
ShowAddParticipantsError(error.type(), chat, { 1, bot });
|
|
|
|
}
|
|
|
|
}).send();
|
2018-12-04 11:46:07 +00:00
|
|
|
}
|
|
|
|
|
2018-06-26 15:34:38 +00:00
|
|
|
void ApiWrap::sendInlineResult(
|
|
|
|
not_null<UserData*> bot,
|
|
|
|
not_null<InlineBots::Result*> data,
|
|
|
|
const SendOptions &options) {
|
2018-09-06 11:13:54 +00:00
|
|
|
sendAction(options);
|
2018-06-26 15:34:38 +00:00
|
|
|
|
|
|
|
const auto history = options.history;
|
|
|
|
const auto peer = history->peer;
|
|
|
|
const auto newId = FullMsgId(peerToChannel(peer->id), clientMsgId());
|
|
|
|
const auto randomId = rand_value<uint64>();
|
|
|
|
|
|
|
|
auto flags = NewMessageFlags(peer) | MTPDmessage::Flag::f_media;
|
|
|
|
auto sendFlags = MTPmessages_SendInlineBotResult::Flag::f_clear_draft | 0;
|
|
|
|
if (options.replyTo) {
|
|
|
|
flags |= MTPDmessage::Flag::f_reply_to_msg_id;
|
|
|
|
sendFlags |= MTPmessages_SendInlineBotResult::Flag::f_reply_to_msg_id;
|
|
|
|
}
|
|
|
|
bool channelPost = peer->isChannel() && !peer->isMegagroup();
|
|
|
|
bool silentPost = channelPost && _session->data().notifySilentPosts(peer);
|
|
|
|
if (channelPost) {
|
|
|
|
flags |= MTPDmessage::Flag::f_views;
|
|
|
|
flags |= MTPDmessage::Flag::f_post;
|
|
|
|
}
|
|
|
|
if (!channelPost) {
|
|
|
|
flags |= MTPDmessage::Flag::f_from_id;
|
|
|
|
} else if (peer->asChannel()->addsSignature()) {
|
|
|
|
flags |= MTPDmessage::Flag::f_post_author;
|
|
|
|
}
|
|
|
|
if (silentPost) {
|
|
|
|
sendFlags |= MTPmessages_SendInlineBotResult::Flag::f_silent;
|
|
|
|
}
|
|
|
|
if (bot) {
|
|
|
|
flags |= MTPDmessage::Flag::f_via_bot_id;
|
|
|
|
}
|
|
|
|
|
2018-09-06 11:13:54 +00:00
|
|
|
auto messageFromId = channelPost ? 0 : _session->userId();
|
2018-06-26 15:34:38 +00:00
|
|
|
auto messagePostAuthor = channelPost
|
2018-09-06 11:13:54 +00:00
|
|
|
? App::peerName(_session->user())
|
2018-06-26 15:34:38 +00:00
|
|
|
: QString();
|
|
|
|
MTPint messageDate = MTP_int(unixtime());
|
|
|
|
UserId messageViaBotId = bot ? peerToUser(bot->id) : 0;
|
|
|
|
MsgId messageId = newId.msg;
|
|
|
|
|
|
|
|
App::historyRegRandom(randomId, newId);
|
|
|
|
|
|
|
|
data->addToHistory(
|
|
|
|
history,
|
|
|
|
flags,
|
|
|
|
messageId,
|
|
|
|
messageFromId,
|
|
|
|
messageDate,
|
|
|
|
messageViaBotId,
|
|
|
|
options.replyTo,
|
|
|
|
messagePostAuthor);
|
|
|
|
|
2018-06-26 15:59:05 +00:00
|
|
|
history->clearCloudDraft();
|
|
|
|
history->setSentDraftText(QString());
|
|
|
|
|
2018-06-26 15:34:38 +00:00
|
|
|
history->sendRequestId = request(MTPmessages_SendInlineBotResult(
|
|
|
|
MTP_flags(sendFlags),
|
|
|
|
peer->input,
|
|
|
|
MTP_int(options.replyTo),
|
|
|
|
MTP_long(randomId),
|
|
|
|
MTP_long(data->getQueryId()),
|
|
|
|
MTP_string(data->getId())
|
|
|
|
)).done([=](const MTPUpdates &result) {
|
|
|
|
applyUpdates(result, randomId);
|
2018-06-26 15:59:05 +00:00
|
|
|
history->clearSentDraftText(QString());
|
|
|
|
}).fail([=](const RPCError &error) {
|
|
|
|
sendMessageFail(error);
|
|
|
|
history->clearSentDraftText(QString());
|
2018-06-26 15:34:38 +00:00
|
|
|
}).afterRequest(history->sendRequestId
|
|
|
|
).send();
|
|
|
|
|
|
|
|
if (const auto main = App::main()) {
|
|
|
|
main->finishForwarding(history);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-16 17:31:07 +00:00
|
|
|
void ApiWrap::sendExistingDocument(
|
|
|
|
not_null<DocumentData*> document,
|
|
|
|
Data::FileOrigin origin,
|
|
|
|
TextWithEntities caption,
|
|
|
|
const SendOptions &options) {
|
2018-09-06 11:13:54 +00:00
|
|
|
sendAction(options);
|
2018-07-16 17:31:07 +00:00
|
|
|
|
|
|
|
const auto history = options.history;
|
|
|
|
const auto peer = history->peer;
|
|
|
|
const auto newId = FullMsgId(peerToChannel(peer->id), clientMsgId());
|
|
|
|
const auto randomId = rand_value<uint64>();
|
|
|
|
|
|
|
|
auto flags = NewMessageFlags(peer) | MTPDmessage::Flag::f_media;
|
|
|
|
auto sendFlags = MTPmessages_SendMedia::Flags(0);
|
|
|
|
if (options.replyTo) {
|
|
|
|
flags |= MTPDmessage::Flag::f_reply_to_msg_id;
|
|
|
|
sendFlags |= MTPmessages_SendMedia::Flag::f_reply_to_msg_id;
|
|
|
|
}
|
|
|
|
bool channelPost = peer->isChannel() && !peer->isMegagroup();
|
2018-09-06 11:13:54 +00:00
|
|
|
bool silentPost = channelPost
|
|
|
|
&& _session->data().notifySilentPosts(peer);
|
2018-07-16 17:31:07 +00:00
|
|
|
if (channelPost) {
|
|
|
|
flags |= MTPDmessage::Flag::f_views;
|
|
|
|
flags |= MTPDmessage::Flag::f_post;
|
|
|
|
}
|
|
|
|
if (!channelPost) {
|
|
|
|
flags |= MTPDmessage::Flag::f_from_id;
|
|
|
|
} else if (peer->asChannel()->addsSignature()) {
|
|
|
|
flags |= MTPDmessage::Flag::f_post_author;
|
|
|
|
}
|
|
|
|
if (silentPost) {
|
|
|
|
sendFlags |= MTPmessages_SendMedia::Flag::f_silent;
|
|
|
|
}
|
2018-09-06 11:13:54 +00:00
|
|
|
auto messageFromId = channelPost ? 0 : _session->userId();
|
2018-07-16 17:31:07 +00:00
|
|
|
auto messagePostAuthor = channelPost
|
2018-09-06 11:13:54 +00:00
|
|
|
? App::peerName(_session->user())
|
|
|
|
: QString();
|
2018-07-16 17:31:07 +00:00
|
|
|
|
|
|
|
TextUtilities::Trim(caption);
|
|
|
|
auto sentEntities = TextUtilities::EntitiesToMTP(
|
|
|
|
caption.entities,
|
|
|
|
TextUtilities::ConvertOption::SkipLocal);
|
|
|
|
if (!sentEntities.v.isEmpty()) {
|
|
|
|
sendFlags |= MTPmessages_SendMedia::Flag::f_entities;
|
|
|
|
}
|
2018-07-16 19:41:43 +00:00
|
|
|
const auto replyTo = options.replyTo;
|
|
|
|
const auto captionText = caption.text;
|
2018-07-16 17:31:07 +00:00
|
|
|
|
|
|
|
App::historyRegRandom(randomId, newId);
|
|
|
|
|
|
|
|
history->addNewDocument(
|
|
|
|
newId.msg,
|
|
|
|
flags,
|
|
|
|
0,
|
2018-07-16 19:41:43 +00:00
|
|
|
replyTo,
|
2018-07-16 17:31:07 +00:00
|
|
|
unixtime(),
|
|
|
|
messageFromId,
|
|
|
|
messagePostAuthor,
|
|
|
|
document,
|
|
|
|
caption,
|
|
|
|
MTPnullMarkup);
|
|
|
|
|
2018-07-16 19:41:43 +00:00
|
|
|
auto failHandler = std::make_shared<Fn<void(const RPCError&)>>();
|
2018-07-16 17:31:07 +00:00
|
|
|
auto performRequest = [=] {
|
|
|
|
history->sendRequestId = request(MTPmessages_SendMedia(
|
|
|
|
MTP_flags(sendFlags),
|
|
|
|
peer->input,
|
|
|
|
MTP_int(replyTo),
|
|
|
|
MTP_inputMediaDocument(
|
|
|
|
MTP_flags(0),
|
|
|
|
document->mtpInput(),
|
|
|
|
MTPint()),
|
|
|
|
MTP_string(captionText),
|
|
|
|
MTP_long(randomId),
|
|
|
|
MTPnullMarkup,
|
|
|
|
sentEntities
|
|
|
|
)).done([=](const MTPUpdates &result) {
|
|
|
|
applyUpdates(result, randomId);
|
|
|
|
}).fail(
|
|
|
|
base::duplicate(*failHandler)
|
|
|
|
).afterRequest(history->sendRequestId
|
|
|
|
).send();
|
|
|
|
};
|
|
|
|
*failHandler = [=](const RPCError &error) {
|
|
|
|
if (error.code() == 400
|
|
|
|
&& error.type().startsWith(qstr("FILE_REFERENCE_"))) {
|
|
|
|
const auto current = document->fileReference();
|
|
|
|
auto refreshed = [=](const Data::UpdatedFileReferences &data) {
|
|
|
|
if (document->fileReference() != current) {
|
|
|
|
performRequest();
|
|
|
|
} else {
|
|
|
|
sendMessageFail(error);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
refreshFileReference(origin, std::move(refreshed));
|
|
|
|
} else {
|
|
|
|
sendMessageFail(error);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
performRequest();
|
|
|
|
|
|
|
|
if (const auto main = App::main()) {
|
|
|
|
main->finishForwarding(history);
|
|
|
|
if (document->sticker()) {
|
|
|
|
main->incrementSticker(document);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-12-19 16:57:42 +00:00
|
|
|
void ApiWrap::uploadAlbumMedia(
|
|
|
|
not_null<HistoryItem*> item,
|
|
|
|
const MessageGroupId &groupId,
|
2017-12-25 14:17:00 +00:00
|
|
|
const MTPInputMedia &media) {
|
2017-12-19 16:57:42 +00:00
|
|
|
const auto localId = item->fullId();
|
2018-04-07 08:47:08 +00:00
|
|
|
const auto failed = [=] {
|
2017-12-19 16:57:42 +00:00
|
|
|
|
|
|
|
};
|
|
|
|
request(MTPmessages_UploadMedia(
|
|
|
|
item->history()->peer->input,
|
|
|
|
media
|
|
|
|
)).done([=](const MTPMessageMedia &result) {
|
|
|
|
const auto item = App::histItemById(localId);
|
|
|
|
if (!item) {
|
|
|
|
failed();
|
2018-05-08 18:06:56 +00:00
|
|
|
return;
|
2017-12-19 16:57:42 +00:00
|
|
|
}
|
2018-01-14 16:02:25 +00:00
|
|
|
if (const auto media = item->media()) {
|
|
|
|
if (const auto photo = media->photo()) {
|
2017-12-25 14:17:00 +00:00
|
|
|
photo->setWaitingForAlbum();
|
2018-01-14 16:02:25 +00:00
|
|
|
} else if (const auto document = media->document()) {
|
2017-12-25 14:17:00 +00:00
|
|
|
document->setWaitingForAlbum();
|
|
|
|
}
|
|
|
|
}
|
2017-12-19 16:57:42 +00:00
|
|
|
|
|
|
|
switch (result.type()) {
|
|
|
|
case mtpc_messageMediaPhoto: {
|
|
|
|
const auto &data = result.c_messageMediaPhoto();
|
|
|
|
if (data.vphoto.type() != mtpc_photo) {
|
|
|
|
failed();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
const auto &photo = data.vphoto.c_photo();
|
|
|
|
const auto flags = MTPDinputMediaPhoto::Flags(0)
|
|
|
|
| (data.has_ttl_seconds()
|
|
|
|
? MTPDinputMediaPhoto::Flag::f_ttl_seconds
|
|
|
|
: MTPDinputMediaPhoto::Flag(0));
|
|
|
|
const auto media = MTP_inputMediaPhoto(
|
|
|
|
MTP_flags(flags),
|
2018-07-13 16:49:46 +00:00
|
|
|
MTP_inputPhoto(
|
|
|
|
photo.vid,
|
|
|
|
photo.vaccess_hash,
|
|
|
|
photo.vfile_reference),
|
2017-12-19 16:57:42 +00:00
|
|
|
data.has_ttl_seconds() ? data.vttl_seconds : MTPint());
|
2017-12-25 14:17:00 +00:00
|
|
|
sendAlbumWithUploaded(item, groupId, media);
|
2017-12-19 16:57:42 +00:00
|
|
|
} break;
|
|
|
|
|
|
|
|
case mtpc_messageMediaDocument: {
|
|
|
|
const auto &data = result.c_messageMediaDocument();
|
|
|
|
if (data.vdocument.type() != mtpc_document) {
|
|
|
|
failed();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
const auto &document = data.vdocument.c_document();
|
|
|
|
const auto flags = MTPDinputMediaDocument::Flags(0)
|
|
|
|
| (data.has_ttl_seconds()
|
|
|
|
? MTPDinputMediaDocument::Flag::f_ttl_seconds
|
|
|
|
: MTPDinputMediaDocument::Flag(0));
|
|
|
|
const auto media = MTP_inputMediaDocument(
|
|
|
|
MTP_flags(flags),
|
2018-07-13 16:49:46 +00:00
|
|
|
MTP_inputDocument(
|
|
|
|
document.vid,
|
|
|
|
document.vaccess_hash,
|
|
|
|
document.vfile_reference),
|
2017-12-19 16:57:42 +00:00
|
|
|
data.has_ttl_seconds() ? data.vttl_seconds : MTPint());
|
2017-12-25 14:17:00 +00:00
|
|
|
sendAlbumWithUploaded(item, groupId, media);
|
2017-12-19 16:57:42 +00:00
|
|
|
} break;
|
|
|
|
}
|
|
|
|
}).fail([=](const RPCError &error) {
|
|
|
|
failed();
|
|
|
|
}).send();
|
|
|
|
}
|
|
|
|
|
2017-12-25 14:17:00 +00:00
|
|
|
void ApiWrap::sendMedia(
|
2017-12-19 16:57:42 +00:00
|
|
|
not_null<HistoryItem*> item,
|
|
|
|
const MTPInputMedia &media,
|
|
|
|
bool silent) {
|
|
|
|
const auto randomId = rand_value<uint64>();
|
2017-12-25 14:17:00 +00:00
|
|
|
App::historyRegRandom(randomId, item->fullId());
|
2017-12-19 16:57:42 +00:00
|
|
|
|
2017-12-25 14:17:00 +00:00
|
|
|
sendMediaWithRandomId(item, media, silent, randomId);
|
2017-12-19 16:57:42 +00:00
|
|
|
}
|
|
|
|
|
2017-12-25 14:17:00 +00:00
|
|
|
void ApiWrap::sendMediaWithRandomId(
|
2017-12-19 16:57:42 +00:00
|
|
|
not_null<HistoryItem*> item,
|
|
|
|
const MTPInputMedia &media,
|
2017-12-25 14:17:00 +00:00
|
|
|
bool silent,
|
|
|
|
uint64 randomId) {
|
2017-12-19 16:57:42 +00:00
|
|
|
const auto history = item->history();
|
|
|
|
const auto replyTo = item->replyToId();
|
2018-01-18 13:59:22 +00:00
|
|
|
|
|
|
|
auto caption = item->originalText();
|
|
|
|
TextUtilities::Trim(caption);
|
|
|
|
auto sentEntities = TextUtilities::EntitiesToMTP(
|
|
|
|
caption.entities,
|
|
|
|
TextUtilities::ConvertOption::SkipLocal);
|
|
|
|
|
2017-12-19 16:57:42 +00:00
|
|
|
const auto flags = MTPmessages_SendMedia::Flags(0)
|
|
|
|
| (replyTo
|
|
|
|
? MTPmessages_SendMedia::Flag::f_reply_to_msg_id
|
|
|
|
: MTPmessages_SendMedia::Flag(0))
|
|
|
|
| (IsSilentPost(item, silent)
|
|
|
|
? MTPmessages_SendMedia::Flag::f_silent
|
2018-01-18 13:59:22 +00:00
|
|
|
: MTPmessages_SendMedia::Flag(0))
|
|
|
|
| (!sentEntities.v.isEmpty()
|
|
|
|
? MTPmessages_SendMedia::Flag::f_entities
|
2017-12-19 16:57:42 +00:00
|
|
|
: MTPmessages_SendMedia::Flag(0));
|
2018-01-18 13:59:22 +00:00
|
|
|
|
2017-12-07 13:02:24 +00:00
|
|
|
history->sendRequestId = request(MTPmessages_SendMedia(
|
2017-12-19 16:57:42 +00:00
|
|
|
MTP_flags(flags),
|
|
|
|
history->peer->input,
|
|
|
|
MTP_int(replyTo),
|
|
|
|
media,
|
2018-01-18 13:59:22 +00:00
|
|
|
MTP_string(caption.text),
|
2017-12-07 13:02:24 +00:00
|
|
|
MTP_long(randomId),
|
2018-01-18 11:04:50 +00:00
|
|
|
MTPnullMarkup,
|
2018-01-18 13:59:22 +00:00
|
|
|
sentEntities
|
2017-12-19 16:57:42 +00:00
|
|
|
)).done([=](const MTPUpdates &result) { applyUpdates(result);
|
|
|
|
}).fail([=](const RPCError &error) { sendMessageFail(error);
|
|
|
|
}).afterRequest(history->sendRequestId
|
2017-12-07 13:02:24 +00:00
|
|
|
).send();
|
2017-12-19 16:57:42 +00:00
|
|
|
}
|
2017-12-07 13:02:24 +00:00
|
|
|
|
2017-12-25 14:17:00 +00:00
|
|
|
void ApiWrap::sendAlbumWithUploaded(
|
|
|
|
not_null<HistoryItem*> item,
|
2017-12-19 16:57:42 +00:00
|
|
|
const MessageGroupId &groupId,
|
2017-12-25 14:17:00 +00:00
|
|
|
const MTPInputMedia &media) {
|
|
|
|
const auto localId = item->fullId();
|
|
|
|
const auto randomId = rand_value<uint64>();
|
|
|
|
App::historyRegRandom(randomId, localId);
|
|
|
|
|
2017-12-19 16:57:42 +00:00
|
|
|
const auto albumIt = _sendingAlbums.find(groupId.raw());
|
|
|
|
Assert(albumIt != _sendingAlbums.end());
|
|
|
|
const auto &album = albumIt->second;
|
2018-12-26 07:22:36 +00:00
|
|
|
album->fillMedia(item, media, randomId);
|
2017-12-25 14:17:00 +00:00
|
|
|
sendAlbumIfReady(album.get());
|
|
|
|
}
|
|
|
|
|
|
|
|
void ApiWrap::sendAlbumWithCancelled(
|
|
|
|
not_null<HistoryItem*> item,
|
|
|
|
const MessageGroupId &groupId) {
|
|
|
|
const auto albumIt = _sendingAlbums.find(groupId.raw());
|
2018-03-09 21:44:24 +00:00
|
|
|
if (albumIt == _sendingAlbums.end()) {
|
2018-01-03 09:06:02 +00:00
|
|
|
// Sometimes we destroy item being sent already after the album
|
|
|
|
// was sent successfully. For example the message could be loaded
|
|
|
|
// from server (by messages.getHistory or updateNewMessage) and
|
|
|
|
// added to history and after that updateMessageID was received with
|
|
|
|
// the same message id, in this case we destroy a detached local
|
|
|
|
// item and sendAlbumWithCancelled is called for already sent album.
|
|
|
|
return;
|
|
|
|
}
|
2017-12-25 14:17:00 +00:00
|
|
|
const auto &album = albumIt->second;
|
2018-12-26 07:22:36 +00:00
|
|
|
album->removeItem(item);
|
2017-12-25 14:17:00 +00:00
|
|
|
sendAlbumIfReady(album.get());
|
|
|
|
}
|
|
|
|
|
|
|
|
void ApiWrap::sendAlbumIfReady(not_null<SendingAlbum*> album) {
|
|
|
|
const auto groupId = album->groupId;
|
|
|
|
if (album->items.empty()) {
|
|
|
|
_sendingAlbums.remove(groupId);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
auto sample = (HistoryItem*)nullptr;
|
|
|
|
auto medias = QVector<MTPInputSingleMedia>();
|
|
|
|
medias.reserve(album->items.size());
|
2017-12-19 16:57:42 +00:00
|
|
|
for (const auto &item : album->items) {
|
|
|
|
if (!item.media) {
|
2017-12-25 14:17:00 +00:00
|
|
|
return;
|
|
|
|
} else if (!sample) {
|
|
|
|
sample = App::histItemById(item.msgId);
|
2017-12-19 16:57:42 +00:00
|
|
|
}
|
2017-12-25 14:17:00 +00:00
|
|
|
medias.push_back(*item.media);
|
2017-12-19 16:57:42 +00:00
|
|
|
}
|
2017-12-25 14:17:00 +00:00
|
|
|
if (!sample) {
|
|
|
|
_sendingAlbums.remove(groupId);
|
|
|
|
return;
|
|
|
|
} else if (medias.size() < 2) {
|
|
|
|
const auto &single = medias.front().c_inputSingleMedia();
|
|
|
|
sendMediaWithRandomId(
|
|
|
|
sample,
|
|
|
|
single.vmedia,
|
|
|
|
album->silent,
|
|
|
|
single.vrandom_id.v);
|
|
|
|
_sendingAlbums.remove(groupId);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
const auto history = sample->history();
|
|
|
|
const auto replyTo = sample->replyToId();
|
|
|
|
const auto flags = MTPmessages_SendMultiMedia::Flags(0)
|
|
|
|
| (replyTo
|
|
|
|
? MTPmessages_SendMultiMedia::Flag::f_reply_to_msg_id
|
|
|
|
: MTPmessages_SendMultiMedia::Flag(0))
|
|
|
|
| (IsSilentPost(sample, album->silent)
|
|
|
|
? MTPmessages_SendMultiMedia::Flag::f_silent
|
|
|
|
: MTPmessages_SendMultiMedia::Flag(0));
|
|
|
|
history->sendRequestId = request(MTPmessages_SendMultiMedia(
|
|
|
|
MTP_flags(flags),
|
|
|
|
history->peer->input,
|
|
|
|
MTP_int(replyTo),
|
|
|
|
MTP_vector<MTPInputSingleMedia>(medias)
|
|
|
|
)).done([=](const MTPUpdates &result) {
|
|
|
|
_sendingAlbums.remove(groupId);
|
|
|
|
applyUpdates(result);
|
|
|
|
}).fail([=](const RPCError &error) {
|
|
|
|
_sendingAlbums.remove(groupId);
|
|
|
|
sendMessageFail(error);
|
|
|
|
}).afterRequest(history->sendRequestId
|
|
|
|
).send();
|
2017-12-07 13:02:24 +00:00
|
|
|
}
|
|
|
|
|
2018-06-26 15:34:38 +00:00
|
|
|
FileLoadTo ApiWrap::fileLoadTaskOptions(const SendOptions &options) const {
|
|
|
|
const auto peer = options.history->peer;
|
|
|
|
return FileLoadTo(
|
|
|
|
peer->id,
|
|
|
|
_session->data().notifySilentPosts(peer),
|
|
|
|
options.replyTo);
|
|
|
|
}
|
|
|
|
|
2018-09-06 10:18:49 +00:00
|
|
|
void ApiWrap::requestSupportContact(FnMut<void(const MTPUser &)> callback) {
|
|
|
|
_supportContactCallbacks.push_back(std::move(callback));
|
|
|
|
if (_supportContactCallbacks.size() > 1) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
request(MTPhelp_GetSupport(
|
|
|
|
)).done([=](const MTPhelp_Support &result) {
|
|
|
|
result.match([&](const MTPDhelp_support &data) {
|
|
|
|
for (auto &handler : base::take(_supportContactCallbacks)) {
|
|
|
|
handler(data.vuser);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}).fail([=](const RPCError &error) {
|
|
|
|
_supportContactCallbacks.clear();
|
|
|
|
}).send();
|
|
|
|
}
|
|
|
|
|
2018-09-06 11:13:54 +00:00
|
|
|
void ApiWrap::uploadPeerPhoto(not_null<PeerData*> peer, QImage &&image) {
|
2019-01-14 06:34:51 +00:00
|
|
|
peer = peer->migrateToOrMe();
|
2018-09-06 11:13:54 +00:00
|
|
|
const auto ready = PreparePeerPhoto(peer->id, std::move(image));
|
|
|
|
|
|
|
|
const auto fakeId = FullMsgId(peerToChannel(peer->id), clientMsgId());
|
|
|
|
const auto already = ranges::find(
|
|
|
|
_peerPhotoUploads,
|
|
|
|
peer,
|
|
|
|
[](const auto &pair) { return pair.second; });
|
|
|
|
if (already != end(_peerPhotoUploads)) {
|
|
|
|
_session->uploader().cancel(already->first);
|
|
|
|
_peerPhotoUploads.erase(already);
|
|
|
|
}
|
|
|
|
_peerPhotoUploads.emplace(fakeId, peer);
|
|
|
|
_session->uploader().uploadMedia(fakeId, ready);
|
|
|
|
}
|
|
|
|
|
|
|
|
void ApiWrap::photoUploadReady(
|
|
|
|
const FullMsgId &msgId,
|
|
|
|
const MTPInputFile &file) {
|
|
|
|
if (const auto maybePeer = _peerPhotoUploads.take(msgId)) {
|
|
|
|
const auto peer = *maybePeer;
|
|
|
|
const auto applier = [=](const MTPUpdates &result) {
|
|
|
|
applyUpdates(result);
|
|
|
|
};
|
|
|
|
if (peer->isSelf()) {
|
|
|
|
request(MTPphotos_UploadProfilePhoto(
|
|
|
|
file
|
|
|
|
)).done([=](const MTPphotos_Photo &result) {
|
|
|
|
result.match([&](const MTPDphotos_photo &data) {
|
2019-01-18 12:27:37 +00:00
|
|
|
_session->data().processPhoto(data.vphoto);
|
|
|
|
_session->data().processUsers(data.vusers);
|
2018-09-06 11:13:54 +00:00
|
|
|
});
|
|
|
|
}).send();
|
|
|
|
} else if (const auto chat = peer->asChat()) {
|
2019-01-18 12:27:37 +00:00
|
|
|
const auto history = _session->data().history(chat);
|
2018-09-06 11:13:54 +00:00
|
|
|
history->sendRequestId = request(MTPmessages_EditChatPhoto(
|
|
|
|
chat->inputChat,
|
|
|
|
MTP_inputChatUploadedPhoto(file)
|
|
|
|
)).done(applier).afterRequest(history->sendRequestId).send();
|
|
|
|
} else if (const auto channel = peer->asChannel()) {
|
2019-01-18 12:27:37 +00:00
|
|
|
const auto history = _session->data().history(channel);
|
2018-09-06 11:13:54 +00:00
|
|
|
history->sendRequestId = request(MTPchannels_EditPhoto(
|
|
|
|
channel->inputChannel,
|
|
|
|
MTP_inputChatUploadedPhoto(file)
|
|
|
|
)).done(applier).afterRequest(history->sendRequestId).send();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void ApiWrap::clearPeerPhoto(not_null<PhotoData*> photo) {
|
2019-01-03 12:36:01 +00:00
|
|
|
const auto self = _session->user();
|
2018-09-06 11:13:54 +00:00
|
|
|
if (self->userpicPhotoId() == photo->id) {
|
|
|
|
request(MTPphotos_UpdateProfilePhoto(
|
|
|
|
MTP_inputPhotoEmpty()
|
|
|
|
)).done([=](const MTPUserProfilePhoto &result) {
|
|
|
|
self->setPhoto(result);
|
|
|
|
}).send();
|
|
|
|
} else if (photo->peer && photo->peer->userpicPhotoId() == photo->id) {
|
|
|
|
const auto applier = [=](const MTPUpdates &result) {
|
|
|
|
applyUpdates(result);
|
|
|
|
};
|
|
|
|
if (const auto chat = photo->peer->asChat()) {
|
|
|
|
request(MTPmessages_EditChatPhoto(
|
|
|
|
chat->inputChat,
|
|
|
|
MTP_inputChatPhotoEmpty()
|
|
|
|
)).done(applier).send();
|
|
|
|
} else if (const auto channel = photo->peer->asChannel()) {
|
|
|
|
request(MTPchannels_EditPhoto(
|
|
|
|
channel->inputChannel,
|
|
|
|
MTP_inputChatPhotoEmpty()
|
|
|
|
)).done(applier).send();
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
request(MTPphotos_DeletePhotos(
|
|
|
|
MTP_vector<MTPInputPhoto>(1, photo->mtpInput())
|
|
|
|
)).send();
|
|
|
|
_session->storage().remove(Storage::UserPhotosRemoveOne(
|
|
|
|
self->bareId(),
|
|
|
|
photo->id));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-09-09 12:10:54 +00:00
|
|
|
void ApiWrap::reloadPasswordState() {
|
|
|
|
if (_passwordRequestId) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
_passwordRequestId = request(MTPaccount_GetPassword(
|
|
|
|
)).done([=](const MTPaccount_Password &result) {
|
|
|
|
_passwordRequestId = 0;
|
|
|
|
result.match([&](const MTPDaccount_password &data) {
|
|
|
|
openssl::AddRandomSeed(bytes::make_span(data.vsecure_random.v));
|
|
|
|
if (_passwordState) {
|
|
|
|
*_passwordState = Core::ParseCloudPasswordState(data);
|
|
|
|
} else {
|
|
|
|
_passwordState = std::make_unique<Core::CloudPasswordState>(
|
|
|
|
Core::ParseCloudPasswordState(data));
|
|
|
|
}
|
|
|
|
_passwordStateChanges.fire_copy(*_passwordState);
|
|
|
|
});
|
|
|
|
}).fail([=](const RPCError &error) {
|
|
|
|
_passwordRequestId = 0;
|
|
|
|
}).send();
|
|
|
|
}
|
|
|
|
|
|
|
|
void ApiWrap::clearUnconfirmedPassword() {
|
2018-11-07 09:39:31 +00:00
|
|
|
_passwordRequestId = request(MTPaccount_CancelPasswordEmail(
|
2018-09-09 12:10:54 +00:00
|
|
|
)).done([=](const MTPBool &result) {
|
|
|
|
_passwordRequestId = 0;
|
|
|
|
reloadPasswordState();
|
|
|
|
}).fail([=](const RPCError &error) {
|
|
|
|
_passwordRequestId = 0;
|
|
|
|
reloadPasswordState();
|
|
|
|
}).send();
|
|
|
|
}
|
|
|
|
|
|
|
|
rpl::producer<Core::CloudPasswordState> ApiWrap::passwordState() const {
|
|
|
|
return _passwordState
|
|
|
|
? _passwordStateChanges.events_starting_with_copy(*_passwordState)
|
2018-09-11 20:02:07 +00:00
|
|
|
: (_passwordStateChanges.events() | rpl::type_erased());
|
2018-09-09 12:10:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
auto ApiWrap::passwordStateCurrent() const
|
2019-01-18 09:24:10 +00:00
|
|
|
-> std::optional<Core::CloudPasswordState> {
|
2018-09-09 12:10:54 +00:00
|
|
|
return _passwordState
|
|
|
|
? base::make_optional(*_passwordState)
|
2018-09-21 16:28:46 +00:00
|
|
|
: std::nullopt;
|
2018-09-09 12:10:54 +00:00
|
|
|
}
|
|
|
|
|
2019-01-18 09:24:10 +00:00
|
|
|
void ApiWrap::reloadContactSignupSilent() {
|
|
|
|
if (_contactSignupSilentRequestId) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
const auto requestId = request(MTPaccount_GetContactSignUpNotification(
|
|
|
|
)).done([=](const MTPBool &result) {
|
|
|
|
_contactSignupSilentRequestId = 0;
|
|
|
|
const auto silent = mtpIsTrue(result);
|
|
|
|
_contactSignupSilent = silent;
|
|
|
|
_contactSignupSilentChanges.fire_copy(silent);
|
|
|
|
}).fail([=](const RPCError &error) {
|
|
|
|
_contactSignupSilentRequestId = 0;
|
|
|
|
}).send();
|
|
|
|
_contactSignupSilentRequestId = requestId;
|
|
|
|
}
|
|
|
|
|
|
|
|
rpl::producer<bool> ApiWrap::contactSignupSilent() const {
|
|
|
|
return _contactSignupSilent
|
|
|
|
? _contactSignupSilentChanges.events_starting_with_copy(
|
|
|
|
*_contactSignupSilent)
|
|
|
|
: (_contactSignupSilentChanges.events() | rpl::type_erased());
|
|
|
|
}
|
|
|
|
|
|
|
|
std::optional<bool> ApiWrap::contactSignupSilentCurrent() const {
|
|
|
|
return _contactSignupSilent;
|
|
|
|
}
|
|
|
|
|
|
|
|
void ApiWrap::saveContactSignupSilent(bool silent) {
|
|
|
|
request(base::take(_contactSignupSilentRequestId)).cancel();
|
|
|
|
|
|
|
|
const auto requestId = request(MTPaccount_SetContactSignUpNotification(
|
|
|
|
MTP_bool(silent)
|
|
|
|
)).done([=](const MTPBool &) {
|
|
|
|
_contactSignupSilentRequestId = 0;
|
|
|
|
_contactSignupSilent = silent;
|
|
|
|
_contactSignupSilentChanges.fire_copy(silent);
|
|
|
|
}).fail([=](const RPCError &error) {
|
|
|
|
_contactSignupSilentRequestId = 0;
|
|
|
|
}).send();
|
|
|
|
_contactSignupSilentRequestId = requestId;
|
|
|
|
}
|
|
|
|
|
2018-09-09 17:38:08 +00:00
|
|
|
void ApiWrap::saveSelfBio(const QString &text, FnMut<void()> done) {
|
|
|
|
if (_saveBioRequestId) {
|
|
|
|
if (text != _saveBioText) {
|
|
|
|
request(_saveBioRequestId).cancel();
|
|
|
|
} else {
|
|
|
|
if (done) {
|
|
|
|
_saveBioDone = std::move(done);
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
_saveBioText = text;
|
|
|
|
_saveBioDone = std::move(done);
|
|
|
|
_saveBioRequestId = request(MTPaccount_UpdateProfile(
|
|
|
|
MTP_flags(MTPaccount_UpdateProfile::Flag::f_about),
|
|
|
|
MTPstring(),
|
|
|
|
MTPstring(),
|
|
|
|
MTP_string(text)
|
|
|
|
)).done([=](const MTPUser &result) {
|
|
|
|
_saveBioRequestId = 0;
|
|
|
|
|
2019-01-18 12:27:37 +00:00
|
|
|
_session->data().processUsers(MTP_vector<MTPUser>(1, result));
|
2019-01-03 12:36:01 +00:00
|
|
|
_session->user()->setAbout(_saveBioText);
|
2018-09-09 17:38:08 +00:00
|
|
|
if (_saveBioDone) {
|
|
|
|
_saveBioDone();
|
|
|
|
}
|
|
|
|
}).fail([=](const RPCError &error) {
|
|
|
|
_saveBioRequestId = 0;
|
|
|
|
}).send();
|
|
|
|
}
|
|
|
|
|
2018-09-11 09:11:52 +00:00
|
|
|
void ApiWrap::reloadPrivacy(Privacy::Key key) {
|
|
|
|
if (_privacyRequestIds.contains(key)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
const auto requestId = request(MTPaccount_GetPrivacy(
|
|
|
|
Privacy::Input(key)
|
|
|
|
)).done([=](const MTPaccount_PrivacyRules &result) {
|
|
|
|
_privacyRequestIds.erase(key);
|
|
|
|
result.match([&](const MTPDaccount_privacyRules &data) {
|
2019-01-18 12:27:37 +00:00
|
|
|
_session->data().processUsers(data.vusers);
|
2018-09-11 09:11:52 +00:00
|
|
|
pushPrivacy(key, data.vrules.v);
|
|
|
|
});
|
|
|
|
}).fail([=](const RPCError &error) {
|
|
|
|
_privacyRequestIds.erase(key);
|
|
|
|
}).send();
|
2018-09-17 17:31:01 +00:00
|
|
|
_privacyRequestIds.emplace(key, requestId);
|
2018-09-11 09:11:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
auto ApiWrap::parsePrivacy(const QVector<MTPPrivacyRule> &rules)
|
|
|
|
-> Privacy {
|
|
|
|
using Option = Privacy::Option;
|
|
|
|
|
|
|
|
// This is simplified version of privacy rules interpretation.
|
|
|
|
// But it should be fine for all the apps
|
|
|
|
// that use the same subset of features.
|
|
|
|
auto result = Privacy();
|
|
|
|
auto optionSet = false;
|
|
|
|
const auto SetOption = [&](Option option) {
|
|
|
|
if (optionSet) return;
|
|
|
|
optionSet = true;
|
|
|
|
result.option = option;
|
|
|
|
};
|
|
|
|
auto &always = result.always;
|
|
|
|
auto &never = result.never;
|
|
|
|
const auto Feed = [&](const MTPPrivacyRule &rule) {
|
|
|
|
rule.match([&](const MTPDprivacyValueAllowAll &) {
|
|
|
|
SetOption(Option::Everyone);
|
|
|
|
}, [&](const MTPDprivacyValueAllowContacts &) {
|
|
|
|
SetOption(Option::Contacts);
|
|
|
|
}, [&](const MTPDprivacyValueAllowUsers &data) {
|
|
|
|
const auto &users = data.vusers.v;
|
|
|
|
always.reserve(always.size() + users.size());
|
|
|
|
for (const auto userId : users) {
|
2019-01-18 12:27:37 +00:00
|
|
|
const auto user = _session->data().user(UserId(userId.v));
|
2018-09-11 09:11:52 +00:00
|
|
|
if (!base::contains(never, user)
|
|
|
|
&& !base::contains(always, user)) {
|
|
|
|
always.push_back(user);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}, [&](const MTPDprivacyValueDisallowContacts &) {
|
|
|
|
// not supported
|
|
|
|
}, [&](const MTPDprivacyValueDisallowAll &) {
|
|
|
|
SetOption(Option::Nobody);
|
|
|
|
}, [&](const MTPDprivacyValueDisallowUsers &data) {
|
|
|
|
const auto &users = data.vusers.v;
|
|
|
|
never.reserve(never.size() + users.size());
|
|
|
|
for (const auto userId : users) {
|
2019-01-18 12:27:37 +00:00
|
|
|
const auto user = _session->data().user(UserId(userId.v));
|
2018-09-11 09:11:52 +00:00
|
|
|
if (!base::contains(always, user)
|
|
|
|
&& !base::contains(never, user)) {
|
|
|
|
never.push_back(user);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
};
|
|
|
|
for (const auto &rule : rules) {
|
|
|
|
Feed(rule);
|
|
|
|
}
|
|
|
|
Feed(MTP_privacyValueDisallowAll()); // disallow by default.
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
void ApiWrap::pushPrivacy(
|
|
|
|
Privacy::Key key,
|
|
|
|
const QVector<MTPPrivacyRule> &rules) {
|
|
|
|
const auto &saved = (_privacyValues[key] = parsePrivacy(rules));
|
|
|
|
const auto i = _privacyChanges.find(key);
|
|
|
|
if (i != end(_privacyChanges)) {
|
|
|
|
i->second.fire_copy(saved);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
auto ApiWrap::privacyValue(Privacy::Key key) -> rpl::producer<Privacy> {
|
|
|
|
if (const auto i = _privacyValues.find(key); i != end(_privacyValues)) {
|
|
|
|
return _privacyChanges[key].events_starting_with_copy(i->second);
|
|
|
|
} else {
|
|
|
|
return _privacyChanges[key].events();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-09-17 17:31:01 +00:00
|
|
|
void ApiWrap::reloadSelfDestruct() {
|
|
|
|
if (_selfDestructRequestId) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
_selfDestructRequestId = request(MTPaccount_GetAccountTTL(
|
|
|
|
)).done([=](const MTPAccountDaysTTL &result) {
|
|
|
|
_selfDestructRequestId = 0;
|
|
|
|
result.match([&](const MTPDaccountDaysTTL &data) {
|
|
|
|
setSelfDestructDays(data.vdays.v);
|
|
|
|
});
|
|
|
|
}).fail([=](const RPCError &error) {
|
|
|
|
_selfDestructRequestId = 0;
|
|
|
|
}).send();
|
|
|
|
}
|
|
|
|
|
|
|
|
rpl::producer<int> ApiWrap::selfDestructValue() const {
|
|
|
|
return _selfDestructDays
|
|
|
|
? _selfDestructChanges.events_starting_with_copy(*_selfDestructDays)
|
|
|
|
: (_selfDestructChanges.events() | rpl::type_erased());
|
|
|
|
}
|
|
|
|
|
|
|
|
void ApiWrap::saveSelfDestruct(int days) {
|
|
|
|
request(_selfDestructRequestId).cancel();
|
|
|
|
_selfDestructRequestId = request(MTPaccount_SetAccountTTL(
|
|
|
|
MTP_accountDaysTTL(MTP_int(days))
|
|
|
|
)).done([=](const MTPBool &result) {
|
|
|
|
_selfDestructRequestId = 0;
|
|
|
|
}).fail([=](const RPCError &result) {
|
|
|
|
_selfDestructRequestId = 0;
|
|
|
|
}).send();
|
|
|
|
setSelfDestructDays(days);
|
|
|
|
}
|
|
|
|
|
|
|
|
void ApiWrap::setSelfDestructDays(int days) {
|
|
|
|
_selfDestructDays = days;
|
|
|
|
_selfDestructChanges.fire_copy(days);
|
|
|
|
}
|
|
|
|
|
2018-12-20 16:02:44 +00:00
|
|
|
void ApiWrap::createPoll(
|
|
|
|
const PollData &data,
|
|
|
|
const SendOptions &options,
|
|
|
|
FnMut<void()> done,
|
|
|
|
FnMut<void(const RPCError &error)> fail) {
|
|
|
|
sendAction(options);
|
|
|
|
|
|
|
|
const auto history = options.history;
|
|
|
|
const auto peer = history->peer;
|
|
|
|
auto sendFlags = MTPmessages_SendMedia::Flags(0);
|
|
|
|
if (options.replyTo) {
|
|
|
|
sendFlags |= MTPmessages_SendMedia::Flag::f_reply_to_msg_id;
|
|
|
|
}
|
2019-02-17 11:43:46 +00:00
|
|
|
if (options.clearDraft) {
|
|
|
|
sendFlags |= MTPmessages_SendMedia::Flag::f_clear_draft;
|
|
|
|
history->clearLocalDraft();
|
|
|
|
history->clearCloudDraft();
|
|
|
|
}
|
2018-12-20 16:02:44 +00:00
|
|
|
const auto channelPost = peer->isChannel() && !peer->isMegagroup();
|
|
|
|
const auto silentPost = channelPost
|
|
|
|
&& _session->data().notifySilentPosts(peer);
|
|
|
|
if (silentPost) {
|
|
|
|
sendFlags |= MTPmessages_SendMedia::Flag::f_silent;
|
|
|
|
}
|
|
|
|
|
|
|
|
const auto replyTo = options.replyTo;
|
|
|
|
history->sendRequestId = request(MTPmessages_SendMedia(
|
|
|
|
MTP_flags(sendFlags),
|
|
|
|
peer->input,
|
|
|
|
MTP_int(replyTo),
|
|
|
|
MTP_inputMediaPoll(PollDataToMTP(&data)),
|
|
|
|
MTP_string(QString()),
|
|
|
|
MTP_long(rand_value<uint64>()),
|
|
|
|
MTPReplyMarkup(),
|
|
|
|
MTPVector<MTPMessageEntity>()
|
|
|
|
)).done([=, done = std::move(done)](const MTPUpdates &result) mutable {
|
|
|
|
applyUpdates(result);
|
|
|
|
done();
|
|
|
|
}).fail([=, fail = std::move(fail)](const RPCError &error) mutable {
|
|
|
|
fail(error);
|
|
|
|
}).afterRequest(history->sendRequestId
|
|
|
|
).send();
|
|
|
|
}
|
|
|
|
|
2018-12-19 11:20:04 +00:00
|
|
|
void ApiWrap::sendPollVotes(
|
|
|
|
FullMsgId itemId,
|
|
|
|
const std::vector<QByteArray> &options) {
|
|
|
|
if (_pollVotesRequestIds.contains(itemId)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
const auto item = App::histItemById(itemId);
|
2018-12-22 19:32:04 +00:00
|
|
|
const auto media = item ? item->media() : nullptr;
|
|
|
|
const auto poll = media ? media->poll() : nullptr;
|
2018-12-19 11:20:04 +00:00
|
|
|
if (!item) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-12-22 19:32:04 +00:00
|
|
|
const auto showSending = poll && !options.empty();
|
|
|
|
const auto hideSending = [=] {
|
|
|
|
if (showSending) {
|
|
|
|
if (const auto item = App::histItemById(itemId)) {
|
|
|
|
poll->sendingVote = QByteArray();
|
|
|
|
_session->data().requestItemRepaint(item);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
if (showSending) {
|
|
|
|
poll->sendingVote = options.front();
|
|
|
|
_session->data().requestItemRepaint(item);
|
|
|
|
}
|
|
|
|
|
2018-12-19 11:20:04 +00:00
|
|
|
auto prepared = QVector<MTPbytes>();
|
|
|
|
prepared.reserve(options.size());
|
|
|
|
ranges::transform(
|
|
|
|
options,
|
|
|
|
ranges::back_inserter(prepared),
|
|
|
|
[](const QByteArray &option) { return MTP_bytes(option); });
|
|
|
|
const auto requestId = request(MTPmessages_SendVote(
|
|
|
|
item->history()->peer->input,
|
|
|
|
MTP_int(item->id),
|
|
|
|
MTP_vector<MTPbytes>(prepared)
|
|
|
|
)).done([=](const MTPUpdates &result) {
|
|
|
|
_pollVotesRequestIds.erase(itemId);
|
2018-12-22 19:32:04 +00:00
|
|
|
hideSending();
|
2018-12-19 11:20:04 +00:00
|
|
|
applyUpdates(result);
|
|
|
|
}).fail([=](const RPCError &error) {
|
|
|
|
_pollVotesRequestIds.erase(itemId);
|
2018-12-22 19:32:04 +00:00
|
|
|
hideSending();
|
2018-12-19 11:20:04 +00:00
|
|
|
}).send();
|
|
|
|
_pollVotesRequestIds.emplace(itemId, requestId);
|
|
|
|
}
|
|
|
|
|
|
|
|
void ApiWrap::closePoll(FullMsgId itemId) {
|
|
|
|
if (_pollCloseRequestIds.contains(itemId)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
const auto item = App::histItemById(itemId);
|
|
|
|
const auto media = item ? item->media() : nullptr;
|
|
|
|
const auto poll = media ? media->poll() : nullptr;
|
|
|
|
if (!poll) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const auto requestId = request(MTPmessages_EditMessage(
|
|
|
|
MTP_flags(MTPmessages_EditMessage::Flag::f_media),
|
|
|
|
item->history()->peer->input,
|
|
|
|
MTP_int(item->id),
|
|
|
|
MTPstring(),
|
2018-12-20 16:02:44 +00:00
|
|
|
MTP_inputMediaPoll(PollDataToMTP(poll)),
|
2018-12-19 11:20:04 +00:00
|
|
|
MTPReplyMarkup(),
|
|
|
|
MTPVector<MTPMessageEntity>()
|
|
|
|
)).done([=](const MTPUpdates &result) {
|
|
|
|
_pollCloseRequestIds.erase(itemId);
|
|
|
|
applyUpdates(result);
|
|
|
|
}).fail([=](const RPCError &error) {
|
|
|
|
_pollCloseRequestIds.erase(itemId);
|
|
|
|
}).send();
|
|
|
|
_pollCloseRequestIds.emplace(itemId, requestId);
|
|
|
|
}
|
|
|
|
|
2018-12-24 17:03:53 +00:00
|
|
|
void ApiWrap::reloadPollResults(not_null<HistoryItem*> item) {
|
|
|
|
const auto itemId = item->fullId();
|
|
|
|
if (!IsServerMsgId(item->id)
|
|
|
|
|| _pollReloadRequestIds.contains(itemId)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
const auto requestId = request(MTPmessages_GetPollResults(
|
|
|
|
item->history()->peer->input,
|
|
|
|
MTP_int(item->id)
|
|
|
|
)).done([=](const MTPUpdates &result) {
|
|
|
|
_pollReloadRequestIds.erase(itemId);
|
|
|
|
applyUpdates(result);
|
|
|
|
}).fail([=](const RPCError &error) {
|
|
|
|
_pollReloadRequestIds.erase(itemId);
|
|
|
|
}).send();
|
|
|
|
_pollReloadRequestIds.emplace(itemId, requestId);
|
|
|
|
}
|
|
|
|
|
2017-12-07 13:02:24 +00:00
|
|
|
void ApiWrap::readServerHistory(not_null<History*> history) {
|
|
|
|
if (history->unreadCount()) {
|
|
|
|
readServerHistoryForce(history);
|
|
|
|
}
|
2018-06-26 18:03:45 +00:00
|
|
|
if (history->unreadMark()) {
|
|
|
|
changeDialogUnreadMark(history, false);
|
|
|
|
}
|
2017-12-07 13:02:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void ApiWrap::readServerHistoryForce(not_null<History*> history) {
|
|
|
|
const auto peer = history->peer;
|
2018-01-31 17:10:29 +00:00
|
|
|
const auto upTo = history->readInbox();
|
|
|
|
if (!upTo) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-12-07 13:02:24 +00:00
|
|
|
if (const auto channel = peer->asChannel()) {
|
|
|
|
if (!channel->amIn()) {
|
|
|
|
return; // no read request for channels that I didn't join
|
|
|
|
} else if (const auto migrateFrom = channel->migrateFrom()) {
|
2019-01-18 12:27:37 +00:00
|
|
|
if (const auto migrated = _session->data().historyLoaded(migrateFrom)) {
|
2017-12-07 13:02:24 +00:00
|
|
|
readServerHistory(migrated);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (_readRequests.contains(peer)) {
|
|
|
|
const auto i = _readRequestsPending.find(peer);
|
|
|
|
if (i == _readRequestsPending.cend()) {
|
|
|
|
_readRequestsPending.emplace(peer, upTo);
|
|
|
|
} else if (i->second < upTo) {
|
|
|
|
i->second = upTo;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
sendReadRequest(peer, upTo);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-02-02 12:51:18 +00:00
|
|
|
void ApiWrap::readFeed(
|
|
|
|
not_null<Data::Feed*> feed,
|
|
|
|
Data::MessagePosition position) {
|
|
|
|
const auto already = feed->unreadPosition();
|
|
|
|
if (already && already >= position) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
feed->setUnreadPosition(position);
|
|
|
|
if (!_feedReadsDelayed.contains(feed)) {
|
|
|
|
if (_feedReadsDelayed.empty()) {
|
|
|
|
_feedReadTimer.callOnce(kFeedReadTimeout);
|
|
|
|
}
|
2019-02-19 06:57:53 +00:00
|
|
|
_feedReadsDelayed.emplace(feed, crl::now() + kFeedReadTimeout);
|
2018-02-02 12:51:18 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void ApiWrap::readFeeds() {
|
|
|
|
auto delay = kFeedReadTimeout;
|
2019-02-19 06:57:53 +00:00
|
|
|
const auto now = crl::now();
|
2018-03-06 17:07:42 +00:00
|
|
|
//for (auto i = begin(_feedReadsDelayed); i != end(_feedReadsDelayed);) { // #feed
|
|
|
|
// const auto feed = i->first;
|
|
|
|
// const auto time = i->second;
|
|
|
|
// // Clang fails to capture structure-binded feed to lambda :(
|
|
|
|
// //const auto [feed, time] = *i;
|
|
|
|
// if (time > now) {
|
|
|
|
// accumulate_min(delay, time - now);
|
|
|
|
// ++i;
|
|
|
|
// } else if (_feedReadRequests.contains(feed)) {
|
|
|
|
// ++i;
|
|
|
|
// } else {
|
|
|
|
// const auto position = feed->unreadPosition();
|
|
|
|
// const auto requestId = request(MTPchannels_ReadFeed(
|
|
|
|
// MTP_int(feed->id()),
|
|
|
|
// MTP_feedPosition(
|
|
|
|
// MTP_int(position.date),
|
|
|
|
// MTP_peerChannel(MTP_int(position.fullId.channel)),
|
|
|
|
// MTP_int(position.fullId.msg))
|
|
|
|
// )).done([=](const MTPUpdates &result) {
|
|
|
|
// applyUpdates(result);
|
|
|
|
// _feedReadRequests.remove(feed);
|
|
|
|
// }).fail([=](const RPCError &error) {
|
|
|
|
// _feedReadRequests.remove(feed);
|
|
|
|
// }).send();
|
|
|
|
// _feedReadRequests.emplace(feed, requestId);
|
|
|
|
|
|
|
|
// i = _feedReadsDelayed.erase(i);
|
|
|
|
// }
|
|
|
|
//}
|
|
|
|
//if (!_feedReadsDelayed.empty()) {
|
|
|
|
// _feedReadTimer.callOnce(delay);
|
|
|
|
//}
|
2018-02-02 12:51:18 +00:00
|
|
|
}
|
|
|
|
|
2017-12-07 13:02:24 +00:00
|
|
|
void ApiWrap::sendReadRequest(not_null<PeerData*> peer, MsgId upTo) {
|
|
|
|
const auto requestId = [&] {
|
|
|
|
const auto finished = [=] {
|
|
|
|
_readRequests.remove(peer);
|
|
|
|
if (const auto next = _readRequestsPending.take(peer)) {
|
|
|
|
sendReadRequest(peer, *next);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
if (const auto channel = peer->asChannel()) {
|
|
|
|
return request(MTPchannels_ReadHistory(
|
|
|
|
channel->inputChannel,
|
|
|
|
MTP_int(upTo)
|
|
|
|
)).done([=](const MTPBool &result) {
|
|
|
|
finished();
|
|
|
|
}).fail([=](const RPCError &error) {
|
|
|
|
finished();
|
|
|
|
}).send();
|
|
|
|
}
|
|
|
|
return request(MTPmessages_ReadHistory(
|
|
|
|
peer->input,
|
|
|
|
MTP_int(upTo)
|
|
|
|
)).done([=](const MTPmessages_AffectedMessages &result) {
|
|
|
|
applyAffectedMessages(peer, result);
|
|
|
|
finished();
|
|
|
|
}).fail([=](const RPCError &error) {
|
|
|
|
finished();
|
|
|
|
}).send();
|
|
|
|
}();
|
|
|
|
_readRequests.emplace(peer, requestId, upTo);
|
|
|
|
}
|
|
|
|
|
2017-04-06 19:02:40 +00:00
|
|
|
ApiWrap::~ApiWrap() = default;
|