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 all commits
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
3 changes: 3 additions & 0 deletions lib/LANraragi.pm
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,9 @@ sub startup {
shutdown_from_pid( get_temp . "/shinobu.pid" );
start_shinobu($self);

# Check if this is a first-time installation.
LANraragi::Model::Config::first_install_actions();

# 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_highlighted_category {

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 the highlighted category, removing it.");
$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;
25 changes: 25 additions & 0 deletions lib/LANraragi/Model/Config.pm
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ use Mojolicious::Plugin::Config;
use Mojo::Home;
use Mojo::JSON qw(decode_json);

use LANraragi::Utils::Logging qw(get_logger);
use LANraragi::Model::Category;

# Find the project root directory to load the conf file
my $home = Mojo::Home->new;
$home->detect;
Expand Down Expand Up @@ -168,6 +171,28 @@ sub get_tagrules {
);
}

# first_install_actions()
# Setup tasks for first-time installations. New installs are checked by confirming updated
# user settings. On first installation, create default 'Favorites' category and set it as
# highlighted. Returns 1 if is first-time installation, else 0.
sub first_install_actions {
my $redis = get_redis_config();
my $logger = get_logger( "Config", "lanraragi" );
unless ( $redis->hexists('LRR_CONFIG', 'htmltitle') ) {
$logger->info("First-time installation detected!");
$redis->hset('LRR_CONFIG', 'htmltitle', 'LANraragi');

$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);
$logger->info("Created default Favorites category.");
$redis->quit();
return 1;
}
$redis->quit();
return 0;
}

sub get_htmltitle { return xml_escape( &get_redis_conf( "htmltitle", "LANraragi" ) ) }
sub get_motd { return xml_escape( &get_redis_conf( "motd", "Welcome to this Library running LANraragi!" ) ) }
sub get_tempmaxsize { return &get_redis_conf( "tempmaxsize", "500" ) }
Expand Down
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/highlighted')->to('api-category#get_highlighted_category');
$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.