Throw appropriate error when trying to add pin that already exists.

This commit is contained in:
Citlali del Rey 2022-11-05 09:09:34 -07:00
parent 3186adefc9
commit 2efd2fa6f3
Signed by: nullobsi
GPG Key ID: 933A1F44222C2634
3 changed files with 90 additions and 80 deletions

View File

@ -332,86 +332,99 @@ sub add($c) {
my $origins = $body->{origins};
# No support for meta.
my @requests;
return $c->pins->exists({
cid => $cid,
uid => $uid,
})->then(sub ($exists){
if ($exists == 1) {
return $c->render(status => 400, openapi => {
error => {
reason => "ALREADY_PINNED",
details => "You already have that CID pinned.",
},
});
}
my @requests;
push @requests, Mojo::Promise->resolve("FIRST");
push @requests, Mojo::Promise->resolve("FIRST");
# Firstly try to direct to possible origins.
if (defined $origins) {
# Firstly try to direct to possible origins.
if (defined $origins) {
for my $origin (@$origins) {
my $url = Mojo::URL->new($c->config->{ipfs}->{gatewayWriteUrl});
$url->path("api/v0/swarm/peering/add");
$url->query({
arg => $origin,
});
push @requests, $c->ua->post_p($url);
}
}
Mojo::Promise->all_settled(@requests)->then(sub(@results) {
# I had to look at the sourcecode for this.
# my $peer_failed = 0;
# for my $res (@results) {
# if ($res->{status} eq 'fulfilled') {
# my $tx = $res->{value};
#
# # Handle first initial empty promise
# if ($tx eq "FIRST") {
# next;
# }
#
# if (!$tx->result->is_success) {
# say "Failure to add IPFS peer.";
# $peer_failed = 1;
# last;
# }
# } else {
# say "Failure to connect!";
# die $res->{reason};
# }
# }
for my $origin (@$origins) {
my $url = Mojo::URL->new($c->config->{ipfs}->{gatewayWriteUrl});
$url->path("api/v0/swarm/peering/add");
$url->path("api/v0/pin/add");
$url->query({
arg => $origin,
arg => $cid,
recursive => "true",
# It seems progress is a stream. Not sure how to handle that?
# Let's just assume it works immediately.
# TODO.
progress => "false",
});
push @requests, $c->ua->post_p($url);
}
}
return $c->ua->post_p($url);
})->then(sub($tx) {
my $res = $tx->result;
Mojo::Promise->all_settled(@requests)->then(sub(@results) {
# I had to look at the sourcecode for this.
# my $peer_failed = 0;
# for my $res (@results) {
# if ($res->{status} eq 'fulfilled') {
# my $tx = $res->{value};
#
# # Handle first initial empty promise
# if ($tx eq "FIRST") {
# next;
# }
#
# if (!$tx->result->is_success) {
# say "Failure to add IPFS peer.";
# $peer_failed = 1;
# last;
# }
# } else {
# say "Failure to connect!";
# die $res->{reason};
# }
# }
my $url = Mojo::URL->new($c->config->{ipfs}->{gatewayWriteUrl});
$url->path("api/v0/pin/add");
$url->query({
arg => $cid,
recursive => "true",
# It seems progress is a stream. Not sure how to handle that?
# Let's just assume it works immediately.
# TODO.
progress => "false",
if ($res->is_success) {
# Now we do DB stuff.
return $c->pins->add({
uid => $uid,
cid => $cid,
name => $name,
});
} else {
# TODO: read error and use appropriate response
say $res->body;
die "Failed to pin.";
}
})->then(sub($res) {
$c->render(openapi => {
requestid => $res->{id},
# TODO
status => "pinned",
created => IpfsUpload::Util::date_format($res->{created_at}),
pin => {
cid => $cid,
name => $name,
},
delegates => $c->config->{ipfs}->{delegates},
}, status => 202)
});
return $c->ua->post_p($url);
})->then(sub($tx) {
my $res = $tx->result;
if ($res->is_success) {
# Now we do DB stuff.
return $c->pins->add({
uid => $uid,
cid => $cid,
name => $name,
});
} else {
# TODO: read error and use appropriate response
say $res->body;
die "Failed to pin.";
}
})->then(sub($res) {
$c->render(openapi => {
requestid => $res->{id},
# TODO
status => "pinned",
created => IpfsUpload::Util::date_format($res->{created_at}),
pin => {
cid => $cid,
name => $name,
},
delegates => $c->config->{ipfs}->{delegates},
}, status => 202)
});
}

View File

@ -23,6 +23,12 @@ sub get($self, $where) {
});
}
sub exists($self, $where) {
return $self->pg->db->select_p('pins', 'id', $where, {limit => 1})->then(sub ($res) {
return $res->rows == 1;
});
}
sub update($self, $update, $where) {
return $self->pg->db->update_p('pins', $update, $where, { returning => ['cid', 'id']})->then(sub ($res) {
return $res->hash;

View File

@ -1,9 +0,0 @@
% layout 'default';
% title 'Welcome';
<h2><%= $msg %></h2>
<p>
This page was generated from the template "templates/example/welcome.html.ep"
and the layout "templates/layouts/default.html.ep",
<%= link_to 'click here' => url_for %> to reload the page or
<%= link_to 'here' => '/index.html' %> to move forward to a static page.
</p>