diff --git a/Telegram/SourceFiles/apiwrap.cpp b/Telegram/SourceFiles/apiwrap.cpp index 2d67839a03..0adc8befe4 100644 --- a/Telegram/SourceFiles/apiwrap.cpp +++ b/Telegram/SourceFiles/apiwrap.cpp @@ -395,13 +395,13 @@ void ApiWrap::gotChatFull(PeerData *peer, const MTPmessages_ChatFull &result, mt } accumulate_max(h->outboxReadBefore, f.vread_outbox_max_id.v + 1); } + ranges::overload([] {}, [](int a) {}); + 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/boxes/confirm_box.cpp b/Telegram/SourceFiles/boxes/confirm_box.cpp index 681a0a7e4d..71f2281105 100644 --- a/Telegram/SourceFiles/boxes/confirm_box.cpp +++ b/Telegram/SourceFiles/boxes/confirm_box.cpp @@ -368,21 +368,30 @@ void ConvertToSupergroupBox::paintEvent(QPaintEvent *e) { PinMessageBox::PinMessageBox(QWidget*, ChannelData *channel, MsgId msgId) : _channel(channel) , _msgId(msgId) -, _text(this, lang(lng_pinned_pin_sure), Ui::FlatLabel::InitType::Simple, st::boxLabel) -, _notify(this, lang(lng_pinned_notify), true, st::defaultBoxCheckbox) { +, _text(this, lang(lng_pinned_pin_sure), Ui::FlatLabel::InitType::Simple, st::boxLabel) { } void PinMessageBox::prepare() { addButton(langFactory(lng_pinned_pin), [this] { pinMessage(); }); addButton(langFactory(lng_cancel), [this] { closeBox(); }); - setDimensions(st::boxWidth, st::boxPadding.top() + _text->height() + st::boxMediumSkip + _notify->heightNoMargins() + st::boxPadding.bottom()); + if (_channel->isMegagroup()) { + _notify.create(this, lang(lng_pinned_notify), true, st::defaultBoxCheckbox); + } + + auto height = st::boxPadding.top() + _text->height() + st::boxPadding.bottom(); + if (_notify) { + height += st::boxMediumSkip + _notify->heightNoMargins(); + } + setDimensions(st::boxWidth, height); } void PinMessageBox::resizeEvent(QResizeEvent *e) { BoxContent::resizeEvent(e); _text->moveToLeft(st::boxPadding.left(), st::boxPadding.top()); - _notify->moveToLeft(st::boxPadding.left(), _text->y() + _text->height() + st::boxMediumSkip); + if (_notify) { + _notify->moveToLeft(st::boxPadding.left(), _text->y() + _text->height() + st::boxMediumSkip); + } } void PinMessageBox::keyPressEvent(QKeyEvent *e) { @@ -397,10 +406,16 @@ void PinMessageBox::pinMessage() { if (_requestId) return; auto flags = MTPchannels_UpdatePinnedMessage::Flags(0); - if (!_notify->checked()) { + if (_notify && !_notify->checked()) { flags |= MTPchannels_UpdatePinnedMessage::Flag::f_silent; } - _requestId = MTP::send(MTPchannels_UpdatePinnedMessage(MTP_flags(flags), _channel->inputChannel, MTP_int(_msgId)), rpcDone(&PinMessageBox::pinDone), rpcFail(&PinMessageBox::pinFail)); + _requestId = MTP::send( + MTPchannels_UpdatePinnedMessage( + MTP_flags(flags), + _channel->inputChannel, + MTP_int(_msgId)), + rpcDone(&PinMessageBox::pinDone), + rpcFail(&PinMessageBox::pinFail)); } void PinMessageBox::pinDone(const MTPUpdates &updates) { diff --git a/Telegram/SourceFiles/boxes/confirm_box.h b/Telegram/SourceFiles/boxes/confirm_box.h index 9ea2e457a2..d17f40b727 100644 --- a/Telegram/SourceFiles/boxes/confirm_box.h +++ b/Telegram/SourceFiles/boxes/confirm_box.h @@ -164,7 +164,7 @@ private: MsgId _msgId; object_ptr _text; - object_ptr _notify; + object_ptr _notify = { nullptr }; mtpRequestId _requestId = 0; diff --git a/Telegram/SourceFiles/calls/calls_panel.cpp b/Telegram/SourceFiles/calls/calls_panel.cpp index cf5d15df91..30c3c0c5e6 100644 --- a/Telegram/SourceFiles/calls/calls_panel.cpp +++ b/Telegram/SourceFiles/calls/calls_panel.cpp @@ -275,13 +275,13 @@ void Panel::replaceCall(not_null call) { updateControlsGeometry(); } -bool Panel::event(QEvent *e) { +bool Panel::eventHook(QEvent *e) { if (e->type() == QEvent::WindowDeactivate) { if (_call && _call->state() == State::Established) { hideDeactivated(); } } - return TWidget::event(e); + return RpWidget::eventHook(e); } void Panel::hideDeactivated() { @@ -364,12 +364,10 @@ void Panel::initLayout() { initGeometry(); - processUserPhoto(); - subscribe(Auth().api().fullPeerUpdated(), [this](PeerData *peer) { - if (peer == _user) { - processUserPhoto(); - } - }); + Notify::PeerUpdateValue(_user, Notify::PeerUpdate::Flag::PhotoChanged) + | rpl::start_with_next( + [this] { processUserPhoto(); }, + lifetime()); subscribe(Auth().downloaderTaskFinished(), [this] { refreshUserPhoto(); }); diff --git a/Telegram/SourceFiles/calls/calls_panel.h b/Telegram/SourceFiles/calls/calls_panel.h index c6981368f4..9ceb37ce07 100644 --- a/Telegram/SourceFiles/calls/calls_panel.h +++ b/Telegram/SourceFiles/calls/calls_panel.h @@ -24,6 +24,7 @@ Copyright (c) 2014-2017 John Preston, https://desktop.telegram.org #include "base/timer.h" #include "calls/calls_call.h" #include "ui/widgets/tooltip.h" +#include "ui/rp_widget.h" namespace Ui { class IconButton; @@ -34,7 +35,11 @@ class FadeWrap; namespace Calls { -class Panel : public TWidget, private base::Subscriber, private Ui::AbstractTooltipShower { +class Panel + : public Ui::RpWidget + , private base::Subscriber + , private Ui::AbstractTooltipShower { + public: Panel(not_null call); @@ -51,7 +56,7 @@ protected: void mouseMoveEvent(QMouseEvent *e) override; void leaveEventHook(QEvent *e) override; void leaveToChildEvent(QEvent *e, QWidget *child) override; - bool event(QEvent *e) override; + bool eventHook(QEvent *e) override; private: using State = Call::State; diff --git a/Telegram/SourceFiles/data/data_peer.cpp b/Telegram/SourceFiles/data/data_peer.cpp index d4136543e1..083d49d52c 100644 --- a/Telegram/SourceFiles/data/data_peer.cpp +++ b/Telegram/SourceFiles/data/data_peer.cpp @@ -957,6 +957,102 @@ void ChannelData::setAvailableMinId(MsgId availableMinId) { } } +void ChannelData::setPinnedMessageId(MsgId messageId) { + if (_pinnedMessageId != messageId) { + _pinnedMessageId = messageId; + Notify::peerUpdatedDelayed(this, Notify::PeerUpdate::Flag::ChannelPinnedChanged); + } +} + +bool ChannelData::canBanMembers() const { + return (adminRights() & AdminRight::f_ban_users) + || amCreator(); +} + +bool ChannelData::canEditMessages() const { + return (adminRights() & AdminRight::f_edit_messages) + || amCreator(); +} + +bool ChannelData::canDeleteMessages() const { + return (adminRights() & AdminRight::f_delete_messages) + || amCreator(); +} + +bool ChannelData::anyoneCanAddMembers() const { + return (flags() & MTPDchannel::Flag::f_democracy); +} + +bool ChannelData::canAddMembers() const { + return (adminRights() & AdminRight::f_invite_users) + || amCreator() + || (anyoneCanAddMembers() + && amIn() + && !hasRestrictions()); +} + +bool ChannelData::canAddAdmins() const { + return (adminRights() & AdminRight::f_add_admins) + || amCreator(); +} + +bool ChannelData::canPinMessages() const { + if (isMegagroup()) { + return (adminRights() & AdminRight::f_pin_messages) + || amCreator(); + } + return (adminRights() & AdminRight::f_edit_messages) + || amCreator(); +} + +bool ChannelData::canPublish() const { + return (adminRights() & AdminRight::f_post_messages) + || amCreator(); +} + +bool ChannelData::canWrite() const { + // Duplicated in Data::CanWriteValue(). + return amIn() + && (canPublish() + || (!isBroadcast() + && !restricted(Restriction::f_send_messages))); +} + +bool ChannelData::canViewMembers() const { + return fullFlags() + & MTPDchannelFull::Flag::f_can_view_participants; +} + +bool ChannelData::canViewAdmins() const { + return (isMegagroup() || hasAdminRights() || amCreator()); +} + +bool ChannelData::canViewBanned() const { + return (hasAdminRights() || amCreator()); +} + +bool ChannelData::canEditInformation() const { + return (adminRights() & AdminRight::f_change_info) + || amCreator(); +} + +bool ChannelData::canEditUsername() const { + return amCreator() + && (fullFlags() + & MTPDchannelFull::Flag::f_can_set_username); +} + +bool ChannelData::canEditStickers() const { + return (fullFlags() + & MTPDchannelFull::Flag::f_can_set_stickers); +} + +bool ChannelData::canDelete() const { + constexpr auto kDeleteChannelMembersLimit = 1000; + return amCreator() + && (membersCount() <= kDeleteChannelMembersLimit); +} + bool ChannelData::canEditLastAdmin(not_null user) const { // Duplicated in ParticipantsBoxController::canEditAdmin :( if (mgInfo) { diff --git a/Telegram/SourceFiles/data/data_peer.h b/Telegram/SourceFiles/data/data_peer.h index 900413c765..db09cf5ce1 100644 --- a/Telegram/SourceFiles/data/data_peer.h +++ b/Telegram/SourceFiles/data/data_peer.h @@ -756,7 +756,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(); @@ -968,75 +967,22 @@ public: return hasRestrictions() && (restrictedUntil() > now); } - bool canBanMembers() const { - return (adminRights() & AdminRight::f_ban_users) - || amCreator(); - } - bool canEditMessages() const { - return (adminRights() & AdminRight::f_edit_messages) - || amCreator(); - } - bool canDeleteMessages() const { - return (adminRights() & AdminRight::f_delete_messages) - || amCreator(); - } - bool anyoneCanAddMembers() const { - return (flags() & MTPDchannel::Flag::f_democracy); - } - bool canAddMembers() const { - return (adminRights() & AdminRight::f_invite_users) - || amCreator() - || (anyoneCanAddMembers() - && amIn() - && !hasRestrictions()); - } - bool canAddAdmins() const { - return (adminRights() & AdminRight::f_add_admins) - || amCreator(); - } - bool canPinMessages() const { - return (adminRights() & AdminRight::f_pin_messages) - || amCreator(); - } - bool canPublish() const { - return (adminRights() & AdminRight::f_post_messages) - || amCreator(); - } - bool canWrite() const { - // Duplicated in Data::CanWriteValue(). - return amIn() - && (canPublish() - || (!isBroadcast() - && !restricted(Restriction::f_send_messages))); - } - bool canViewMembers() const { - return fullFlags() - & MTPDchannelFull::Flag::f_can_view_participants; - } - bool canViewAdmins() const { - return (isMegagroup() || hasAdminRights() || amCreator()); - } - bool canViewBanned() const { - return (hasAdminRights() || amCreator()); - } - bool canEditInformation() const { - return (adminRights() & AdminRight::f_change_info) - || amCreator(); - } - bool canEditUsername() const { - return amCreator() - && (fullFlags() - & MTPDchannelFull::Flag::f_can_set_username); - } - bool canEditStickers() const { - return (fullFlags() - & MTPDchannelFull::Flag::f_can_set_stickers); - } - bool canDelete() const { - constexpr auto kDeleteChannelMembersLimit = 1000; - return amCreator() - && (membersCount() <= kDeleteChannelMembersLimit); - } + bool canBanMembers() const; + bool canEditMessages() const; + bool canDeleteMessages() const; + bool anyoneCanAddMembers() const; + bool canAddMembers() const; + bool canAddAdmins() const; + bool canPinMessages() const; + bool canPublish() const; + bool canWrite() const; + bool canViewMembers() const; + bool canViewAdmins() const; + bool canViewBanned() const; + bool canEditInformation() const; + bool canEditUsername() const; + bool canEditStickers() const; + bool canDelete() const; bool canEditAdmin(not_null user) const; bool canRestrictUser(not_null user) const; @@ -1105,6 +1051,14 @@ public: } void setAvailableMinId(MsgId availableMinId); + MsgId pinnedMessageId() const { + return _pinnedMessageId; + } + void setPinnedMessageId(MsgId messageId); + void clearPinnedMessage() { + setPinnedMessageId(0); + } + private: void flagsUpdated(MTPDchannel::Flags diff); void fullFlagsUpdated(MTPDchannelFull::Flags diff); @@ -1121,6 +1075,7 @@ private: int _restrictedCount = 0; int _kickedCount = 0; MsgId _availableMinId = 0; + MsgId _pinnedMessageId = 0; AdminRightFlags _adminRights; RestrictionFlags _restrictions; diff --git a/Telegram/SourceFiles/history/history.cpp b/Telegram/SourceFiles/history/history.cpp index 9d14e5e40e..fd6b24bc92 100644 --- a/Telegram/SourceFiles/history/history.cpp +++ b/Telegram/SourceFiles/history/history.cpp @@ -1076,9 +1076,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; @@ -2355,8 +2356,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 e795749ccc..92f71a7960 100644 --- a/Telegram/SourceFiles/history/history_inner_widget.cpp +++ b/Telegram/SourceFiles/history/history_inner_widget.cpp @@ -1219,8 +1219,8 @@ 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); - _menu->addAction(lang(ispinned ? lng_context_unpin_msg : lng_context_pin_msg), _widget, ispinned ? SLOT(onUnpinMessage()) : SLOT(onPinMessage())); + auto isPinned = item->isPinned(); + _menu->addAction(lang(isPinned ? lng_context_unpin_msg : lng_context_pin_msg), _widget, isPinned ? SLOT(onUnpinMessage()) : SLOT(onPinMessage())); } } if (lnkPhoto) { @@ -1293,8 +1293,8 @@ 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); - _menu->addAction(lang(ispinned ? lng_context_unpin_msg : lng_context_pin_msg), _widget, ispinned ? SLOT(onUnpinMessage()) : SLOT(onPinMessage())); + auto isPinned = item->isPinned(); + _menu->addAction(lang(isPinned ? lng_context_unpin_msg : lng_context_pin_msg), _widget, isPinned ? SLOT(onUnpinMessage()) : SLOT(onPinMessage())); } } } else { @@ -1306,8 +1306,8 @@ 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); - _menu->addAction(lang(ispinned ? lng_context_unpin_msg : lng_context_pin_msg), _widget, ispinned ? SLOT(onUnpinMessage()) : SLOT(onPinMessage())); + auto isPinned = item->isPinned(); + _menu->addAction(lang(isPinned ? lng_context_unpin_msg : lng_context_pin_msg), _widget, isPinned ? SLOT(onUnpinMessage()) : SLOT(onPinMessage())); } } if (item && !isUponSelected) { diff --git a/Telegram/SourceFiles/history/history_item.cpp b/Telegram/SourceFiles/history/history_item.cpp index aa33adba06..1a7a469cfa 100644 --- a/Telegram/SourceFiles/history/history_item.cpp +++ b/Telegram/SourceFiles/history/history_item.cpp @@ -649,9 +649,9 @@ void HistoryItem::finishEditionToEmpty() { finishEdition(-1); _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) { @@ -735,9 +735,9 @@ void HistoryItem::destroy() { auto wasAtBottom = history()->loadedAtBottom(); _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) { @@ -846,11 +846,18 @@ void HistoryItem::setId(MsgId newId) { } } +bool HistoryItem::isPinned() const { + if (auto channel = _history->peer->asChannel()) { + return (channel->pinnedMessageId() == id); + } + return false; +} + bool HistoryItem::canPin() const { - if (id < 0 || !_history->peer->isMegagroup() || !toHistoryMessage()) { + if (id < 0 || !toHistoryMessage()) { return false; } - if (auto channel = _history->peer->asMegagroup()) { + if (auto channel = _history->peer->asChannel()) { return channel->canPinMessages(); } return false; @@ -873,7 +880,13 @@ bool HistoryItem::canForward() const { bool HistoryItem::canEdit(const QDateTime &cur) const { auto messageToMyself = _history->peer->isSelf(); - auto messageTooOld = messageToMyself + auto canPinInMegagroup = [&] { + if (auto megagroup = _history->peer->asMegagroup()) { + return megagroup->canPinMessages(); + } + return false; + }(); + auto messageTooOld = (messageToMyself || canPinInMegagroup) ? false : (date.secsTo(cur) >= Global::EditTimeLimit()); if (id < 0 || messageTooOld) { diff --git a/Telegram/SourceFiles/history/history_item.h b/Telegram/SourceFiles/history/history_item.h index aa2aebe84c..28e0d66770 100644 --- a/Telegram/SourceFiles/history/history_item.h +++ b/Telegram/SourceFiles/history/history_item.h @@ -749,6 +749,7 @@ public: return _text.isEmpty(); } + bool isPinned() const; bool canPin() const; bool canForward() const; bool canEdit(const QDateTime &cur) const; diff --git a/Telegram/SourceFiles/history/history_top_bar_widget.cpp b/Telegram/SourceFiles/history/history_top_bar_widget.cpp index d6d56e8453..180f83da30 100644 --- a/Telegram/SourceFiles/history/history_top_bar_widget.cpp +++ b/Telegram/SourceFiles/history/history_top_bar_widget.cpp @@ -395,7 +395,11 @@ void HistoryTopBarWidget::updateControlsGeometry() { _clearSelection->moveToRight(st::topBarActionSkip, selectedButtonsTop); if (_unreadBadge) { - _unreadBadge->setGeometryToLeft(0, st::titleUnreadCounterTop, _back->width(), st::dialogsUnreadHeight); + _unreadBadge->setGeometryToLeft( + 0, + otherButtonsTop + st::titleUnreadCounterTop, + _back->width(), + st::dialogsUnreadHeight); } if (_back->isHidden()) { _leftTaken = st::topBarArrowPadding.right(); diff --git a/Telegram/SourceFiles/history/history_widget.cpp b/Telegram/SourceFiles/history/history_widget.cpp index 8423699af1..395163d9f9 100644 --- a/Telegram/SourceFiles/history/history_widget.cpp +++ b/Telegram/SourceFiles/history/history_widget.cpp @@ -725,6 +725,7 @@ HistoryWidget::HistoryWidget(QWidget *parent, not_null cont updateHistoryGeometry(); updateControlsVisibility(); updateControlsGeometry(); + this->update(); } } if (update.flags & (UpdateFlag::UserIsBlocked @@ -5236,8 +5237,8 @@ void HistoryWidget::updatePinnedBar(bool force) { _pinnedBar->text.setText(st::messageTextStyle, TextUtilities::Clean(_pinnedBar->msg->notificationText()), _textDlgOptions); update(); } else if (force) { - if (_peer && _peer->isMegagroup()) { - _peer->asChannel()->mgInfo->pinnedMsgId = 0; + if (auto channel = _peer ? _peer->asChannel() : nullptr) { + channel->clearPinnedMessage(); } destroyPinnedBar(); updateControlsGeometry(); @@ -5246,21 +5247,26 @@ 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 pinnedId = [&] { + if (auto channel = _peer ? _peer->asChannel() : nullptr) { + return channel->pinnedMessageId(); + } + return 0; + }(); + if (pinnedId && !_peer->asChannel()->canPinMessages()) { auto it = Global::HiddenPinnedMessages().constFind(_peer->id); if (it != Global::HiddenPinnedMessages().cend()) { - if (it.value() == pinnedMsgId) { - pinnedMsgId = 0; + if (it.value() == pinnedId) { + pinnedId = 0; } else { Global::RefHiddenPinnedMessages().remove(_peer->id); Local::writeUserSettings(); } } } - if (pinnedMsgId) { + if (pinnedId) { if (!_pinnedBar) { - _pinnedBar = std::make_unique(pinnedMsgId, this); + _pinnedBar = std::make_unique(pinnedId, this); if (_a_show.animating()) { _pinnedBar->cancel->hide(); _pinnedBar->shadow->hide(); @@ -5277,9 +5283,9 @@ bool HistoryWidget::pinnedMsgVisibilityUpdated() { if (_scroll->scrollTop() != unreadBarTop()) { synteticScrollToY(_scroll->scrollTop() + st::historyReplyHeight); } - } else if (_pinnedBar->msgId != pinnedMsgId) { - _pinnedBar->msgId = pinnedMsgId; - _pinnedBar->msg = 0; + } else if (_pinnedBar->msgId != pinnedId) { + _pinnedBar->msgId = pinnedId; + _pinnedBar->msg = nullptr; _pinnedBar->text.clear(); updatePinnedBar(); } @@ -5541,26 +5547,30 @@ void HistoryWidget::onEditMessage() { } void HistoryWidget::onPinMessage() { - HistoryItem *to = App::contextItem(); - if (!to || !to->canPin() || !_peer || !_peer->isMegagroup()) return; + auto to = App::contextItem(); + if (!to || !to->canPin()) return; - Ui::show(Box(_peer->asChannel(), to->id)); + Ui::show(Box( + to->history()->peer->asChannel(), + to->id)); } void HistoryWidget::onUnpinMessage() { - if (!_peer || !_peer->isMegagroup()) return; + if (!_peer || !_peer->isChannel()) return; Ui::show(Box(lang(lng_pinned_unpin_sure), lang(lng_pinned_unpin), base::lambda_guarded(this, [this] { - if (!_peer || !_peer->isMegagroup()) return; + auto channel = _peer ? _peer->asChannel() : nullptr; + if (!channel) return; - _peer->asChannel()->mgInfo->pinnedMsgId = 0; - if (pinnedMsgVisibilityUpdated()) { - updateControlsGeometry(); - update(); - } + channel->clearPinnedMessage(); Ui::hideLayer(); - MTP::send(MTPchannels_UpdatePinnedMessage(MTP_flags(0), _peer->asChannel()->inputChannel, MTP_int(0)), rpcDone(&HistoryWidget::unpinDone)); + MTP::send( + MTPchannels_UpdatePinnedMessage( + MTP_flags(0), + channel->inputChannel, + MTP_int(0)), + rpcDone(&HistoryWidget::unpinDone)); }))); } @@ -5571,8 +5581,9 @@ void HistoryWidget::unpinDone(const MTPUpdates &updates) { } void HistoryWidget::onPinnedHide() { - if (!_peer || !_peer->isMegagroup()) return; - if (!_peer->asChannel()->mgInfo->pinnedMsgId) { + auto channel = _peer ? _peer->asChannel() : nullptr; + auto pinnedId = channel->pinnedMessageId(); + if (!pinnedId) { if (pinnedMsgVisibilityUpdated()) { updateControlsGeometry(); update(); @@ -5580,10 +5591,10 @@ void HistoryWidget::onPinnedHide() { return; } - if (_peer->asChannel()->canPinMessages()) { + if (channel->canPinMessages()) { onUnpinMessage(); } else { - Global::RefHiddenPinnedMessages().insert(_peer->id, _peer->asChannel()->mgInfo->pinnedMsgId); + Global::RefHiddenPinnedMessages().insert(_peer->id, pinnedId); Local::writeUserSettings(); if (pinnedMsgVisibilityUpdated()) { updateControlsGeometry(); diff --git a/Telegram/SourceFiles/mainwidget.cpp b/Telegram/SourceFiles/mainwidget.cpp index 71189e35c0..b1eabc5bf5 100644 --- a/Telegram/SourceFiles/mainwidget.cpp +++ b/Telegram/SourceFiles/mainwidget.cpp @@ -5449,12 +5449,8 @@ void MainWidget::feedUpdate(const MTPUpdate &update) { case mtpc_updateChannelPinnedMessage: { 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;