diff --git a/Telegram/SourceFiles/apiwrap.cpp b/Telegram/SourceFiles/apiwrap.cpp index b7425050c..2e376960b 100644 --- a/Telegram/SourceFiles/apiwrap.cpp +++ b/Telegram/SourceFiles/apiwrap.cpp @@ -387,13 +387,12 @@ void ApiWrap::gotChatFull(PeerData *peer, const MTPmessages_ChatFull &result, mt } accumulate_max(h->outboxReadBefore, f.vread_outbox_max_id.v + 1); } + if (f.has_pinned_msg_id()) { + channel->setPinnedMessageId(f.vpinned_msg_id.v); + } else { + channel->clearPinnedMessage(); + } if (channel->isMegagroup()) { - if (f.has_pinned_msg_id()) { - channel->mgInfo->pinnedMsgId = f.vpinned_msg_id.v; - } else { - channel->mgInfo->pinnedMsgId = 0; - } - auto stickersChanged = (canEditStickers != channel->canEditStickers()); auto stickerSet = (f.has_stickerset() ? &f.vstickerset.c_stickerSet() : nullptr); auto newSetId = (stickerSet ? stickerSet->vid.v : 0); diff --git a/Telegram/SourceFiles/history/history.cpp b/Telegram/SourceFiles/history/history.cpp index 8db28ab46..ed52c040a 100644 --- a/Telegram/SourceFiles/history/history.cpp +++ b/Telegram/SourceFiles/history/history.cpp @@ -1119,9 +1119,10 @@ HistoryItem *History::createItem(const MTPMessage &msg, bool applyServiceAction, } break; case mtpc_messageActionPinMessage: { - if (m.has_reply_to_msg_id() && result && result->history()->peer->isMegagroup()) { - result->history()->peer->asChannel()->mgInfo->pinnedMsgId = m.vreply_to_msg_id.v; - Notify::peerUpdatedDelayed(result->history()->peer, Notify::PeerUpdate::Flag::ChannelPinnedChanged); + if (m.has_reply_to_msg_id() && result) { + if (auto channel = result->history()->peer->asChannel()) { + channel->setPinnedMessageId(m.vreply_to_msg_id.v); + } } } break; @@ -2256,8 +2257,8 @@ void History::clear(bool leaveItems) { lastKeyboardInited = false; } else { setUnreadCount(0); - if (peer->isMegagroup()) { - peer->asChannel()->mgInfo->pinnedMsgId = 0; + if (auto channel = peer->asChannel()) { + channel->clearPinnedMessage(); } clearLastKeyboard(); } diff --git a/Telegram/SourceFiles/history/history_inner_widget.cpp b/Telegram/SourceFiles/history/history_inner_widget.cpp index 810607fb6..99e1d9165 100644 --- a/Telegram/SourceFiles/history/history_inner_widget.cpp +++ b/Telegram/SourceFiles/history/history_inner_widget.cpp @@ -1240,7 +1240,7 @@ void HistoryInner::showContextMenu(QContextMenuEvent *e, bool showFromTouch) { _menu->addAction(lang(lng_context_edit_msg), _widget, SLOT(onEditMessage())); } if (item->canPin()) { - bool ispinned = (item->history()->peer->asChannel()->mgInfo->pinnedMsgId == item->id); + bool ispinned = (item->history()->peer->asChannel()->pinnedMessageId() == item->id); _menu->addAction(lang(ispinned ? lng_context_unpin_msg : lng_context_pin_msg), _widget, ispinned ? SLOT(onUnpinMessage()) : SLOT(onPinMessage())); } @@ -1332,7 +1332,7 @@ void HistoryInner::showContextMenu(QContextMenuEvent *e, bool showFromTouch) { _menu->addAction(lang(lng_context_edit_msg), _widget, SLOT(onEditMessage())); } if (item->canPin()) { - bool ispinned = (item->history()->peer->asChannel()->mgInfo->pinnedMsgId == item->id); + bool ispinned = (item->history()->peer->asChannel()->pinnedMessageId() == item->id); _menu->addAction(lang(ispinned ? lng_context_unpin_msg : lng_context_pin_msg), _widget, ispinned ? SLOT(onUnpinMessage()) : SLOT(onPinMessage())); } @@ -1346,7 +1346,7 @@ void HistoryInner::showContextMenu(QContextMenuEvent *e, bool showFromTouch) { _menu->addAction(lang(lng_context_edit_msg), _widget, SLOT(onEditMessage())); } if (item->canPin()) { - bool ispinned = (item->history()->peer->asChannel()->mgInfo->pinnedMsgId == item->id); + bool ispinned = (item->history()->peer->asChannel()->pinnedMessageId() == item->id); _menu->addAction(lang(ispinned ? lng_context_unpin_msg : lng_context_pin_msg), _widget, ispinned ? SLOT(onUnpinMessage()) : SLOT(onPinMessage())); } diff --git a/Telegram/SourceFiles/history/history_item.cpp b/Telegram/SourceFiles/history/history_item.cpp index 57b533893..7ac0b342b 100644 --- a/Telegram/SourceFiles/history/history_item.cpp +++ b/Telegram/SourceFiles/history/history_item.cpp @@ -647,8 +647,10 @@ void HistoryItem::finishEditionToEmpty() { _history->removeNotification(this); if (history()->isChannel()) { - if (history()->peer->isMegagroup() && history()->peer->asChannel()->mgInfo->pinnedMsgId == id) { - history()->peer->asChannel()->mgInfo->pinnedMsgId = 0; + if (auto channel = history()->peer->asChannel()) { + if (channel->pinnedMessageId() == id) { + channel->clearPinnedMessage(); + } } } if (history()->lastKeyboardId == id) { @@ -714,8 +716,10 @@ void HistoryItem::destroy() { _history->removeNotification(this); detach(); if (history()->isChannel()) { - if (history()->peer->isMegagroup() && history()->peer->asChannel()->mgInfo->pinnedMsgId == id) { - history()->peer->asChannel()->mgInfo->pinnedMsgId = 0; + if (auto channel = history()->peer->asChannel()) { + if (channel->pinnedMessageId() == id) { + channel->clearPinnedMessage(); + } } } if (history()->lastMsg == this) { diff --git a/Telegram/SourceFiles/history/history_widget.cpp b/Telegram/SourceFiles/history/history_widget.cpp index cdab8ce36..c566c9003 100644 --- a/Telegram/SourceFiles/history/history_widget.cpp +++ b/Telegram/SourceFiles/history/history_widget.cpp @@ -5667,7 +5667,9 @@ void HistoryWidget::updatePinnedBar(bool force) { update(); } else if (force) { if (_peer && _peer->isMegagroup()) { - _peer->asChannel()->mgInfo->pinnedMsgId = 0; + if (auto channel = _peer->asChannel()) { + channel->clearPinnedMessage(); + } } destroyPinnedBar(); updateControlsGeometry(); @@ -5676,8 +5678,9 @@ void HistoryWidget::updatePinnedBar(bool force) { bool HistoryWidget::pinnedMsgVisibilityUpdated() { auto result = false; - auto pinnedMsgId = (_peer && _peer->isMegagroup()) ? _peer->asChannel()->mgInfo->pinnedMsgId : 0; - if (pinnedMsgId && !_peer->asChannel()->canPinMessages()) { + auto channel = _peer ? _peer->asChannel() : nullptr; + auto pinnedMsgId = channel ? channel->pinnedMessageId() : 0; + if (pinnedMsgId && !channel->canPinMessages()) { auto it = Global::HiddenPinnedMessages().constFind(_peer->id); if (it != Global::HiddenPinnedMessages().cend()) { if (it.value() == pinnedMsgId) { @@ -5999,9 +6002,9 @@ void HistoryWidget::onUnpinMessage() { if (!_peer || !_peer->isMegagroup()) return; Ui::show(Box(lang(lng_pinned_unpin_sure), lang(lng_pinned_unpin), base::lambda_guarded(this, [this] { - if (!_peer || !_peer->isMegagroup()) return; + if (!_peer || !_peer->asChannel()) return; - _peer->asChannel()->mgInfo->pinnedMsgId = 0; + _peer->asChannel()->clearPinnedMessage(); if (pinnedMsgVisibilityUpdated()) { updateControlsGeometry(); update(); @@ -6021,8 +6024,8 @@ void HistoryWidget::unpinDone(const MTPUpdates &updates) { } void HistoryWidget::onPinnedHide() { - if (!_peer || !_peer->isMegagroup()) return; - if (!_peer->asChannel()->mgInfo->pinnedMsgId) { + if (!_peer || !_peer->asChannel()) return; + if (!_peer->asChannel()->pinnedMessageId()) { if (pinnedMsgVisibilityUpdated()) { updateControlsGeometry(); update(); @@ -6033,7 +6036,7 @@ void HistoryWidget::onPinnedHide() { if (_peer->asChannel()->canPinMessages()) { onUnpinMessage(); } else { - Global::RefHiddenPinnedMessages().insert(_peer->id, _peer->asChannel()->mgInfo->pinnedMsgId); + Global::RefHiddenPinnedMessages().insert(_peer->id, _peer->asChannel()->pinnedMessageId()); Local::writeUserSettings(); if (pinnedMsgVisibilityUpdated()) { updateControlsGeometry(); diff --git a/Telegram/SourceFiles/mainwidget.cpp b/Telegram/SourceFiles/mainwidget.cpp index e1cff0401..72431affc 100644 --- a/Telegram/SourceFiles/mainwidget.cpp +++ b/Telegram/SourceFiles/mainwidget.cpp @@ -5373,10 +5373,7 @@ void MainWidget::feedUpdate(const MTPUpdate &update) { auto &d = update.c_updateChannelPinnedMessage(); if (auto channel = App::channelLoaded(d.vchannel_id.v)) { - if (channel->isMegagroup()) { - channel->mgInfo->pinnedMsgId = d.vid.v; - Auth().api().fullPeerUpdated().notify(channel); - } + channel->setPinnedMessageId(d.vid.v); } } break; diff --git a/Telegram/SourceFiles/structs.cpp b/Telegram/SourceFiles/structs.cpp index 1a898302d..4653e8714 100644 --- a/Telegram/SourceFiles/structs.cpp +++ b/Telegram/SourceFiles/structs.cpp @@ -915,6 +915,13 @@ void ChannelData::setRestrictionReason(const QString &text) { } } +void ChannelData::setPinnedMessageId(MsgId messageId) { + if (_pinnedMessageId != messageId) { + _pinnedMessageId = messageId; + Notify::peerUpdatedDelayed(this, Notify::PeerUpdate::Flag::ChannelPinnedChanged); + } +} + bool ChannelData::canNotEditLastAdmin(not_null user) const { if (mgInfo) { auto i = mgInfo->lastAdmins.constFind(user); diff --git a/Telegram/SourceFiles/structs.h b/Telegram/SourceFiles/structs.h index 9ff298ff3..1b661e1b4 100644 --- a/Telegram/SourceFiles/structs.h +++ b/Telegram/SourceFiles/structs.h @@ -755,7 +755,6 @@ struct MegagroupInfo { UserData *creator = nullptr; // nullptr means unknown int botStatus = 0; // -1 - no bots, 0 - unknown, 1 - one bot, that sees all history, 2 - other - MsgId pinnedMsgId = 0; bool joinedMessageFound = false; MTPInputStickerSet stickerSet = MTP_inputStickerSetEmpty(); @@ -997,6 +996,14 @@ class ChannelData : public PeerData { } void setRestrictionReason(const QString &reason); + MsgId pinnedMessageId() const { + return _pinnedMessageId; + } + void setPinnedMessageId(MsgId messageId); + void clearPinnedMessage() { + setPinnedMessageId(0); + } + private: bool canNotEditLastAdmin(not_null user) const; @@ -1008,6 +1015,8 @@ class ChannelData : public PeerData { int _restrictedCount = 0; int _kickedCount = 0; + MsgId _pinnedMessageId = 0; + MTPChannelAdminRights _adminRights = MTP_channelAdminRights(MTP_flags(0)); MTPChannelBannedRights _restrictedRights = MTP_channelBannedRights(MTP_flags(0), MTP_int(0));