diff --git a/docs/Empire.pod b/docs/Empire.pod index 64a66fb1..ee4d3959 100644 --- a/docs/Empire.pod +++ b/docs/Empire.pod @@ -901,7 +901,7 @@ B Once this is done it cannot be redone for 1 month, so make sure the =head3 session_id -A ssession id. +A session id. =head3 params @@ -962,4 +962,87 @@ Returns an array ref of species templates that can be used to help the user popu ... ] +=head2 view_authorized_sitters + +Returns the currently authorized sitters for this baby. + + { + "status" : { ... }, + "auths" : [ + { + "id" : 12345, + "name" : "Some Empire", + "expiry" : "2015-10-10 17:20:03" + }, + ... + ] + } + +=head2 authorize_sitters ( session_id, options ) + +Authorizes other empires to babysit your account. Each authorisation will +be created if needed, and authorized for the full server-defined amount of +time. + +Returns view of authorized sitters (see view_authorized_sitters) plus any +rejected IDs. + + { + "status" : { ... }, + "auths" : [ + { + id => ..., + name => ... + }, + ... + ], + "rejected_ids" : [ 57, "No Such Empire", ... ], + } + + +=head3 session_id + +A session id. + +=head3 options + +One or more of the following options: + +=head4 allied + +If true, all allies are automatically selected. + +=head4 alliance + +The name of another alliance all of whom are automatically selected. + +=head4 alliance_id + +The ID of another alliance all of whom are automatically selected. + +=head4 empires + +An array of IDs and/or names of specific empires being authorized. + +=head4 revalidate_all + +A quick method for selecting all currently-authorized empires to +extend their authorization period. + +=head2 deauthorize_sitters ( session_id, options ) + +Remove sitters from being permitted to sit this account. + +=head3 session_id + +A session id. + +=head3 options + +One or more of the following options: + +=head4 empires + +An array of IDs (not names) of specific empires being removed. + =cut diff --git a/lib/Lacuna/DB/Result/Empire.pm b/lib/Lacuna/DB/Result/Empire.pm index e3fd04ff..ad05a27c 100644 --- a/lib/Lacuna/DB/Result/Empire.pm +++ b/lib/Lacuna/DB/Result/Empire.pm @@ -131,6 +131,15 @@ sub _build_has_new_messages { $self->received_messages->search({has_read => 0})->count; } +# I'm the sitter for these babies. +__PACKAGE__->has_many('babyauths', 'Lacuna::DB::Result::SitterAuths', 'sitter_id'); + +# I'm the baby for these sitters. +__PACKAGE__->has_many('sitterauths', 'Lacuna::DB::Result::SitterAuths', 'baby_id'); + +__PACKAGE__->many_to_many('babies', 'babyauths', 'baby'); +__PACKAGE__->many_to_many('sitters', 'sitterauths', 'sitter'); + for my $affin (qw( manufacturing_affinity deception_affinity diff --git a/lib/Lacuna/RPC/Empire.pm b/lib/Lacuna/RPC/Empire.pm index d7272ae8..ccf5942a 100644 --- a/lib/Lacuna/RPC/Empire.pm +++ b/lib/Lacuna/RPC/Empire.pm @@ -1152,46 +1152,118 @@ sub get_species_templates { sub authorize_sitters { - my ($self, $session_id, @sitters) = @_; + my ($self, $session_id, $opts) = @_; my $session = $self->get_session($session_id); $session->check_captcha; + my $baby = $self->get_empire_by_session($session); my $baby_id = $session->empire_id; - my $rs = Lacuna->db->resultset('SitterAuths'); + my $rs = $baby->sitters; + my $auths = Lacuna->db->resultset('SitterAuths'); + + my @sitters; + if ($opts->{allied}) + { + if ($baby->alliance_id) + { + push @sitters, $baby->alliance->members->get_column('id')->all; + } + } + if ($opts->{alliance}) + { + my $alliance = + Lacuna->db->resultset('Alliance')->find({name => $opts->{alliance}}); + if ($alliance) + { + push @sitters, $alliance->members->all; + } + } + if ($opts->{alliance_id}) + { + my $alliance = + Lacuna->db->resultset('Alliance')->find({id => $opts->{alliance_id}}); + if ($alliance) + { + push @sitters, $alliance->members->all; + } + } + if ($opts->{empires} and ref $opts->{empires} eq 'ARRAY') + { + push @sitters, @{$opts->{empires}}; + } + if ($opts->{revalidate_all}) + { + push @sitters, $rs->get_column('me.sitter_id')->all; + } + confess [1009, "No sitters selected"] unless @sitters; + + my @bad_ids; for my $sitter (@sitters) { - my $sit = Lacuna->db->empire($sitter); - my $sitter_id = $sit->id; + my $sit = eval { $sitter->isa('Lacuna::DB::Result::Empire') } ? + $sitter : + Lacuna->db->empire($sitter); + if ($sit) + { + my $sitter_id = $sit->id; + next if $sitter_id == $baby_id; - my $auth = $rs->find(baby_id => $baby_id, sitter_id => $sitter_id); - $auth //= $rs->new({baby_id => $baby_id, sitter_id => $sitter_id}); - $auth->reauthorise; - $auth->update_or_insert; + my $auth = $auths->find({baby_id => $baby_id, sitter_id => $sitter_id}); + $auth //= $auths->new({baby_id => $baby_id, sitter_id => $sitter_id}); + $auth->reauthorise; + $auth->update_or_insert; + } + else + { + push @bad_ids, $sitter; + } } - return { status => $self->format_status($session->empire) }; + my $rc = $self->view_authorized_sitters($session); + $rc->{rejected_ids} = \@bad_ids; + return $rc; } sub view_authorized_sitters { my ($self, $session_id) = @_; my $session = $self->get_session($session_id); + my $baby = $self->get_empire_by_session($session); - my $baby_id = $session->empire_id; - my $rs = Lacuna->db->resultset('SitterAuths')->search({baby_id => $baby_id}); + my $rs = $baby->sitters() + ->search( + { expiry => { '>' => \q[UTC_TIMESTAMP()] } }, + { + '+select' => [ 'me.expiry' ], + '+as' => [ 'expiry' ], + } + ); my @auths; - while (my $auth = $rs->next) + while (my $e = $rs->next) { push @auths, { - id => $auth->sitter_id, - name => $auth->sitter->name, + id => $e->id, + name => $e->name, + expiry => $e->get_column('expiry'), }; } return { status => $self->format_status($session->empire), auths => \@auths }; } +sub deauthorize_sitters +{ + my ($self, $session_id, $opts) = @_; + my $session = $self->get_session($session_id); + my $baby = $self->get_empire_by_session($session); + + my $baby_id = $session->empire_id; + my $rs = $baby->sitters(); + + +} + __PACKAGE__->register_rpc_method_names( { name => "create", options => { with_plack_request => 1 } }, { name => "fetch_captcha", options => { with_plack_request => 1 } },