Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement Highlighted Categories (aka like button) #1173

Draft
wants to merge 6 commits into
base: dev
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions lib/LANraragi.pm
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ use LANraragi::Utils::Minion;
use LANraragi::Utils::I18N;
use LANraragi::Utils::I18NInitializer;

use LANraragi::Model::Category;
use LANraragi::Model::Search;
use LANraragi::Model::Config;

Expand Down Expand Up @@ -184,6 +185,17 @@ sub startup {
shutdown_from_pid( get_temp . "/shinobu.pid" );
start_shinobu($self);

# First-install actions: check if redis LRR_CONFIG is updated.
psilabs-dev marked this conversation as resolved.
Show resolved Hide resolved
unless ( $self->LRR_CONF->get_redis_config->hexists('LRR_CONFIG', 'htmltitle') ) {
Difegue marked this conversation as resolved.
Show resolved Hide resolved
$self->LRR_LOGGER->info("First-time installation detected!");
$self->LRR_CONF->get_redis_config->hset('LRR_CONFIG', 'htmltitle', 'LANraragi');

$self->LRR_LOGGER->debug("Creating first category...");
my $default_category_id = LANraragi::Model::Category::create_category("Favorites", "", 0, "");
LANraragi::Model::Category::update_highlight_category($default_category_id);
$self->LRR_LOGGER->info("Created default Favorites category.");
}

# Hook to SIGTERM to cleanly kill minion+shinobu on server shutdown
# As this is executed during before_dispatch, this code won't work if you SIGTERM without loading a single page!
# (https://stackoverflow.com/questions/60814220/how-to-manage-myself-sigint-and-sigterm-signals)
Expand Down
57 changes: 57 additions & 0 deletions lib/LANraragi/Controller/Api/Category.pm
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use Redis;
use Encode;

use LANraragi::Model::Category;
use LANraragi::Model::Config;
use LANraragi::Utils::Generic qw(render_api_response);

sub get_category_list {
Expand Down Expand Up @@ -138,5 +139,61 @@ sub remove_from_category {
}
}

sub get_highlight_category {
psilabs-dev marked this conversation as resolved.
Show resolved Hide resolved

my $self = shift;
my $catid = LANraragi::Model::Category::get_highlight_category();
return $self->render(
json => {
operation => "get_highlight_category",
success => 1,
category_id => $catid
}
);

}

sub update_highlight_category {

my $self = shift;
my $catid = $self->stash('id');
my ($status_code, $message);
($status_code, $catid, $message) = LANraragi::Model::Category::update_highlight_category($catid);
unless ( $status_code == 200 ) {
return $self->render(
json => {
operation => "update_highlight_category",
success => 0,
category_id => $catid,
error => $message
},
status => $status_code
);
}
return $self->render(
json => {
operation => "update_highlight_category",
category_id => $catid,
success => 1
},
status => 200
);

}

sub delete_highlight_category {

my $self = shift;
my $catid = LANraragi::Model::Category::delete_highlight_category();
return $self->render(
json => {
operation => "delete_highlight_category",
category_id => $catid,
success => 1
}
);

}

1;

53 changes: 53 additions & 0 deletions lib/LANraragi/Model/Category.pm
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,7 @@ sub create_category {

# delete_category(id)
# Deletes the category with the given ID.
# If category is also highlighed, remove the ID from highlight.
# Returns 0 if the given ID isn't a category ID, 1 otherwise
sub delete_category {

Expand All @@ -169,6 +170,10 @@ sub delete_category {
}

if ( $redis->exists($cat_id) ) {
if ( $redis->hget('LRR_CONFIG', 'highlight') eq $cat_id ) {
$logger->info("$cat_id is a highlight, removing.");
psilabs-dev marked this conversation as resolved.
Show resolved Hide resolved
$redis->hdel('LRR_CONFIG', 'highlight');
}
$redis->del($cat_id);
$redis->quit;
return 1;
Expand Down Expand Up @@ -287,4 +292,52 @@ sub remove_from_category {
return 0;
}

# get_highlight_category()
# Gets the category ID with highlighted flag.
# If no such category exists, returns an empty string.
sub get_highlight_category {

my $redis = LANraragi::Model::Config->get_redis;
my $catid = $redis->hget('LRR_CONFIG', 'highlight') || "";
$redis->quit();
return $catid;

}

# update_highlight_category(cat_id)
# Moves the highlighted flag to a different category ID.
# The category ID must be a valid, static category ID.
sub update_highlight_category {

my $cat_id = shift;
unless (defined $cat_id && $cat_id =~ /^SET_\d{10}$/) {
return (400, $cat_id, "Input category ID is invalid.");
}
my $redis = LANraragi::Model::Config->get_redis;
unless ( $redis->exists($cat_id) ) {
$redis->quit();
return (404, $cat_id, "Category does not exist!");
}
unless ( $redis->hget( $cat_id, "search" ) eq "" ) {
$redis->quit();
return (400, $cat_id, "Cannot assign highlight property to a dynamic category.");
}
$redis->hset('LRR_CONFIG', 'highlight', $cat_id);
$redis->quit();
return (200, $cat_id, "success");

}

# delete_highlight_category()
# Remove highlight status from highlighted category.
# Returns the category ID which had this highlight status, otherwise
# return empty string.
sub delete_highlight_category {
my $redis = LANraragi::Model::Config->get_redis;
my $cat_id = $redis->hget('LRR_CONFIG', 'highlight') || "";
$redis->hdel('LRR_CONFIG', 'highlight');
$redis->quit();
return $cat_id;
}

1;
3 changes: 3 additions & 0 deletions lib/LANraragi/Utils/Routing.pm
Original file line number Diff line number Diff line change
Expand Up @@ -148,9 +148,12 @@ sub apply_routes {

# Category API
$public_api->get('/api/categories')->to('api-category#get_category_list');
$public_api->get('/api/categories/highlight')->to('api-category#get_highlight_category');
psilabs-dev marked this conversation as resolved.
Show resolved Hide resolved
$public_api->get('/api/categories/:id')->to('api-category#get_category');
$logged_in_api->put('/api/categories/highlight/:id')->to('api-category#update_highlight_category');
$logged_in_api->put('/api/categories')->to('api-category#create_category');
$logged_in_api->put('/api/categories/:id')->to('api-category#update_category');
$logged_in_api->delete('/api/categories/highlight')->to('api-category#delete_highlight_category');
$logged_in_api->delete('/api/categories/:id')->to('api-category#delete_category');
$logged_in_api->put('/api/categories/:id/:archive')->to('api-category#add_to_category');
$logged_in_api->delete('/api/categories/:id/:archive')->to('api-category#remove_from_category');
Expand Down
Binary file added public/img/highlight/highlight-off-placeholder.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added public/img/highlight/highlight-on-placeholder.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading