OSDN Git Service

Plugin: add "AddThumbnail" plugin
[newslash/newslash.git] / src / newslash_web / lib / Newslash / Plugin / AddThumbnail.pm
1 package Newslash::Plugin::AddThumbnail;
2 use Mojo::Base 'Mojolicious::Plugin';
3 use Data::Dumper;
4 sub register {
5     my ($self, $app, $conf) = @_;
6
7     $app->hook(before_render => sub {
8                    my ($c, $args) = @_;
9                    return if $c->stash("thumbnail");
10                    my $cfg = $app->config->{Site} || {};
11                    my $base_url = $cfg->{base_url};
12                    my $icon_url = $base_url . $cfg->{topic_icon_base_url} . "/";
13
14                    my $item = $c->stash("item") || $args->{item};
15                    if ($item) {
16                        if (($item->{primary_topic} || {})->{image}) {
17                            $c->stash(thumbnail => $icon_url . $item->{primary_topic}->{image});
18                            return;
19                        }
20                    }
21                    if ($cfg->{default_thumbnail}) {
22                        $c->stash(thumbnail => $base_url . $cfg->{default_thumbnail});
23                        return;
24                    }
25                });
26 }
27
28
29
30 1;