burguillos.info/lib/BurguillosInfo/Posts.pm

236 lines
7.7 KiB
Perl
Raw Normal View History

2022-11-09 13:31:58 +01:00
package BurguillosInfo::Posts;
use v5.34.1;
use strict;
use warnings;
2023-05-03 00:07:43 +02:00
use feature 'signatures';
2022-11-13 02:01:24 +01:00
use Data::Dumper;
2022-11-13 04:37:53 +01:00
use MIME::Base64;
2022-11-13 02:01:24 +01:00
2023-05-03 00:07:43 +02:00
use BurguillosInfo::Categories;
2022-11-09 13:31:58 +01:00
use Const::Fast;
use Mojo::DOM;
use Path::Tiny;
use DateTime::Format::ISO8601;
use DateTime;
use BurguillosInfo::Preview;
2022-11-09 13:31:58 +01:00
2023-09-11 01:10:23 +02:00
const my $CURRENT_FILE => __FILE__;
const my $ROOT_PROJECT => path($CURRENT_FILE)->parent->parent->parent;
const my $PUBLIC_DIR => $ROOT_PROJECT->child('public');
const my $POSTS_DIR => $ROOT_PROJECT->child('content/posts');
2022-11-09 13:31:58 +01:00
my $cached_posts_by_category;
my $cached_posts_by_slug;
sub new {
return bless {}, shift;
}
sub _ReturnCacheFilter {
my $self = shift;
my %posts_by_category_filtered;
my %posts_by_slug_filtered;
my $iso8601 = DateTime::Format::ISO8601->new;
my $current_date = DateTime->now;
for my $category ( keys %$cached_posts_by_category ) {
for my $post ( @{ $cached_posts_by_category->{$category} } ) {
2023-05-03 00:07:43 +02:00
my $date_post;
2023-09-11 01:10:23 +02:00
eval { $date_post = $iso8601->parse_datetime( $post->{date} ); };
2023-05-03 00:07:43 +02:00
if ($@) {
print Data::Dumper::Dumper $post;
}
if ( $date_post > $current_date ) {
next;
}
$posts_by_slug_filtered{ $post->{slug} } = $post;
$posts_by_category_filtered{ $post->{category} } //= [];
push @{ $posts_by_category_filtered{ $post->{category} } }, $post;
}
}
2022-11-19 20:00:37 +01:00
return ( \%posts_by_category_filtered, \%posts_by_slug_filtered );
}
2023-05-03 00:07:43 +02:00
sub _GeneratePostFromFile ( $self, $post_file ) {
2023-09-11 01:10:23 +02:00
warn "Bad file $post_file, omiting...", return
2023-05-03 00:07:43 +02:00
if !-f $post_file || $post_file !~ /\.xml$/;
my $dom = Mojo::DOM->new( $post_file->slurp_utf8 );
my $title = $dom->at(':root > title')->text
or die "Missing title at $post_file.";
my $author = $dom->at(':root > author')->text
or die "Missing author at $post_file.";
my $date = $dom->at(':root > date')->text
or die "Missing date at $post_file.";
my $ogdesc = $dom->at(':root > ogdesc')->text
or die "Missing ogdesc at $post_file";
my $category = $dom->at(':root > category')->text
or die "Missing category at $post_file.";
my $slug = $dom->at(':root > slug')->text
or die "Missing slug at $post_file.";
my $content_tag = $dom->at(':root > content')
2023-05-03 00:07:43 +02:00
or die "Missing content at $post_file.";
for my $tag ($content_tag->children->each) {
$tag->content($tag->content =~ s/\n//gr);
}
my $content = $content_tag->content;
2023-09-11 01:10:23 +02:00
my $pinned_node = $dom->at(':root > pinned');
2023-05-03 00:07:43 +02:00
my $image_element = $dom->at(':root > img');
my $image;
2023-09-17 01:39:11 +02:00
my $image_bottom_preview;
2023-09-11 01:10:23 +02:00
my $attributes = $self->_GetAttributes( $post_file, $dom );
2023-05-03 00:07:43 +02:00
2023-09-11 01:10:23 +02:00
my $pinned;
if ( defined $pinned_node ) {
2023-09-13 18:10:12 +02:00
$pinned = int( $pinned_node->text );
2023-09-11 01:10:23 +02:00
}
2023-05-03 00:07:43 +02:00
if ( defined $image_element ) {
2023-09-17 01:39:11 +02:00
$image = $image_element->attr->{src};
$image_bottom_preview = $image_element->attr->{'bottom-preview'};
2022-11-09 13:31:58 +01:00
}
2023-05-03 00:07:43 +02:00
my $last_modification_date_element =
$dom->at(':root > last_modification_date');
my $last_modification_date;
if ( defined $last_modification_date_element ) {
$last_modification_date = $last_modification_date_element->content;
}
return {
2023-09-17 01:39:11 +02:00
title => $title,
author => $author,
date => $date,
ogdesc => $ogdesc,
category => $category,
slug => $slug,
content => $content,
attributes => $attributes,
image_bottom_preview => $image_bottom_preview,
2023-05-03 00:07:43 +02:00
(
( defined $last_modification_date )
? ( last_modification_date => $last_modification_date )
: ()
),
( ( defined $image ) ? ( image => $image ) : () ),
2023-09-11 01:10:23 +02:00
(
( defined $pinned ) ? ( pinned => $pinned )
: ()
)
2023-05-03 00:07:43 +02:00
};
}
2023-09-11 01:10:23 +02:00
sub _GetAttributes ( $self, $post_file, $dom ) {
my $attributes_tag = $dom->at(':root > attributes');
my %attributes;
2023-09-11 01:10:23 +02:00
if ( defined $attributes_tag ) {
my @attribute_list =
$attributes_tag->find('attributes > attribute')->map('text')->each;
%attributes = map {
my $identifier = $_;
2023-09-11 01:10:23 +02:00
( $identifier => 1 );
} @attribute_list;
}
return \%attributes;
}
2023-05-03 00:07:43 +02:00
sub _GeneratePostCache ($self) {
2022-11-09 13:31:58 +01:00
$cached_posts_by_category = {};
$cached_posts_by_slug = {};
2022-11-10 23:49:13 +01:00
for my $post_file ( sort { $b cmp $a } $POSTS_DIR->children ) {
2023-05-03 00:07:43 +02:00
my $post = $self->_GeneratePostFromFile($post_file);
2023-09-11 01:10:23 +02:00
if ( !defined $post ) {
2023-05-03 00:07:43 +02:00
next;
}
2023-09-11 01:10:23 +02:00
my $category = $post->{category};
2022-11-13 02:01:24 +01:00
$cached_posts_by_category->{$category} //= [];
2023-05-03 00:07:43 +02:00
my $slug = $post->{slug};
2022-11-09 13:31:58 +01:00
my $category_posts = $cached_posts_by_category->{$category};
2022-11-13 02:01:24 +01:00
$cached_posts_by_slug->{$slug} = $post;
push @$category_posts, $post;
}
2023-05-03 00:07:43 +02:00
}
sub Retrieve {
my $self = shift;
if ( defined $cached_posts_by_category && defined $cached_posts_by_slug ) {
return $self->_ReturnCacheFilter;
}
$self->_GeneratePostCache();
return $self->_ReturnCacheFilter;
2022-11-13 02:01:24 +01:00
}
2023-05-03 00:07:43 +02:00
my $cache_all_post_categories = {};
2023-09-11 01:10:23 +02:00
2023-05-03 00:07:43 +02:00
sub RetrieveAllPostsForCategory ( $self, $category_name ) {
my $categories = BurguillosInfo::Categories->new->Retrieve;
my $category = $categories->{$category_name};
2023-09-11 01:10:23 +02:00
if ( defined $cache_all_post_categories->{$category_name} ) {
my $posts = $cache_all_post_categories->{$category_name};
return $self->shufflePostsIfRequired( $category, $posts );
}
my $posts = $self->RetrieveDirectPostsForCategory($category_name);
2023-05-03 00:07:43 +02:00
for my $child_category ( $category->{children}->@* ) {
my $child_category_name = $child_category->{slug};
push @$posts,
2023-09-11 01:10:23 +02:00
@{ $self->RetrieveDirectPostsForCategory($child_category_name) };
2023-05-03 00:07:43 +02:00
}
2023-09-11 01:10:23 +02:00
@$posts = sort {
DateTime::Format::ISO8601->parse_datetime( $b->{date} )
<=> DateTime::Format::ISO8601->parse_datetime( $a->{date} )
2023-05-03 00:07:43 +02:00
} @$posts;
$cache_all_post_categories->{$category_name} = $posts;
2023-09-11 01:10:23 +02:00
return $self->shufflePostsIfRequired( $category, $posts );
}
sub comparePinned ( $self, $a, $b ) {
my $cmp = $b->{pinned} <=> $a->{pinned};
if ( $cmp != 0 ) {
return $cmp;
}
return int( rand(3) ) - 1;
}
2023-09-11 01:10:23 +02:00
sub shufflePostsIfRequired ( $self, $category, $posts ) {
2023-09-17 01:39:11 +02:00
my $pinned_posts = [
sort { $self->comparePinned( $a, $b ) }
2023-09-17 05:56:37 +02:00
grep { defined $_->{pinned} } @$posts
2023-09-17 01:39:11 +02:00
];
$posts = [ grep { !exists $_->{pinned} } @$posts ];
2023-09-11 01:10:23 +02:00
if ( exists $category->{random} && $category->{random} ) {
require List::AllUtils;
$posts = [ List::AllUtils::shuffle @$posts ];
}
2023-09-13 18:10:12 +02:00
return [ @$pinned_posts, @$posts ];
2023-05-03 00:07:43 +02:00
}
sub RetrieveDirectPostsForCategory ( $self, $category_name ) {
my ($post_by_category) = $self->Retrieve;
my $categories = BurguillosInfo::Categories->new->Retrieve;
my $category = $categories->{$category_name};
if ( !defined $category ) {
die "$category_name category does not exists";
}
my $posts = $post_by_category->{$category_name};
$posts //= [];
return $self->shufflePostsIfRequired( $category, [@$posts] );
2023-05-03 00:07:43 +02:00
}
sub PreviewOg($self, $post, $isWhatsApp) {
2023-09-17 01:39:11 +02:00
my $title = $post->{title};
my $content = $post->{content};
my $image_file = $post->{image};
my $image_bottom_preview = $post->{image_bottom_preview};
if ($isWhatsApp) {
return BurguillosInfo::Preview->WhatsappAlternativeGenerate( $title, $content, $image_file,
$image_bottom_preview );
}
2023-09-17 01:39:11 +02:00
return BurguillosInfo::Preview->Generate( $title, $content, $image_file,
$image_bottom_preview );
2022-11-14 00:30:05 +01:00
}
2022-11-09 13:31:58 +01:00
1;