feat: hide messages from blocked users

Co-authored-by: Ilya Fedin <fedin-ilja2010@ya.ru>
This commit is contained in:
AlexeyZavar 2024-04-19 00:05:00 +03:00
parent b4c5963a1a
commit 1c85a81f1f
8 changed files with 114 additions and 11 deletions

View file

@ -195,6 +195,9 @@ AyuGramSettings::AyuGramSettings() {
saveDeletedMessages = true; saveDeletedMessages = true;
saveMessagesHistory = true; saveMessagesHistory = true;
// ~ Message filters
hideFromBlocked = false;
// ~ QoL toggles // ~ QoL toggles
disableAds = true; disableAds = true;
disableStories = false; disableStories = false;
@ -241,7 +244,7 @@ AyuGramSettings::AyuGramSettings() {
* showPeerId = 0 means no ID shown * showPeerId = 0 means no ID shown
* showPeerId = 1 means ID shown as for Telegram API devs * showPeerId = 1 means ID shown as for Telegram API devs
* showPeerId = 2 means ID shown as for Bot API devs (-100) * showPeerId = 2 means ID shown as for Bot API devs (-100)
*/ */
showPeerId = 2; showPeerId = 2;
showMessageSeconds = false; showMessageSeconds = false;
showMessageShot = true; showMessageShot = true;
@ -307,6 +310,10 @@ void AyuGramSettings::set_saveMessagesHistory(bool val) {
saveMessagesHistory = val; saveMessagesHistory = val;
} }
void AyuGramSettings::set_hideFromBlocked(bool val) {
hideFromBlocked = val;
}
void AyuGramSettings::set_disableAds(bool val) { void AyuGramSettings::set_disableAds(bool val) {
disableAds = val; disableAds = val;
} }

View file

@ -29,6 +29,8 @@ public:
bool saveDeletedMessages; bool saveDeletedMessages;
bool saveMessagesHistory; bool saveMessagesHistory;
bool hideFromBlocked;
bool disableAds; bool disableAds;
bool disableStories; bool disableStories;
bool disableCustomBackgrounds; bool disableCustomBackgrounds;
@ -87,6 +89,8 @@ public:
void set_saveDeletedMessages(bool val); void set_saveDeletedMessages(bool val);
void set_saveMessagesHistory(bool val); void set_saveMessagesHistory(bool val);
void set_hideFromBlocked(bool val);
void set_disableAds(bool val); void set_disableAds(bool val);
void set_disableStories(bool val); void set_disableStories(bool val);
void set_disableCustomBackgrounds(bool val); void set_disableCustomBackgrounds(bool val);
@ -143,6 +147,7 @@ NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE_WITH_DEFAULT(
useScheduledMessages, useScheduledMessages,
saveDeletedMessages, saveDeletedMessages,
saveMessagesHistory, saveMessagesHistory,
hideFromBlocked,
disableAds, disableAds,
disableStories, disableStories,
disableCustomBackgrounds, disableCustomBackgrounds,

View file

@ -563,6 +563,31 @@ void SetupSpyEssentials(not_null<Ui::VerticalLayout*> container) {
container->lifetime()); container->lifetime());
} }
void SetupMessageFilters(not_null<Ui::VerticalLayout*> container) {
auto settings = &AyuSettings::getInstance();
AddSubsectionTitle(container, tr::ayu_RegexFilters());
AddButtonWithIcon(
container,
tr::ayu_FiltersHideFromBlocked(),
st::settingsButtonNoIcon
)->toggleOn(
rpl::single(settings->hideFromBlocked)
)->toggledValue(
) | rpl::filter(
[=](bool enabled)
{
return (enabled != settings->hideFromBlocked);
}) | start_with_next(
[=](bool enabled)
{
settings->set_hideFromBlocked(enabled);
AyuSettings::save();
},
container->lifetime());
}
void SetupQoLToggles(not_null<Ui::VerticalLayout*> container) { void SetupQoLToggles(not_null<Ui::VerticalLayout*> container) {
auto settings = &AyuSettings::getInstance(); auto settings = &AyuSettings::getInstance();
@ -1317,6 +1342,12 @@ void SetupAyuGramSettings(not_null<Ui::VerticalLayout*> container,
AddDivider(container); AddDivider(container);
AddSkip(container);
SetupMessageFilters(container);
AddSkip(container);
AddDivider(container);
AddSkip(container); AddSkip(container);
SetupQoLToggles(container); SetupQoLToggles(container);
AddSkip(container); AddSkip(container);

View file

@ -2422,10 +2422,6 @@ void Session::registerMessage(not_null<HistoryItem*> item) {
const auto list = messagesListForInsert(peerId); const auto list = messagesListForInsert(peerId);
const auto itemId = item->id; const auto itemId = item->id;
if (AyuState::isHidden(item)) {
return;
}
const auto i = list->find(itemId); const auto i = list->find(itemId);
if (i != list->end()) { if (i != list->end()) {
LOG(("App Error: Trying to re-registerMessage().")); LOG(("App Error: Trying to re-registerMessage()."));

View file

@ -1312,10 +1312,6 @@ void History::viewReplaced(not_null<const Element*> was, Element *now) {
void History::addItemToBlock(not_null<HistoryItem*> item) { void History::addItemToBlock(not_null<HistoryItem*> item) {
Expects(!item->mainView()); Expects(!item->mainView());
if (AyuState::isHidden(item)) {
return;
}
auto block = prepareBlockForAddingItem(); auto block = prepareBlockForAddingItem();
block->messages.push_back(item->createView(_delegateMixin->delegate())); block->messages.push_back(item->createView(_delegateMixin->delegate()));

View file

@ -54,6 +54,10 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL
#include <QtGui/QGuiApplication> #include <QtGui/QGuiApplication>
// AyuGram includes
#include "ayu/ayu_settings.h"
namespace { namespace {
const auto kPsaForwardedPrefix = "cloud_lng_forwarded_psa_"; const auto kPsaForwardedPrefix = "cloud_lng_forwarded_psa_";
@ -436,16 +440,27 @@ void HistoryMessageReply::updateData(
&& (asExternal || _fields.manualQuote); && (asExternal || _fields.manualQuote);
_multiline = !_fields.storyId && (asExternal || nonEmptyQuote); _multiline = !_fields.storyId && (asExternal || nonEmptyQuote);
const auto settings = &AyuSettings::getInstance();
const auto author = resolvedMessage
? resolvedMessage->from().get()
: resolvedStory
? resolvedStory->peer().get()
: nullptr;
const auto blocked = settings->hideFromBlocked
&& author
&& author->isUser()
&& author->asUser()->isBlocked();
const auto displaying = resolvedMessage const auto displaying = resolvedMessage
|| resolvedStory || resolvedStory
|| ((nonEmptyQuote || _fields.externalMedia) || ((nonEmptyQuote || _fields.externalMedia)
&& (!_fields.messageId || force)); && (!_fields.messageId || force));
_displaying = displaying ? 1 : 0; _displaying = displaying && !blocked ? 1 : 0;
const auto unavailable = !resolvedMessage const auto unavailable = !resolvedMessage
&& !resolvedStory && !resolvedStory
&& ((!_fields.storyId && !_fields.messageId) || force); && ((!_fields.storyId && !_fields.messageId) || force);
_unavailable = unavailable ? 1 : 0; _unavailable = unavailable && !blocked ? 1 : 0;
if (force) { if (force) {
if (!_displaying && (_fields.messageId || _fields.storyId)) { if (!_displaying && (_fields.messageId || _fields.storyId)) {

View file

@ -49,6 +49,8 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL
#include "styles/style_chat.h" #include "styles/style_chat.h"
// AyuGram includes // AyuGram includes
#include "ayu/ayu_settings.h"
#include "ayu/ayu_state.h"
#include "ayu/features/messageshot/message_shot.h" #include "ayu/features/messageshot/message_shot.h"
@ -713,6 +715,17 @@ bool Element::isHiddenByGroup() const {
} }
bool Element::isHidden() const { bool Element::isHidden() const {
if (AyuState::isHidden(data())) {
return true;
}
const auto settings = &AyuSettings::getInstance();
if (settings->hideFromBlocked) {
if (data()->from()->isUser() &&
data()->from()->asUser()->isBlocked()) {
return true;
}
}
return isHiddenByGroup(); return isHiddenByGroup();
} }

View file

@ -53,6 +53,7 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL
// AyuGram includes // AyuGram includes
#include "ayu/ayu_settings.h" #include "ayu/ayu_settings.h"
#include "api/api_blocked_peers.h"
namespace Main { namespace Main {
@ -79,6 +80,43 @@ constexpr auto kTmpPasswordReserveTime = TimeId(10);
return MTP::ConfigFields().internalLinksDomain; return MTP::ConfigFields().internalLinksDomain;
} }
void InitializeBlockedPeers(not_null<Main::Session*> session) {
const auto offset = std::make_shared<int>(0);
const auto allLoaded = std::make_shared<bool>(false);
const auto applySlice = [=](
const Api::BlockedPeers::Slice &slice,
auto self) -> void {
if (slice.list.empty()) {
*allLoaded = true;
}
*offset += slice.list.size();
for (const auto &item : slice.list) {
if (const auto peer = session->data().peerLoaded(item.id)) {
peer->setIsBlocked(true);
}
}
if (*offset >= slice.total) {
*allLoaded = true;
}
if (!*allLoaded) {
session->api().blockedPeers().request(
*offset,
[=](const Api::BlockedPeers::Slice &slice) {
self(slice, self);
});
}
};
session->api().blockedPeers().slice(
) | rpl::take(
1
) | rpl::start_with_next([=](const Api::BlockedPeers::Slice &result) {
applySlice(result, applySlice);
}, session->lifetime());
}
} // namespace } // namespace
Session::Session( Session::Session(
@ -189,6 +227,8 @@ Session::Session(
_api->requestNotifySettings(MTP_inputNotifyBroadcasts()); _api->requestNotifySettings(MTP_inputNotifyBroadcasts());
Core::App().downloadManager().trackSession(this); Core::App().downloadManager().trackSession(this);
InitializeBlockedPeers(this);
} }
void Session::setTmpPassword(const QByteArray &password, TimeId validUntil) { void Session::setTmpPassword(const QByteArray &password, TimeId validUntil) {