From 39e075dc43c6ac281f599b3ffefb65f4f6ccb7c7 Mon Sep 17 00:00:00 2001 From: Darin McBride Date: Wed, 18 Nov 2015 11:27:34 -0700 Subject: [PATCH] Add in the inbox handling. Also clean up some empire get_status returns, and other fiddly bits. --- docs/Empire.pod | 41 ++++---- docs/Inbox.pod | 3 + lib/LR.pm | 2 +- lib/Lacuna/DB/Result/Building/Embassy.pm | 6 +- lib/Lacuna/DB/Result/Empire.pm | 124 ++++++++++++++++++----- lib/Lacuna/RPC.pm | 107 +++++++++++++------ lib/Lacuna/RPC/Building.pm | 2 +- lib/Lacuna/RPC/Building/Development.pm | 5 +- lib/Lacuna/RPC/Empire.pm | 54 +++------- lib/Lacuna/RPC/Inbox.pm | 104 ++++++++++++++----- 10 files changed, 293 insertions(+), 155 deletions(-) diff --git a/docs/Empire.pod b/docs/Empire.pod index 231a4657..04af7e2d 100644 --- a/docs/Empire.pod +++ b/docs/Empire.pod @@ -225,14 +225,23 @@ B You should probably B call this method directly, as it is a wast ... ], "babies" : { - "baby name" : [ - { "id" : "xxxx", "name" : "...", "x": "#", "y": "#" }, - ... - ], - "another baby name" : [ - { "id" : "xxxx", "name" : "...", "x": "#", "y": "#" }, - ... - ] + "baby name" : { + "alliance_id" : 3, # key doesn't exist if not in alliance + "id" : 12355, # empire ID + "has_new_messages" : 30, + "bodies" : [ + { "id" : "xxxx", "name" : "...", "x": "#", "y": "#" }, + ... + ], + }, + "another baby name" : { + "has_new_messages" : 30, + "id" : 12884, + "bodies" : [ + { "id" : "xxxx", "name" : "...", "x": "#", "y": "#" }, + ... + ], + } } }, "colonies" : { @@ -983,22 +992,6 @@ 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 diff --git a/docs/Inbox.pod b/docs/Inbox.pod index cc08c668..dc2997f1 100644 --- a/docs/Inbox.pod +++ b/docs/Inbox.pod @@ -107,6 +107,9 @@ An integer representing which page of the inbox you'd like to view. Defaults to An array reference of tags. Only messages containing these tags will be displayed in the results. +=head4 empire + +A name or ID of the baby empire to check the inbox of. =head2 view_archived ( session_id, [ options ]) diff --git a/lib/LR.pm b/lib/LR.pm index ee321dce..da7d7dba 100644 --- a/lib/LR.pm +++ b/lib/LR.pm @@ -41,7 +41,7 @@ sub _clean(@) } } elsif (ref $_ eq 'HASH') { my $x = $_; - { + +{ map { my $o = $x->{$_}; if (eval { $o->can('id') }) diff --git a/lib/Lacuna/DB/Result/Building/Embassy.pm b/lib/Lacuna/DB/Result/Building/Embassy.pm index 320ccaa6..aea1fcee 100644 --- a/lib/Lacuna/DB/Result/Building/Embassy.pm +++ b/lib/Lacuna/DB/Result/Building/Embassy.pm @@ -283,7 +283,7 @@ before 'can_downgrade' => sub { if (defined $alliance && $self->body->empire_id == $alliance->leader_id) { my $alliance_members = $alliance->members->count; my $best_other_embassy = $self->body->empire - ->highest_embassy($self->body_id); + ->next_highest_embassy($self->body_id); my $allowed_members = max( defined $best_other_embassy ? $best_other_embassy->max_members : 0, $self->max_members($self->level - 1) @@ -302,7 +302,7 @@ before 'can_demolish' => sub { # embassy able to take over. my $alliance_members = $alliance->members->count; my $best_other_embassy = $self->body->empire - ->highest_embassy($self->body_id); + ->next_highest_embassy($self->body_id); my $allowed_members = defined $best_other_embassy ? $best_other_embassy->max_members : 0; if ($alliance_members > $allowed_members) { confess [1013, 'You cannot demolish this Embassy while you are an alliance leader. Reassign leadership or dissolve the alliance first.'] @@ -315,7 +315,7 @@ sub propositions { my ($self) = @_; my $alliance = $self->alliance; return Lacuna->db->resultset('Lacuna::DB::Result::Propositions')-> - search({ "station.alliance_id" => $alliance->id, }, { prefetch => "station" }); + search({ "station.alliance_id" => $alliance->id, }, { prefetch => ["station",'proposed_by'] }); } no Moose; diff --git a/lib/Lacuna/DB/Result/Empire.pm b/lib/Lacuna/DB/Result/Empire.pm index 2a336440..70e46613 100644 --- a/lib/Lacuna/DB/Result/Empire.pm +++ b/lib/Lacuna/DB/Result/Empire.pm @@ -14,7 +14,7 @@ use Email::Stuff; use Email::Valid; use UUID::Tiny ':std'; use Lacuna::Constants qw(INFLATION); - +use PerlX::Maybe qw(provided maybe); __PACKAGE__->table('empire'); __PACKAGE__->add_columns( @@ -96,6 +96,10 @@ __PACKAGE__->add_columns( outlaw_date => { data_type => 'datetime', is_nullable => 0, default_value => "2010-10-03 18:17:26" }, ); +# tell DBIC that this is unique so it can be sorted properly when dealing +# with has_many, prefetch, and order_by +__PACKAGE__->add_unique_constraint(name => ['name']); + sub sqlt_deploy_hook { my ($self, $sqlt_table) = @_; $sqlt_table->add_index(name => 'idx_self_destruct', fields => ['self_destruct_active','self_destruct_date']); @@ -132,10 +136,36 @@ sub _build_has_new_messages { } # I'm the sitter for these babies. -__PACKAGE__->has_many('babyauths', 'Lacuna::DB::Result::SitterAuths', 'sitter_id'); +__PACKAGE__->has_many('allbabyauths', 'Lacuna::DB::Result::SitterAuths', 'sitter_id'); +__PACKAGE__->has_many('babyauths', 'Lacuna::DB::Result::SitterAuths', sub { + my $args = shift; + return ( + { + "$args->{foreign_alias}.sitter_id" => { -ident => "$args->{self_alias}.id" }, + "$args->{foreign_alias}.expiry" => { '>=' => \q[UTC_TIMESTAMP()] }, + }, + $args->{self_rowobj} && { + "$args->{foreign_alias}.sitter_id" => $args->{self_rowobj}->id, + "$args->{foreign_alias}.expiry" => { '>=' => \q[UTC_TIMESTAMP()] }, + } + ); + }); # I'm the baby for these sitters. -__PACKAGE__->has_many('sitterauths', 'Lacuna::DB::Result::SitterAuths', 'baby_id'); +__PACKAGE__->has_many('allsitterauths', 'Lacuna::DB::Result::SitterAuths', 'baby_id'); +__PACKAGE__->has_many('sitterauths', 'Lacuna::DB::Result::SitterAuths', sub { + my $args = shift; + return ( + { + "$args->{foreign_alias}.baby_id" => { -ident => "$args->{self_alias}.id" }, + "$args->{foreign_alias}.expiry" => { '>=' => \q[UTC_TIMESTAMP()] }, + }, + $args->{self_rowobj} && { + "$args->{foreign_alias}.baby_id" => $args->{self_rowobj}->id, + "$args->{foreign_alias}.expiry" => { '>=' => \q[UTC_TIMESTAMP()] }, + } + ); + }); __PACKAGE__->many_to_many('babies', 'babyauths', 'baby'); __PACKAGE__->many_to_many('sitters', 'sitterauths', 'sitter'); @@ -186,7 +216,22 @@ for my $affin (qw( }; } +has is_active => ( + is => 'ro', + isa => 'Bool', + lazy_build => 1, + ); +sub _build_is_active { + my ($self) = @_; + Lacuna->cache->get('empire_active', $self->id); +} + +sub set_active { + my ($self) = @_; + Lacuna->cache->set('empire_active', $self->id, 1, 30 * 60); + $self->clear_is_active; +} sub observatory_probes { my ($self,$args) = @_; @@ -538,12 +583,14 @@ has rpc_rate => ( sub get_status { my ($self) = @_; - my $planet_rs = $self->planets->search({},{ -order_by => 'name' }); + my $real_empire = $self->current_session ? $self->current_session->empire : $self; + + my $planet_rs = $real_empire->planets->search({},{ -order_by => 'name' }); if ($self->alliance_id) { $planet_rs = Lacuna->db->resultset('Map::Body')-> search( - {-or => { empire_id => $self->id, alliance_id => $self->alliance_id }}, - { -order_by => 'name' }, + { -or => { empire_id => $real_empire->id, alliance_id => $real_empire->alliance_id } }, + { order_by => 'name' }, ); } my %planets; @@ -553,7 +600,7 @@ sub get_status { while (my $planet = $planet_rs->next) { $planets{$planet->id} = $planet->name; - my $type = 'mine'; + my $type = 'colony'; if ($planet->get_type eq 'space station') { $stations{$planet->id} = $planet->name; $type = 'station' @@ -565,6 +612,7 @@ sub get_status { push @{$bodies{$type}}, { id => $planet->id, name => $planet->name, + zone => $planet->zone, x => $planet->x, y => $planet->y, #,,, }; @@ -572,39 +620,53 @@ sub get_status { # shouldn't have to check this once sitter_password goes away. if ($self->current_session() && - !$self->current_session()->is_sitter()) + !$self->current_session()->_is_sitter()) { - $planet_rs = Lacuna->db->resultset('Map::Body')-> + $planet_rs = + Lacuna->db->resultset('Map::Body')-> search( { - 'sitterauths.sitter_id' => $self->id, + 'sitterauths.sitter_id' => $real_empire->id, 'me.class' => { '!=' => 'Lacuna::DB::Result::Map::Body::Planet::Station' }, }, { - join => { empire => 'sitterauths' }, - -order_by => 'name', - '+select' => [ qw/empire.name/ ], - '+as' => [ qw/empire_name/ ], + prefetch => { 'empire', 'sitterauths' }, + order_by => ['me.name', 'me.id'], }); + while (my $planet = $planet_rs->next) { - my $empire_name = $planet->get_column('empire_name'); - push @{$bodies{babies}{$empire_name}}, { + my $empire = $planet->empire; + + # I'm not sure if we can get more than one, but getting an error message + # if that happens will aid in debugging. If this seems to work consistently, + # we should remove this block. + confess [ 999, "Contact an admin / developer: More than one sitterauth is counting? sitter: ".$real_empire->id.", baby: ". $empire->id ] + unless $empire->sitterauths->count == 1; + + # if we haven't seen this empire yet, put in its basic stats. + $bodies{babies}{$empire->name} ||= { + id => $empire->id, + has_new_messages => $empire->has_new_messages, + sitter_expiry => format_date($empire->sitterauths->first->expiry), + provided $empire->highest_embassy, primary_embassy_id => $empire->highest_embassy->id, + maybe alliance_id => $empire->alliance_id, + }; + + push @{$bodies{babies}{$empire->name}{planets}}, { id => $planet->id, name => $planet->name, + zone => $planet->zone, x => $planet->x, y => $planet->y, #,,, }; } } - my $embassy = $self->highest_embassy; - my $embassy_id = defined $embassy ? $embassy->id : undef; - my $status = { rpc_count => $self->rpc_count, is_isolationist => $self->is_isolationist, - status_message => $self->status_message, + status_message => $real_empire->status_message, name => $self->name, id => $self->id, essentia => $self->essentia, @@ -622,9 +684,9 @@ sub get_status { insurrect_value => $self->next_colony_cost("spy"), self_destruct_active=> $self->self_destruct_active, self_destruct_date => $self->self_destruct_date_formatted, - primary_embassy_id => $embassy_id, + provided $self->highest_embassy, primary_embassy_id => $self->highest_embassy->id, + maybe alliance_id => $self->alliance_id, }; - $status->{alliance_id} = $self->alliance_id if $self->alliance_id; return $status; } @@ -1153,8 +1215,8 @@ before delete => sub { $self->sent_messages->delete; $self->received_messages->delete; $self->medals->delete; - $self->babyauths->delete; - $self->sitterauths->delete; + $self->allbabyauths->delete; + $self->allsitterauths->delete; my $planets = $self->planets; while ( my $planet = $planets->next ) { @@ -1267,7 +1329,19 @@ sub pay_taxes { } } -sub highest_embassy { +# was being called repeatedly, so move it over to a cached value. +has highest_embassy => ( + is => 'ro', + isa => 'Maybe[Lacuna::DB::Result::Building::Embassy]', + lazy_build => 1, + ); + +sub _build_highest_embassy { + my ($self) = @_; + $self->next_highest_embassy; +} + +sub next_highest_embassy { my ($self, $excluding_body_id) = @_; my $search_rs = Lacuna->db->resultset('Building')->search({ diff --git a/lib/Lacuna/RPC.pm b/lib/Lacuna/RPC.pm index 2404ca85..3d696d91 100644 --- a/lib/Lacuna/RPC.pm +++ b/lib/Lacuna/RPC.pm @@ -30,7 +30,15 @@ sub get_session { } $opts = { session_id => $session->id } unless ref $opts; - if ($opts->{building_id}) { + # mark the real player as active, allowing sitters to aid + # him/her more fully, not as an inactive. + $session->empire->set_active; + + if ($opts->{empire_id}) { + my $empire = $self->get_baby($session, $opts->{empire_id}); + $session->current_empire($empire); + } + elsif ($opts->{building_id}) { my $building = $self->get_building($session, $opts->{building_id}, %$opts); @@ -65,6 +73,7 @@ sub get_session { $session->current_empire($session->empire) unless $session->current_empire; $session->current_empire->current_session($session); + $session->empire->current_session($session); my $empire = $session->current_empire; if (defined $empire) { @@ -118,44 +127,61 @@ sub get_session { return $session; } +sub get_baby { + my ($self, $session, $empire_id) = @_; + if (ref $empire_id && $empire_id->isa('Lacuna::DB::Result::Empire')) { + return $empire_id; + } + return $session->empire if $session->empire->id == $empire_id; + + my $empire = $session->empire->babies->find({empire_id => $empire_id}); + confess [ 1002, "Empire does not exist or is not sat by you." ] + unless $empire; + + $empire; +} + sub get_body { # makes for uniform error handling, and prevents staleness my ($self, $session, $body_id) = @_; - my $body; if (ref $body_id && $body_id->isa('Lacuna::DB::Result::Map::Body')) { - $body = $body_id; - } else { - $body = Lacuna->db->resultset('Map::Body')-> - search( - { - 'me.id' => $body_id, - -or => [ - { 'me.empire_id' => $session->empire->id }, - { 'me.alliance_id' => $session->empire->alliance_id }, - $session->_is_sitter ? - { - 'sitterauths.sitter_id' => $session->empire->id, - 'me.class' => { '!=' => 'Lacuna::DB::Result::Map::Body::Planet::Station' }, - } : (), - ] - }, - { - join => { 'empire' => 'sitterauths' }, - prefetch => [ 'empire' ], - } - )->first; - confess [ 1002, 'Body does not exist or is not owned by you or any empire you sit.', $body_id ] - unless $body; - + return $body_id; } + + my $join = $session->_is_sitter ? 'empire' : { 'empire' => 'sitterauths' }; + + my $body = Lacuna->db->resultset('Map::Body')-> + search( + { + 'me.id' => $body_id, + -or => [ + { 'me.empire_id' => $session->empire->id }, + { 'me.alliance_id' => $session->empire->alliance_id }, + $session->_is_sitter ? () : { + 'sitterauths.sitter_id' => $session->empire->id, + 'me.class' => { '!=' => 'Lacuna::DB::Result::Map::Body::Planet::Station' }, + }, + ] + }, + { + join => $join, #{ 'empire' => 'sitterauths' }, + prefetch => [ 'empire' ], + } + )->first; + confess [ 1002, 'Body does not exist or is not owned by you or any empire you sit.', $body_id ] + unless $body; + return $body; } + sub get_building { # makes for uniform error handling, and prevents staleness my ($self, $session, $building_id, %options) = @_; if (ref $building_id && $building_id->isa('Lacuna::DB::Result::Building')) { return $building_id; } + my $join = $session->_is_sitter ? 'empire' : { 'empire' => 'sitterauths' }; + my $building = Lacuna->db->resultset('Building')-> search( @@ -164,23 +190,24 @@ sub get_building { # makes for uniform error handling, and prevents staleness -or => [ { 'body.empire_id' => $session->empire->id }, { 'body.alliance_id' => $session->empire->alliance_id }, - $session->_is_sitter ? - { + $session->_is_sitter ? () : { 'sitterauths.sitter_id' => $session->empire->id, 'body.class' => { '!=' => 'Lacuna::DB::Result::Map::Body::Planet::Station' }, - } : (), + }, ] }, { - join => { body => { 'empire' => 'sitterauths' }}, + join => { body => $join }, #{ 'empire' => 'sitterauths' }}, prefetch => { body => 'empire' }, } )->first; confess [ 1002, 'Building does not exist or is not owned by you or any empire you sit.', $building_id ] unless $building; - confess [ 1002, 'That building is not a '. $self->model_class->name ] - unless $building->class eq $self->model_class; + unless ($options{nocheck_type}) { + confess [ 1002, 'That building is not a '. $self->model_class->name ] + unless $building->class eq $self->model_class; + } $building->is_offline unless $options{skip_offline}; @@ -197,10 +224,24 @@ sub format_status { rpc_limit => Lacuna->config->get('rpc_limit') || 2500, }, ); + + # can just pass in the session, we'll extract the body and empire from it + my $real_empire; + if ($empire->isa('Lacuna::Session')) + { + $body = $empire->current_body; + $empire = $empire->current_empire; + $real_empire = $empire->empire; + } + else + { + $real_empire = $empire->current_session->empire; + } + if (defined $empire) { my $cache = Lacuna->cache; my $alert = $cache->get('announcement','alert'); - if ($alert && !$cache->get('announcement'.$alert, $empire->id)) { + if ($alert && !$cache->get('announcement'.$alert, $real_empire->id)) { $out{server}{announcement} = 1; } $out{empire} = $empire->get_status; diff --git a/lib/Lacuna/RPC/Building.pm b/lib/Lacuna/RPC/Building.pm index e9846c37..f8bc672c 100644 --- a/lib/Lacuna/RPC/Building.pm +++ b/lib/Lacuna/RPC/Building.pm @@ -123,7 +123,7 @@ sub view { }, pending_build => $building->upgrade_status, }, - status => $self->format_status($empire, $building->body), + status => $self->format_status($session), ); if ($building->is_working) { $out{building}{work} = { diff --git a/lib/Lacuna/RPC/Building/Development.pm b/lib/Lacuna/RPC/Building/Development.pm index abee41b0..814eef8f 100644 --- a/lib/Lacuna/RPC/Building/Development.pm +++ b/lib/Lacuna/RPC/Building/Development.pm @@ -99,7 +99,10 @@ sub cancel_build { my $session = $self->get_session({session_id => $args->{session_id}, building_id => $args->{building_id} }); my $empire = $session->current_empire; my $building = $session->current_building; - my $scheduled_building = Lacuna->db->resultset('Building')->find({id => $args->{scheduled_id}}); + my $scheduled_building = $self->get_building($session,$args->{scheduled_id},nocheck_type=>1); + if (!$scheduled_building) { + confess [1003, "That building does not exist, or is not yours."]; + } if ($scheduled_building->body_id != $building->body_id) { confess [1003, "That building is not on the same planet as your development ministry."]; } diff --git a/lib/Lacuna/RPC/Empire.pm b/lib/Lacuna/RPC/Empire.pm index 5c63df8c..6d7ff1f7 100644 --- a/lib/Lacuna/RPC/Empire.pm +++ b/lib/Lacuna/RPC/Empire.pm @@ -409,8 +409,7 @@ sub found { sub get_status { my ($self, $session_id) = @_; my $session = $self->get_session({session_id => $session_id}); - my $empire = $session->current_empire; - return $self->format_status($empire); + return $self->format_status($session); } sub view_profile { @@ -1022,7 +1021,7 @@ sub view_species_stats { my $empire = $session->current_empire; return { species => $empire->get_species_stats, - status => $self->format_status($empire), + status => $self->format_status($session), }; } @@ -1154,7 +1153,7 @@ sub get_species_templates { sub authorize_sitters { my ($self, $session_id, $opts) = @_; - my $session = $self->get_session($session_id); + my $session = $self->get_session({session_id => $session_id}); $session->check_captcha; my $baby = $self->get_empire_by_session($session); @@ -1220,47 +1219,19 @@ sub authorize_sitters } } - 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 $rs = $baby->sitters() - ->search( - { expiry => { '>' => \q[UTC_TIMESTAMP()] } }, - { - '+select' => [ 'me.expiry' ], - '+as' => [ 'expiry' ], - } - ); - - my @auths; - while (my $e = $rs->next) - { - push @auths, { - id => $e->id, - name => $e->name, - expiry => $e->get_column('expiry'), - }; - } - - return { status => $self->format_status($session->empire), auths => \@auths }; + return { + status => $self->format_status($session), + rejected_ids => \@bad_ids, + }; } sub deauthorize_sitters { my ($self, $session_id, $opts) = @_; - my $session = $self->get_session($session_id); - my $baby = $self->get_empire_by_session($session); + my $session = $self->get_session({session_id => $session_id}); + my $baby = $self->current_empire; my $baby_id = $session->empire_id; - my $rs = Lacuna->db->resultset('SitterAuths'); confess [1009, "The 'empires' option must be an array of empire IDs"] unless $opts->{empires} and ref $opts->{empires} eq 'ARRAY' and @@ -1270,10 +1241,13 @@ sub deauthorize_sitters my $now = $dtf->format_datetime(DateTime->now); # set expiry to immediate + my $rs = Lacuna->db->resultset('SitterAuths'); $rs->search({baby_id => $baby_id, sitter_id => { in => $opts->{empires} }}) ->update({expiry => $now}); - return $self->view_authorized_sitters($session); + return { + status => $self->format_status($session), + }; } __PACKAGE__->register_rpc_method_names( @@ -1300,7 +1274,7 @@ __PACKAGE__->register_rpc_method_names( get_full_status get_status boost_building boost_storage boost_water boost_energy boost_ore boost_food boost_happiness boost_spy_training view_boosts - authorize_sitters view_authorized_sitters deauthorize_sitters + authorize_sitters deauthorize_sitters ), ); diff --git a/lib/Lacuna/RPC/Inbox.pm b/lib/Lacuna/RPC/Inbox.pm index b9a38ffd..bcd0d876 100644 --- a/lib/Lacuna/RPC/Inbox.pm +++ b/lib/Lacuna/RPC/Inbox.pm @@ -8,19 +8,51 @@ use DateTime; use Lacuna::Verify; use Lacuna::Util qw(format_date); use List::Util qw(none); +use PerlX::Maybe qw(provided); + +# This function basically handles all the "or baby" logic for +# messages. Can be further refined by the caller with extra ->search +# calls, but this should keep any caller from accidentally reaching +# messages it shouldn't be able to. + +# options: +# * from => false if from real empire is not to be looked at. +sub message_rs { + my ($self, $session, $message_ids, %opts) = @_; + my $dtf = Lacuna->db->storage->datetime_parser; + my $now = $dtf->format_datetime(DateTime->now); + + # build a list of filters to OR together. + my @or = { 'me.to_id' => $session->empire->id }; + push @or, { 'me.from_id' => $session->empire->id } if + not exists $opts{from} or $opts{from}; + push @or, { + 'sitterauths.sitter_id' => $session->empire->id, + 'sitterauths.expiry' => { '<' => $now }, + 'me.tag' => { '!=' => 'Correspondence' }, + } unless $session->_is_sitter; + + my $message = Lacuna->db->resultset('Message')-> + search( + { + provided $message_ids, 'me.id' => { -in => $message_ids }, + -or => \@or, + }, + { + join => { receiver => 'sitterauths' }, + prefetch => 'receiver', + } + ); + + return $message; +} sub read_message { my ($self, $session_id, $message_id) = @_; my $session = $self->get_session({session_id => $session_id }); my $empire = $session->current_empire; - my $message = Lacuna->db->resultset('Lacuna::DB::Result::Message')->find($message_id); - unless (defined $message) { - confess [1002, 'Message does not exist.', $message_id]; - } - unless ($empire->id ~~ [$message->from_id, $message->to_id]) { - confess [1010, "You can't read a message that isn't yours.", $message_id]; - } + my $message = $self->messages_rs($session, $message_id)->first; if ($empire->id eq $message->to_id && !$message->has_read) { $message->has_read(1); $message->update; @@ -49,13 +81,11 @@ sub read_message { sub archive_messages { my ($self, $session_id, $message_ids) = @_; - my $session = $self->get_session({session_id => $session_id }); + my $session = $self->get_session({session_id => $session_id}); my $empire = $session->current_empire; - my $messages = Lacuna->db->resultset('Lacuna::DB::Result::Message') + my $messages = $self->messages_rs($session, $message_ids, from => 0) ->search( { - id => [ 'in', $message_ids ], - to_id => $empire->id, has_archived => 0, }); @@ -78,15 +108,13 @@ sub trash_messages { my ($self, $session_id, $message_ids) = @_; my $session = $self->get_session({session_id => $session_id }); my $empire = $session->current_empire; - my $messages = Lacuna->db->resultset('Lacuna::DB::Result::Message') + my $messages = $self->messages_rs($session, $message_ids, from => 0) ->search( { - id => [ 'in', $message_ids ], - to_id => $empire->id, has_trashed => 0, }); - my @updating = map { $_->id } $messages->search(undef, { columns => [ 'id' ]})->all; + my @updating = $messages->get_column('id')->all; if (@updating) { $messages->update( @@ -126,6 +154,8 @@ sub trash_messages_where { $where{tag} = $spec->{tags} if $spec->{tags} && ref $spec->{tags} eq 'ARRAY'; $where{tag} ||= $spec->{tag} if $spec->{tag} && !ref $spec->{tag}; $where{from_name} = [ $spec->{from} ] if $spec->{from} && !ref $spec->{from}; + $where{empire_id} = $empire->id unless $spec->{all_babies}; + $where{empire_id} = $spec->{empire_id} if $spec->{empire_id} and !ref $spec->{empire_id} or none { ref $_ } @{$spec->{empire_id}}; if ($spec->{subject}) { @@ -164,16 +194,15 @@ sub trash_messages_where { # the parts the caller can't override: $where{has_archived} = 0; - $where{to_id} = $empire->id; $where{has_trashed} = 0; # only look at ones not already trashed - my $messages = Lacuna->db->resultset('Lacuna::DB::Result::Message')->search(\%where); + my $messages = $self->messages_rs($session, undef, from => 0)->search(\%where); # check if we have anything to delete my $count; if ($opts->{save_ids}) { - my @deleting = map { $_->id } $messages->search(undef, { columns => [ 'id' ] })->all; + my @deleting = $messages->get_column('id')->all; if (@deleting) { $count = @deleting; @@ -294,12 +323,32 @@ sub view_inbox { my $session_id = shift; my $session = $self->get_session({session_id => $session_id }); my $empire = $session->current_empire; + my $options = shift || {}; my $where = { has_archived => 0, has_trashed => 0, to_id => $empire->id, }; - return $self->view_messages($where, $empire, @_); + if (!$session->_is_sitter && $options->{empire} && + $options->{empire} ne $empire->name && + $options->{empire} ne $empire->id) { + + my $to_empire = $empire->babies-> + search([ { name => $options->{empire} }, { id => $options->{empire} } ])->first; + + confess [ 1002, "The empire $options->{empire} is not one of the empires you can sit for", $options->{empire} ] + unless $empire; + + $where->{to_id} = $to_empire->id; + + # can't view correspondence of baby empires. + #$where->{tag} = { '!=', 'Correspondence' }; + #if ($options->{tags}) { + # @{$options->{tags}} = grep !/Correspondence/i, @{$options->{tags}}; + # delete $options->{tags} unless @{$options->{tags}}; + #} + } + return $self->view_messages($where, $empire, $options, @_); } sub view_archived { @@ -359,14 +408,15 @@ sub view_messages { if ($options->{tags}) { $where->{tag} = ['in',$options->{tags}]; } - my $messages = Lacuna->db->resultset('Lacuna::DB::Result::Message')->search( - $where, - { - order_by => { -desc => 'date_sent' }, - rows => 25, - page => $options->{page_number}, - } - ); + my $messages = $self->messages_rs($empire->current_session, undef, from => 0)-> + search( + $where, + { + order_by => { -desc => 'date_sent' }, + rows => 25, + page => $options->{page_number}, + } + ); my @box; while (my $message = $messages->next) { push @box, { -- 2.51.2