From a70613d92925ad730f47760fa26abeb79da05503 Mon Sep 17 00:00:00 2001 From: John Preston Date: Thu, 27 Sep 2018 23:31:48 +0300 Subject: [PATCH] Fix client side generated changelogs display. Also use message date if available in MediaView. --- Telegram/Resources/langs/lang.strings | 4 +- Telegram/SourceFiles/apiwrap.cpp | 22 ++++-- Telegram/SourceFiles/apiwrap.h | 8 +- Telegram/SourceFiles/base/assertion.h | 4 + Telegram/SourceFiles/core/changelogs.cpp | 5 +- Telegram/SourceFiles/data/data_session.cpp | 73 ++++++++++++++++++- Telegram/SourceFiles/data/data_session.h | 9 +++ Telegram/SourceFiles/history/history.cpp | 26 ++++++- Telegram/SourceFiles/history/history.h | 1 + Telegram/SourceFiles/mainwidget.cpp | 68 +---------------- Telegram/SourceFiles/mainwidget.h | 4 - Telegram/SourceFiles/mainwindow.cpp | 65 ----------------- Telegram/SourceFiles/mainwindow.h | 12 --- Telegram/SourceFiles/mediaview.cpp | 10 +-- .../SourceFiles/settings/settings_chat.cpp | 25 ------- Telegram/SourceFiles/settings/settings_chat.h | 1 - .../SourceFiles/settings/settings_common.cpp | 2 +- 17 files changed, 141 insertions(+), 198 deletions(-) diff --git a/Telegram/Resources/langs/lang.strings b/Telegram/Resources/langs/lang.strings index 1bd0e33ed..f7e6ec51d 100644 --- a/Telegram/Resources/langs/lang.strings +++ b/Telegram/Resources/langs/lang.strings @@ -331,15 +331,13 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL "lng_settings_language" = "Language"; "lng_settings_default_scale" = "Default interface scale"; -"lng_settings_edit_info" = "Edit information"; "lng_settings_connection_type" = "Connection type"; "lng_settings_downloading_update" = "Downloading update {progress}..."; -"lng_settings_use_night_mode" = "Use night mode"; "lng_settings_privacy_title" = "Privacy"; "lng_settings_last_seen" = "Last seen"; "lng_settings_calls" = "Voice calls"; "lng_settings_groups_invite" = "Groups"; -"lng_settings_group_privacy_about" = "Change who can add you to groups and channel."; +"lng_settings_group_privacy_about" = "Change who can add you to groups and channels."; "lng_settings_sessions_about" = "Control your sessions on other devices."; "lng_settings_passcode_disable" = "Disable passcode"; "lng_settings_password_disable" = "Disable cloud password"; diff --git a/Telegram/SourceFiles/apiwrap.cpp b/Telegram/SourceFiles/apiwrap.cpp index 9946d7dda..261fb909b 100644 --- a/Telegram/SourceFiles/apiwrap.cpp +++ b/Telegram/SourceFiles/apiwrap.cpp @@ -610,11 +610,23 @@ void ApiWrap::requestDialogEntry(not_null feed) { // }).send(); //} -void ApiWrap::requestDialogEntry(not_null history) { - if (_dialogRequests.contains(history)) { +void ApiWrap::requestDialogEntry( + not_null history, + Fn callback) { + const auto[i, ok] = _dialogRequests.try_emplace(history); + if (callback) { + i->second.push_back(std::move(callback)); + } + if (!ok) { return; } - _dialogRequests.emplace(history); + const auto finalize = [=] { + if (const auto callbacks = _dialogRequests.take(history)) { + for (const auto callback : *callbacks) { + callback(); + } + } + }; auto peers = QVector( 1, MTP_inputDialogPeer(history->peer->input)); @@ -623,9 +635,9 @@ void ApiWrap::requestDialogEntry(not_null history) { )).done([=](const MTPmessages_PeerDialogs &result) { applyPeerDialogs(result); historyDialogEntryApplied(history); - _dialogRequests.remove(history); + finalize(); }).fail([=](const RPCError &error) { - _dialogRequests.remove(history); + finalize(); }).send(); } diff --git a/Telegram/SourceFiles/apiwrap.h b/Telegram/SourceFiles/apiwrap.h index c37f3fbaa..7913a6672 100644 --- a/Telegram/SourceFiles/apiwrap.h +++ b/Telegram/SourceFiles/apiwrap.h @@ -84,7 +84,9 @@ public: void requestContacts(); void requestDialogEntry(not_null feed); //void requestFeedDialogsEntries(not_null feed); - void requestDialogEntry(not_null history); + void requestDialogEntry( + not_null history, + Fn callback = nullptr); //void applyFeedSources(const MTPDchannels_feedSources &data); // #feed //void setFeedChannels( // not_null feed, @@ -622,7 +624,9 @@ private: mtpRequestId _contactsRequestId = 0; mtpRequestId _contactsStatusesRequestId = 0; base::flat_set> _dialogFeedRequests; - base::flat_set> _dialogRequests; + base::flat_map< + not_null, + std::vector>> _dialogRequests; base::flat_map, mtpRequestId> _unreadMentionsRequests; diff --git a/Telegram/SourceFiles/base/assertion.h b/Telegram/SourceFiles/base/assertion.h index 39cb7a296..c67873f61 100644 --- a/Telegram/SourceFiles/base/assertion.h +++ b/Telegram/SourceFiles/base/assertion.h @@ -66,3 +66,7 @@ inline constexpr void validate(bool condition, const char *message, const char * #undef Unexpected #endif // Unexpected #define Unexpected(message) (::base::assertion::fail("Unexpected: " message, __FILE__, __LINE__)) + +#ifdef _DEBUG +#define AssertIsDebug(...) +#endif // _DEBUG diff --git a/Telegram/SourceFiles/core/changelogs.cpp b/Telegram/SourceFiles/core/changelogs.cpp index 70e4686a2..1564c17d4 100644 --- a/Telegram/SourceFiles/core/changelogs.cpp +++ b/Telegram/SourceFiles/core/changelogs.cpp @@ -146,10 +146,9 @@ void Changelogs::addLocalLogs() { void Changelogs::addLocalLog(const QString &text) { auto textWithEntities = TextWithEntities{ text }; TextUtilities::ParseEntities(textWithEntities, TextParseLinks); - App::wnd()->serviceNotification( + _session->data().serviceNotification( textWithEntities, - MTP_messageMediaEmpty(), - unixtime()); + MTP_messageMediaEmpty()); _addedSomeLocal = true; }; diff --git a/Telegram/SourceFiles/data/data_session.cpp b/Telegram/SourceFiles/data/data_session.cpp index 37536d510..10ba61523 100644 --- a/Telegram/SourceFiles/data/data_session.cpp +++ b/Telegram/SourceFiles/data/data_session.cpp @@ -329,7 +329,7 @@ void Session::notifyItemIdChange(IdChange event) { view->refreshDataId(); }; enumerateItemViews(event.item, refreshViewDataId); - if (const auto group = Auth().data().groups().find(event.item)) { + if (const auto group = groups().find(event.item)) { const auto leader = group->items.back(); if (leader != event.item) { enumerateItemViews(leader, refreshViewDataId); @@ -1922,6 +1922,77 @@ rpl::producer<> Session::defaultNotifyUpdates( : defaultChatNotifyUpdates(); } +void Session::serviceNotification( + const TextWithEntities &message, + const MTPMessageMedia &media) { + const auto date = unixtime(); + if (!App::userLoaded(ServiceUserId)) { + App::feedUsers(MTP_vector(1, MTP_user( + MTP_flags( + MTPDuser::Flag::f_first_name + | MTPDuser::Flag::f_phone + | MTPDuser::Flag::f_status + | MTPDuser::Flag::f_verified), + MTP_int(ServiceUserId), + MTPlong(), + MTP_string("Telegram"), + MTPstring(), + MTPstring(), + MTP_string("42777"), + MTP_userProfilePhotoEmpty(), + MTP_userStatusRecently(), + MTPint(), + MTPstring(), + MTPstring(), + MTPstring()))); + } + const auto history = App::history(peerFromUser(ServiceUserId)); + if (!history->lastMessageKnown()) { + _session->api().requestDialogEntry(history, [=] { + insertCheckedServiceNotification(message, media, date); + }); + } else { + insertCheckedServiceNotification(message, media, date); + } +} + +void Session::insertCheckedServiceNotification( + const TextWithEntities &message, + const MTPMessageMedia &media, + TimeId date) { + const auto history = App::history(peerFromUser(ServiceUserId)); + if (!history->isReadyFor(ShowAtUnreadMsgId)) { + history->setUnreadCount(0); + history->getReadyFor(ShowAtTheEndMsgId); + } + const auto flags = MTPDmessage::Flag::f_entities + | MTPDmessage::Flag::f_from_id + | MTPDmessage_ClientFlag::f_clientside_unread; + auto sending = TextWithEntities(), left = message; + while (TextUtilities::CutPart(sending, left, MaxMessageSize)) { + App::histories().addNewMessage( + MTP_message( + MTP_flags(flags), + MTP_int(clientMsgId()), + MTP_int(ServiceUserId), + MTP_peerUser(MTP_int(_session->userId())), + MTPnullFwdHeader, + MTPint(), + MTPint(), + MTP_int(date), + MTP_string(sending.text), + media, + MTPnullMarkup, + TextUtilities::EntitiesToMTP(sending.entities), + MTPint(), + MTPint(), + MTPstring(), + MTPlong()), + NewMessageUnread); + } + sendHistoryChangeNotifications(); +} + void Session::forgetMedia() { for (const auto &[id, photo] : _photos) { photo->forget(); diff --git a/Telegram/SourceFiles/data/data_session.h b/Telegram/SourceFiles/data/data_session.h index c20db99df..81b75122e 100644 --- a/Telegram/SourceFiles/data/data_session.h +++ b/Telegram/SourceFiles/data/data_session.h @@ -410,6 +410,10 @@ public: rpl::producer<> defaultNotifyUpdates( not_null peer) const; + void serviceNotification( + const TextWithEntities &message, + const MTPMessageMedia &media); + void forgetMedia(); void setMimeForwardIds(MessageIdsList &&list); @@ -521,6 +525,11 @@ private: not_null item, Method method); + void insertCheckedServiceNotification( + const TextWithEntities &message, + const MTPMessageMedia &media, + TimeId date); + not_null _session; Storage::DatabasePointer _cache; diff --git a/Telegram/SourceFiles/history/history.cpp b/Telegram/SourceFiles/history/history.cpp index 6fb0744b5..c61fbbd46 100644 --- a/Telegram/SourceFiles/history/history.cpp +++ b/Telegram/SourceFiles/history/history.cpp @@ -2176,8 +2176,13 @@ void History::markFullyLoaded() { void History::setLastMessage(HistoryItem *item) { if (item) { - if (_lastMessage && !*_lastMessage) { - Local::removeSavedPeer(peer); + if (_lastMessage) { + if (!*_lastMessage) { + Local::removeSavedPeer(peer); + } else if (!IsServerMsgId((*_lastMessage)->id) + && (*_lastMessage)->date() > item->date()) { + return; + } } _lastMessage = item; if (const auto feed = peer->feed()) { @@ -2280,12 +2285,25 @@ void History::applyDialog(const MTPDdialog &data) { } } +bool History::skipUnreadUpdateForClientSideUnread() const { + if (peer->id != peerFromUser(ServiceUserId)) { + return false; + } else if (!_unreadCount || !*_unreadCount) { + return false; + } else if (!_lastMessage || IsServerMsgId((*_lastMessage)->id)) { + return false; + } + return true; +} + void History::applyDialogFields( int unreadCount, MsgId maxInboxRead, MsgId maxOutboxRead) { - setUnreadCount(unreadCount); - setInboxReadTill(maxInboxRead); + if (!skipUnreadUpdateForClientSideUnread()) { + setUnreadCount(unreadCount); + setInboxReadTill(maxInboxRead); + } setOutboxReadTill(maxOutboxRead); } diff --git a/Telegram/SourceFiles/history/history.h b/Telegram/SourceFiles/history/history.h index e4822988f..4b3c5ae73 100644 --- a/Telegram/SourceFiles/history/history.h +++ b/Telegram/SourceFiles/history/history.h @@ -471,6 +471,7 @@ private: void addItemsToLists(const std::vector> &items); void clearSendAction(not_null from); + bool skipUnreadUpdateForClientSideUnread() const; HistoryItem *lastAvailableMessage() const; void getNextFirstUnreadMessage(); diff --git a/Telegram/SourceFiles/mainwidget.cpp b/Telegram/SourceFiles/mainwidget.cpp index 4f420e465..3d48ba73d 100644 --- a/Telegram/SourceFiles/mainwidget.cpp +++ b/Telegram/SourceFiles/mainwidget.cpp @@ -1690,71 +1690,6 @@ void MainWidget::dialogsCancelled() { _history->activate(); } -void MainWidget::insertCheckedServiceNotification(const TextWithEntities &message, const MTPMessageMedia &media, int32 date) { - auto flags = MTPDmessage::Flag::f_entities | MTPDmessage::Flag::f_from_id | MTPDmessage_ClientFlag::f_clientside_unread; - auto sending = TextWithEntities(), left = message; - HistoryItem *item = nullptr; - while (TextUtilities::CutPart(sending, left, MaxMessageSize)) { - auto localEntities = TextUtilities::EntitiesToMTP(sending.entities); - item = App::histories().addNewMessage( - MTP_message( - MTP_flags(flags), - MTP_int(clientMsgId()), - MTP_int(ServiceUserId), - MTP_peerUser(MTP_int(Auth().userId())), - MTPnullFwdHeader, - MTPint(), - MTPint(), - MTP_int(date), - MTP_string(sending.text), - media, - MTPnullMarkup, - localEntities, - MTPint(), - MTPint(), - MTPstring(), - MTPlong()), - NewMessageUnread); - } - Auth().data().sendHistoryChangeNotifications(); -} - -void MainWidget::serviceHistoryDone(const MTPmessages_Messages &msgs) { - auto handleResult = [&](auto &&result) { - App::feedUsers(result.vusers); - App::feedChats(result.vchats); - App::feedMsgs(result.vmessages, NewMessageLast); - }; - - switch (msgs.type()) { - case mtpc_messages_messages: - handleResult(msgs.c_messages_messages()); - break; - - case mtpc_messages_messagesSlice: - handleResult(msgs.c_messages_messagesSlice()); - break; - - case mtpc_messages_channelMessages: - LOG(("API Error: received messages.channelMessages! (MainWidget::serviceHistoryDone)")); - handleResult(msgs.c_messages_channelMessages()); - break; - - case mtpc_messages_messagesNotModified: - LOG(("API Error: received messages.messagesNotModified! (MainWidget::serviceHistoryDone)")); - break; - } - - App::wnd()->showDelayedServiceMsgs(); -} - -bool MainWidget::serviceHistoryFail(const RPCError &error) { - if (MTP::isDefaultHandledError(error)) return false; - - App::wnd()->showDelayedServiceMsgs(); - return false; -} - bool MainWidget::isIdle() const { return _isIdle; } @@ -3664,7 +3599,6 @@ void MainWidget::start() { update(); _started = true; - App::wnd()->sendServiceHistoryRequest(); Local::readInstalledStickers(); Local::readFeaturedStickers(); Local::readRecentStickers(); @@ -4905,7 +4839,7 @@ void MainWidget::feedUpdate(const MTPUpdate &update) { } else if (d.is_popup()) { Ui::show(Box(text)); } else { - App::wnd()->serviceNotification(text, d.vmedia); + Auth().data().serviceNotification(text, d.vmedia); emit App::wnd()->checkNewAuthorization(); } } break; diff --git a/Telegram/SourceFiles/mainwidget.h b/Telegram/SourceFiles/mainwidget.h index 7372ff4a7..aacf3052f 100644 --- a/Telegram/SourceFiles/mainwidget.h +++ b/Telegram/SourceFiles/mainwidget.h @@ -239,10 +239,6 @@ public: void checkLastUpdate(bool afterSleep); - void insertCheckedServiceNotification(const TextWithEntities &message, const MTPMessageMedia &media, int32 date); - void serviceHistoryDone(const MTPmessages_Messages &msgs); - bool serviceHistoryFail(const RPCError &error); - bool isIdle() const; QPixmap cachedBackground(const QRect &forRect, int &x, int &y); diff --git a/Telegram/SourceFiles/mainwindow.cpp b/Telegram/SourceFiles/mainwindow.cpp index d77c09cc3..24ee85f13 100644 --- a/Telegram/SourceFiles/mainwindow.cpp +++ b/Telegram/SourceFiles/mainwindow.cpp @@ -212,71 +212,6 @@ void MainWindow::setupIntro() { } fixOrder(); - - _delayedServiceMsgs.clear(); - if (_serviceHistoryRequest) { - MTP::cancel(_serviceHistoryRequest); - _serviceHistoryRequest = 0; - } -} - -void MainWindow::serviceNotification(const TextWithEntities &message, const MTPMessageMedia &media, int32 date, bool force) { - if (date <= 0) date = unixtime(); - auto h = (_main && App::userLoaded(ServiceUserId)) ? App::history(ServiceUserId).get() : nullptr; - if (!h || (!force && h->isEmpty())) { - _delayedServiceMsgs.push_back(DelayedServiceMsg(message, media, date)); - return sendServiceHistoryRequest(); - } - - _main->insertCheckedServiceNotification(message, media, date); -} - -void MainWindow::showDelayedServiceMsgs() { - for (auto &delayed : base::take(_delayedServiceMsgs)) { - serviceNotification(delayed.message, delayed.media, delayed.date, true); - } -} - -void MainWindow::sendServiceHistoryRequest() { - if (!_main || !_main->started() || _delayedServiceMsgs.isEmpty() || _serviceHistoryRequest) return; - - auto user = App::userLoaded(ServiceUserId); - if (!user) { - auto userFlags = MTPDuser::Flag::f_first_name | MTPDuser::Flag::f_phone | MTPDuser::Flag::f_status | MTPDuser::Flag::f_verified; - user = App::feedUsers(MTP_vector(1, MTP_user( - MTP_flags(userFlags), - MTP_int(ServiceUserId), - MTPlong(), - MTP_string("Telegram"), - MTPstring(), - MTPstring(), - MTP_string("42777"), - MTP_userProfilePhotoEmpty(), - MTP_userStatusRecently(), - MTPint(), - MTPstring(), - MTPstring(), - MTPstring()))); - } - auto offsetId = 0; - auto offsetDate = 0; - auto addOffset = 0; - auto limit = 1; - auto maxId = 0; - auto minId = 0; - auto historyHash = 0; - _serviceHistoryRequest = MTP::send( - MTPmessages_GetHistory( - user->input, - MTP_int(offsetId), - MTP_int(offsetDate), - MTP_int(addOffset), - MTP_int(limit), - MTP_int(maxId), - MTP_int(minId), - MTP_int(historyHash)), - _main->rpcDone(&MainWidget::serviceHistoryDone), - _main->rpcFail(&MainWidget::serviceHistoryFail)); } void MainWindow::setupMain() { diff --git a/Telegram/SourceFiles/mainwindow.h b/Telegram/SourceFiles/mainwindow.h index dc4171dcb..3fe9471ae 100644 --- a/Telegram/SourceFiles/mainwindow.h +++ b/Telegram/SourceFiles/mainwindow.h @@ -53,9 +53,6 @@ public: void clearPasscodeLock(); void setupIntro(); void setupMain(); - void serviceNotification(const TextWithEntities &message, const MTPMessageMedia &media = MTP_messageMediaEmpty(), int32 date = 0, bool force = false); - void sendServiceHistoryRequest(); - void showDelayedServiceMsgs(); MainWidget *chatsWidget() { return mainWidget(); @@ -166,15 +163,6 @@ private: void placeSmallCounter(QImage &img, int size, int count, style::color bg, const QPoint &shift, style::color color) override; QImage icon16, icon32, icon64, iconbig16, iconbig32, iconbig64; - struct DelayedServiceMsg { - DelayedServiceMsg(const TextWithEntities &message, const MTPMessageMedia &media, int32 date) : message(message), media(media), date(date) { - } - TextWithEntities message; - MTPMessageMedia media; - int32 date; - }; - QList _delayedServiceMsgs; - mtpRequestId _serviceHistoryRequest = 0; TimeMs _lastTrayClickTime = 0; object_ptr _passcodeLock = { nullptr }; diff --git a/Telegram/SourceFiles/mediaview.cpp b/Telegram/SourceFiles/mediaview.cpp index a064d67f4..647ef4118 100644 --- a/Telegram/SourceFiles/mediaview.cpp +++ b/Telegram/SourceFiles/mediaview.cpp @@ -336,12 +336,12 @@ void MediaView::updateControls() { const auto dNow = QDateTime::currentDateTime(); const auto d = [&] { - if (_photo) { + if (const auto item = App::histItemById(_msgid)) { + return ItemDateTime(item); + } else if (_photo) { return ParseDateTime(_photo->date); } else if (_doc) { return ParseDateTime(_doc->date); - } else if (const auto item = App::histItemById(_msgid)) { - return ItemDateTime(item); } return dNow; }(); @@ -588,8 +588,8 @@ void MediaView::step_radial(TimeMs ms, bool timer) { } const auto wasAnimating = _radial.animating(); const auto updated = _radial.update( - radialProgress(), - !radialLoading(), + radialProgress(), + !radialLoading(), ms + radialTimeShift()); if (timer && (wasAnimating || _radial.animating()) && (!anim::Disabled() || updated)) { update(radialRect()); diff --git a/Telegram/SourceFiles/settings/settings_chat.cpp b/Telegram/SourceFiles/settings/settings_chat.cpp index d7609e670..0ceee5995 100644 --- a/Telegram/SourceFiles/settings/settings_chat.cpp +++ b/Telegram/SourceFiles/settings/settings_chat.cpp @@ -707,30 +707,6 @@ void SetupChatBackground(not_null container) { }, adaptive->lifetime()); } -void SetupNightMode(not_null container) { - const auto calling = Ui::AttachAsChild(container, 0); - AddButton( - container, - lng_settings_use_night_mode, - st::settingsButton - )->toggleOn( - rpl::single(Window::Theme::IsNightMode()) - )->toggledValue( - ) | rpl::start_with_next([=](bool toggled) { - ++*calling; - const auto change = [=] { - if (!--*calling && toggled != Window::Theme::IsNightMode()) { - Window::Theme::ToggleNightMode(); - Window::Theme::KeepApplied(); - } - }; - App::CallDelayed( - st::settingsButton.toggle.duration, - container, - change); - }, container->lifetime()); -} - void SetupUseDefaultTheme(not_null container) { using Update = const Window::Theme::BackgroundUpdate; container->add( @@ -940,7 +916,6 @@ void SetupThemeOptions(not_null container) { AddSubsectionTitle(container, lng_settings_themes); SetupDefaultThemes(container); - //SetupNightMode(container); AddButton( container, diff --git a/Telegram/SourceFiles/settings/settings_chat.h b/Telegram/SourceFiles/settings/settings_chat.h index a3fcb3986..5446efe6b 100644 --- a/Telegram/SourceFiles/settings/settings_chat.h +++ b/Telegram/SourceFiles/settings/settings_chat.h @@ -12,7 +12,6 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL namespace Settings { void SetupDataStorage(not_null container); -void SetupNightMode(not_null container); void SetupUseDefaultTheme(not_null container); void SetupDefaultThemes(not_null container); diff --git a/Telegram/SourceFiles/settings/settings_common.cpp b/Telegram/SourceFiles/settings/settings_common.cpp index 5e752664e..bc7b5530a 100644 --- a/Telegram/SourceFiles/settings/settings_common.cpp +++ b/Telegram/SourceFiles/settings/settings_common.cpp @@ -169,7 +169,7 @@ void AddSubsectionTitle( void FillMenu(Fn showOther, MenuCallback addAction) { addAction( - lang(lng_settings_edit_info), + lang(lng_settings_information), [=] { showOther(Type::Information); }); addAction( lang(lng_settings_logout),