Skip to content

Commit

Permalink
Enable messages unpinning for channels
Browse files Browse the repository at this point in the history
Also make History Widget react on pinning Channel message event and show
it in UI.

This commit is based on upstream commit
telegramdesktop/tdesktop@75d8d01

Related to #114.
Closes #8.
  • Loading branch information
leha-bot committed Sep 21, 2018
1 parent ae75810 commit 4a63c69
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 17 deletions.
6 changes: 3 additions & 3 deletions Telegram/SourceFiles/history/history_inner_widget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
7 changes: 7 additions & 0 deletions Telegram/SourceFiles/history/history_item.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
1 change: 1 addition & 0 deletions Telegram/SourceFiles/history/history_item.h
Original file line number Diff line number Diff line change
Expand Up @@ -717,6 +717,7 @@ class HistoryItem : public HistoryElement, public RuntimeComposer, public ClickH
return _text.isEmpty();
}

bool isPinned() const;
bool canPin() const;
bool canForward() const;
bool canEdit(const QDateTime &cur) const;
Expand Down
26 changes: 12 additions & 14 deletions Telegram/SourceFiles/history/history_widget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -6024,19 +6020,21 @@ 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();
}
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();
Expand Down

0 comments on commit 4a63c69

Please sign in to comment.