-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Eject unneeded srcs from tests Fix some unused param warnings
- Loading branch information
Showing
23 changed files
with
326 additions
and
340 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
#include <tgbot/TgException.h> | ||
#include <trivial_helpers/_std_chrono_templates.h> | ||
#include <trivial_helpers/_tgbot.h> | ||
|
||
#include <global_handlers/SpamBlockManager.hpp> | ||
|
||
void SpamBlockManager::runFunction(const std::stop_token &token) { | ||
while (!token.stop_requested()) { | ||
consumeAndDetect(); | ||
delayUnlessStop(sSpamDetectDelay); | ||
} | ||
} | ||
|
||
void SpamBlockManager::onDetected(ChatId chat, UserId user, | ||
std::vector<MessageId> messageIds) const { | ||
// Initial set - all false set | ||
static auto perms = std::make_shared<TgBot::ChatPermissions>(); | ||
switch (_config) { | ||
case Config::DELETE_AND_MUTE: | ||
LOG(INFO) << fmt::format("Try mute offending user"); | ||
try { | ||
_api->muteChatMember(chat, user, perms, | ||
to_secs(kMuteDuration).count()); | ||
} catch (const TgBot::TgException &e) { | ||
LOG(WARNING) << fmt::format("Cannot mute: {}", e.what()); | ||
} | ||
[[fallthrough]]; | ||
case Config::DELETE: { | ||
try { | ||
_api->deleteMessages(chat, messageIds); | ||
} catch (const TgBot::TgException &e) { | ||
DLOG(INFO) << "Error deleting messages: " << e.what(); | ||
} | ||
[[fallthrough]]; | ||
} | ||
case Config::LOGGING_ONLY: | ||
SpamBlockBase::onDetected(chat, user, messageIds); | ||
break; | ||
default: | ||
break; | ||
}; | ||
} | ||
|
||
bool SpamBlockManager::shouldBeSkipped(const Message::Ptr &message) const { | ||
if (_auth->isAuthorized(message, AuthContext::Flags::None)) { | ||
return true; | ||
} | ||
|
||
// Ignore old messages | ||
if (!AuthContext::isUnderTimeLimit(message)) { | ||
return true; | ||
} | ||
|
||
// Bot's PM is not a concern | ||
if (message->chat->type == TgBot::Chat::Type::Private) { | ||
return true; | ||
} | ||
return false; | ||
} | ||
|
||
SpamBlockManager::SpamBlockManager(TgBotApi::Ptr api, AuthContext *auth) | ||
: _api(api), _auth(auth) { | ||
api->onAnyMessage([this](TgBotApi::CPtr, const Message::Ptr &message) { | ||
addMessage(message); | ||
return TgBotApi::AnyMessageResult::Handled; | ||
}); | ||
run(); | ||
} |
Oops, something went wrong.