/* This file is part of Telegram Desktop, the official desktop version of Telegram messaging app, see https://telegram.org Telegram Desktop is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. It is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. In addition, as a special exception, the copyright holders give permission to link the code of portions of this program with the OpenSSL library. Full license: https://github.com/telegramdesktop/tdesktop/blob/master/LICENSE Copyright (c) 2014-2017 John Preston, https://desktop.telegram.org */ #pragma once #include #include "base/timer.h" #include "core/single_timer.h" #include "mtproto/sender.h" #include "base/flat_map.h" #include "base/flat_set.h" #include "chat_helpers/stickers.h" class AuthSession; namespace Storage { enum class SharedMediaType : char; } // namespace Storage enum class SparseIdsLoadDirection; namespace Api { inline const MTPVector *getChatsFromMessagesChats(const MTPmessages_Chats &chats) { switch (chats.type()) { case mtpc_messages_chats: return &chats.c_messages_chats().vchats; case mtpc_messages_chatsSlice: return &chats.c_messages_chatsSlice().vchats; } return nullptr; } } // namespace Api class ApiWrap : private MTP::Sender, private base::Subscriber { public: ApiWrap(not_null session); void start(); void applyUpdates(const MTPUpdates &updates, uint64 sentMessageRandomId = 0); using RequestMessageDataCallback = base::lambda; void requestMessageData(ChannelData *channel, MsgId msgId, RequestMessageDataCallback callback); void requestFullPeer(PeerData *peer); void requestPeer(PeerData *peer); void requestPeers(const QList &peers); void requestLastParticipants(ChannelData *channel); void requestBots(ChannelData *channel); void requestParticipantsCountDelayed(ChannelData *channel); void requestChannelMembersForAdd( not_null channel, base::lambda callback); void processFullPeer(PeerData *peer, const MTPmessages_ChatFull &result); void processFullPeer(UserData *user, const MTPUserFull &result); void requestSelfParticipant(ChannelData *channel); void kickParticipant(PeerData *peer, UserData *user, const MTPChannelBannedRights ¤tRights); void unblockParticipant(PeerData *peer, UserData *user); void requestWebPageDelayed(WebPageData *page); void clearWebPageRequest(WebPageData *page); void clearWebPageRequests(); void scheduleStickerSetRequest(uint64 setId, uint64 access); void requestStickerSets(); void saveStickerSets(const Stickers::Order &localOrder, const Stickers::Order &localRemoved); void updateStickers(); void setGroupStickerSet(not_null megagroup, const MTPInputStickerSet &set); void joinChannel(ChannelData *channel); void leaveChannel(ChannelData *channel); void blockUser(UserData *user); void unblockUser(UserData *user); void exportInviteLink(PeerData *peer); void requestNotifySetting(PeerData *peer); void saveDraftToCloudDelayed(History *history); void savePrivacy(const MTPInputPrivacyKey &key, QVector &&rules); void handlePrivacyChange(mtpTypeId keyTypeId, const MTPVector &rules); int onlineTillFromStatus(const MTPUserStatus &status, int currentOnlineTill); void clearHistory(not_null peer); base::Observable &fullPeerUpdated() { return _fullPeerUpdated; } bool isQuitPrevent(); void applyUpdatesNoPtsCheck(const MTPUpdates &updates); void applyUpdateNoPtsCheck(const MTPUpdate &update); void jumpToDate(not_null peer, const QDate &date); void preloadEnoughUnreadMentions(not_null history); void checkForUnreadMentions(const base::flat_set &possiblyReadMentions, ChannelData *channel = nullptr); void editChatAdmins( not_null chat, bool adminsEnabled, base::flat_set> &&admins); using SliceType = SparseIdsLoadDirection; void requestSharedMedia( not_null peer, Storage::SharedMediaType type, MsgId messageId, SliceType slice); void requestSharedMediaCount( not_null peer, Storage::SharedMediaType type); void requestUserPhotos( not_null user, PhotoId afterId); void stickerSetInstalled(uint64 setId) { _stickerSetInstalled.fire_copy(setId); } auto stickerSetInstalled() const { return _stickerSetInstalled.events(); } void readFeaturedSetDelayed(uint64 setId); void parseChannelParticipants( const MTPchannels_ChannelParticipants &result, base::lambda &list)> callbackList, base::lambda callbackNotModified = nullptr); void parseRecentChannelParticipants( not_null channel, const MTPchannels_ChannelParticipants &result, base::lambda &list)> callbackList, base::lambda callbackNotModified = nullptr); ~ApiWrap(); private: struct MessageDataRequest { using Callbacks = QList; mtpRequestId requestId = 0; Callbacks callbacks; }; using MessageDataRequests = QMap; using SharedMediaType = Storage::SharedMediaType; void requestAppChangelogs(); void addLocalChangelogs(int oldAppVersion); void updatesReceived(const MTPUpdates &updates); void checkQuitPreventFinished(); void saveDraftsToCloud(); void resolveMessageDatas(); void gotMessageDatas(ChannelData *channel, const MTPmessages_Messages &result, mtpRequestId requestId); void finalizeMessageDataRequest( ChannelData *channel, mtpRequestId requestId); QVector collectMessageIds(const MessageDataRequests &requests); MessageDataRequests *messageDataRequests(ChannelData *channel, bool onlyExisting = false); void gotChatFull(PeerData *peer, const MTPmessages_ChatFull &result, mtpRequestId req); void gotUserFull(UserData *user, const MTPUserFull &result, mtpRequestId req); void lastParticipantsDone( ChannelData *peer, const MTPchannels_ChannelParticipants &result, mtpRequestId req); void applyLastParticipantsList( ChannelData *peer, int availableCount, const QVector &list, bool bots, bool fromStart); void resolveWebPages(); void gotWebPages(ChannelData *channel, const MTPmessages_Messages &result, mtpRequestId req); void gotStickerSet(uint64 setId, const MTPmessages_StickerSet &result); PeerData *notifySettingReceived(MTPInputNotifyPeer peer, const MTPPeerNotifySettings &settings); void stickerSetDisenabled(mtpRequestId requestId); void stickersSaveOrder(); void requestStickers(TimeId now); void requestRecentStickers(TimeId now); void requestFavedStickers(TimeId now); void requestFeaturedStickers(TimeId now); void requestSavedGifs(TimeId now); void readFeaturedSets(); void cancelEditChatAdmins(not_null chat); void saveChatAdmins(not_null chat); void sendSaveChatAdminsRequests(not_null chat); template void requestMessageAfterDate( not_null peer, const QDate &date, Callback &&callback); int applyAffectedHistory( not_null peer, const MTPmessages_AffectedHistory &result); void sharedMediaDone( not_null peer, SharedMediaType type, MsgId messageId, SliceType slice, const MTPmessages_Messages &result); void userPhotosDone( not_null user, PhotoId photoId, const MTPphotos_Photos &result); not_null _session; mtpRequestId _changelogSubscription = 0; MessageDataRequests _messageDataRequests; QMap _channelMessageDataRequests; SingleQueuedInvokation _messageDataResolveDelayed; using PeerRequests = QMap; PeerRequests _fullPeerRequests; PeerRequests _peerRequests; PeerRequests _participantsRequests; PeerRequests _botsRequests; base::DelayedCallTimer _participantsCountRequestTimer; ChannelData *_channelMembersForAdd = nullptr; mtpRequestId _channelMembersForAddRequestId = 0; base::lambda _channelMembersForAddCallback; typedef QPair KickRequest; typedef QMap KickRequests; KickRequests _kickRequests; QMap _selfParticipantRequests; QMap _webPagesPending; base::Timer _webPagesTimer; QMap > _stickerSetRequests; QMap _channelAmInRequests; QMap _blockRequests; QMap _exportInviteRequests; QMap _notifySettingRequests; QMap _draftsSaveRequestIds; base::Timer _draftsSaveTimer; base::flat_set _stickerSetDisenableRequests; Stickers::Order _stickersOrder; mtpRequestId _stickersReorderRequestId = 0; mtpRequestId _stickersClearRecentRequestId = 0; mtpRequestId _stickersUpdateRequest = 0; mtpRequestId _recentStickersUpdateRequest = 0; mtpRequestId _favedStickersUpdateRequest = 0; mtpRequestId _featuredStickersUpdateRequest = 0; mtpRequestId _savedGifsUpdateRequest = 0; base::Timer _featuredSetsReadTimer; base::flat_set _featuredSetsRead; QMap _privacySaveRequests; mtpRequestId _contactsStatusesRequestId = 0; base::flat_map, mtpRequestId> _unreadMentionsRequests; base::flat_map< not_null, mtpRequestId> _chatAdminsEnabledRequests; base::flat_map< not_null, base::flat_set>> _chatAdminsToSave; base::flat_map< not_null, base::flat_set> _chatAdminsSaveRequests; base::flat_map, SharedMediaType, MsgId, SliceType>, mtpRequestId> _sharedMediaRequests; base::flat_map, mtpRequestId> _userPhotosRequests; base::Observable _fullPeerUpdated; rpl::event_stream _stickerSetInstalled; };