2021-10-18 20:37:25 +00:00
|
|
|
/*
|
|
|
|
This file is part of Telegram Desktop,
|
|
|
|
the official desktop application for the Telegram messaging service.
|
|
|
|
|
|
|
|
For license and copyright information please follow this link:
|
|
|
|
https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL
|
|
|
|
*/
|
|
|
|
#include "boxes/pin_messages_box.h"
|
|
|
|
|
|
|
|
#include "apiwrap.h"
|
|
|
|
#include "data/data_chat.h"
|
|
|
|
#include "data/data_user.h"
|
|
|
|
#include "lang/lang_keys.h"
|
2023-04-21 11:09:06 +00:00
|
|
|
#include "history/history.h"
|
|
|
|
#include "history/history_item.h"
|
2021-10-18 20:37:25 +00:00
|
|
|
#include "main/main_session.h"
|
2022-03-08 09:17:47 +00:00
|
|
|
#include "ui/boxes/confirm_box.h"
|
2021-10-18 20:37:25 +00:00
|
|
|
#include "ui/widgets/checkbox.h"
|
|
|
|
#include "styles/style_layers.h"
|
|
|
|
#include "styles/style_boxes.h"
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
2022-10-11 15:08:19 +00:00
|
|
|
[[nodiscard]] bool IsOldForPin(
|
|
|
|
MsgId id,
|
|
|
|
not_null<PeerData*> peer,
|
|
|
|
MsgId topicRootId) {
|
2021-10-18 20:37:25 +00:00
|
|
|
const auto normal = peer->migrateToOrMe();
|
|
|
|
const auto migrated = normal->migrateFrom();
|
2022-10-11 15:08:19 +00:00
|
|
|
const auto top = Data::ResolveTopPinnedId(normal, topicRootId, migrated);
|
2021-10-18 20:37:25 +00:00
|
|
|
if (!top) {
|
|
|
|
return false;
|
|
|
|
} else if (peer == migrated) {
|
2021-12-09 07:32:54 +00:00
|
|
|
return peerIsChannel(top.peer) || (id < top.msg);
|
2021-10-18 20:37:25 +00:00
|
|
|
} else if (migrated) {
|
2021-12-09 07:32:54 +00:00
|
|
|
return peerIsChannel(top.peer) && (id < top.msg);
|
2021-10-18 20:37:25 +00:00
|
|
|
} else {
|
|
|
|
return (id < top.msg);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace
|
|
|
|
|
2022-03-08 09:17:47 +00:00
|
|
|
void PinMessageBox(
|
|
|
|
not_null<Ui::GenericBox*> box,
|
2023-04-21 11:09:06 +00:00
|
|
|
not_null<HistoryItem*> item) {
|
2022-03-08 09:17:47 +00:00
|
|
|
struct State {
|
|
|
|
QPointer<Ui::Checkbox> pinForPeer;
|
|
|
|
QPointer<Ui::Checkbox> notify;
|
|
|
|
mtpRequestId requestId = 0;
|
|
|
|
};
|
2021-10-18 20:37:25 +00:00
|
|
|
|
2023-04-21 11:09:06 +00:00
|
|
|
const auto peer = item->history()->peer;
|
|
|
|
const auto msgId = item->id;
|
|
|
|
const auto topicRootId = item->topic() ? item->topicRootId() : MsgId();
|
|
|
|
const auto pinningOld = IsOldForPin(msgId, peer, topicRootId);
|
2022-03-08 09:17:47 +00:00
|
|
|
const auto state = box->lifetime().make_state<State>();
|
|
|
|
const auto api = box->lifetime().make_state<MTP::Sender>(
|
|
|
|
&peer->session().mtp());
|
2021-10-18 20:37:25 +00:00
|
|
|
|
2022-03-08 09:17:47 +00:00
|
|
|
auto checkbox = [&]() -> object_ptr<Ui::Checkbox> {
|
|
|
|
if (peer->isUser() && !peer->isSelf()) {
|
|
|
|
auto object = object_ptr<Ui::Checkbox>(
|
|
|
|
box,
|
|
|
|
tr::lng_pinned_also_for_other(
|
|
|
|
tr::now,
|
|
|
|
lt_user,
|
|
|
|
peer->shortName()),
|
|
|
|
false,
|
|
|
|
st::urlAuthCheckbox);
|
|
|
|
object->setAllowTextLines();
|
|
|
|
state->pinForPeer = Ui::MakeWeak(object.data());
|
|
|
|
return object;
|
|
|
|
} else if (!pinningOld && (peer->isChat() || peer->isMegagroup())) {
|
|
|
|
auto object = object_ptr<Ui::Checkbox>(
|
|
|
|
box,
|
|
|
|
tr::lng_pinned_notify(tr::now),
|
|
|
|
true,
|
|
|
|
st::urlAuthCheckbox);
|
|
|
|
object->setAllowTextLines();
|
|
|
|
state->notify = Ui::MakeWeak(object.data());
|
|
|
|
return object;
|
|
|
|
}
|
|
|
|
return { nullptr };
|
|
|
|
}();
|
2021-10-18 20:37:25 +00:00
|
|
|
|
2022-03-08 09:17:47 +00:00
|
|
|
auto pinMessage = [=] {
|
|
|
|
if (state->requestId) {
|
|
|
|
return;
|
|
|
|
}
|
2021-10-18 20:37:25 +00:00
|
|
|
|
2022-03-08 09:17:47 +00:00
|
|
|
auto flags = MTPmessages_UpdatePinnedMessage::Flags(0);
|
|
|
|
if (state->notify && !state->notify->checked()) {
|
|
|
|
flags |= MTPmessages_UpdatePinnedMessage::Flag::f_silent;
|
|
|
|
}
|
|
|
|
if (state->pinForPeer && !state->pinForPeer->checked()) {
|
|
|
|
flags |= MTPmessages_UpdatePinnedMessage::Flag::f_pm_oneside;
|
|
|
|
}
|
|
|
|
state->requestId = api->request(MTPmessages_UpdatePinnedMessage(
|
|
|
|
MTP_flags(flags),
|
|
|
|
peer->input,
|
|
|
|
MTP_int(msgId)
|
|
|
|
)).done([=](const MTPUpdates &result) {
|
|
|
|
peer->session().api().applyUpdates(result);
|
|
|
|
box->closeBox();
|
|
|
|
}).fail([=] {
|
|
|
|
box->closeBox();
|
|
|
|
}).send();
|
|
|
|
};
|
2021-10-18 20:37:25 +00:00
|
|
|
|
2022-03-08 09:17:47 +00:00
|
|
|
Ui::ConfirmBox(box, {
|
|
|
|
.text = (pinningOld
|
|
|
|
? tr::lng_pinned_pin_old_sure()
|
|
|
|
: (peer->isChat() || peer->isMegagroup())
|
|
|
|
? tr::lng_pinned_pin_sure_group()
|
|
|
|
: tr::lng_pinned_pin_sure()),
|
|
|
|
.confirmed = std::move(pinMessage),
|
|
|
|
.confirmText = tr::lng_pinned_pin(),
|
|
|
|
});
|
2021-10-18 20:37:25 +00:00
|
|
|
|
2022-03-08 09:17:47 +00:00
|
|
|
if (checkbox) {
|
|
|
|
auto padding = st::boxPadding;
|
|
|
|
padding.setTop(padding.bottom());
|
|
|
|
box->addRow(std::move(checkbox), std::move(padding));
|
2021-10-18 20:37:25 +00:00
|
|
|
}
|
|
|
|
}
|