check train/trip id when updating associated data

this avoid race conditions when a user changes their checkin between the time
of request (old trip id) and the time of data update (new trip id). These
could lead to funny effects such as buses suddenly having S-Bahn carriage
formations.
This commit is contained in:
Birte Kristina Friesel 2024-05-24 21:39:09 +02:00
parent f7a9213961
commit 5a2285bd59
No known key found for this signature in database
GPG key ID: 19E6E524EBB177BA
2 changed files with 57 additions and 34 deletions

View file

@ -1241,7 +1241,8 @@ sub startup {
return;
}
my $route = $in_transit->{route};
my $route = $in_transit->{route};
my $train_id = $in_transit->{train_id};
# TODO get_tripid_p is only needed on the first call, afterwards the tripid is known.
$self->hafas->get_tripid_p( train => $train )->then(
@ -1249,9 +1250,10 @@ sub startup {
my ($trip_id) = @_;
$self->in_transit->update_data(
uid => $uid,
db => $db,
data => { trip_id => $trip_id }
uid => $uid,
db => $db,
data => { trip_id => $trip_id },
train_id => $train_id,
);
return $self->hafas->get_route_p(
@ -1340,6 +1342,7 @@ sub startup {
$train->qos_messages
],
him_messages => \@messages,
train_id => $train_id,
);
if ($polyline) {
@ -1348,6 +1351,7 @@ sub startup {
db => $db,
polyline => $polyline,
old_id => $in_transit->{polyline_id},
train_id => $train_id,
);
}
@ -1421,14 +1425,16 @@ sub startup {
}
}
$self->in_transit->update_data(
uid => $uid,
db => $db,
data => $data
uid => $uid,
db => $db,
data => $data,
train_id => $train_id,
);
$self->in_transit->update_user_data(
uid => $uid,
db => $db,
user_data => $user_data
user_data => $user_data,
train_id => $train_id,
);
}
elsif ( not $is_departure
@ -1436,9 +1442,10 @@ sub startup {
{
$data->{wagonorder_arr} = $wagonorder;
$self->in_transit->update_data(
uid => $uid,
db => $db,
data => $data
uid => $uid,
db => $db,
data => $data,
train_id => $train_id,
);
}
return;
@ -1458,9 +1465,10 @@ sub startup {
my $data = { stationinfo_dep => $station_info };
$self->in_transit->update_data(
uid => $uid,
db => $db,
data => $data
uid => $uid,
db => $db,
data => $data,
train_id => $train_id,
);
return;
}
@ -1479,9 +1487,10 @@ sub startup {
my $data = { stationinfo_arr => $station_info };
$self->in_transit->update_data(
uid => $uid,
db => $db,
data => $data
uid => $uid,
db => $db,
data => $data,
train_id => $train_id,
);
return;
}

View file

@ -577,7 +577,8 @@ sub set_polyline {
$self->set_polyline_id(
uid => $uid,
db => $db,
polyline_id => $polyline_id
polyline_id => $polyline_id,
train_id => $opt{train_id},
);
}
@ -590,11 +591,13 @@ sub set_polyline_id {
my $db = $opt{db} // $self->{pg}->db;
my $polyline_id = $opt{polyline_id};
$db->update(
'in_transit',
{ polyline_id => $polyline_id },
{ user_id => $uid }
);
my %where = ( user_id => $uid );
if ( $opt{train_id} ) {
$where{train_id} = $opt{train_id};
}
$db->update( 'in_transit', { polyline_id => $polyline_id }, \%where );
}
sub set_route_data {
@ -607,6 +610,12 @@ sub set_route_data {
my $qos_msg = $opt{qos_messages};
my $him_msg = $opt{him_messages};
my %where = ( user_id => $uid );
if ( $opt{train_id} ) {
$where{train_id} = $opt{train_id};
}
my $res_h = $db->select( 'in_transit', ['data'], { user_id => $uid } )
->expand->hash;
@ -623,7 +632,7 @@ sub set_route_data {
route => JSON->new->encode($route),
data => JSON->new->encode($data)
},
{ user_id => $uid }
\%where
);
}
@ -850,6 +859,12 @@ sub update_data {
my $db = $opt{db} // $self->{pg}->db;
my $new_data = $opt{data} // {};
my %where = ( user_id => $uid );
if ( $opt{train_id} ) {
$where{train_id} = $opt{train_id};
}
my $res_h = $db->select( 'in_transit', ['data'], { user_id => $uid } )
->expand->hash;
@ -859,11 +874,7 @@ sub update_data {
$data->{$k} = $v;
}
$db->update(
'in_transit',
{ data => JSON->new->encode($data) },
{ user_id => $uid }
);
$db->update( 'in_transit', { data => JSON->new->encode($data) }, \%where );
}
sub update_user_data {
@ -873,6 +884,12 @@ sub update_user_data {
my $db = $opt{db} // $self->{pg}->db;
my $new_data = $opt{user_data} // {};
my %where = ( user_id => $uid );
if ( $opt{train_id} ) {
$where{train_id} = $opt{train_id};
}
my $res_h = $db->select( 'in_transit', ['user_data'], { user_id => $uid } )
->expand->hash;
@ -882,11 +899,8 @@ sub update_user_data {
$data->{$k} = $v;
}
$db->update(
'in_transit',
{ user_data => JSON->new->encode($data) },
{ user_id => $uid }
);
$db->update( 'in_transit',
{ user_data => JSON->new->encode($data) }, \%where );
}
sub update_visibility {