Enable messages unpinning for channels

Also make History Widget react on pinning Channel message event and show
it in UI.

This commit is based on upstream commit
75d8d01b17

Related to #114.
Closes #8.
This commit is contained in:
leha-bot 2018-09-17 04:56:50 +03:00 committed by Alex
parent ae75810cd5
commit 4a63c69d7f
4 changed files with 23 additions and 17 deletions

View File

@ -1240,9 +1240,9 @@ 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()->pinnedMessageId() == item->id);
_menu->addAction(lang(ispinned ? lng_context_unpin_msg : lng_context_pin_msg), _widget,
ispinned ? SLOT(onUnpinMessage()) : SLOT(onPinMessage()));
bool isPinned = item->isPinned();
_menu->addAction(lang(isPinned ? lng_context_unpin_msg : lng_context_pin_msg), _widget,
isPinned ? SLOT(onUnpinMessage()) : SLOT(onPinMessage()));
}
}
if (lnkPhoto) {

View File

@ -822,6 +822,13 @@ 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 || !toHistoryMessage()) {
return false;

View File

@ -717,6 +717,7 @@ public:
return _text.isEmpty();
}
bool isPinned() const;
bool canPin() const;
bool canForward() const;
bool canEdit(const QDateTime &cur) const;

View File

@ -5992,23 +5992,19 @@ 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<PinMessageBox>(_peer->asChannel(), to->id));
}
void HistoryWidget::onUnpinMessage() {
if (!_peer || !_peer->isMegagroup()) return;
if (!_peer || !_peer->isChannel()) return;
Ui::show(Box<ConfirmBox>(lang(lng_pinned_unpin_sure), lang(lng_pinned_unpin), base::lambda_guarded(this, [this] {
if (!_peer || !_peer->asChannel()) return;
_peer->asChannel()->clearPinnedMessage();
if (pinnedMsgVisibilityUpdated()) {
updateControlsGeometry();
update();
}
auto channel = _peer ? _peer->asChannel() : nullptr;
if (!channel) return;
channel->clearPinnedMessage();
Ui::hideLayer();
MTP::send(MTPchannels_UpdatePinnedMessage(
@ -6024,8 +6020,10 @@ void HistoryWidget::unpinDone(const MTPUpdates &updates) {
}
void HistoryWidget::onPinnedHide() {
if (!_peer || !_peer->asChannel()) return;
if (!_peer->asChannel()->pinnedMessageId()) {
auto channel = _peer ? _peer->asChannel() : nullptr;
if (!channel) return;
auto pinnedId = channel->pinnedMessageId();
if (!pinnedId) {
if (pinnedMsgVisibilityUpdated()) {
updateControlsGeometry();
update();
@ -6033,10 +6031,10 @@ void HistoryWidget::onPinnedHide() {
return;
}
if (_peer->asChannel()->canPinMessages()) {
if (channel->canPinMessages()) {
onUnpinMessage();
} else {
Global::RefHiddenPinnedMessages().insert(_peer->id, _peer->asChannel()->pinnedMessageId());
Global::RefHiddenPinnedMessages().insert(_peer->id, pinnedId);
Local::writeUserSettings();
if (pinnedMsgVisibilityUpdated()) {
updateControlsGeometry();