Skip to content

Commit

Permalink
/tool_ctrl <tool name> <status> now sets a tool's status
Browse files Browse the repository at this point in the history
/tools shows tool status in /tools (if set)
  • Loading branch information
castaway committed Oct 13, 2024
1 parent 8cc291d commit 4f40995
Show file tree
Hide file tree
Showing 5 changed files with 172 additions and 10 deletions.
20 changes: 20 additions & 0 deletions NOTES.txt
Original file line number Diff line number Diff line change
Expand Up @@ -344,3 +344,23 @@ March 2024
return $message->reply($response);

.. members_only tidyup .. it appears that "active_members" returns "sod all" even tho its an admin and a supergroup.. so redone this in a different fashion!


insert into allowed (person_id, tool_id, added_on, is_admin) select person_id, '69F67AEC-0277-11EF-9DD6-430397B5F489', current_timestamp, 0 from allowed where tool_id in (select id from tools where name ilike '%blaster%') on conflict do nothing;

July 2024

* oneall https://app.oneall.com/applications/application/providers/?applicationid=527391
* More social networks? Discord needs a "team" and verifications
** https://discord.com/developers/applications/1267148296003125311/verification-onboarding
* facebook / instagram needs an account with a phone number
** https://developers.facebook.com/async/registration/dialog/?src=default

Aug 2024

TelegramBot, sendMessage reply_markup - ForceReply
** working

* /tools - if single item requested (eg /tools laser) - also show status/description
** buggy - with >1 status returns 2 lasers..
** need "most recent status"..
2 changes: 0 additions & 2 deletions cpanfile.snapshot
Original file line number Diff line number Diff line change
Expand Up @@ -552,7 +552,6 @@ DISTRIBUTIONS
requirements:
Carp::Clan 0
Catalyst::Component::InstancePerContext 0
Catalyst::Devel 1.0
Catalyst::Runtime 5.80005
CatalystX::Component::Traits 0.14
DBD::SQLite 0
Expand All @@ -563,7 +562,6 @@ DISTRIBUTIONS
Module::Runtime 0.012
Moose 1.12
MooseX::MarkAsMethods 0.13
MooseX::NonMoose 0.16
MooseX::Types 0
MooseX::Types::LoadableClass 0.009
Storable 0
Expand Down
2 changes: 1 addition & 1 deletion lib/AccessSystem/Schema/Result/Tool.pm
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ sub current_status {
my $last = $self->statuses_rs->search(
{},
{
order_by => [{ '-desc' => 'when' }],
order_by => [{ '-desc' => 'entered_at' }],
rows => 1,
})->single;

Expand Down
9 changes: 6 additions & 3 deletions lib/AccessSystem/Schema/ResultSet/Tool.pm
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,14 @@ use base 'DBIx::Class::ResultSet';
sub find_tool {
my ($self, $input, $args, $rc_class) = @_;

my $tools = $self->active->search_rs({ 'me.name' => $input }, $args);
# my $tools = $self->active->search_rs({ 'me.name' => $input }, $args);
my $tools = $self->search_rs({ 'me.name' => $input }, $args);
$tools->result_class($rc_class) if $rc_class;
if ($tools->count == 1) {
return ($tools->first, $tools);
}
$tools = $self->active->search_rs({ 'me.name' => { '-like' => "%$input%" }}, $args);
# $tools = $self->active->search_rs({ 'me.name' => { '-like' => "%$input%" }}, $args);
$tools = $self->search_rs({ 'me.name' => { '-like' => "%$input%" }}, $args);
$tools->result_class($rc_class) if $rc_class;
my $tool;
if ($tools->count == 1) {
Expand All @@ -23,7 +25,8 @@ sub find_tool {
return ($tool, $tools) if $tool;
try {
# Pg syntax, but not other databases, sigh
my $pgtools = $self->active->search_rs({ 'me.name' => { '-ilike' => "%$input%" }}, $args);
# my $pgtools = $self->active->search_rs({ 'me.name' => { '-ilike' => "%$input%" }}, $args);
my $pgtools = $self->search_rs({ 'me.name' => { '-ilike' => "%$input%" }}, $args);
if ($pgtools->count) {
$tools = $pgtools;
$tools->result_class($rc_class) if $rc_class;
Expand Down
149 changes: 145 additions & 4 deletions lib/AccessSystem/TelegramBot.pm
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,10 @@ sub read_message ($self, $message) {
match => qr{^/add_tool},
help => '/add_tool <tool>',
},
tool_ctrl => {
match => qr{^/tool_ctrl},
help => '/tool_ctrl <name> [<status>]',
},
find_member => {
match => qr{^/whois\b},,
help => '/whois <name>',
Expand Down Expand Up @@ -260,6 +264,19 @@ sub read_message ($self, $message) {
return $self->$method($message_text, $message);
}
}
# Message is a reply text to something we asked for:
if($message->reply_to_message
&& $self->waiting_on_response->{$message->from->id}
&& $self->waiting_on_response->{$message->from->id}{reply_id} == $message->reply_to_message->message_id
) {
# Assume its the response!
# $self->waiting_on_response->{$message->reply_to_message->from->id}{reply_text} = $message->text;
my $waiting = $self->waiting_on_response->{$message->from->id};
my $method = $waiting->{action};
$self->$method($message->reply_to_message->text,
$message,
['tool_ctrl', 'reply', $message->text]);
}
} elsif (ref($message) eq 'Telegram::Bot::Object::Message' && $message->new_chat_members) {
$self->check_if_ban($message);
} elsif (ref($message) eq 'Telegram::Bot::Object::CallbackQuery') {
Expand Down Expand Up @@ -388,19 +405,31 @@ Output a list of tool names.

sub tools ($self, $text, $message) {
my $tools = $self->db->resultset('Tool')->active;
$tools->result_class('DBIx::Class::ResultClass::HashRefInflator');
my $t;
#$tools->result_class('DBIx::Class::ResultClass::HashRefInflator');
if ($text =~ m{/tools ([\w\d\s]+)}) {
(undef, $tools) = $tools->find_tool($1, undef, 'DBIx::Class::ResultClass::HashRefInflator');
($t, $tools) = $self->db->resultset('Tool')->find_tool($1, undef); # , 'DBIx::Class::ResultClass::HashRefInflator');
}

my $tool_str = join("\n",
map {
$_->{name} . ($_->{requires_induction}
$_->name . ($_->requires_induction
? ' (induction)'
: '')
}
grep { $_->{name} !~ /oneall_login_callback/ }
grep { $_->name !~ /oneall_login_callback/ }
($tools->all));
if(!$t && $tools->count == 1) {
$t = $tools->first;
}
if ($t) {
my $ts = $t->current_status;
if($ts) {
$tool_str .= " Status: " . $ts->status . "\n" . $ts->description;
} else {
$tool_str .= " Status: Unknown";
}
}
$tool_str ||= '<None found>';
$message->reply($tool_str);
}
Expand Down Expand Up @@ -458,6 +487,118 @@ sub add_tool ($self, $text, $message, $args = undef) {
}
}

=head2 tool_ctrl
View/Change the status of a tool.
/tool_ctrl Tool Name <status>
=cut

sub tool_ctrl ($self, $text, $message, $args = undef) {
my $member = $self->authorize($message);
return if !$member;

my ($tool, $status, $desc, $t_status, $tool_or_keyb);

# Something's very wrong if we have no Door ..
my $door = $self->db->resultset('Tool')->find({ name => 'The Door' });
return $message->reply("Something's broken, we have no Door")
if !$door;

# must be admin or inductor
my $allowed = $member->allowed->find({ tool_id => $door->id });
return $message->reply("Something's broken, you're not allowed/valid")
if !$allowed;
# pending door (not paid yet)
return $message->reply("Something's broken, you haven't paid yet?")
if $allowed->pending_acceptance;

if (!$args && $text =~ m{/tool_ctrl ([\w\d ]+) ([\w]+)$}) {
my $tool_name = $1;
my $status = $2;
print "TS Name: $tool_name\n";

($t_status, $tool_or_keyb) = $self->find_tool($tool_name, 'tool_ctrl');
if($t_status eq 'success') {
$tool = $tool_or_keyb;
$self->waiting_on_response->{$message->from->id} =
{tool => $tool, status => $status};
}
}

if($args) {
my $waiting = $self->waiting_on_response->{$message->from->id};
$desc = $waiting->{reply_text};
if($args->[1] eq 'tool') {
$tool = $self->db->resultset('Tool')->find({name => $args->[2]});
$waiting->{tool} = $tool;
}
if($args->[1] eq 'reply') {
$desc = $args->[2];
$waiting->{reply_text} = $desc;
}
$tool ||= $waiting->{tool};
$desc ||= $waiting->{reply_text};
$status ||= $waiting->{status};
}
if (!$desc) {
# Around again to get a status description (fore_reply)
$self->waiting_on_response->{$message->from->id}{action} = 'tool_ctrl';
my $new_msg = $message->_brain->sendMessage({
text => 'Describe the changed status',
chat_id => $message->chat->id,
reply_parameters => Telegram::Bot::Object::ReplyParameters->new({message_id => $message->message_id }),
reply_markup => Telegram::Bot::Object::ForceReply->new({
force_reply => JSON::true,
input_field_placeholder => 'The frobulator is broken',
selective => JSON::true,
})
});
$self->waiting_on_response->{$message->from->id}{reply_id} = $new_msg->message_id;
return $new_msg;
}

if($tool && $desc) {
my $member_inductor = $member->allowed->find({ tool_id => $tool->id });
if (!$allowed->is_admin && ($member_inductor && !$member_inductor->is_admin)) {
# member is not a director, and not an inductor on the tool
return $message->reply("You're not allowed to do that");
}
my $text = 'Status must be "dead", "broken", or "working"';
if($status =~ /dead|broken|working/) {
my $ts = $tool->create_related('statuses', {
status => $status,
who_id => $member->id,
description => $desc
});
if ($ts) {
$text = 'Status updated.';
}
}
return $message->reply($text);
}

if ($t_status eq 'keyboard') {
my $waiting = $self->waiting_on_response->{$message->from->id} || {};
$waiting->{action} = 'tool_ctrl';
$waiting->{type} = 'tool';
$waiting->{text} = 'text';
$self->waiting_on_response->{$message->from->id} = $waiting;

return $message->_brain->sendMessage(
{
chat_id => $message->chat->id,
text => "No exact match for tool, pick one:",
reply_markup => Telegram::Bot::Object::InlineKeyboardMarkup->new(
{
inline_keyboard => $tool_or_keyb,
})
});
}
return $message->reply("Try /tool_ctrl <name of tool, letters, numbers and whitespace allowed> <dead|broken|working>");
}

=head2 find_member (whois)
Simple member lookup (are they a member?)
Expand Down

0 comments on commit 4f40995

Please sign in to comment.