Add settings migration when group -> supergroup promotion event discovered

This commit is contained in:
eleksir
2022-12-12 17:10:02 +03:00
parent 7d74a86dad
commit d43cb5995b
3 changed files with 114 additions and 3 deletions

View File

@@ -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:

View File

@@ -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}");
}
}
}
}

View File

@@ -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);