OSDN Git Service

add authors page
authorhylom <hylom@users.sourceforge.jp>
Fri, 29 Mar 2019 11:22:10 +0000 (20:22 +0900)
committerhylom <hylom@users.sourceforge.jp>
Fri, 29 Mar 2019 11:22:10 +0000 (20:22 +0900)
src/newslash_web/css/newslash.less
src/newslash_web/css/newslash/authors.less [new file with mode: 0644]
src/newslash_web/lib/Newslash/Model/Stories.pm
src/newslash_web/lib/Newslash/Plugin/NewslashHelpers.pm
src/newslash_web/lib/Newslash/Web.pm
src/newslash_web/lib/Newslash/Web/Controller/Authors.pm [new file with mode: 0644]

index ca8af4e..5e02fcf 100644 (file)
@@ -29,6 +29,7 @@
 @import "newslash/progress_bar.less";
 @import "newslash/wiki_content.less";
 @import "newslash/timeline.less";
+@import "newslash/authors.less";
 
 @import "newslash/ads.less";
 @import "newslash/system_error.less";
diff --git a/src/newslash_web/css/newslash/authors.less b/src/newslash_web/css/newslash/authors.less
new file mode 100644 (file)
index 0000000..2a13cc8
--- /dev/null
@@ -0,0 +1,16 @@
+.authors-list {
+    .authors {
+        dt {
+            &:extend(.rectangle-header);
+            &:extend(.medium-text);
+        }
+        dd {
+            margin-bottom: 1em;
+        }
+    }
+
+    h2 {
+        &:extend(.rectangle-header);
+        &:extend(.large-text);
+    }
+}
index 3da3e5a..0dbf8cd 100644 (file)
@@ -38,6 +38,28 @@ sub key_definition {
 
 use constant FACULTIES => { 1000 => [qw(hits hitparade)] };
 
+sub count_by_author {
+    my $self = shift;
+    my $sql = <<"EOSQL";
+SELECT uid, COUNT(stoid) AS count
+  FROM stories
+  WHERE stories.time <= NOW()
+  GROUP BY uid;
+EOSQL
+
+    my $dbh = $self->connect_db;
+    my $sth = $dbh->prepare($sql);
+    $sth->execute();
+    my $rs = $sth->fetchall_hashref("uid");
+    if (!defined $rs) {
+        $self->set_error("select failed", $dbh->errstr, $dbh->err);
+        $self->disconnect_db;
+        return;
+    }
+    $self->disconnect_db;
+    return $rs;
+}
+
 sub count {
     my $self = shift;
     my $join = 'LEFT JOIN firehose ON (stories.stoid = firehose.srcid AND firehose.type = "story")';
index 28ddd8b..918bfab 100644 (file)
@@ -45,6 +45,7 @@ sub register {
                   the_path
                   get_timestamp_by_id
                   tidy_html clean_html escape_html format_htmltext escape_plaintext strip_by_mode
+                  get_authors
                 ]) {
         $app->helper($k => $self->can("_$k"))
     }
@@ -165,6 +166,19 @@ sub _get_timestamp_by_id {
     my ($c, $content_type, $id) = @_;
 }
 
+=head2 get_authors()
+
+  get authors
+
+=cut
+
+sub _get_authors {
+    my ($c) = @_;
+    my $users = $c->model('users');
+    my $authors = $users->select(author => { gt => 0 });
+    return $authors;
+}
+
 
 ######################################################################
 # 
index 7000586..da3a598 100644 (file)
@@ -337,7 +337,7 @@ sub startup {
     # about
     $r->get('/about')->to('about#about');
 
-    #faq
+    # faq
     $r->get('/faq/editorial')->to('faq#editorial');
     $r->get('/faq/UI')->to('faq#UI');
     $r->get('/faq/firehose')->to('faq#firehose');
@@ -348,6 +348,9 @@ sub startup {
     $r->get('/faq/tech')->to('faq#tech');
     $r->get('/faq/')->to('faq#faq');
 
+    # authors
+    $r->get('/authors')->to('authors#authors');
+
     # Admin
     # pages under /admin needs seclev equal or greater than 10000;
     my $admin = $r->under('/admin' => sub { my $c = shift; $c->stash(seclev => 10000); return 1; });
diff --git a/src/newslash_web/lib/Newslash/Web/Controller/Authors.pm b/src/newslash_web/lib/Newslash/Web/Controller/Authors.pm
new file mode 100644 (file)
index 0000000..d991f9c
--- /dev/null
@@ -0,0 +1,46 @@
+package Newslash::Web::Controller::Authors;
+use Mojo::Base 'Mojolicious::Controller';
+use Data::Dumper;
+
+sub authors {
+    my $c = shift;
+    my $authors = $c->get_authors();
+
+    if (!$authors) {
+        $c->rendered(500);
+        return;
+    }
+
+    if (!@$authors) {
+        $c->rendered(500);
+        return;
+    }
+
+    # get stories count
+    my $stories = $c->model('stories');
+    my $users= $c->model('users');
+    my $counts = $stories->count_by_author;
+    if (!defined $counts) {
+        $c->rendered(500);
+        return;
+    }
+
+    for my $author (@$authors) {
+        $users->get_user_config($author);
+        if ($counts->{$author->{uid}}) {
+            $author->{number_of_stories} = $counts->{$author->{uid}}->{count} || 0;
+        }
+        else {
+            $author->{number_of_stories} = 0;
+        }
+    }
+
+    my @sorted = sort { $b->{number_of_stories} <=> $a->{number_of_stories} } @$authors;
+
+    $c->render(page => { type => "authors" },
+               authors => \@sorted);
+}
+
+
+1;
+