summaryrefslogtreecommitdiff
path: root/lib/VNWeb/Reviews/Page.pm
blob: 8aa35d4c6a30f8cb9edc89f071a97397ddd9d78d (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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
package VNWeb::Reviews::Page;

use VNWeb::Prelude;
use VNWeb::Releases::Lib;
use VNWeb::Reviews::Lib;


my $COMMENT = form_compile any => {
    id  => { vndbid => 'w' },
    msg => { maxlength => 32768 }
};

elm_api ReviewsComment => undef, $COMMENT, sub {
    my($data) = @_;
    my $w = tuwf->dbRowi('SELECT id, locked FROM reviews WHERE id =', \$data->{id});
    return tuwf->resNotFound if !$w->{id};
    return elm_Unauth if !can_edit t => $w;

    my $num = sql 'COALESCE((SELECT MAX(num)+1 FROM reviews_posts WHERE id =', \$data->{id}, '),1)';
    my $msg = bb_subst_links $data->{msg};
    $num = tuwf->dbVali('INSERT INTO reviews_posts', { id => $w->{id}, num => $num, uid => auth->uid, msg => $msg }, 'RETURNING num');
    elm_Redirect "/$w->{id}.$num#last";
};



sub review_ {
    my($w) = @_;

    input_ type => 'checkbox', class => 'visuallyhidden', id => 'reviewspoil', (auth->pref('spoilers')||0) == 2 ? ('checked', 'checked') : (), undef;
    my @spoil = $w->{spoiler} ? (class => 'reviewspoil') : ();
    table_ class => 'fullreview', sub {
        tr_ sub {
            td_ 'Subject';
            td_ sub {
                a_ href => "/v$w->{vid}", $w->{title};
                if($w->{rid}) {
                    br_;
                    abbr_ class => "icons $_", title => $PLATFORM{$_}, '' for grep $_ ne 'oth', $w->{platforms}->@*;
                    abbr_ class => "icons lang $_", title => $LANGUAGE{$_}, '' for $w->{lang}->@*;
                    abbr_ class => "icons rt$w->{rtype}", title => $w->{rtype}, '';
                    a_ href => "/r$w->{rid}", title => $w->{roriginal}||$w->{rtitle}, $w->{rtitle};
                }
            };
        };
        tr_ sub {
            td_ 'By';
            td_ sub {
                b_ style => 'float: right', 'Vote: '.fmtvote($w->{vote}) if $w->{vote};
                user_ $w;
                my($date, $lastmod) = map $_&&fmtdate($_,'compact'), $w->@{'date', 'lastmod'};
                txt_ " on $date";
                b_ class => 'grayedout', " last updated on $lastmod" if $lastmod && $date ne $lastmod;
                br_ if $w->{c_flagged} || $w->{locked};
                if($w->{c_flagged}) {
                    br_;
                    b_ class => 'grayedout', 'Flagged: this review is below the voting threshold and not visible on the VN page.';
                }
                if($w->{locked}) {
                    br_;
                    b_ class => 'grayedout', 'Locked: commenting on this review has been disabled.';
                }
            }
        };
        tr_ class => 'reviewnotspoil', sub {
            td_ '';
            td_ sub {
                label_ class => 'fake_link', for => 'reviewspoil', 'This review contains spoilers, click to view.';
            };
        } if $w->{spoiler};
        tr_ @spoil, sub {
            td_ 'Review';
            td_ sub { lit_ reviews_format $w }
        };
        tr_ @spoil, sub {
            td_ '';
            td_ style => 'text-align: right', sub {
                reviews_vote_ $w;
            };
        };
    }
}


TUWF::get qr{/$RE{wid}(?:(?<sep>[\./])$RE{num})?}, sub {
    my($id, $sep, $num) = (tuwf->capture('id'), tuwf->capture('sep')||'', tuwf->capture('num'));
    my $w = tuwf->dbRowi(
        'SELECT r.id, r.vid, r.rid, r.isfull, r.text, r.spoiler, r.locked, COALESCE(c.count,0) AS count, r.c_flagged, r.c_up, r.c_down, uv.vote, rm.id IS NULL AS can
              , v.title, rel.title AS rtitle, rel.original AS roriginal, rel.type AS rtype, rv.vote AS my, COALESCE(rv.overrule,false) AS overrule
              , ', sql_user(), ',', sql_totime('r.date'), 'AS date,', sql_totime('r.lastmod'), 'AS lastmod
           FROM reviews r
           JOIN vn v ON v.id = r.vid
           LEFT JOIN releases rel ON rel.id = r.rid
           LEFT JOIN users u ON u.id = r.uid
           LEFT JOIN ulist_vns uv ON uv.uid = r.uid AND uv.vid = r.vid
           LEFT JOIN (SELECT id, COUNT(*) FROM reviews_posts GROUP BY id) AS c(id,count) ON c.id = r.id
           LEFT JOIN reviews_votes rv ON rv.id = r.id AND rv.uid =', \auth->uid, '
           LEFT JOIN reviews rm ON rm.vid = r.vid AND rm.uid =', \auth->uid, '
          WHERE r.id =', \$id
    );
    return tuwf->resNotFound if !$w->{id};

    enrich_flatten lang => rid => id => sub { sql 'SELECT id, lang FROM releases_lang WHERE id IN', $_, 'ORDER BY id, lang' }, $w;
    enrich_flatten platforms => rid => id => sub { sql 'SELECT id, platform FROM releases_platforms WHERE id IN', $_, 'ORDER BY id, platform' }, $w;

    my $page = $sep eq '/' ? $num||1 : $sep ne '.' ? 1
        : ceil((tuwf->dbVali('SELECT COUNT(*) FROM reviews_posts WHERE num <=', \$num, 'AND id =', \$id)||9999)/25);
    $num = 0 if $sep ne '.';

    my $posts = tuwf->dbPagei({ results => 25, page => $page },
        'SELECT rp.id, rp.num, rp.hidden, rp.msg',
             ',', sql_user(),
             ',', sql_totime('rp.date'), ' as date',
             ',', sql_totime('rp.edited'), ' as edited
           FROM reviews_posts rp
           LEFT JOIN users u ON rp.uid = u.id
          WHERE rp.id =', \$id, '
          ORDER BY rp.num'
    );
    return tuwf->resNotFound if $num && !grep $_->{num} == $num, @$posts;

    auth->notiRead($id, undef);
    auth->notiRead($id, [ map $_->{num}, $posts->@* ]) if @$posts;

    my $title = "Review of $w->{title}";
    framework_ title => $title, index => 1, type => 'w', dbobj => $w,
        $num||$page>1 ? (pagevars => {sethash=>$num?$num:'threadstart'}) : (),
    sub {
        div_ class => 'mainbox', sub {
            itemmsg_ w => $w;
            h1_ $title;
            review_ $w;
        };
        if(grep !$_->{hidden}, @$posts) {
            h1_ class => 'boxtitle', 'Comments';
            VNWeb::Discussions::Thread::posts_($w, $posts, $page);
        } else {
            div_ id => 'threadstart', '';
        }
        elm_ 'Reviews.Comment' => $COMMENT, { id => $w->{id}, msg => '' } if $w->{count} <= $page*25 && can_edit t => $w;
    };
};

1;