Unify interface & API pin filtering logic.

This commit is contained in:
Citlali del Rey 2022-12-17 10:44:07 -08:00
parent 6e1a61331d
commit cea82293f7
Signed by: nullobsi
GPG Key ID: 933A1F44222C2634
4 changed files with 95 additions and 76 deletions

View File

@ -16,40 +16,44 @@ sub landing($c) {
my $gateway = $c->config->{ipfs}->{gatewayPubUrl};
my $limit = $c->param('limit') || 10;
my $before = $c->param('before');
my %query = (
uid => $uid,
);
if (defined $before) {
$query{created_at} = { '<' => $before };
if (!IpfsUpload::Util::update_query($c, \%query)) {
# Error!
return;
}
$c->pins->list(\%query, $limit)->then(sub ($res) {
for my $pin (@$res) {
my $url = Mojo::URL->new($gateway);
$url->path($pin->{cid});
if ($pin->{name}) {
$url->query(
filename => $pin->{name},
);
}
$pin->{publicUrl} = $url;
}
$c->pins->count(\%query)->then(sub ($count) {
$c->stash(
pins => $res,
limit => $limit,
count => $count
);
$c->pins->list(\%query, $limit)->then(sub($res) {
for my $pin (@$res) {
my $url = Mojo::URL->new($gateway);
$url->path($pin->{cid});
if ($pin->{name}) {
$url->query(
filename => $pin->{name},
);
}
$pin->{publicUrl} = $url;
}
$c->stash(
pins => $res,
limit => $limit,
);
if (@$res == $limit) {
$c->stash(nextPage => $res->[-1]->{created_at});
} else {
$c->stash(nextPage => 0);
}
if (@$res == $limit) {
$c->stash(nextPage => $res->[-1]->{created_at});
} else {
$c->stash(nextPage => 0);
}
$c->render('interface/landingPage');
})
$c->render('interface/landingPage');
})
});
}
sub token_list($c) {

View File

@ -13,61 +13,14 @@ sub list($c) {
my $uid = $c->stash('uid');
my $limit = $c->param('limit') || 10;
my %query = (
uid => $uid,
);
my $cid = $c->param('cid');
my $name = $c->param('name');
# TODO: Text matching strategy
my $match = $c->param('match');
# TODO: filter by status (only pinned)
my $status = $c->param('status');
my $before = $c->param('before');
my $after = $c->param('after');
my $limit = $c->param('limit') || 10;
# Only app_name is supported.
my $meta = $c->param('meta');
my $app_name;
if (defined $meta) {
eval {
$meta = decode_json $meta;
$app_name = $meta->{app_name};
};
return $c->render(status => 400, openapi =>{
error => {
reason => "INVALID_PARAM",
details => "Parameter 'meta' is invalid.",
},
}) if $@;
}
# Not supporting meta.
if (defined $cid) {
$query{cid} = $cid;
}
if (defined $name) {
$query{name} = $name;
}
if (defined $before) {
$query{created_at} = { '<' => $before };
}
if (defined $after) {
if (defined $query{created_at}) {
$query{created_at}->{'>'} = $after;
} else {
$query{created_at} = { '>' => $after };
}
}
if (defined $app_name) {
$query{app_name} = $app_name;
if (!IpfsUpload::Util::update_query($c, \%query)) {
# Error!
return;
}
$c->pins->count(\%query)->then(sub ($count) {

View File

@ -41,4 +41,62 @@ sub check_auth($c) {
return 0;
}
sub update_query($c, $query) {
my $cid = $c->param('cid');
my $name = $c->param('name');
# TODO: Text matching strategy
my $match = $c->param('match');
# TODO: filter by status (only pinned)
my $status = $c->param('status');
my $before = $c->param('before');
my $after = $c->param('after');
# Only app_name is supported.
my $meta = $c->param('meta');
my $app_name;
if (defined $meta) {
eval {
$meta = decode_json $meta;
$app_name = $meta->{app_name};
};
$c->render(status => 400, openapi =>{
error => {
reason => "INVALID_PARAM",
details => "Parameter 'meta' is invalid.",
},
}) if $@;
return 0;
}
# Not supporting meta.
if (defined $cid) {
$query->{cid} = $cid;
}
if (defined $name) {
$query->{name} = $name;
}
if (defined $before) {
$query->{created_at} = { '<' => $before };
}
if (defined $after) {
if (defined $query->{created_at}) {
$query->{created_at}->{'>'} = $after;
} else {
$query->{created_at} = { '>' => $after };
}
}
if (defined $app_name) {
$query->{app_name} = $app_name;
}
return 1;
}
1;

View File

@ -15,6 +15,10 @@
</p>
% }
<p>
<%= $count %> pins found.
</p>
<ul>
% for my $pin (@$pins) {
<li>
@ -27,6 +31,6 @@
% if (scalar @$pins == 0) {
<p>Seems like there's nothing here.</p>
% } elsif (scalar @$pins == $limit) {
<a href="/my?before=<%= $nextPage %>">Next page</a>
<a href="<%= url_with->query({before => $nextPage}) %>">Next page</a>
% }