2022-03-28 23:53: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 "menu/menu_ttl_validator.h"
|
|
|
|
|
|
|
|
#include "apiwrap.h"
|
|
|
|
#include "data/data_channel.h"
|
|
|
|
#include "data/data_chat.h"
|
|
|
|
#include "data/data_peer.h"
|
2022-04-15 10:59:44 +00:00
|
|
|
#include "data/data_user.h"
|
2022-03-28 23:53:25 +00:00
|
|
|
#include "lang/lang_keys.h"
|
|
|
|
#include "main/main_session.h"
|
|
|
|
#include "menu/menu_ttl.h"
|
2022-03-31 21:32:03 +00:00
|
|
|
#include "ui/layers/generic_box.h"
|
2022-03-28 23:53:25 +00:00
|
|
|
#include "ui/layers/show.h"
|
|
|
|
#include "ui/text/text_utilities.h"
|
|
|
|
#include "ui/toast/toast.h"
|
|
|
|
#include "ui/toasts/common_toasts.h"
|
2022-03-31 16:45:11 +00:00
|
|
|
#include "ui/text/format_values.h"
|
2022-03-28 23:53:25 +00:00
|
|
|
#include "styles/style_chat.h"
|
|
|
|
#include "styles/style_menu_icons.h"
|
|
|
|
|
|
|
|
namespace TTLMenu {
|
|
|
|
namespace {
|
|
|
|
|
|
|
|
constexpr auto kToastDuration = crl::time(3500);
|
|
|
|
|
|
|
|
void ShowAutoDeleteToast(
|
|
|
|
not_null<QWidget*> parent,
|
|
|
|
not_null<PeerData*> peer) {
|
|
|
|
const auto period = peer->messagesTTL();
|
|
|
|
if (!period) {
|
|
|
|
Ui::Toast::Show(parent, tr::lng_ttl_about_tooltip_off(tr::now));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const auto duration = (period == 5)
|
|
|
|
? u"5 seconds"_q
|
2022-03-31 16:45:11 +00:00
|
|
|
: Ui::FormatTTL(period);
|
2022-03-28 23:53:25 +00:00
|
|
|
const auto text = peer->isBroadcast()
|
|
|
|
? tr::lng_ttl_about_tooltip_channel(tr::now, lt_duration, duration)
|
|
|
|
: tr::lng_ttl_about_tooltip(tr::now, lt_duration, duration);
|
|
|
|
Ui::ShowMultilineToast({
|
|
|
|
.parentOverride = parent,
|
|
|
|
.text = { text },
|
|
|
|
.duration = kToastDuration,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace
|
|
|
|
|
|
|
|
TTLValidator::TTLValidator(
|
|
|
|
std::shared_ptr<Ui::Show> show,
|
|
|
|
not_null<PeerData*> peer)
|
|
|
|
: _peer(peer)
|
|
|
|
, _show(std::move(show)) {
|
|
|
|
}
|
|
|
|
|
|
|
|
Args TTLValidator::createArgs() const {
|
|
|
|
const auto peer = _peer;
|
|
|
|
const auto show = _show;
|
|
|
|
struct State {
|
|
|
|
TimeId savingPeriod = 0;
|
|
|
|
mtpRequestId savingRequestId = 0;
|
|
|
|
};
|
|
|
|
const auto state = std::make_shared<State>();
|
2022-11-30 10:06:17 +00:00
|
|
|
auto callback = [=, toastParent = show->toastParent()](
|
|
|
|
TimeId period,
|
|
|
|
Fn<void()>) {
|
2022-03-28 23:53:25 +00:00
|
|
|
auto &api = peer->session().api();
|
|
|
|
if (state->savingRequestId) {
|
|
|
|
if (period == state->savingPeriod) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
api.request(state->savingRequestId).cancel();
|
|
|
|
}
|
|
|
|
state->savingPeriod = period;
|
|
|
|
state->savingRequestId = api.request(MTPmessages_SetHistoryTTL(
|
|
|
|
peer->input,
|
|
|
|
MTP_int(period)
|
|
|
|
)).done([=](const MTPUpdates &result) {
|
|
|
|
peer->session().api().applyUpdates(result);
|
|
|
|
ShowAutoDeleteToast(toastParent, peer);
|
|
|
|
state->savingRequestId = 0;
|
|
|
|
}).fail([=] {
|
|
|
|
state->savingRequestId = 0;
|
|
|
|
}).send();
|
2022-11-27 14:54:24 +00:00
|
|
|
show->hideLayer();
|
2022-03-28 23:53:25 +00:00
|
|
|
};
|
2022-11-29 20:09:57 +00:00
|
|
|
auto about1 = peer->isUser()
|
2022-03-28 23:53:25 +00:00
|
|
|
? tr::lng_ttl_edit_about(lt_user, rpl::single(peer->shortName()))
|
|
|
|
: peer->isBroadcast()
|
|
|
|
? tr::lng_ttl_edit_about_channel()
|
|
|
|
: tr::lng_ttl_edit_about_group();
|
2022-11-29 20:09:57 +00:00
|
|
|
auto about2 = tr::lng_ttl_edit_about2(
|
|
|
|
lt_link,
|
|
|
|
tr::lng_ttl_edit_about2_link(
|
|
|
|
) | rpl::map([=](const QString &s) {
|
|
|
|
return Ui::Text::Link(s, "tg://settings/auto_delete");
|
|
|
|
}),
|
|
|
|
Ui::Text::WithEntities);
|
|
|
|
auto about = rpl::combine(
|
|
|
|
std::move(about1),
|
|
|
|
std::move(about2)
|
|
|
|
) | rpl::map([](const QString &s1, TextWithEntities &&s2) {
|
|
|
|
return TextWithEntities{ s1 }.append(u"\n\n"_q).append(std::move(s2));
|
|
|
|
});
|
2022-03-28 23:53:25 +00:00
|
|
|
const auto ttl = peer->messagesTTL();
|
|
|
|
return { std::move(show), ttl, std::move(about), std::move(callback) };
|
|
|
|
}
|
|
|
|
|
|
|
|
bool TTLValidator::can() const {
|
2022-03-31 21:32:03 +00:00
|
|
|
return (_peer->isUser()
|
|
|
|
&& !_peer->isSelf()
|
2022-04-15 10:59:44 +00:00
|
|
|
&& !_peer->isNotificationsUser()
|
|
|
|
&& !_peer->asUser()->isInaccessible())
|
2022-03-28 23:53:25 +00:00
|
|
|
|| (_peer->isChat()
|
2022-12-01 10:46:04 +00:00
|
|
|
&& _peer->asChat()->canEditInformation()
|
2022-04-28 12:13:58 +00:00
|
|
|
&& _peer->asChat()->amIn())
|
2022-03-28 23:53:25 +00:00
|
|
|
|| (_peer->isChannel()
|
2022-12-01 10:46:04 +00:00
|
|
|
&& _peer->asChannel()->canEditInformation()
|
2022-04-28 12:13:58 +00:00
|
|
|
&& _peer->asChannel()->amIn());
|
2022-03-28 23:53:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void TTLValidator::showToast() const {
|
|
|
|
ShowAutoDeleteToast(_show->toastParent(), _peer);
|
|
|
|
}
|
|
|
|
|
|
|
|
const style::icon *TTLValidator::icon() const {
|
|
|
|
return &st::menuIconTTL;
|
|
|
|
}
|
|
|
|
|
2022-03-31 21:32:03 +00:00
|
|
|
void TTLValidator::showBox() const {
|
|
|
|
_show->showBox(Box(TTLBox, createArgs()));
|
|
|
|
}
|
|
|
|
|
2022-03-28 23:53:25 +00:00
|
|
|
} // namespace TTLMenu
|