Skip to content

Commit

Permalink
Enable pins for channels
Browse files Browse the repository at this point in the history
Based on upstream commit telegramdesktop/tdesktop@75d8d01

Related to #114.
Closes #8.
  • Loading branch information
leha-bot committed Aug 23, 2018
1 parent e6805cf commit b7c8a62
Show file tree
Hide file tree
Showing 8 changed files with 51 additions and 31 deletions.
11 changes: 5 additions & 6 deletions Telegram/SourceFiles/apiwrap.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
11 changes: 6 additions & 5 deletions Telegram/SourceFiles/history/history.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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();
}
Expand Down
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,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()));
}
Expand Down Expand Up @@ -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()));
}
Expand All @@ -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()));
}
Expand Down
12 changes: 8 additions & 4 deletions Telegram/SourceFiles/history/history_item.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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) {
Expand Down
19 changes: 11 additions & 8 deletions Telegram/SourceFiles/history/history_widget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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) {
Expand Down Expand Up @@ -5999,9 +6002,9 @@ void HistoryWidget::onUnpinMessage() {
if (!_peer || !_peer->isMegagroup()) return;

Ui::show(Box<ConfirmBox>(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();
Expand All @@ -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();
Expand All @@ -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();
Expand Down
5 changes: 1 addition & 4 deletions Telegram/SourceFiles/mainwidget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
7 changes: 7 additions & 0 deletions Telegram/SourceFiles/structs.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<UserData *> user) const {
if (mgInfo) {
auto i = mgInfo->lastAdmins.constFind(user);
Expand Down
11 changes: 10 additions & 1 deletion Telegram/SourceFiles/structs.h
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand Down Expand Up @@ -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<UserData *> user) const;

Expand All @@ -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));

Expand Down

0 comments on commit b7c8a62

Please sign in to comment.