From d43cb5995b3b88577a412621717ca89940eca9be Mon Sep 17 00:00:00 2001 From: eleksir Date: Mon, 12 Dec 2022 17:10:02 +0300 Subject: [PATCH] Add settings migration when group -> supergroup promotion event discovered --- lib/BotLib/Admin.pm | 74 ++++++++++++++++++++++++++++++++++++++++++++- lib/RedisLib.pm | 31 ++++++++++++++++++- lib/TelegramBot.pm | 12 +++++++- 3 files changed, 114 insertions(+), 3 deletions(-) diff --git a/lib/BotLib/Admin.pm b/lib/BotLib/Admin.pm index c5b8846..b6ce9f5 100644 --- a/lib/BotLib/Admin.pm +++ b/lib/BotLib/Admin.pm @@ -18,7 +18,7 @@ our @ISA = qw / Exporter /; ## no critic (ClassHierarchies::ProhibitExplicitI our @EXPORT_OK = qw (@ForbiddenMessageTypes @PluginList GetForbiddenTypes AddForbiddenType DelForbiddenType ListForbidden FortuneToggle FortuneToggleList FortuneStatus PluginToggle PluginStatus PluginEnabled ChanMsgToggle ChanMsgStatus ChanMsgEnabled GreetMsgToggle GreetMsgStatus - GreetMsgEnabled GoodbyeMsgToggle GoodbyeMsgStatus GoodbyeMsgEnabled); + GreetMsgEnabled GoodbyeMsgToggle GoodbyeMsgStatus GoodbyeMsgEnabled MigrateSettingsToNewChatID); my $c = LoadConf (); my $cachedir = $c->{cachedir}; @@ -108,6 +108,29 @@ sub ListForbidden { return join "\n", @list; } +sub StatusForbidden { + my $chatid = shift; + my $ftype = shift; + + my $cache = CHI->new ( + driver => 'BerkeleyDB', + root_dir => $cachedir, + namespace => __PACKAGE__ . '_' . 'censor' . '_' . utf2sha1 ($chatid), + ); + + my $type = $cache->get ($ftype); + + if ($type) { + return 1; + } else { + unless (defined $type) { + $cache->set ($ftype, 0, 'never'); + } + + return 0; + } +} + sub FortuneToggle (@) { my $chatid = shift; my $action = shift // undef; @@ -534,6 +557,55 @@ sub PluginToggle (@) { return $phrase; } +sub MigrateSettingsToNewChatID { + my $old_id = shift; + my $new_id = shift; + + if (FortuneStatus ($old_id)) { + FortuneToggle ($new_id, 1); + } else { + FortuneToggle ($new_id, 0); + } + + if (ChanMsgStatus ($old_id)) { + ChanMsgToggle ($new_id, 1); + } else { + ChanMsgToggle ($new_id, 0); + } + + if (GreetMsgStatus ($old_id)) { + GreetMsgToggle ($new_id, 1); + } else { + GreetMsgToggle ($new_id, 0); + } + + if (GoodbyeMsgStatus ($old_id)) { + GoodbyeMsgToggle ($new_id, 1); + } else { + GoodbyeMsgToggle ($new_id, 0); + } + + # TODO: forbidden types + + foreach my $plugin (@PluginList) { + if (PluginEnabled ($old_id, $plugin)) { + PluginToggle ($new_id, $plugin, 1); + } else { + PluginToggle ($new_id, $plugin, 0); + } + } + + foreach my $ftype (@ForbiddenMessageTypes) { + if (StatusForbidden($old_id, $ftype)) { + AddForbiddenType($new_id, $ftype); + } + + # Looks like we don't need to add forbiddent type for allowed types... right? + } + + return; +} + 1; # vim: set ft=perl noet ai ts=4 sw=4 sts=4: diff --git a/lib/RedisLib.pm b/lib/RedisLib.pm index aad0bdc..27f8059 100644 --- a/lib/RedisLib.pm +++ b/lib/RedisLib.pm @@ -7,10 +7,12 @@ use utf8; use open qw (:std :utf8); use Data::Dumper qw (Dumper); +use JSON::XS qw (decode_json); use Log::Any qw ($log); use Mojo::Redis (); use Mojo::Redis::Connection (); +use BotLib::Admin qw (MigrateSettingsToNewChatID); use BotLib::Conf qw (LoadConf); use Teapot::Bot::Object::ChatPermissions (); @@ -62,7 +64,34 @@ sub redis_parse_message { my $r = $main::TGM->sendMessage ($message); if ($r->{error}) { - $log->error ("An error occured while sending message to chat $m->{chatid}"); + if (defined ($r->{http_code}) && $r->{http_code} == 400) { + unless (defined $r->{$message}) { + $log->error ("An error occured while sending message to chat $m->{chatid}"); + return; + } + + my $j = eval { decode_json ($r->{$message}); }; + + unless (defined $j) { + $log->error ("An error occured while sending message to chat $m->{chatid}"); + return; + } + + unless (defined $j->{migrate_to_chat_id}) { + $log->error ("An error occured while sending message to chat $m->{chatid}"); + return; + } + + MigrateSettingsToNewChatID ($m->{chatid}, $j->{migrate_to_chat_id}); + + $message->{chat_id} = $j->{migrate_to_chat_id}; + $r = undef; + $r = $main::TGM->sendMessage ($message); + + if ($r->{error}) { + $log->error ("An error occured while sending message to chat $j->{migrate_to_chat_id}"); + } + } } } diff --git a/lib/TelegramBot.pm b/lib/TelegramBot.pm index 46ea2b5..12eaa72 100644 --- a/lib/TelegramBot.pm +++ b/lib/TelegramBot.pm @@ -14,7 +14,7 @@ use Log::Any qw ($log); use Math::Random::Secure qw (irand); use Mojo::Base 'Teapot::Bot::Brain'; -use BotLib::Admin qw (FortuneToggleList ChanMsgEnabled GreetMsgEnabled GoodbyeMsgEnabled); +use BotLib::Admin qw (FortuneToggleList ChanMsgEnabled GreetMsgEnabled GoodbyeMsgEnabled MigrateSettingsToNewChatID); use BotLib qw (Command); use BotLib::Conf qw (LoadConf); use BotLib::Util qw (trim fmatch BotSleep Highlight IsCensored RandomCommonPhrase); @@ -99,6 +99,16 @@ sub __on_msg { my ($self, $msg) = @_; # chat info my $chatid; + + # Ловим сообщения о миграции. + if ($msg->can ('migrate_to_chat_id') && $msg->can ('migrate_from_chat_id')) { + if (defined ($msg->migrate_from_chat_id) && defined ($msg->migrate_from_chat_id)) { + MigrateSettingsToNewChatID ($msg->migrate_from_chat_id, $msg->migrate_to_chat_id); + } + + return; + } + my $chatname = 'Noname chat'; # user sending message info my ($userid, $username, $fullname, $highlight, $vis_a_vi) = Highlight ($msg);