2022-11-09 13:31:58 +01:00
|
|
|
package BurguillosInfo::Controller::Page;
|
|
|
|
|
|
|
|
use v5.34.1;
|
|
|
|
|
|
|
|
use strict;
|
|
|
|
use warnings;
|
|
|
|
|
2022-11-15 23:30:37 +01:00
|
|
|
use BurguillosInfo::Categories;
|
|
|
|
use BurguillosInfo::Posts;
|
|
|
|
|
2022-11-09 13:31:58 +01:00
|
|
|
use Data::Dumper;
|
|
|
|
|
2023-05-12 18:32:19 +02:00
|
|
|
use Mojo::Base 'Mojolicious::Controller', '-signatures';
|
2022-11-09 13:31:58 +01:00
|
|
|
|
2022-11-10 10:49:53 +01:00
|
|
|
use DateTime::Format::ISO8601;
|
|
|
|
use DateTime::Format::Mail;
|
|
|
|
|
2022-11-09 13:31:58 +01:00
|
|
|
sub index {
|
|
|
|
my $self = shift;
|
|
|
|
my $categories = BurguillosInfo::Categories->new->Retrieve;
|
|
|
|
my $current_category = $categories->{'index'};
|
|
|
|
|
|
|
|
# Render template "example/welcome.html.ep" with message
|
2023-09-19 17:21:43 +02:00
|
|
|
my $base_url = $self->config('base_url');
|
|
|
|
$self->stash( ogimage =>
|
|
|
|
( $base_url . '/' . $current_category->{slug} . '-preview.png' ) );
|
2022-11-09 13:31:58 +01:00
|
|
|
$self->render(
|
|
|
|
categories => $categories,
|
2023-09-19 17:21:43 +02:00
|
|
|
current_category => $current_category,
|
2022-11-09 13:31:58 +01:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2022-11-10 10:49:53 +01:00
|
|
|
sub category_rss {
|
2023-07-30 14:46:09 +02:00
|
|
|
require BurguillosInfo;
|
2022-11-10 10:49:53 +01:00
|
|
|
my $self = shift;
|
|
|
|
my $categories = BurguillosInfo::Categories->new->Retrieve;
|
|
|
|
my $category_name = $self->param('category');
|
|
|
|
my $current_category = $categories->{$category_name};
|
|
|
|
my ( $posts_categories, $posts_slug ) =
|
|
|
|
BurguillosInfo::Posts->new->Retrieve;
|
|
|
|
if ( !defined $current_category && $category_name ne 'all' ) {
|
|
|
|
$self->render( template => '404', status => 404 );
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
my $dom = Mojo::DOM->new_tag( 'rss', version => '2.0', undef );
|
|
|
|
my $channel_tag = Mojo::DOM->new_tag('channel');
|
2023-09-19 17:21:43 +02:00
|
|
|
my $base_url = BurguillosInfo->new->config->{base_url};
|
2022-11-10 10:49:53 +01:00
|
|
|
if ( $category_name eq 'all' ) {
|
|
|
|
my $title_tag = Mojo::DOM->new_tag( 'title', 'Burguillos.info' );
|
|
|
|
my $description_tag = Mojo::DOM->new_tag( 'description',
|
|
|
|
'Todas las noticias de Burguillos.info.' );
|
2023-07-30 14:46:09 +02:00
|
|
|
my $link_tag = Mojo::DOM->new_tag( 'link', $base_url );
|
2022-11-10 10:49:53 +01:00
|
|
|
$channel_tag->child_nodes->first->append_content($title_tag);
|
|
|
|
$channel_tag->child_nodes->first->append_content($description_tag);
|
|
|
|
$channel_tag->child_nodes->first->append_content($link_tag);
|
|
|
|
for my $category ( keys %$posts_categories ) {
|
|
|
|
my $posts = $posts_categories->{$category};
|
|
|
|
for my $post (@$posts) {
|
|
|
|
$channel_tag->child_nodes->first->append_content(
|
|
|
|
_post_to_rss($post) );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
my $category = $current_category;
|
|
|
|
my $title_tag = Mojo::DOM->new_tag( 'title',
|
|
|
|
"Burguillos.info - " . $category->{title} );
|
|
|
|
my $description_tag = Mojo::DOM->new_tag( 'description',
|
|
|
|
'Todas las noticias de la categoria de Burguillos.info '
|
|
|
|
. $category->{title} );
|
2023-09-19 17:21:43 +02:00
|
|
|
my $link_tag =
|
|
|
|
Mojo::DOM->new_tag( 'link', $base_url . '/' . $category->{slug} );
|
2022-11-10 10:49:53 +01:00
|
|
|
$channel_tag->child_nodes->first->append_content($title_tag);
|
|
|
|
$channel_tag->child_nodes->first->append_content($description_tag);
|
|
|
|
$channel_tag->child_nodes->first->append_content($link_tag);
|
|
|
|
my $posts = $posts_categories->{$category_name};
|
|
|
|
|
|
|
|
for my $post (@$posts) {
|
|
|
|
$channel_tag->child_nodes->first->append_content(
|
|
|
|
_post_to_rss($post) );
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
$dom->child_nodes->first->append_content($channel_tag);
|
|
|
|
$self->render(
|
|
|
|
format => 'xml',
|
|
|
|
text => $dom,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
sub _post_to_rss {
|
2023-07-30 14:46:09 +02:00
|
|
|
require BurguillosInfo;
|
2022-11-10 10:49:53 +01:00
|
|
|
my $post = shift;
|
|
|
|
my $item_tag = Mojo::DOM->new_tag('item');
|
|
|
|
my $title_tag = Mojo::DOM->new_tag( 'title', $post->{title} );
|
2023-09-19 17:21:43 +02:00
|
|
|
my $base_url = BurguillosInfo->new->config->{base_url};
|
|
|
|
my $link =
|
|
|
|
Mojo::DOM->new_tag( 'link', $base_url = '/posts/' . $post->{slug} );
|
2022-11-10 10:49:53 +01:00
|
|
|
my $description = Mojo::DOM->new_tag( 'description',
|
|
|
|
Mojo::DOM->new( $post->{content} )->all_text );
|
|
|
|
my $guid = Mojo::DOM->new_tag( 'guid', $post->{slug} );
|
|
|
|
my $date = Mojo::DOM->new_tag(
|
|
|
|
'pubDate',
|
2023-05-08 01:14:36 +02:00
|
|
|
''
|
|
|
|
. DateTime::Format::Mail->format_datetime(
|
2022-11-10 10:49:53 +01:00
|
|
|
DateTime::Format::ISO8601->parse_datetime( $post->{date} )
|
2023-05-08 01:14:36 +02:00
|
|
|
)
|
2022-11-10 10:49:53 +01:00
|
|
|
);
|
|
|
|
|
|
|
|
$item_tag->child_nodes->first->append_content($title_tag);
|
|
|
|
$item_tag->child_nodes->first->append_content($link);
|
|
|
|
$item_tag->child_nodes->first->append_content($description);
|
|
|
|
$item_tag->child_nodes->first->append_content($guid);
|
|
|
|
$item_tag->child_nodes->first->append_content($date);
|
|
|
|
return $item_tag;
|
|
|
|
}
|
|
|
|
|
2022-11-09 13:31:58 +01:00
|
|
|
sub post {
|
|
|
|
my $self = shift;
|
|
|
|
my $slug = $self->param('slug');
|
|
|
|
my ( $posts_categories, $posts_slug ) =
|
|
|
|
BurguillosInfo::Posts->new->Retrieve;
|
2023-05-08 01:14:36 +02:00
|
|
|
my $categories = BurguillosInfo::Categories->new->Retrieve;
|
|
|
|
my $post = $posts_slug->{$slug};
|
2022-11-09 13:31:58 +01:00
|
|
|
if ( !defined $post ) {
|
|
|
|
$self->render( template => '404', status => 404 );
|
|
|
|
return;
|
|
|
|
}
|
2022-11-13 02:01:24 +01:00
|
|
|
my $current_category = $categories->{ $post->{category} };
|
2023-05-08 01:14:36 +02:00
|
|
|
my $base_url = $self->config('base_url');
|
|
|
|
$self->stash(
|
|
|
|
ogimage => $base_url . '/posts/' . $post->{slug} . '-preview.png' );
|
|
|
|
$self->stash( useragent => $self->req->headers->user_agent );
|
2022-11-09 13:31:58 +01:00
|
|
|
$self->render( post => $post, current_category => $current_category );
|
|
|
|
}
|
|
|
|
|
|
|
|
sub category {
|
|
|
|
my $self = shift;
|
|
|
|
my $categories = BurguillosInfo::Categories->new->Retrieve;
|
|
|
|
my $category_name = $self->param('category');
|
|
|
|
my $current_category = $categories->{$category_name};
|
2023-05-08 01:14:36 +02:00
|
|
|
my $base_url = $self->config('base_url');
|
2022-11-09 13:31:58 +01:00
|
|
|
if ( !defined $current_category ) {
|
|
|
|
$self->render( template => '404', status => 404 );
|
|
|
|
return;
|
|
|
|
}
|
2023-09-19 17:21:43 +02:00
|
|
|
$self->stash( ogimage =>
|
|
|
|
( $base_url . '/' . $current_category->{slug} . '-preview.png' ) );
|
2022-11-09 13:31:58 +01:00
|
|
|
$self->render(
|
2023-09-19 17:21:43 +02:00
|
|
|
template => 'page/index',
|
|
|
|
categories => $categories,
|
2022-11-09 13:31:58 +01:00
|
|
|
current_category => $current_category
|
|
|
|
);
|
|
|
|
}
|
2022-11-13 02:01:24 +01:00
|
|
|
|
2023-05-08 01:14:36 +02:00
|
|
|
sub get_category_preview {
|
|
|
|
my $self = shift;
|
|
|
|
my $category_slug = $self->param('category');
|
|
|
|
my $category_model = BurguillosInfo::Categories->new;
|
|
|
|
my $categories = $category_model->Retrieve;
|
|
|
|
if ( !defined $categories->{$category_slug} ) {
|
|
|
|
$self->render( template => '404', status => 404 );
|
|
|
|
return;
|
|
|
|
}
|
2023-09-19 17:21:43 +02:00
|
|
|
my $category = $categories->{$category_slug};
|
2023-09-17 10:17:50 +02:00
|
|
|
my $is_whatsapp = $self->req->headers->user_agent =~ /whatsapp/i;
|
2023-09-19 17:47:43 +02:00
|
|
|
$is_whatsapp ||= $self->req->headers->user_agent =~ /facebook/i;
|
2023-05-08 01:14:36 +02:00
|
|
|
$self->render(
|
|
|
|
format => 'png',
|
2023-09-19 17:21:43 +02:00
|
|
|
data => $category_model->PreviewOg( $category, $is_whatsapp )
|
2023-05-08 01:14:36 +02:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2022-11-13 02:01:24 +01:00
|
|
|
sub get_post_preview {
|
2023-05-08 01:14:36 +02:00
|
|
|
my $self = shift;
|
|
|
|
my $slug = $self->param('slug');
|
|
|
|
my $post_model = BurguillosInfo::Posts->new;
|
|
|
|
my ( $posts_categories, $posts_slug ) = $post_model->Retrieve;
|
|
|
|
if ( !defined $posts_slug->{$slug} ) {
|
|
|
|
$self->render( template => '404', status => 404 );
|
|
|
|
return;
|
|
|
|
}
|
2023-09-17 10:17:50 +02:00
|
|
|
my $is_whatsapp = $self->req->headers->user_agent =~ /whatsapp/i;
|
2023-09-19 17:47:43 +02:00
|
|
|
$is_whatsapp ||= $self->req->headers->user_agent =~ /facebook/i;
|
2023-09-17 10:17:50 +02:00
|
|
|
say $self->req->headers->user_agent;
|
2023-05-08 01:14:36 +02:00
|
|
|
my $post = $posts_slug->{$slug};
|
|
|
|
$self->render(
|
|
|
|
format => 'png',
|
2023-09-19 17:21:43 +02:00
|
|
|
data => $post_model->PreviewOg( $post, $is_whatsapp )
|
2023-05-08 01:14:36 +02:00
|
|
|
);
|
2022-11-13 02:01:24 +01:00
|
|
|
}
|
2022-11-09 13:31:58 +01:00
|
|
|
1;
|