/* This file is part of Telegram Desktop, the official desktop application for the Telegram messaging service. For license and copyright information please follow this link: https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL */ #pragma once #include "storage/storage_databases.h" #include "chat_helpers/stickers.h" #include "dialogs/dialogs_key.h" #include "data/data_groups.h" #include "data/data_notify_settings.h" #include "history/history_location_manager.h" #include "base/timer.h" class Image; class HistoryItem; class BoxContent; struct WebPageCollage; enum class WebPageType; namespace HistoryView { struct Group; class Element; } // namespace HistoryView class AuthSession; namespace Media { namespace Clip { class Reader; } // namespace Clip } // namespace Media namespace Export { class Controller; namespace View { class PanelController; } // namespace View } // namespace Export namespace Passport { struct SavedCredentials; } // namespace Passport namespace Data { class Feed; enum class FeedUpdateFlag; struct FeedUpdate; class WallPaper; class Session final { public: using ViewElement = HistoryView::Element; explicit Session(not_null session); ~Session(); [[nodiscard]] AuthSession &session() const { return *_session; } void clear(); void startExport(PeerData *peer = nullptr); void startExport(const MTPInputPeer &singlePeer); void suggestStartExport(TimeId availableAt); void clearExportSuggestion(); [[nodiscard]] auto currentExportView() const -> rpl::producer; bool exportInProgress() const; void stopExportWithConfirmation(FnMut callback); void stopExport(); [[nodiscard]] auto passportCredentials() const -> const Passport::SavedCredentials*; void rememberPassportCredentials( Passport::SavedCredentials data, crl::time rememberFor); void forgetPassportCredentials(); [[nodiscard]] Storage::Cache::Database &cache(); [[nodiscard]] Storage::Cache::Database &cacheBigFile(); [[nodiscard]] not_null peer(PeerId id); [[nodiscard]] not_null peer(UserId id) = delete; [[nodiscard]] not_null user(UserId id); [[nodiscard]] not_null chat(ChatId id); [[nodiscard]] not_null channel(ChannelId id); [[nodiscard]] not_null user(PeerId id) = delete; [[nodiscard]] not_null chat(PeerId id) = delete; [[nodiscard]] not_null channel(PeerId id) = delete; [[nodiscard]] PeerData *peerLoaded(PeerId id) const; [[nodiscard]] PeerData *peerLoaded(UserId id) const = delete; [[nodiscard]] UserData *userLoaded(UserId id) const; [[nodiscard]] ChatData *chatLoaded(ChatId id) const; [[nodiscard]] ChannelData *channelLoaded(ChannelId id) const; [[nodiscard]] UserData *userLoaded(PeerId id) const = delete; [[nodiscard]] ChatData *chatLoaded(PeerId id) const = delete; [[nodiscard]] ChannelData *channelLoaded(PeerId id) const = delete; not_null processUser(const MTPUser &data); not_null processChat(const MTPChat &data); // Returns last user, if there were any. UserData *processUsers(const MTPVector &data); PeerData *processChats(const MTPVector &data); void applyMaximumChatVersions(const MTPVector &data); void enumerateUsers(Fn)> action) const; void enumerateGroups(Fn)> action) const; void enumerateChannels(Fn)> action) const; [[nodiscard]] PeerData *peerByUsername(const QString &username) const; [[nodiscard]] not_null history(PeerId peerId); [[nodiscard]] History *historyLoaded(PeerId peerId) const; [[nodiscard]] not_null history(UserId userId) = delete; [[nodiscard]] History *historyLoaded(UserId userId) const = delete; [[nodiscard]] not_null history(not_null peer); [[nodiscard]] History *historyLoaded(const PeerData *peer); void registerSendAction( not_null history, not_null user, const MTPSendMessageAction &action, TimeId when); [[nodiscard]] base::Variable &contactsLoaded() { return _contactsLoaded; } [[nodiscard]] base::Variable &allChatsLoaded() { return _allChatsLoaded; } [[nodiscard]] base::Observable &moreChatsLoaded() { return _moreChatsLoaded; } struct ItemVisibilityQuery { not_null item; not_null isVisible; }; [[nodiscard]] base::Observable &queryItemVisibility() { return _queryItemVisibility; } struct IdChange { not_null item; MsgId oldId = 0; }; void notifyItemIdChange(IdChange event); [[nodiscard]] rpl::producer itemIdChanged() const; void notifyItemLayoutChange(not_null item); [[nodiscard]] rpl::producer> itemLayoutChanged() const; void notifyViewLayoutChange(not_null view); [[nodiscard]] rpl::producer> viewLayoutChanged() const; void requestItemRepaint(not_null item); [[nodiscard]] rpl::producer> itemRepaintRequest() const; void requestViewRepaint(not_null view); [[nodiscard]] rpl::producer> viewRepaintRequest() const; void requestItemResize(not_null item); [[nodiscard]] rpl::producer> itemResizeRequest() const; void requestViewResize(not_null view); [[nodiscard]] rpl::producer> viewResizeRequest() const; void requestItemViewRefresh(not_null item); [[nodiscard]] rpl::producer> itemViewRefreshRequest() const; void requestItemTextRefresh(not_null item); void requestAnimationPlayInline(not_null item); [[nodiscard]] rpl::producer> animationPlayInlineRequest() const; void notifyHistoryUnloaded(not_null history); [[nodiscard]] rpl::producer> historyUnloaded() const; void notifyItemRemoved(not_null item); [[nodiscard]] rpl::producer> itemRemoved() const; void notifyViewRemoved(not_null view); [[nodiscard]] rpl::producer> viewRemoved() const; void notifyHistoryCleared(not_null history); [[nodiscard]] rpl::producer> historyCleared() const; void notifyHistoryChangeDelayed(not_null history); [[nodiscard]] rpl::producer> historyChanged() const; void sendHistoryChangeNotifications(); using MegagroupParticipant = std::tuple< not_null, not_null>; void removeMegagroupParticipant( not_null channel, not_null user); [[nodiscard]] rpl::producer megagroupParticipantRemoved() const; [[nodiscard]] rpl::producer> megagroupParticipantRemoved( not_null channel) const; void addNewMegagroupParticipant( not_null channel, not_null user); [[nodiscard]] rpl::producer megagroupParticipantAdded() const; [[nodiscard]] rpl::producer> megagroupParticipantAdded( not_null channel) const; void notifyFeedUpdated(not_null feed, FeedUpdateFlag update); [[nodiscard]] rpl::producer feedUpdated() const; void notifyStickersUpdated(); [[nodiscard]] rpl::producer<> stickersUpdated() const; void notifySavedGifsUpdated(); [[nodiscard]] rpl::producer<> savedGifsUpdated() const; bool stickersUpdateNeeded(crl::time now) const { return stickersUpdateNeeded(_lastStickersUpdate, now); } void setLastStickersUpdate(crl::time update) { _lastStickersUpdate = update; } bool recentStickersUpdateNeeded(crl::time now) const { return stickersUpdateNeeded(_lastRecentStickersUpdate, now); } void setLastRecentStickersUpdate(crl::time update) { _lastRecentStickersUpdate = update; } bool favedStickersUpdateNeeded(crl::time now) const { return stickersUpdateNeeded(_lastFavedStickersUpdate, now); } void setLastFavedStickersUpdate(crl::time update) { _lastFavedStickersUpdate = update; } bool featuredStickersUpdateNeeded(crl::time now) const { return stickersUpdateNeeded(_lastFeaturedStickersUpdate, now); } void setLastFeaturedStickersUpdate(crl::time update) { _lastFeaturedStickersUpdate = update; } bool savedGifsUpdateNeeded(crl::time now) const { return stickersUpdateNeeded(_lastSavedGifsUpdate, now); } void setLastSavedGifsUpdate(crl::time update) { _lastSavedGifsUpdate = update; } int featuredStickerSetsUnreadCount() const { return _featuredStickerSetsUnreadCount.current(); } void setFeaturedStickerSetsUnreadCount(int count) { _featuredStickerSetsUnreadCount = count; } [[nodiscard]] rpl::producer featuredStickerSetsUnreadCountValue() const { return _featuredStickerSetsUnreadCount.value(); } const Stickers::Sets &stickerSets() const { return _stickerSets; } Stickers::Sets &stickerSetsRef() { return _stickerSets; } const Stickers::Order &stickerSetsOrder() const { return _stickerSetsOrder; } Stickers::Order &stickerSetsOrderRef() { return _stickerSetsOrder; } const Stickers::Order &featuredStickerSetsOrder() const { return _featuredStickerSetsOrder; } Stickers::Order &featuredStickerSetsOrderRef() { return _featuredStickerSetsOrder; } const Stickers::Order &archivedStickerSetsOrder() const { return _archivedStickerSetsOrder; } Stickers::Order &archivedStickerSetsOrderRef() { return _archivedStickerSetsOrder; } const Stickers::SavedGifs &savedGifs() const { return _savedGifs; } Stickers::SavedGifs &savedGifsRef() { return _savedGifs; } HistoryItemsList idsToItems(const MessageIdsList &ids) const; MessageIdsList itemsToIds(const HistoryItemsList &items) const; MessageIdsList itemOrItsGroup(not_null item) const; void applyUpdate(const MTPDupdateMessagePoll &update); void applyUpdate(const MTPDupdateChatParticipants &update); void applyUpdate(const MTPDupdateChatParticipantAdd &update); void applyUpdate(const MTPDupdateChatParticipantDelete &update); void applyUpdate(const MTPDupdateChatParticipantAdmin &update); void applyUpdate(const MTPDupdateChatDefaultBannedRights &update); int pinnedDialogsCount() const; const std::deque &pinnedDialogsOrder() const; void setPinnedDialog(const Dialogs::Key &key, bool pinned); void applyPinnedDialogs(const QVector &list); void applyPinnedDialogs(const QVector &list); void reorderTwoPinnedDialogs( const Dialogs::Key &key1, const Dialogs::Key &key2); void photoLoadSettingsChanged(); void documentLoadSettingsChanged(); void notifyPhotoLayoutChanged(not_null photo); void notifyDocumentLayoutChanged( not_null document); void requestDocumentViewRepaint(not_null document); void markMediaRead(not_null document); void requestPollViewRepaint(not_null poll); HistoryItem *addNewMessage(const MTPMessage &data, NewMessageType type); struct SendActionAnimationUpdate { not_null history; int width = 0; int height = 0; bool textUpdated = false; }; [[nodiscard]] auto sendActionAnimationUpdated() const -> rpl::producer; void updateSendActionAnimation(SendActionAnimationUpdate &&update); int unreadBadge() const; bool unreadBadgeMuted() const; int unreadBadgeIgnoreOne(History *history) const; bool unreadBadgeMutedIgnoreOne(History *history) const; int unreadOnlyMutedBadge() const; void unreadIncrement(int count, bool muted); void unreadMuteChanged(int count, bool muted); void unreadEntriesChanged( int withUnreadDelta, int mutedWithUnreadDelta); void selfDestructIn(not_null item, crl::time delay); [[nodiscard]] not_null photo(PhotoId id); not_null processPhoto(const MTPPhoto &data); not_null processPhoto(const MTPDphoto &data); not_null processPhoto( const MTPPhoto &data, const PreparedPhotoThumbs &thumbs); [[nodiscard]] not_null photo( PhotoId id, const uint64 &access, const QByteArray &fileReference, TimeId date, bool hasSticker, const ImagePtr &thumbnailInline, const ImagePtr &thumbnailSmall, const ImagePtr &thumbnail, const ImagePtr &large); void photoConvert( not_null original, const MTPPhoto &data); [[nodiscard]] PhotoData *photoFromWeb( const MTPWebDocument &data, ImagePtr thumbnail = ImagePtr(), bool willBecomeNormal = false); [[nodiscard]] not_null document(DocumentId id); not_null processDocument(const MTPDocument &data); not_null processDocument(const MTPDdocument &data); not_null processDocument( const MTPdocument &data, QImage &&thumb); [[nodiscard]] not_null document( DocumentId id, const uint64 &access, const QByteArray &fileReference, TimeId date, const QVector &attributes, const QString &mime, const ImagePtr &thumbnailInline, const ImagePtr &thumbnail, int32 dc, int32 size, const StorageImageLocation &thumbLocation); void documentConvert( not_null original, const MTPDocument &data); [[nodiscard]] DocumentData *documentFromWeb( const MTPWebDocument &data, ImagePtr thumb); [[nodiscard]] not_null webpage(WebPageId id); not_null processWebpage(const MTPWebPage &data); not_null processWebpage(const MTPDwebPage &data); not_null processWebpage(const MTPDwebPagePending &data); [[nodiscard]] not_null webpage( WebPageId id, const QString &siteName, const TextWithEntities &content); [[nodiscard]] not_null webpage( WebPageId id, WebPageType type, const QString &url, const QString &displayUrl, const QString &siteName, const QString &title, const TextWithEntities &description, PhotoData *photo, DocumentData *document, WebPageCollage &&collage, int duration, const QString &author, TimeId pendingTill); [[nodiscard]] not_null game(GameId id); not_null processGame(const MTPDgame &data); [[nodiscard]] not_null game( GameId id, const uint64 &accessHash, const QString &shortName, const QString &title, const QString &description, PhotoData *photo, DocumentData *document); void gameConvert( not_null original, const MTPGame &data); [[nodiscard]] not_null poll(PollId id); not_null processPoll(const MTPPoll &data); not_null processPoll(const MTPDmessageMediaPoll &data); [[nodiscard]] not_null location( const LocationCoords &coords); void registerPhotoItem( not_null photo, not_null item); void unregisterPhotoItem( not_null photo, not_null item); void registerDocumentItem( not_null document, not_null item); void unregisterDocumentItem( not_null document, not_null item); void registerWebPageView( not_null page, not_null view); void unregisterWebPageView( not_null page, not_null view); void registerWebPageItem( not_null page, not_null item); void unregisterWebPageItem( not_null page, not_null item); void registerGameView( not_null game, not_null view); void unregisterGameView( not_null game, not_null view); void registerPollView( not_null poll, not_null view); void unregisterPollView( not_null poll, not_null view); void registerContactView( UserId contactId, not_null view); void unregisterContactView( UserId contactId, not_null view); void registerContactItem( UserId contactId, not_null item); void unregisterContactItem( UserId contactId, not_null item); void registerAutoplayAnimation( not_null<::Media::Clip::Reader*> reader, not_null view); void unregisterAutoplayAnimation( not_null<::Media::Clip::Reader*> reader); HistoryItem *findWebPageItem(not_null page) const; QString findContactPhone(not_null contact) const; QString findContactPhone(UserId contactId) const; void notifyWebPageUpdateDelayed(not_null page); void notifyGameUpdateDelayed(not_null game); void notifyPollUpdateDelayed(not_null poll); bool hasPendingWebPageGamePollNotification() const; void sendWebPageGamePollNotifications(); void stopAutoplayAnimations(); void registerItemView(not_null view); void unregisterItemView(not_null view); not_null feed(FeedId id); Feed *feedLoaded(FeedId id); void setDefaultFeedId(FeedId id); FeedId defaultFeedId() const; rpl::producer defaultFeedIdValue() const; void requestNotifySettings(not_null peer); void applyNotifySetting( const MTPNotifyPeer ¬ifyPeer, const MTPPeerNotifySettings &settings); void updateNotifySettings( not_null peer, std::optional muteForSeconds, std::optional silentPosts = std::nullopt); bool notifyIsMuted( not_null peer, crl::time *changesIn = nullptr) const; bool notifySilentPosts(not_null peer) const; bool notifyMuteUnknown(not_null peer) const; bool notifySilentPostsUnknown(not_null peer) const; bool notifySettingsUnknown(not_null peer) const; rpl::producer<> defaultUserNotifyUpdates() const; rpl::producer<> defaultChatNotifyUpdates() const; rpl::producer<> defaultBroadcastNotifyUpdates() const; rpl::producer<> defaultNotifyUpdates( not_null peer) const; void serviceNotification( const TextWithEntities &message, const MTPMessageMedia &media = MTP_messageMediaEmpty()); void checkNewAuthorization(); rpl::producer<> newAuthorizationChecks() const; void setMimeForwardIds(MessageIdsList &&list); MessageIdsList takeMimeForwardIds(); void setProxyPromoted(PeerData *promoted); PeerData *proxyPromoted() const; Groups &groups() { return _groups; } const Groups &groups() const { return _groups; } bool updateWallpapers(const MTPaccount_WallPapers &data); void removeWallpaper(const WallPaper &paper); const std::vector &wallpapers() const; int32 wallpapersHash() const; void clearLocalStorage(); private: void suggestStartExport(); void setupContactViewsViewer(); void setupChannelLeavingViewer(); void checkSelfDestructItems(); int computeUnreadBadge( int full, int muted, int entriesFull, int entriesMuted) const; bool computeUnreadBadgeMuted( int full, int muted, int entriesFull, int entriesMuted) const; void photoApplyFields( not_null photo, const MTPPhoto &data); void photoApplyFields( not_null photo, const MTPDphoto &data); void photoApplyFields( not_null photo, const uint64 &access, const QByteArray &fileReference, TimeId date, bool hasSticker, const ImagePtr &thumbnailInline, const ImagePtr &thumbnailSmall, const ImagePtr &thumbnail, const ImagePtr &large); void documentApplyFields( not_null document, const MTPDocument &data); void documentApplyFields( not_null document, const MTPDdocument &data); void documentApplyFields( not_null document, const uint64 &access, const QByteArray &fileReference, TimeId date, const QVector &attributes, const QString &mime, const ImagePtr &thumbnailInline, const ImagePtr &thumbnail, int32 dc, int32 size, const StorageImageLocation &thumbLocation); DocumentData *documentFromWeb( const MTPDwebDocument &data, ImagePtr thumb); DocumentData *documentFromWeb( const MTPDwebDocumentNoProxy &data, ImagePtr thumb); void webpageApplyFields( not_null page, const MTPDwebPage &data); void webpageApplyFields( not_null page, WebPageType type, const QString &url, const QString &displayUrl, const QString &siteName, const QString &title, const TextWithEntities &description, PhotoData *photo, DocumentData *document, WebPageCollage &&collage, int duration, const QString &author, TimeId pendingTill); void gameApplyFields( not_null game, const MTPDgame &data); void gameApplyFields( not_null game, const uint64 &accessHash, const QString &shortName, const QString &title, const QString &description, PhotoData *photo, DocumentData *document); bool stickersUpdateNeeded(crl::time lastUpdate, crl::time now) const { constexpr auto kStickersUpdateTimeout = crl::time(3600'000); return (lastUpdate == 0) || (now >= lastUpdate + kStickersUpdateTimeout); } void userIsContactUpdated(not_null user); void clearPinnedDialogs(); void setIsPinned(const Dialogs::Key &key, bool pinned); NotifySettings &defaultNotifySettings(not_null peer); const NotifySettings &defaultNotifySettings( not_null peer) const; void unmuteByFinished(); void unmuteByFinishedDelayed(crl::time delay); void updateNotifySettingsLocal(not_null peer); template void enumerateItemViews( not_null item, Method method); void insertCheckedServiceNotification( const TextWithEntities &message, const MTPMessageMedia &media, TimeId date); void step_typings(crl::time ms, bool timer); void setWallpapers(const QVector &data, int32 hash); not_null _session; Storage::DatabasePointer _cache; Storage::DatabasePointer _bigFileCache; std::unique_ptr _export; std::unique_ptr _exportPanel; rpl::event_stream _exportViewChanges; TimeId _exportAvailableAt = 0; QPointer _exportSuggestion; base::Variable _contactsLoaded = { false }; base::Variable _allChatsLoaded = { false }; base::Observable _moreChatsLoaded; base::Observable _queryItemVisibility; rpl::event_stream _itemIdChanges; rpl::event_stream> _itemLayoutChanges; rpl::event_stream> _viewLayoutChanges; rpl::event_stream> _itemRepaintRequest; rpl::event_stream> _viewRepaintRequest; rpl::event_stream> _itemResizeRequest; rpl::event_stream> _viewResizeRequest; rpl::event_stream> _itemViewRefreshRequest; rpl::event_stream> _itemTextRefreshRequest; rpl::event_stream> _animationPlayInlineRequest; rpl::event_stream> _itemRemoved; rpl::event_stream> _viewRemoved; rpl::event_stream> _historyUnloaded; rpl::event_stream> _historyCleared; base::flat_set> _historiesChanged; rpl::event_stream> _historyChanged; rpl::event_stream _megagroupParticipantRemoved; rpl::event_stream _megagroupParticipantAdded; rpl::event_stream _feedUpdates; rpl::event_stream<> _stickersUpdated; rpl::event_stream<> _savedGifsUpdated; crl::time _lastStickersUpdate = 0; crl::time _lastRecentStickersUpdate = 0; crl::time _lastFavedStickersUpdate = 0; crl::time _lastFeaturedStickersUpdate = 0; crl::time _lastSavedGifsUpdate = 0; rpl::variable _featuredStickerSetsUnreadCount = 0; Stickers::Sets _stickerSets; Stickers::Order _stickerSetsOrder; Stickers::Order _featuredStickerSetsOrder; Stickers::Order _archivedStickerSetsOrder; Stickers::SavedGifs _savedGifs; int _unreadFull = 0; int _unreadMuted = 0; int _unreadEntriesFull = 0; int _unreadEntriesMuted = 0; base::Timer _selfDestructTimer; std::vector _selfDestructItems; // When typing in this history started. base::flat_map, crl::time> _sendActions; BasicAnimation _a_sendActions; std::unordered_map< PhotoId, std::unique_ptr> _photos; std::unordered_map< not_null, base::flat_set>> _photoItems; std::unordered_map< DocumentId, std::unique_ptr> _documents; std::unordered_map< not_null, base::flat_set>> _documentItems; std::unordered_map< WebPageId, std::unique_ptr> _webpages; std::unordered_map< not_null, base::flat_set>> _webpageItems; std::unordered_map< not_null, base::flat_set>> _webpageViews; std::unordered_map< LocationCoords, std::unique_ptr> _locations; std::unordered_map< PollId, std::unique_ptr> _polls; std::unordered_map< GameId, std::unique_ptr> _games; std::unordered_map< not_null, base::flat_set>> _gameViews; std::unordered_map< not_null, base::flat_set>> _pollViews; std::unordered_map< UserId, base::flat_set>> _contactItems; std::unordered_map< UserId, base::flat_set>> _contactViews; base::flat_map< not_null<::Media::Clip::Reader*>, not_null> _autoplayAnimations; base::flat_set> _webpagesUpdated; base::flat_set> _gamesUpdated; base::flat_set> _pollsUpdated; std::deque _pinnedDialogs; base::flat_map> _feeds; rpl::variable _defaultFeedId = FeedId(); Groups _groups; std::unordered_map< not_null, std::vector>> _views; PeerData *_proxyPromoted = nullptr; NotifySettings _defaultUserNotifySettings; NotifySettings _defaultChatNotifySettings; NotifySettings _defaultBroadcastNotifySettings; rpl::event_stream<> _defaultUserNotifyUpdates; rpl::event_stream<> _defaultChatNotifyUpdates; rpl::event_stream<> _defaultBroadcastNotifyUpdates; std::unordered_set> _mutedPeers; base::Timer _unmuteByFinishedTimer; std::unordered_map> _peers; std::unordered_map> _histories; MessageIdsList _mimeForwardIds; using CredentialsWithGeneration = std::pair< const Passport::SavedCredentials, int>; std::unique_ptr _passportCredentials; rpl::event_stream<> _newAuthorizationChecks; rpl::event_stream _sendActionAnimationUpdate; std::vector _wallpapers; int32 _wallpapersHash = 0; rpl::lifetime _lifetime; }; } // namespace Data