From 46612ef128e22c6f6fdcff79685f7992acebfeb1 Mon Sep 17 00:00:00 2001 From: John Preston Date: Thu, 4 Jan 2018 12:21:06 +0300 Subject: [PATCH] Remove Notify::userIsContactChanged(). Replace with Notify::peerUpdatedDelayed(). --- Telegram/SourceFiles/apiwrap.cpp | 6 +- Telegram/SourceFiles/app.cpp | 108 ++++++++++-------- Telegram/SourceFiles/auth_session.cpp | 25 ++++ Telegram/SourceFiles/auth_session.h | 6 + .../SourceFiles/boxes/add_contact_box.cpp | 24 ++-- Telegram/SourceFiles/data/data_peer.cpp | 22 +++- Telegram/SourceFiles/data/data_peer.h | 18 ++- .../dialogs/dialogs_inner_widget.cpp | 26 +++-- .../dialogs/dialogs_inner_widget.h | 2 +- .../SourceFiles/dialogs/dialogs_widget.cpp | 9 -- Telegram/SourceFiles/dialogs/dialogs_widget.h | 1 - Telegram/SourceFiles/facades.cpp | 4 - Telegram/SourceFiles/facades.h | 1 - .../history/history_media_types.cpp | 3 +- .../SourceFiles/history/history_widget.cpp | 9 +- Telegram/SourceFiles/mainwidget.cpp | 55 +++++---- Telegram/SourceFiles/mainwidget.h | 2 +- Telegram/SourceFiles/storage/localstorage.cpp | 62 ++++++++-- 18 files changed, 249 insertions(+), 134 deletions(-) diff --git a/Telegram/SourceFiles/apiwrap.cpp b/Telegram/SourceFiles/apiwrap.cpp index 4aad67183..57ce4803a 100644 --- a/Telegram/SourceFiles/apiwrap.cpp +++ b/Telegram/SourceFiles/apiwrap.cpp @@ -325,10 +325,8 @@ void ApiWrap::requestContacts() { const auto userId = contact.c_contact().vuser_id.v; if (userId == Auth().userId() && App::self()) { - if (App::self()->contact < 1) { - App::self()->contact = 1; - Notify::userIsContactChanged(App::self()); - } + App::self()->setContactStatus( + UserData::ContactStatus::Contact); } } _session->data().contactsLoaded().set(true); diff --git a/Telegram/SourceFiles/app.cpp b/Telegram/SourceFiles/app.cpp index 45efcad41..a4ac78929 100644 --- a/Telegram/SourceFiles/app.cpp +++ b/Telegram/SourceFiles/app.cpp @@ -234,7 +234,7 @@ namespace { UserData *feedUser(const MTPUser &user) { UserData *data = nullptr; - bool wasContact = false, minimal = false; + bool minimal = false; const MTPUserStatus *status = 0, emptyStatus = MTP_userStatusEmpty(); Notify::PeerUpdate update; @@ -247,7 +247,6 @@ namespace { auto peer = peerFromUser(d.vid.v); data = App::user(peer); auto canShareThisContact = data->canShareThisContactFast(); - wasContact = data->isContact(); data->input = MTP_inputPeerUser(d.vid, MTP_long(0)); data->inputUser = MTP_inputUser(d.vid, MTP_long(0)); @@ -255,12 +254,17 @@ namespace { data->setPhoto(MTP_userProfilePhotoEmpty()); //data->setFlags(MTPDuser_ClientFlag::f_inaccessible | 0); data->setFlags(MTPDuser::Flag::f_deleted); + if (!data->phone().isEmpty()) { + data->setPhone(QString()); + update.flags |= UpdateFlag::UserPhoneChanged; + } data->setBotInfoVersion(-1); status = &emptyStatus; - data->contact = -1; + data->setContactStatus(UserData::ContactStatus::PhoneUnknown); - if (canShareThisContact != data->canShareThisContactFast()) update.flags |= UpdateFlag::UserCanShareContact; - if (wasContact != data->isContact()) update.flags |= UpdateFlag::UserIsContact; + if (canShareThisContact != data->canShareThisContactFast()) { + update.flags |= UpdateFlag::UserCanShareContact; + } } break; case mtpc_user: { auto &d = user.c_user(); @@ -269,7 +273,6 @@ namespace { auto peer = peerFromUser(d.vid.v); data = App::user(peer); auto canShareThisContact = data->canShareThisContactFast(); - wasContact = data->isContact(); if (minimal) { auto mask = 0 //| MTPDuser_ClientFlag::f_inaccessible @@ -318,16 +321,30 @@ namespace { } bool nameChanged = (data->firstName != fname) || (data->lastName != lname); - bool showPhone = !isServiceUser(data->id) && !d.is_self() && !d.is_contact() && !d.is_mutual_contact(); - bool showPhoneChanged = !isServiceUser(data->id) && !d.is_self() && ((showPhone && data->contact) || (!showPhone && !data->contact)); + bool showPhone = !isServiceUser(data->id) + && !d.is_self() + && !d.is_contact() + && !d.is_mutual_contact(); + bool showPhoneChanged = !isServiceUser(data->id) + && !d.is_self() + && ((showPhone + && data->contactStatus() == UserData::ContactStatus::Contact) + || (!showPhone + && data->contactStatus() == UserData::ContactStatus::CanAdd)); if (minimal) { showPhoneChanged = false; - showPhone = !isServiceUser(data->id) && (data->id != Auth().userPeerId()) && !data->contact; + showPhone = !isServiceUser(data->id) + && (data->id != Auth().userPeerId()) + && (data->contactStatus() == UserData::ContactStatus::CanAdd); } // see also Local::readPeer - QString pname = (showPhoneChanged || phoneChanged || nameChanged) ? ((showPhone && !phone.isEmpty()) ? formatPhone(phone) : QString()) : data->nameOrPhone; + const auto pname = (showPhoneChanged || phoneChanged || nameChanged) + ? ((showPhone && !phone.isEmpty()) + ? formatPhone(phone) + : QString()) + : data->nameOrPhone; if (!minimal && d.is_self() && uname != data->username) { CrashReports::SetAnnotation("Username", uname); @@ -355,19 +372,20 @@ namespace { } else { data->setBotInfoVersion(-1); } - data->contact = (d.is_contact() || d.is_mutual_contact()) ? 1 : (data->phone().isEmpty() ? -1 : 0); - if (data->contact == 1 && cReportSpamStatuses().value(data->id, dbiprsHidden) != dbiprsHidden) { - cRefReportSpamStatuses().insert(data->id, dbiprsHidden); - Local::writeReportSpamStatuses(); - } + data->setContactStatus((d.is_contact() || d.is_mutual_contact()) + ? UserData::ContactStatus::Contact + : data->phone().isEmpty() + ? UserData::ContactStatus::PhoneUnknown + : UserData::ContactStatus::CanAdd); if (d.is_self() && ::self != data) { ::self = data; Global::RefSelfChanged().notify(); } } - if (canShareThisContact != data->canShareThisContactFast()) update.flags |= UpdateFlag::UserCanShareContact; - if (wasContact != data->isContact()) update.flags |= UpdateFlag::UserIsContact; + if (canShareThisContact != data->canShareThisContactFast()) { + update.flags |= UpdateFlag::UserCanShareContact; + } } break; } @@ -392,13 +410,12 @@ namespace { } } - if (data->contact < 0 && !data->phone().isEmpty() && data->id != Auth().userPeerId()) { - data->contact = 0; + if (data->contactStatus() == UserData::ContactStatus::PhoneUnknown + && !data->phone().isEmpty() + && data->id != Auth().userPeerId()) { + data->setContactStatus(UserData::ContactStatus::CanAdd); } if (App::main()) { - if ((data->contact > 0 && !wasContact) || (wasContact && data->contact < 1)) { - Notify::userIsContactChanged(data); - } if (update.flags) { update.peer = data; Notify::peerUpdatedDelayed(update); @@ -1082,43 +1099,40 @@ namespace { } void feedUserLink(MTPint userId, const MTPContactLink &myLink, const MTPContactLink &foreignLink) { - UserData *user = userLoaded(userId.v); - if (user) { - auto wasContact = user->isContact(); - bool wasShowPhone = !user->contact; + if (const auto user = userLoaded(userId.v)) { + const auto wasShowPhone = (user->contactStatus() == UserData::ContactStatus::CanAdd); switch (myLink.type()) { case mtpc_contactLinkContact: - user->contact = 1; - if (user->contact == 1 && cReportSpamStatuses().value(user->id, dbiprsHidden) != dbiprsHidden) { - cRefReportSpamStatuses().insert(user->id, dbiprsHidden); - Local::writeReportSpamStatuses(); - } + user->setContactStatus(UserData::ContactStatus::Contact); break; case mtpc_contactLinkHasPhone: - user->contact = 0; + user->setContactStatus(UserData::ContactStatus::CanAdd); break; case mtpc_contactLinkNone: case mtpc_contactLinkUnknown: - user->contact = -1; + user->setContactStatus(UserData::ContactStatus::PhoneUnknown); break; } - if (user->contact < 1) { - if (user->contact < 0 && !user->phone().isEmpty() && user->id != Auth().userPeerId()) { - user->contact = 0; - } + if (user->contactStatus() == UserData::ContactStatus::PhoneUnknown + && !user->phone().isEmpty() + && user->id != Auth().userPeerId()) { + user->setContactStatus(UserData::ContactStatus::CanAdd); } - if (wasContact != user->isContact()) { - Notify::peerUpdatedDelayed(user, Notify::PeerUpdate::Flag::UserIsContact); - } - if ((user->contact > 0 && !wasContact) || (wasContact && user->contact < 1)) { - Notify::userIsContactChanged(user); - } - - bool showPhone = !isServiceUser(user->id) && !user->isSelf() && !user->contact; - bool showPhoneChanged = !isServiceUser(user->id) && !user->isSelf() && ((showPhone && !wasShowPhone) || (!showPhone && wasShowPhone)); + const auto showPhone = !isServiceUser(user->id) + && !user->isSelf() + && user->contactStatus() == UserData::ContactStatus::CanAdd; + const auto showPhoneChanged = !isServiceUser(user->id) + && !user->isSelf() + && (showPhone != wasShowPhone); if (showPhoneChanged) { - user->setName(TextUtilities::SingleLine(user->firstName), TextUtilities::SingleLine(user->lastName), showPhone ? App::formatPhone(user->phone()) : QString(), TextUtilities::SingleLine(user->username)); + user->setName( + TextUtilities::SingleLine(user->firstName), + TextUtilities::SingleLine(user->lastName), + showPhone + ? App::formatPhone(user->phone()) + : QString(), + TextUtilities::SingleLine(user->username)); } } } diff --git a/Telegram/SourceFiles/auth_session.cpp b/Telegram/SourceFiles/auth_session.cpp index d3465c22d..928c3048e 100644 --- a/Telegram/SourceFiles/auth_session.cpp +++ b/Telegram/SourceFiles/auth_session.cpp @@ -23,6 +23,7 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL #include "window/section_widget.h" #include "chat_helpers/tabbed_selector.h" #include "boxes/send_files_box.h" +#include "observer_peer.h" namespace { @@ -30,6 +31,20 @@ constexpr auto kAutoLockTimeoutLateMs = TimeMs(3000); } // namespace +AuthSessionData::AuthSessionData() { + Notify::PeerUpdateViewer( + Notify::PeerUpdate::Flag::UserIsContact + ) | rpl::map([](const Notify::PeerUpdate &update) { + return update.peer->asUser(); + }) | rpl::filter([](UserData *user) { + return user != nullptr; + }) | rpl::start_with_next([=](not_null user) { + userIsContactUpdated(user); + }, _lifetime); +} + +AuthSessionData::~AuthSessionData() = default; + AuthSessionData::Variables::Variables() : sendFilesWay(SendFilesWay::Album) , selectorTab(ChatHelpers::SelectorTab::Emoji) @@ -354,6 +369,16 @@ rpl::producer<> AuthSessionData::savedGifsUpdated() const { return _savedGifsUpdated.events(); } +void AuthSessionData::userIsContactUpdated(not_null user) { + const auto &items = App::sharedContactItems(); + const auto i = items.constFind(peerToUser(user->id)); + if (i != items.cend()) { + for (const auto item : std::as_const(i.value())) { + item->setPendingInitDimensions(); + } + } +} + HistoryItemsList AuthSessionData::idsToItems( const MessageIdsList &ids) const { return ranges::view::all( diff --git a/Telegram/SourceFiles/auth_session.h b/Telegram/SourceFiles/auth_session.h index dea84ad3f..0b78a1456 100644 --- a/Telegram/SourceFiles/auth_session.h +++ b/Telegram/SourceFiles/auth_session.h @@ -43,6 +43,9 @@ class Changelogs; class AuthSessionData final { public: + AuthSessionData(); + ~AuthSessionData(); + base::Variable &contactsLoaded() { return _contactsLoaded; } @@ -293,6 +296,7 @@ private: return (lastUpdate == 0) || (now >= lastUpdate + kStickersUpdateTimeout); } + void userIsContactUpdated(not_null user); base::Variable _contactsLoaded = { false }; base::Variable _allChatsLoaded = { false }; @@ -328,6 +332,8 @@ private: Variables _variables; TimeMs _lastTimeVideoPlayedAt = 0; + rpl::lifetime _lifetime; + }; // One per Messenger. diff --git a/Telegram/SourceFiles/boxes/add_contact_box.cpp b/Telegram/SourceFiles/boxes/add_contact_box.cpp index a8b257c9a..c9c1fd19f 100644 --- a/Telegram/SourceFiles/boxes/add_contact_box.cpp +++ b/Telegram/SourceFiles/boxes/add_contact_box.cpp @@ -241,19 +241,23 @@ bool AddContactBox::onSaveUserFail(const RPCError &error) { void AddContactBox::onImportDone(const MTPcontacts_ImportedContacts &res) { if (!isBoxShown() || !App::main()) return; - auto &d = res.c_contacts_importedContacts(); + const auto &d = res.c_contacts_importedContacts(); App::feedUsers(d.vusers); - auto &v = d.vimported.v; - UserData *user = nullptr; - if (!v.isEmpty()) { - auto &c = v.front().c_importedContact(); - if (c.vclient_id.v != _contactId) return; - - user = App::userLoaded(c.vuser_id.v); - } + const auto &v = d.vimported.v; + const auto user = [&]() -> UserData* { + if (!v.isEmpty()) { + auto &c = v.front().c_importedContact(); + if (c.vclient_id.v == _contactId) { + return App::userLoaded(c.vuser_id.v); + } + } + return nullptr; + }(); if (user) { - Notify::userIsContactChanged(user, true); + if (user->contactStatus() == UserData::ContactStatus::Contact) { + Ui::showPeerHistory(user, ShowAtTheEndMsgId); + } Ui::hideLayer(); } else { hideChildren(); diff --git a/Telegram/SourceFiles/data/data_peer.cpp b/Telegram/SourceFiles/data/data_peer.cpp index 2e2a979e4..a0546747a 100644 --- a/Telegram/SourceFiles/data/data_peer.cpp +++ b/Telegram/SourceFiles/data/data_peer.cpp @@ -22,6 +22,7 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL #include "messenger.h" #include "mainwindow.h" #include "window/window_controller.h" +#include "storage/localstorage.h" #include "ui/empty_userpic.h" #include "ui/text_options.h" @@ -348,7 +349,26 @@ const Text &BotCommand::descriptionText() const { } bool UserData::canShareThisContact() const { - return canShareThisContactFast() || !App::phoneFromSharedContact(peerToUser(id)).isEmpty(); + return canShareThisContactFast() + || !App::phoneFromSharedContact(peerToUser(id)).isEmpty(); +} + +void UserData::setContactStatus(ContactStatus status) { + if (_contactStatus != status) { + const auto changed = (_contactStatus == ContactStatus::Contact) + != (status == ContactStatus::Contact); + _contactStatus = status; + if (changed) { + Notify::peerUpdatedDelayed( + this, + Notify::PeerUpdate::Flag::UserIsContact); + } + } + if (_contactStatus == ContactStatus::Contact + && cReportSpamStatuses().value(id, dbiprsHidden) != dbiprsHidden) { + cRefReportSpamStatuses().insert(id, dbiprsHidden); + Local::writeReportSpamStatuses(); + } } // see Local::readPeer as well diff --git a/Telegram/SourceFiles/data/data_peer.h b/Telegram/SourceFiles/data/data_peer.h index 3a0b96c56..d17a8d13a 100644 --- a/Telegram/SourceFiles/data/data_peer.h +++ b/Telegram/SourceFiles/data/data_peer.h @@ -384,7 +384,7 @@ public: return !isInaccessible(); } bool isContact() const { - return (contact > 0); + return (_contactStatus == ContactStatus::Contact); } bool canShareThisContact() const; @@ -410,9 +410,18 @@ public: QString nameOrPhone; Text phoneText; TimeId onlineTill = 0; - int32 contact = -1; // -1 - not contact, cant add (self, empty, deleted, foreign), 0 - not contact, can add (request), 1 - contact - enum class BlockStatus { + enum class ContactStatus : char { + PhoneUnknown, + CanAdd, + Contact, + }; + ContactStatus contactStatus() const { + return _contactStatus; + } + void setContactStatus(ContactStatus status); + + enum class BlockStatus : char { Unknown, Blocked, NotBlocked, @@ -425,7 +434,7 @@ public: } void setBlockStatus(BlockStatus blockStatus); - enum class CallsStatus { + enum class CallsStatus : char { Unknown, Enabled, Disabled, @@ -461,6 +470,7 @@ private: QString _restrictionReason; QString _about; QString _phone; + ContactStatus _contactStatus = ContactStatus::PhoneUnknown; BlockStatus _blockStatus = BlockStatus::Unknown; CallsStatus _callsStatus = CallsStatus::Unknown; int _commonChatsCount = 0; diff --git a/Telegram/SourceFiles/dialogs/dialogs_inner_widget.cpp b/Telegram/SourceFiles/dialogs/dialogs_inner_widget.cpp index f80bd0261..379263a3b 100644 --- a/Telegram/SourceFiles/dialogs/dialogs_inner_widget.cpp +++ b/Telegram/SourceFiles/dialogs/dialogs_inner_widget.cpp @@ -109,7 +109,8 @@ DialogsInner::DialogsInner(QWidget *parent, not_null contro using UpdateFlag = Notify::PeerUpdate::Flag; auto changes = UpdateFlag::PinnedChanged | UpdateFlag::NameChanged - | UpdateFlag::PhotoChanged; + | UpdateFlag::PhotoChanged + | UpdateFlag::UserIsContact; subscribe(Notify::PeerUpdated(), Notify::PeerUpdatedHandler(changes, [this](const Notify::PeerUpdate &update) { if (update.flags & UpdateFlag::PinnedChanged) { stopReorderPinned(); @@ -121,6 +122,11 @@ DialogsInner::DialogsInner(QWidget *parent, not_null contro this->update(); emit App::main()->dialogsUpdated(); } + if (update.flags & UpdateFlag::UserIsContact) { + if (const auto user = update.peer->asUser()) { + userIsContactUpdated(user); + } + } })); refresh(); @@ -1073,6 +1079,7 @@ void DialogsInner::createDialog(History *history) { void DialogsInner::removeDialog(History *history) { if (!history) return; + if (history->peer == _menuPeer && _menu) { InvokeQueued(this, [this] { _menu = nullptr; }); } @@ -1636,20 +1643,17 @@ void DialogsInner::peerSearchReceived(const QString &query, const QVector user) { if (user->loadedStatus != PeerData::FullLoaded) { - LOG(("API Error: notify_userIsContactChanged() called for a not loaded user!")); + LOG(("API Error: " + "notify_userIsContactChanged() called for a not loaded user!")); return; } - if (user->contact > 0) { - auto history = App::history(user->id); + if (user->contactStatus() == UserData::ContactStatus::Contact) { + const auto history = App::history(user->id); _contacts->addByName(history); - if (auto row = shownDialogs()->getRow(user->id)) { - if (fromThisApp) { - _selected = row; - _importantSwitchSelected = false; - } - } else if (!_dialogs->contains(user->id)) { + if (!shownDialogs()->getRow(user->id) + && !_dialogs->contains(user->id)) { _contactsNoDialogs->addByName(history); } } else { diff --git a/Telegram/SourceFiles/dialogs/dialogs_inner_widget.h b/Telegram/SourceFiles/dialogs/dialogs_inner_widget.h index 1f1b14b05..6482300bc 100644 --- a/Telegram/SourceFiles/dialogs/dialogs_inner_widget.h +++ b/Telegram/SourceFiles/dialogs/dialogs_inner_widget.h @@ -95,7 +95,6 @@ public: base::Observable searchFromUserChanged; - void notify_userIsContactChanged(UserData *user, bool fromThisApp); void notify_historyMuteUpdated(History *history); ~DialogsInner(); @@ -138,6 +137,7 @@ private: struct PeerSearchResult; using PeerSearchResults = std::vector>; + void userIsContactUpdated(not_null user); void mousePressReleased(Qt::MouseButton button); void clearIrrelevantState(); void updateSelected() { diff --git a/Telegram/SourceFiles/dialogs/dialogs_widget.cpp b/Telegram/SourceFiles/dialogs/dialogs_widget.cpp index f9f01ba71..2d5dd803a 100644 --- a/Telegram/SourceFiles/dialogs/dialogs_widget.cpp +++ b/Telegram/SourceFiles/dialogs/dialogs_widget.cpp @@ -300,15 +300,6 @@ void DialogsWidget::onCancel() { } } -void DialogsWidget::notify_userIsContactChanged(UserData *user, bool fromThisApp) { - if (fromThisApp) { - _filter->setText(QString()); - _filter->updatePlaceholder(); - onFilterUpdate(); - } - _inner->notify_userIsContactChanged(user, fromThisApp); -} - void DialogsWidget::notify_historyMuteUpdated(History *history) { _inner->notify_historyMuteUpdated(history); } diff --git a/Telegram/SourceFiles/dialogs/dialogs_widget.h b/Telegram/SourceFiles/dialogs/dialogs_widget.h index b031db3eb..2768936f9 100644 --- a/Telegram/SourceFiles/dialogs/dialogs_widget.h +++ b/Telegram/SourceFiles/dialogs/dialogs_widget.h @@ -88,7 +88,6 @@ public: bool wheelEventFromFloatPlayer(QEvent *e) override; QRect rectForFloatPlayer() const override; - void notify_userIsContactChanged(UserData *user, bool fromThisApp); void notify_historyMuteUpdated(History *history); signals: diff --git a/Telegram/SourceFiles/facades.cpp b/Telegram/SourceFiles/facades.cpp index 905b17873..98e8ee6bd 100644 --- a/Telegram/SourceFiles/facades.cpp +++ b/Telegram/SourceFiles/facades.cpp @@ -302,10 +302,6 @@ void userIsBotChanged(UserData *user) { if (MainWidget *m = App::main()) m->notify_userIsBotChanged(user); } -void userIsContactChanged(UserData *user, bool fromThisApp) { - if (MainWidget *m = App::main()) m->notify_userIsContactChanged(user, fromThisApp); -} - void botCommandsChanged(UserData *user) { if (MainWidget *m = App::main()) { m->notify_botCommandsChanged(user); diff --git a/Telegram/SourceFiles/facades.h b/Telegram/SourceFiles/facades.h index b9f55634b..a892e051d 100644 --- a/Telegram/SourceFiles/facades.h +++ b/Telegram/SourceFiles/facades.h @@ -219,7 +219,6 @@ enum ClipStopperType { namespace Notify { void userIsBotChanged(UserData *user); -void userIsContactChanged(UserData *user, bool fromThisApp = false); void botCommandsChanged(UserData *user); void inlineBotRequesting(bool requesting); diff --git a/Telegram/SourceFiles/history/history_media_types.cpp b/Telegram/SourceFiles/history/history_media_types.cpp index 340733dee..a4e4f2408 100644 --- a/Telegram/SourceFiles/history/history_media_types.cpp +++ b/Telegram/SourceFiles/history/history_media_types.cpp @@ -3286,7 +3286,8 @@ void HistoryContact::initDimensions() { Data::PeerUserpicColor(_userId ? _userId : _parent->id), _name.originalText()); } - if (_contact && _contact->contact > 0) { + if (_contact + && _contact->contactStatus() == UserData::ContactStatus::Contact) { _linkl = sendMessageClickHandler(_contact); _link = lang(lng_profile_send_message).toUpper(); } else if (_userId) { diff --git a/Telegram/SourceFiles/history/history_widget.cpp b/Telegram/SourceFiles/history/history_widget.cpp index 4a4c8c2d5..5b76aca2c 100644 --- a/Telegram/SourceFiles/history/history_widget.cpp +++ b/Telegram/SourceFiles/history/history_widget.cpp @@ -1864,7 +1864,8 @@ void HistoryWidget::updateReportSpamStatus() { if (i != cReportSpamStatuses().cend()) { if (i.value() == dbiprsNoButton) { setReportSpamStatus(dbiprsHidden); - if (!_peer->isUser() || _peer->asUser()->contact < 1) { + if (!_peer->isUser() + || _peer->asUser()->contactStatus() != UserData::ContactStatus::Contact) { MTP::send(MTPmessages_HideReportSpam(_peer->input)); } @@ -1882,7 +1883,8 @@ void HistoryWidget::updateReportSpamStatus() { if (i != cReportSpamStatuses().cend()) { if (i.value() == dbiprsNoButton) { setReportSpamStatus(dbiprsHidden); - if (!_peer->isUser() || _peer->asUser()->contact < 1) { + if (!_peer->isUser() + || _peer->asUser()->contactStatus() != UserData::ContactStatus::Contact) { MTP::send(MTPmessages_HideReportSpam(_peer->input)); } } else { @@ -1900,7 +1902,8 @@ void HistoryWidget::updateReportSpamStatus() { auto status = dbiprsRequesting; if (!Auth().data().contactsLoaded().value() || _firstLoadRequest) { status = dbiprsUnknown; - } else if (_peer->isUser() && _peer->asUser()->contact > 0) { + } else if (_peer->isUser() + && _peer->asUser()->contactStatus() == UserData::ContactStatus::Contact) { status = dbiprsHidden; } else { requestReportSpamSetting(); diff --git a/Telegram/SourceFiles/mainwidget.cpp b/Telegram/SourceFiles/mainwidget.cpp index 5ec896863..b35abe7c9 100644 --- a/Telegram/SourceFiles/mainwidget.cpp +++ b/Telegram/SourceFiles/mainwidget.cpp @@ -829,24 +829,6 @@ void MainWidget::notify_userIsBotChanged(UserData *bot) { _history->notify_userIsBotChanged(bot); } -void MainWidget::notify_userIsContactChanged(UserData *user, bool fromThisApp) { - if (!user) return; - - _dialogs->notify_userIsContactChanged(user, fromThisApp); - - const SharedContactItems &items(App::sharedContactItems()); - SharedContactItems::const_iterator i = items.constFind(peerToUser(user->id)); - if (i != items.cend()) { - for_const (auto item, i.value()) { - item->setPendingInitDimensions(); - } - } - - if (user->contact > 0 && fromThisApp) { - Ui::showPeerHistory(user->id, ShowAtTheEndMsgId); - } -} - void MainWidget::notify_migrateUpdated(PeerData *peer) { _history->notify_migrateUpdated(peer); } @@ -4528,7 +4510,9 @@ void MainWidget::updateOnline(bool gotOtherOffline) { if (App::self()) { App::self()->onlineTill = unixtime() + (isOnline ? (Global::OnlineUpdatePeriod() / 1000) : -1); - Notify::peerUpdatedDelayed(App::self(), Notify::PeerUpdate::Flag::UserOnlineChanged); + Notify::peerUpdatedDelayed( + App::self(), + Notify::PeerUpdate::Flag::UserOnlineChanged); } if (!isOnline) { // Went offline, so we need to save message draft to the cloud. saveDraftToCloud(); @@ -5157,7 +5141,9 @@ void MainWidget::feedUpdate(const MTPUpdate &update) { case mtpc_userStatusOffline: user->onlineTill = d.vstatus.c_userStatusOffline().vwas_online.v; break; case mtpc_userStatusOnline: user->onlineTill = d.vstatus.c_userStatusOnline().vexpires.v; break; } - Notify::peerUpdatedDelayed(user, Notify::PeerUpdate::Flag::UserOnlineChanged); + Notify::peerUpdatedDelayed( + user, + Notify::PeerUpdate::Flag::UserOnlineChanged); } if (d.vuser_id.v == Auth().userId()) { if (d.vstatus.type() == mtpc_userStatusOffline || d.vstatus.type() == mtpc_userStatusEmpty) { @@ -5174,10 +5160,18 @@ void MainWidget::feedUpdate(const MTPUpdate &update) { case mtpc_updateUserName: { auto &d = update.c_updateUserName(); if (auto user = App::userLoaded(d.vuser_id.v)) { - if (user->contact <= 0) { - user->setName(TextUtilities::SingleLine(qs(d.vfirst_name)), TextUtilities::SingleLine(qs(d.vlast_name)), user->nameOrPhone, TextUtilities::SingleLine(qs(d.vusername))); + if (user->contactStatus() != UserData::ContactStatus::Contact) { + user->setName( + TextUtilities::SingleLine(qs(d.vfirst_name)), + TextUtilities::SingleLine(qs(d.vlast_name)), + user->nameOrPhone, + TextUtilities::SingleLine(qs(d.vusername))); } else { - user->setName(TextUtilities::SingleLine(user->firstName), TextUtilities::SingleLine(user->lastName), user->nameOrPhone, TextUtilities::SingleLine(qs(d.vusername))); + user->setName( + TextUtilities::SingleLine(user->firstName), + TextUtilities::SingleLine(user->lastName), + user->nameOrPhone, + TextUtilities::SingleLine(qs(d.vusername))); } } } break; @@ -5233,9 +5227,20 @@ void MainWidget::feedUpdate(const MTPUpdate &update) { auto newPhone = qs(d.vphone); if (newPhone != user->phone()) { user->setPhone(newPhone); - user->setName(user->firstName, user->lastName, (user->contact || isServiceUser(user->id) || user->isSelf() || user->phone().isEmpty()) ? QString() : App::formatPhone(user->phone()), user->username); + user->setName( + user->firstName, + user->lastName, + ((user->contactStatus() == UserData::ContactStatus::Contact + || isServiceUser(user->id) + || user->isSelf() + || user->phone().isEmpty()) + ? QString() + : App::formatPhone(user->phone())), + user->username); - Notify::peerUpdatedDelayed(user, Notify::PeerUpdate::Flag::UserPhoneChanged); + Notify::peerUpdatedDelayed( + user, + Notify::PeerUpdate::Flag::UserPhoneChanged); } } } break; diff --git a/Telegram/SourceFiles/mainwidget.h b/Telegram/SourceFiles/mainwidget.h index d4b76523b..fc3d57140 100644 --- a/Telegram/SourceFiles/mainwidget.h +++ b/Telegram/SourceFiles/mainwidget.h @@ -330,7 +330,6 @@ public: void notify_inlineKeyboardMoved(const HistoryItem *item, int oldKeyboardTop, int newKeyboardTop); bool notify_switchInlineBotButtonReceived(const QString &query, UserData *samePeerBot, MsgId samePeerReplyTo); void notify_userIsBotChanged(UserData *bot); - void notify_userIsContactChanged(UserData *user, bool fromThisApp); void notify_migrateUpdated(PeerData *peer); void notify_historyMuteUpdated(History *history); @@ -431,6 +430,7 @@ private: [[nodiscard]] bool saveThirdSectionToStackBack() const; [[nodiscard]] auto thirdSectionForCurrentMainSection( not_null peer) -> std::unique_ptr; + void userIsContactUpdated(not_null user); void createPlayer(); void switchToPanelPlayer(); diff --git a/Telegram/SourceFiles/storage/localstorage.cpp b/Telegram/SourceFiles/storage/localstorage.cpp index 910db6860..06e9796f0 100644 --- a/Telegram/SourceFiles/storage/localstorage.cpp +++ b/Telegram/SourceFiles/storage/localstorage.cpp @@ -4007,20 +4007,52 @@ void _writePeer(QDataStream &stream, PeerData *peer) { stream << quint64(peer->id) << quint64(peer->userpicPhotoId()); Serialize::writeStorageImageLocation(stream, peer->userpicLocation()); if (const auto user = peer->asUser()) { - stream << user->firstName << user->lastName << user->phone() << user->username << quint64(user->accessHash()); + stream + << user->firstName + << user->lastName + << user->phone() + << user->username + << quint64(user->accessHash()); if (AppVersion >= 9012) { stream << qint32(user->flags()); } if (AppVersion >= 9016) { - stream << (user->botInfo ? user->botInfo->inlinePlaceholder : QString()); + const auto botInlinePlaceholder = user->botInfo + ? user->botInfo->inlinePlaceholder + : QString(); + stream << botInlinePlaceholder; } - stream << qint32(user->onlineTill) << qint32(user->contact) << qint32(user->botInfo ? user->botInfo->version : -1); + const auto contactSerialized = [&] { + switch (user->contactStatus()) { + case UserData::ContactStatus::Contact: return 1; + case UserData::ContactStatus::CanAdd: return 0; + case UserData::ContactStatus::PhoneUnknown: return -1; + } + Unexpected("contactStatus in _writePeer()"); + }(); + stream + << qint32(user->onlineTill) + << qint32(contactSerialized) + << qint32(user->botInfo ? user->botInfo->version : -1); } else if (const auto chat = peer->asChat()) { - stream << chat->name << qint32(chat->count) << qint32(chat->date) << qint32(chat->version) << qint32(chat->creator); - stream << qint32(0) << quint32(chat->flags()) << chat->inviteLink(); - } else if (auto channel = peer->asChannel()) { - stream << channel->name << quint64(channel->access) << qint32(channel->date) << qint32(channel->version); - stream << qint32(0) << quint32(channel->flags()) << channel->inviteLink(); + stream + << chat->name + << qint32(chat->count) + << qint32(chat->date) + << qint32(chat->version) + << qint32(chat->creator) + << qint32(0) + << quint32(chat->flags()) + << chat->inviteLink(); + } else if (const auto channel = peer->asChannel()) { + stream + << channel->name + << quint64(channel->access) + << qint32(channel->date) + << qint32(channel->version) + << qint32(0) + << quint32(channel->flags()) + << channel->inviteLink(); } } @@ -4049,8 +4081,12 @@ PeerData *_readPeer(FileReadDescriptor &from, int32 fileVersion = 0) { } from.stream >> onlineTill >> contact >> botInfoVersion; - bool showPhone = !isServiceUser(user->id) && (user->id != Auth().userPeerId()) && (contact <= 0); - QString pname = (showPhone && !phone.isEmpty()) ? App::formatPhone(phone) : QString(); + const auto showPhone = !isServiceUser(user->id) + && (user->id != Auth().userPeerId()) + && (contact <= 0); + const auto pname = (showPhone && !phone.isEmpty()) + ? App::formatPhone(phone) + : QString(); if (!wasLoaded) { user->setPhone(phone); @@ -4059,7 +4095,11 @@ PeerData *_readPeer(FileReadDescriptor &from, int32 fileVersion = 0) { user->setFlags(MTPDuser::Flags::from_raw(flags)); user->setAccessHash(access); user->onlineTill = onlineTill; - user->contact = contact; + user->setContactStatus((contact > 0) + ? UserData::ContactStatus::Contact + : (contact == 0) + ? UserData::ContactStatus::CanAdd + : UserData::ContactStatus::PhoneUnknown); user->setBotInfoVersion(botInfoVersion); if (!inlinePlaceholder.isEmpty() && user->botInfo) { user->botInfo->inlinePlaceholder = inlinePlaceholder;