summaryrefslogtreecommitdiff
path: root/lib/VNWeb/Misc/Feeds.pm
blob: 6d3a3ebae9187b824adeef1622121e76d2bfe437 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
package VNWeb::Misc::Feeds;

use VNWeb::Prelude;
use TUWF::XML ':xml';


sub datetime { strftime '%Y-%m-%dT%H:%M:%SZ', gmtime shift }


sub feed {
    my($path, $title, $data) = @_;
    my $base = tuwf->reqBaseURI();

    xml;
    tag feed => xmlns => 'http://www.w3.org/2005/Atom', 'xml:lang' => 'en', 'xml:base' => "$base/", sub {
        tag title => $title;
        tag updated => datetime max grep $_, map +($_->{published}, $_->{updated}), @$data;
        tag id => $base.$path;
        tag link => rel => 'self', type => 'application/atom+xml', href => $base.tuwf->reqPath(), undef;
        tag link => rel => 'alternate', type => 'text/html', href => $base.$path, undef;

        tag entry => sub {
            tag id => "$base/$_->{id}";
            tag title => $_->{title};
            tag updated => datetime($_->{updated} || $_->{published});
            tag published => datetime $_->{published} if $_->{published};
            tag author => sub {
                tag name => $_->{user_name};
                tag uri => "$base/u$_->{user_id}";
            } if $_->{user_id};
            tag link => rel => 'alternate', type => 'text/html', href => "$base/$_->{id}", undef;
            tag summary => type => 'html', bb_format $_->{summary}, maxlength => 300 if $_->{summary};
        } for @$data;
    }
}


TUWF::get qr{/feeds/announcements.atom}, sub {
    feed '/t/an', 'VNDB Site Announcements', tuwf->dbAlli('
      SELECT t.id, t.title, tp.msg AS summary
           , ', sql_totime('tp.date'), 'AS published,', sql_totime('tp.edited'), 'AS updated,', sql_user(), '
        FROM threads t
        JOIN threads_posts tp ON tp.tid = t.id AND tp.num = 1
        JOIN threads_boards tb ON tb.tid = t.id AND tb.type = \'an\'
        LEFT JOIN users u ON u.id = tp.uid
       WHERE NOT t.hidden AND NOT t.private
       ORDER BY tb.tid DESC
       LIMIT 10'
    );
};


TUWF::get qr{/feeds/changes.atom}, sub {
    my($lst) = VNWeb::Misc::History::fetch(undef, undef, {m=>1,h=>1,p=>1}, {results=>25});
    for (@$lst) {
        $_->{id}      = "$_->{type}$_->{itemid}.$_->{rev}";
        $_->{summary} = $_->{comments};
        $_->{updated} = $_->{added};
    }
    feed '/hist', 'VNDB Recent Changes', $lst;
};


TUWF::get qr{/feeds/posts.atom}, sub {
    feed '/t', 'VNDB Recent Posts', tuwf->dbAlli('
      SELECT t.id||\'.\'||tp.num AS id, t.title||\' (#\'||tp.num||\')\' AS title, tp.msg AS summary
           , ', sql_totime('tp.date'), 'AS published,', sql_totime('tp.edited'), 'AS updated,', sql_user(), '
        FROM threads_posts tp
        JOIN threads t ON t.id = tp.tid
        LEFT JOIN users u ON u.id = tp.uid
       WHERE NOT tp.hidden AND NOT t.hidden AND NOT t.private
       ORDER BY tp.date DESC
       LIMIT ', \25
    );
};


1;