OSDN Git Service

Model: add Timeline
authorhylom <hylom@users.sourceforge.jp>
Fri, 3 Feb 2017 12:16:59 +0000 (21:16 +0900)
committerhylom <hylom@users.sourceforge.jp>
Fri, 3 Feb 2017 12:16:59 +0000 (21:16 +0900)
src/newslash_web/lib/Newslash/Model.pm
src/newslash_web/lib/Newslash/Model/Timeline.pm [new file with mode: 0644]

index 240dd08..9abf42d 100644 (file)
@@ -12,5 +12,6 @@ load(boxes => "Newslash::Model::Boxes");
 load(moderations => "Newslash::Model::Moderations");
 load(metamoderations => "Newslash::Model::Metamoderations");
 load(globjs => "Newslash::Model::Globjs");
+load(timeline => "Newslash::Model::Timeline");
 1;
 
diff --git a/src/newslash_web/lib/Newslash/Model/Timeline.pm b/src/newslash_web/lib/Newslash/Model/Timeline.pm
new file mode 100644 (file)
index 0000000..07ac5b9
--- /dev/null
@@ -0,0 +1,85 @@
+package Newslash::Model::Timeline;
+use Newslash::Model::Base -base;
+
+use Data::Dumper;
+
+sub select {
+    my $self = shift;
+    my $params = {@_};
+
+    my $uid = $params->{uid};
+    return if !$uid;
+
+    my ($limit_clause, $limit_values) = $self->build_limit_clause({columns => [qw(createtime)]}, $params);
+
+    my $sql = <<"EOSQL";
+SELECT firehose.*,
+       firehose_text.*,
+       topics.*,
+       users.nickname AS author
+  FROM firehose
+  JOIN firehose_text ON firehose.id = firehose_text.id
+  JOIN users ON firehose.uid = users.uid
+  JOIN topics ON firehose.tid = topics.tid
+  WHERE firehose.uid = ?
+  ORDER BY createtime DESC $limit_clause
+EOSQL
+    my @params;
+    push @params, $uid;
+    push @params, @$limit_values;
+
+    my $dbh = $self->connect_db;
+    my $sth = $dbh->prepare($sql);
+    $sth->execute(@params);
+    my $rs = $sth->fetchall_arrayref({});
+    $self->disconnect_db;
+
+    if ($rs) {
+        for my $item (@$rs) {
+            $self->_generalize($item);
+        }
+    }
+    return $rs;
+}
+
+sub _generalize {
+    my ($self, $item) = @_;
+
+    if ($item->{type} eq 'comment') {
+        $item->{cid} = $item->{srcid};
+    }
+    elsif ($item->{type} eq 'story') {
+        $item->{stoid} = $item->{srcid};
+    }
+    elsif ($item->{type} eq 'journal') {
+        $item->{id} = $item->{srcid};
+    }
+    elsif ($item->{type} eq 'submission') {
+        $item->{subid} = $item->{srcid};
+    }
+
+    $item->{content_type} = $item->{type};
+
+    $item->{time} = $item->{createtime};
+
+    $item->{primary_topic} = {};
+    for my $k (qw(tid keyword textname series
+                  image witdh height submittable
+                  searchable storypickable usersprite)) {
+        $item->{primary_topic}->{$k} = $item->{$k};
+        delete $item->{$k};
+    }
+
+    #my $max_weight = 0;
+    #for my $t (@{$item->{topics}}) {
+    #    if ($t->{tid} == $item->{tid}) {
+    #        $item->{primary_topic} = $t;
+     #   }
+        #if ($t->{weight} && $t->{weight} > $max_weight) {
+        #    $max_weight = $t->{weight};
+        #}
+    #}
+}
+
+
+1;