2022-04-05 20:51:42 +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/choose_filter_box.h"
|
|
|
|
|
|
|
|
#include "apiwrap.h"
|
2022-05-09 13:09:46 +00:00
|
|
|
#include "boxes/premium_limits_box.h"
|
2022-04-05 20:51:42 +00:00
|
|
|
#include "core/application.h" // primaryWindow
|
2022-04-05 20:51:42 +00:00
|
|
|
#include "data/data_chat_filters.h"
|
|
|
|
#include "data/data_session.h"
|
|
|
|
#include "history/history.h"
|
|
|
|
#include "lang/lang_keys.h"
|
|
|
|
#include "main/main_session.h"
|
2022-04-05 20:51:42 +00:00
|
|
|
#include "ui/text/text_utilities.h" // Ui::Text::Bold
|
2022-04-05 20:51:42 +00:00
|
|
|
#include "ui/widgets/buttons.h"
|
2022-04-10 10:49:14 +00:00
|
|
|
#include "ui/widgets/popup_menu.h"
|
2023-05-02 09:33:19 +00:00
|
|
|
#include "window/window_controller.h"
|
2022-04-05 20:51:42 +00:00
|
|
|
#include "window/window_session_controller.h"
|
2022-04-10 10:49:14 +00:00
|
|
|
#include "styles/style_media_player.h" // mediaPlayerMenuCheck
|
2022-04-05 20:51:42 +00:00
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
|
|
|
Data::ChatFilter ChangedFilter(
|
|
|
|
const Data::ChatFilter &filter,
|
|
|
|
not_null<History*> history,
|
|
|
|
bool add) {
|
|
|
|
auto always = base::duplicate(filter.always());
|
|
|
|
auto never = base::duplicate(filter.never());
|
|
|
|
if (add) {
|
|
|
|
never.remove(history);
|
|
|
|
} else {
|
2023-09-01 07:59:00 +00:00
|
|
|
always.remove(history);
|
|
|
|
}
|
|
|
|
const auto result = Data::ChatFilter(
|
|
|
|
filter.id(),
|
|
|
|
filter.title(),
|
|
|
|
filter.iconEmoji(),
|
|
|
|
filter.flags(),
|
|
|
|
std::move(always),
|
|
|
|
filter.pinned(),
|
|
|
|
std::move(never));
|
|
|
|
const auto in = result.contains(history);
|
|
|
|
if (in == add) {
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
always = base::duplicate(result.always());
|
|
|
|
never = base::duplicate(result.never());
|
|
|
|
if (add) {
|
|
|
|
always.insert(history);
|
|
|
|
} else {
|
|
|
|
never.insert(history);
|
2022-04-05 20:51:42 +00:00
|
|
|
}
|
|
|
|
return Data::ChatFilter(
|
|
|
|
filter.id(),
|
|
|
|
filter.title(),
|
|
|
|
filter.iconEmoji(),
|
|
|
|
filter.flags(),
|
|
|
|
std::move(always),
|
|
|
|
filter.pinned(),
|
|
|
|
std::move(never));
|
|
|
|
}
|
|
|
|
|
|
|
|
void ChangeFilterById(
|
|
|
|
FilterId filterId,
|
|
|
|
not_null<History*> history,
|
|
|
|
bool add) {
|
2022-05-17 09:24:35 +00:00
|
|
|
Expects(filterId != 0);
|
|
|
|
|
2022-04-05 20:51:42 +00:00
|
|
|
const auto list = history->owner().chatsFilters().list();
|
|
|
|
const auto i = ranges::find(list, filterId, &Data::ChatFilter::id);
|
|
|
|
if (i != end(list)) {
|
|
|
|
const auto was = *i;
|
|
|
|
const auto filter = ChangedFilter(was, history, add);
|
|
|
|
history->owner().chatsFilters().set(filter);
|
|
|
|
history->session().api().request(MTPmessages_UpdateDialogFilter(
|
|
|
|
MTP_flags(MTPmessages_UpdateDialogFilter::Flag::f_filter),
|
|
|
|
MTP_int(filter.id()),
|
|
|
|
filter.tl()
|
2023-01-17 15:47:58 +00:00
|
|
|
)).done([=, chat = history->peer->name(), name = filter.title()] {
|
|
|
|
const auto account = &history->session().account();
|
|
|
|
if (const auto controller = Core::App().windowFor(account)) {
|
2023-05-02 09:33:19 +00:00
|
|
|
controller->showToast((add
|
2022-04-05 20:51:42 +00:00
|
|
|
? tr::lng_filters_toast_add
|
|
|
|
: tr::lng_filters_toast_remove)(
|
|
|
|
tr::now,
|
|
|
|
lt_chat,
|
|
|
|
Ui::Text::Bold(chat),
|
|
|
|
lt_folder,
|
2022-08-09 11:12:19 +00:00
|
|
|
Ui::Text::Bold(name),
|
2023-05-02 09:33:19 +00:00
|
|
|
Ui::Text::WithEntities));
|
2022-04-05 20:51:42 +00:00
|
|
|
}
|
|
|
|
}).fail([=](const MTP::Error &error) {
|
2022-04-05 20:51:42 +00:00
|
|
|
// Revert filter on fail.
|
|
|
|
history->owner().chatsFilters().set(was);
|
|
|
|
}).send();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace
|
|
|
|
|
|
|
|
ChooseFilterValidator::ChooseFilterValidator(not_null<History*> history)
|
|
|
|
: _history(history) {
|
|
|
|
}
|
|
|
|
|
|
|
|
bool ChooseFilterValidator::canAdd() const {
|
|
|
|
for (const auto &filter : _history->owner().chatsFilters().list()) {
|
2022-05-17 09:24:35 +00:00
|
|
|
if (filter.id() && !filter.contains(_history)) {
|
2022-04-05 20:51:42 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool ChooseFilterValidator::canRemove(FilterId filterId) const {
|
2022-05-17 09:24:35 +00:00
|
|
|
Expects(filterId != 0);
|
|
|
|
|
2022-04-05 20:51:42 +00:00
|
|
|
const auto list = _history->owner().chatsFilters().list();
|
|
|
|
const auto i = ranges::find(list, filterId, &Data::ChatFilter::id);
|
|
|
|
if (i != end(list)) {
|
|
|
|
const auto &filter = *i;
|
|
|
|
return filter.contains(_history)
|
|
|
|
&& ((filter.always().size() > 1) || filter.flags());
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2022-06-07 05:05:33 +00:00
|
|
|
ChooseFilterValidator::LimitData ChooseFilterValidator::limitReached(
|
2023-08-11 13:27:17 +00:00
|
|
|
FilterId filterId,
|
|
|
|
bool always) const {
|
2022-05-17 09:24:35 +00:00
|
|
|
Expects(filterId != 0);
|
|
|
|
|
2022-05-09 13:09:46 +00:00
|
|
|
const auto list = _history->owner().chatsFilters().list();
|
|
|
|
const auto i = ranges::find(list, filterId, &Data::ChatFilter::id);
|
2022-11-08 13:57:54 +00:00
|
|
|
const auto limit = _history->owner().pinnedChatsLimit(filterId);
|
2023-08-11 13:27:17 +00:00
|
|
|
const auto &chatsList = always ? i->always() : i->never();
|
2022-06-07 05:05:33 +00:00
|
|
|
return {
|
|
|
|
.reached = (i != end(list))
|
2023-08-11 13:27:17 +00:00
|
|
|
&& !ranges::contains(chatsList, _history)
|
|
|
|
&& (chatsList.size() >= limit),
|
|
|
|
.count = int(chatsList.size()),
|
2022-06-07 05:05:33 +00:00
|
|
|
};
|
2022-05-09 13:09:46 +00:00
|
|
|
}
|
|
|
|
|
2022-04-05 20:51:42 +00:00
|
|
|
void ChooseFilterValidator::add(FilterId filterId) const {
|
|
|
|
ChangeFilterById(filterId, _history, true);
|
|
|
|
}
|
|
|
|
|
|
|
|
void ChooseFilterValidator::remove(FilterId filterId) const {
|
|
|
|
ChangeFilterById(filterId, _history, false);
|
|
|
|
}
|
|
|
|
|
2022-04-10 10:49:14 +00:00
|
|
|
void FillChooseFilterMenu(
|
2022-05-09 13:09:46 +00:00
|
|
|
not_null<Window::SessionController*> controller,
|
2022-04-10 10:49:14 +00:00
|
|
|
not_null<Ui::PopupMenu*> menu,
|
|
|
|
not_null<History*> history) {
|
2022-10-19 10:59:37 +00:00
|
|
|
const auto weak = base::make_weak(controller);
|
2022-04-10 10:49:14 +00:00
|
|
|
const auto validator = ChooseFilterValidator(history);
|
|
|
|
for (const auto &filter : history->owner().chatsFilters().list()) {
|
|
|
|
const auto id = filter.id();
|
2022-05-17 09:24:35 +00:00
|
|
|
if (!id) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2022-04-10 10:49:14 +00:00
|
|
|
const auto contains = filter.contains(history);
|
|
|
|
const auto action = menu->addAction(filter.title(), [=] {
|
2023-08-11 13:27:17 +00:00
|
|
|
const auto toAdd = !filter.contains(history);
|
|
|
|
const auto r = validator.limitReached(id, toAdd);
|
|
|
|
if (r.reached) {
|
2022-06-07 05:05:33 +00:00
|
|
|
controller->show(Box(
|
|
|
|
FilterChatsLimitBox,
|
|
|
|
&controller->session(),
|
2023-07-30 03:02:14 +00:00
|
|
|
r.count,
|
2023-08-11 13:27:17 +00:00
|
|
|
toAdd));
|
|
|
|
return;
|
|
|
|
} else if (toAdd ? validator.canAdd() : validator.canRemove(id)) {
|
|
|
|
if (toAdd) {
|
|
|
|
validator.add(id);
|
|
|
|
} else {
|
|
|
|
validator.remove(id);
|
|
|
|
}
|
2022-04-10 10:49:14 +00:00
|
|
|
}
|
|
|
|
}, contains ? &st::mediaPlayerMenuCheck : nullptr);
|
|
|
|
action->setEnabled(contains
|
|
|
|
? validator.canRemove(id)
|
|
|
|
: validator.canAdd());
|
|
|
|
}
|
|
|
|
history->owner().chatsFilters().changed(
|
|
|
|
) | rpl::start_with_next([=] {
|
|
|
|
menu->hideMenu();
|
|
|
|
}, menu->lifetime());
|
|
|
|
}
|