Added ability to remove peer from chats filters when leave it.

This commit is contained in:
23rd 2024-11-24 10:27:54 +03:00
parent aa8d543ed8
commit 728d9a0993
4 changed files with 75 additions and 1 deletions

View file

@ -5168,6 +5168,10 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL
"lng_filters_link" = "Share Folder";
"lng_filters_link_has" = "Invite links";
"lng_filters_checkbox_remove_bot" = "Remove bot from all folders";
"lng_filters_checkbox_remove_group" = "Remove group from all folders";
"lng_filters_checkbox_remove_channel" = "Remove channel from all folders";
"lng_filters_link_create" = "Create an Invite Link";
"lng_filters_link_cant" = "You cant share folders which include or exclude specific chat types like 'Groups', 'Contacts', etc.";
"lng_filters_link_about" = "Share access to some of this folder's groups and channels with others.";

View file

@ -19,6 +19,7 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL
#include "core/ui_integration.h"
#include "data/data_channel.h"
#include "data/data_chat.h"
#include "data/data_chat_filters.h"
#include "data/data_chat_participant_status.h"
#include "data/data_histories.h"
#include "data/data_peer.h"
@ -512,6 +513,7 @@ void DeleteChatBox(not_null<Ui::GenericBox*> box, not_null<PeerData*> peer) {
const auto container = box->verticalLayout();
const auto maybeUser = peer->asUser();
const auto isBot = maybeUser && maybeUser->isBot();
Ui::AddSkip(container);
Ui::AddSkip(container);
@ -595,7 +597,7 @@ void DeleteChatBox(not_null<Ui::GenericBox*> box, not_null<PeerData*> peer) {
}();
const auto maybeBotCheckbox = [&]() -> Ui::Checkbox* {
if (!maybeUser || !maybeUser->isBot()) {
if (!isBot) {
return nullptr;
}
Ui::AddSkip(container);
@ -608,6 +610,40 @@ void DeleteChatBox(not_null<Ui::GenericBox*> box, not_null<PeerData*> peer) {
st::defaultBoxCheckbox));
}();
const auto removeFromChatsFilters = [=](
not_null<History*> history) -> std::vector<FilterId> {
auto result = std::vector<FilterId>();
for (const auto &filter : peer->owner().chatsFilters().list()) {
if (filter.withoutAlways(history) != filter) {
result.push_back(filter.id());
}
}
return result;
};
const auto maybeChatsFiltersCheckbox = [&]() -> Ui::Checkbox* {
const auto history = (isBot || !maybeUser)
? peer->owner().history(peer).get()
: nullptr;
if (!history || removeFromChatsFilters(history).empty()) {
return nullptr;
}
Ui::AddSkip(container);
Ui::AddSkip(container);
return box->addRow(
object_ptr<Ui::Checkbox>(
container,
(maybeBotCheckbox
? tr::lng_filters_checkbox_remove_bot
: (peer->isChannel() && !peer->isMegagroup())
? tr::lng_filters_checkbox_remove_channel
: tr::lng_filters_checkbox_remove_group)(
tr::now,
Ui::Text::WithEntities),
false,
st::defaultBoxCheckbox));
}();
Ui::AddSkip(container);
auto buttonText = maybeUser
@ -622,10 +658,35 @@ void DeleteChatBox(not_null<Ui::GenericBox*> box, not_null<PeerData*> peer) {
box->addButton(std::move(buttonText), [=] {
const auto revoke = maybeCheckbox && maybeCheckbox->checked();
const auto stopBot = maybeBotCheckbox && maybeBotCheckbox->checked();
const auto removeFromChats = maybeChatsFiltersCheckbox
&& maybeChatsFiltersCheckbox->checked();
Core::App().closeChatFromWindows(peer);
if (stopBot) {
peer->session().api().blockedPeers().block(peer);
}
if (removeFromChats) {
const auto history = peer->owner().history(peer).get();
const auto removeFrom = removeFromChatsFilters(history);
for (const auto &filter : peer->owner().chatsFilters().list()) {
if (!ranges::contains(removeFrom, filter.id())) {
continue;
}
const auto result = filter.withoutAlways(history);
if (result == filter) {
continue;
}
const auto tl = result.tl();
peer->owner().chatsFilters().apply(MTP_updateDialogFilter(
MTP_flags(MTPDupdateDialogFilter::Flag::f_filter),
MTP_int(filter.id()),
tl));
peer->session().api().request(MTPmessages_UpdateDialogFilter(
MTP_flags(MTPmessages_UpdateDialogFilter::Flag::f_filter),
MTP_int(filter.id()),
tl
)).send();
}
}
// Don't delete old history by default,
// because Android app doesn't.
//

View file

@ -188,6 +188,14 @@ ChatFilter ChatFilter::withChatlist(bool chatlist, bool hasMyLinks) const {
return result;
}
ChatFilter ChatFilter::withoutAlways(not_null<History*> history) const {
auto result = *this;
if (CanRemoveFromChatFilter(result, history)) {
result._always.remove(history);
}
return result;
}
MTPDialogFilter ChatFilter::tl(FilterId replaceId) const {
auto always = _always;
auto pinned = QVector<MTPInputPeer>();

View file

@ -64,6 +64,7 @@ public:
[[nodiscard]] ChatFilter withChatlist(
bool chatlist,
bool hasMyLinks) const;
[[nodiscard]] ChatFilter withoutAlways(not_null<History*>) const;
[[nodiscard]] static ChatFilter FromTL(
const MTPDialogFilter &data,