From 72a35ba58b6f6060616229fd0e29fd7108684607 Mon Sep 17 00:00:00 2001 From: John Preston Date: Fri, 7 Mar 2025 18:26:27 +0400 Subject: [PATCH] Show new-peer-info photo/name change. --- Telegram/SourceFiles/data/data_types.h | 2 + Telegram/SourceFiles/history/history.cpp | 96 +++++++++++++++++++ Telegram/SourceFiles/history/history.h | 4 + Telegram/SourceFiles/history/history_item.h | 3 + .../history/view/history_view_element.cpp | 3 + 5 files changed, 108 insertions(+) diff --git a/Telegram/SourceFiles/data/data_types.h b/Telegram/SourceFiles/data/data_types.h index cf9f6870a..9f0562e1b 100644 --- a/Telegram/SourceFiles/data/data_types.h +++ b/Telegram/SourceFiles/data/data_types.h @@ -349,6 +349,8 @@ enum class MessageFlag : uint64 { EstimatedDate = (1ULL << 49), ReactionsAllowed = (1ULL << 50), + + HideDisplayDate = (1ULL << 51), }; inline constexpr bool is_flag_type(MessageFlag) { return true; } using MessageFlags = base::flags; diff --git a/Telegram/SourceFiles/history/history.cpp b/Telegram/SourceFiles/history/history.cpp index 129eed4de..1eb7d9107 100644 --- a/Telegram/SourceFiles/history/history.cpp +++ b/Telegram/SourceFiles/history/history.cpp @@ -137,6 +137,10 @@ void History::setHasPendingResizedItems() { void History::itemRemoved(not_null item) { if (item == _joinedMessage) { _joinedMessage = nullptr; + } else if (item == _newPeerNameChange) { + _newPeerNameChange = nullptr; + } else if (item == _newPeerPhotoChange) { + _newPeerPhotoChange = nullptr; } item->removeMainView(); if (_lastServerMessage == item) { @@ -1229,6 +1233,8 @@ void History::mainViewRemoved( not_null block, not_null view) { Expects(_joinedMessage != view->data()); + Expects(_newPeerNameChange != view->data()); + Expects(_newPeerPhotoChange != view->data()); if (_firstUnreadView == view) { getNextFirstUnreadMessage(); @@ -3243,6 +3249,85 @@ HistoryItem *History::insertJoinedMessage() { return _joinedMessage; } +void History::checkNewPeerMessages() { + if (!loadedAtTop()) { + return; + } + const auto user = peer->asUser(); + if (!user) { + return; + } + const auto photo = user->photoChangeDate(); + const auto name = user->nameChangeDate(); + if (!photo && _newPeerPhotoChange) { + _newPeerPhotoChange->destroy(); + } + if (!name && _newPeerNameChange) { + _newPeerNameChange->destroy(); + } + if ((!photo || _newPeerPhotoChange) && (!name || _newPeerNameChange)) { + return; + } + + const auto when = [](TimeId date) { + const auto now = base::unixtime::now(); + const auto passed = now - date; + if (passed < 3600) { + return tr::lng_new_contact_updated_now(tr::now); + } else if (passed < 24 * 3600) { + return tr::lng_new_contact_updated_hours( + tr::now, + lt_count, + (passed / 3600)); + } else if (passed < 60 * 24 * 3600) { + return tr::lng_new_contact_updated_days( + tr::now, + lt_count, + (passed / (24 * 3600))); + } + return tr::lng_new_contact_updated_months( + tr::now, + lt_count, + (passed / (30 * 24 * 3600))); + }; + + auto firstDate = TimeId(); + for (const auto &block : blocks) { + for (const auto &message : block->messages) { + const auto item = message->data(); + if (item != _newPeerPhotoChange && item != _newPeerNameChange) { + firstDate = item->date(); + break; + } + } + if (firstDate) { + break; + } + } + if (!firstDate) { + firstDate = base::unixtime::serialize( + QDateTime(QDate(2013, 8, 1), QTime(0, 0))); + } + const auto add = [&](tr::phrase phrase, TimeId date) { + const auto result = makeMessage({ + .id = owner().nextLocalMessageId(), + .flags = MessageFlag::Local | MessageFlag::HideDisplayDate, + .date = (--firstDate), + }, PreparedServiceText{ TextWithEntities{ + phrase(tr::now, lt_when, when(date)), + } }); + insertMessageToBlocks(result); + return result; + }; + + if (photo && !_newPeerPhotoChange) { + _newPeerPhotoChange = add(tr::lng_new_contact_updated_photo, photo); + } + if (name && !_newPeerNameChange) { + _newPeerNameChange = add(tr::lng_new_contact_updated_name, name); + } +} + void History::insertMessageToBlocks(not_null item) { Expects(item->mainView() == nullptr); @@ -3296,6 +3381,8 @@ void History::checkLocalMessages() { && peer->asChannel()->inviter && goodDate(peer->asChannel()->inviteDate)) { insertJoinedMessage(); + } else { + checkNewPeerMessages(); } } @@ -3309,6 +3396,15 @@ void History::removeJoinedMessage() { } } +void History::removeNewPeerMessages() { + if (_newPeerNameChange) { + _newPeerNameChange->destroy(); + } + if (_newPeerPhotoChange) { + _newPeerPhotoChange->destroy(); + } +} + void History::reactionsEnabledChanged(bool enabled) { if (!enabled) { for (const auto &item : _items) { diff --git a/Telegram/SourceFiles/history/history.h b/Telegram/SourceFiles/history/history.h index af7d86a4a..57b1203d0 100644 --- a/Telegram/SourceFiles/history/history.h +++ b/Telegram/SourceFiles/history/history.h @@ -82,6 +82,7 @@ public: [[nodiscard]] HistoryItem *joinedMessageInstance() const; void checkLocalMessages(); void removeJoinedMessage(); + void removeNewPeerMessages(); void reactionsEnabledChanged(bool enabled); @@ -545,6 +546,7 @@ private: HistoryItem *insertJoinedMessage(); void insertMessageToBlocks(not_null item); + void checkNewPeerMessages(); [[nodiscard]] Dialogs::BadgesState computeBadgesState() const; [[nodiscard]] Dialogs::BadgesState adjustBadgesStateByFolder( @@ -563,6 +565,8 @@ private: Element *_unreadBarView = nullptr; Element *_firstUnreadView = nullptr; HistoryItem *_joinedMessage = nullptr; + HistoryItem *_newPeerNameChange = nullptr; + HistoryItem *_newPeerPhotoChange = nullptr; bool _loadedAtTop = false; bool _loadedAtBottom = true; diff --git a/Telegram/SourceFiles/history/history_item.h b/Telegram/SourceFiles/history/history_item.h index 8649400bc..06a115695 100644 --- a/Telegram/SourceFiles/history/history_item.h +++ b/Telegram/SourceFiles/history/history_item.h @@ -322,6 +322,9 @@ public: [[nodiscard]] bool hideEditedBadge() const { return (_flags & MessageFlag::HideEdited); } + [[nodiscard]] bool hideDisplayDate() const { + return (_flags & MessageFlag::HideDisplayDate); + } [[nodiscard]] bool isLocal() const { return _flags & MessageFlag::Local; } diff --git a/Telegram/SourceFiles/history/view/history_view_element.cpp b/Telegram/SourceFiles/history/view/history_view_element.cpp index cd8139c9e..e78b6c65d 100644 --- a/Telegram/SourceFiles/history/view/history_view_element.cpp +++ b/Telegram/SourceFiles/history/view/history_view_element.cpp @@ -1427,6 +1427,9 @@ bool Element::countIsTopicRootReply() const { void Element::setDisplayDate(bool displayDate) { const auto item = data(); + if (item->hideDisplayDate()) { + displayDate = false; + } if (displayDate && !Has()) { AddComponents(DateBadge::Bit()); Get()->init(