2021-10-18 23:37:25 +03: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 15:09:06 +04:00
|
|
|
#include "history/history.h"
|
|
|
|
#include "history/history_item.h"
|
2021-10-18 23:37:25 +03:00
|
|
|
#include "main/main_session.h"
|
2022-03-08 12:17:47 +03:00
|
|
|
#include "ui/boxes/confirm_box.h"
|
2021-10-18 23:37:25 +03:00
|
|
|
#include "ui/widgets/checkbox.h"
|
|
|
|
#include "styles/style_layers.h"
|
|
|
|
#include "styles/style_boxes.h"
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
2022-10-11 19:08:19 +04:00
|
|
|
[[nodiscard]] bool IsOldForPin(
|
|
|
|
MsgId id,
|
|
|
|
not_null<PeerData*> peer,
|
2025-06-02 15:00:36 +04:00
|
|
|
MsgId topicRootId,
|
|
|
|
PeerId monoforumPeerId) {
|
2021-10-18 23:37:25 +03:00
|
|
|
const auto normal = peer->migrateToOrMe();
|
|
|
|
const auto migrated = normal->migrateFrom();
|
2025-06-02 15:00:36 +04:00
|
|
|
const auto top = Data::ResolveTopPinnedId(
|
|
|
|
normal,
|
|
|
|
topicRootId,
|
|
|
|
monoforumPeerId,
|
|
|
|
migrated);
|
2021-10-18 23:37:25 +03:00
|
|
|
if (!top) {
|
|
|
|
return false;
|
|
|
|
} else if (peer == migrated) {
|
2021-12-09 11:32:54 +04:00
|
|
|
return peerIsChannel(top.peer) || (id < top.msg);
|
2021-10-18 23:37:25 +03:00
|
|
|
} else if (migrated) {
|
2021-12-09 11:32:54 +04:00
|
|
|
return peerIsChannel(top.peer) && (id < top.msg);
|
2021-10-18 23:37:25 +03:00
|
|
|
} else {
|
|
|
|
return (id < top.msg);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace
|
|
|
|
|
2022-03-08 12:17:47 +03:00
|
|
|
void PinMessageBox(
|
|
|
|
not_null<Ui::GenericBox*> box,
|
2023-04-21 15:09:06 +04:00
|
|
|
not_null<HistoryItem*> item) {
|
2022-03-08 12:17:47 +03:00
|
|
|
struct State {
|
2025-07-18 10:07:39 +04:00
|
|
|
base::weak_qptr<Ui::Checkbox> pinForPeer;
|
|
|
|
base::weak_qptr<Ui::Checkbox> notify;
|
2022-03-08 12:17:47 +03:00
|
|
|
mtpRequestId requestId = 0;
|
|
|
|
};
|
2021-10-18 23:37:25 +03:00
|
|
|
|
2023-04-21 15:09:06 +04:00
|
|
|
const auto peer = item->history()->peer;
|
|
|
|
const auto msgId = item->id;
|
|
|
|
const auto topicRootId = item->topic() ? item->topicRootId() : MsgId();
|
2025-06-02 15:00:36 +04:00
|
|
|
const auto monoforumPeerId = item->history()->peer->amMonoforumAdmin()
|
|
|
|
? item->sublistPeerId()
|
|
|
|
: PeerId();
|
|
|
|
const auto pinningOld = IsOldForPin(
|
|
|
|
msgId,
|
|
|
|
peer,
|
|
|
|
topicRootId,
|
|
|
|
monoforumPeerId);
|
2022-03-08 12:17:47 +03:00
|
|
|
const auto state = box->lifetime().make_state<State>();
|
|
|
|
const auto api = box->lifetime().make_state<MTP::Sender>(
|
|
|
|
&peer->session().mtp());
|
2021-10-18 23:37:25 +03:00
|
|
|
|
2022-03-08 12:17:47 +03: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();
|
2025-07-18 10:07:39 +04:00
|
|
|
state->pinForPeer = base::make_weak(object.data());
|
2022-03-08 12:17:47 +03:00
|
|
|
return object;
|
2025-06-02 16:43:39 +04:00
|
|
|
} else if (!pinningOld
|
|
|
|
&& (peer->isChat() || peer->isMegagroup())
|
|
|
|
&& !peer->isMonoforum()) {
|
2022-03-08 12:17:47 +03:00
|
|
|
auto object = object_ptr<Ui::Checkbox>(
|
|
|
|
box,
|
|
|
|
tr::lng_pinned_notify(tr::now),
|
|
|
|
true,
|
|
|
|
st::urlAuthCheckbox);
|
|
|
|
object->setAllowTextLines();
|
2025-07-18 10:07:39 +04:00
|
|
|
state->notify = base::make_weak(object.data());
|
2022-03-08 12:17:47 +03:00
|
|
|
return object;
|
|
|
|
}
|
|
|
|
return { nullptr };
|
|
|
|
}();
|
2021-10-18 23:37:25 +03:00
|
|
|
|
2022-03-08 12:17:47 +03:00
|
|
|
auto pinMessage = [=] {
|
|
|
|
if (state->requestId) {
|
|
|
|
return;
|
|
|
|
}
|
2021-10-18 23:37:25 +03:00
|
|
|
|
2022-03-08 12:17:47 +03: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 23:37:25 +03:00
|
|
|
|
2022-03-08 12:17:47 +03: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 23:37:25 +03:00
|
|
|
|
2022-03-08 12:17:47 +03:00
|
|
|
if (checkbox) {
|
|
|
|
auto padding = st::boxPadding;
|
|
|
|
padding.setTop(padding.bottom());
|
|
|
|
box->addRow(std::move(checkbox), std::move(padding));
|
2021-10-18 23:37:25 +03:00
|
|
|
}
|
|
|
|
}
|