OSDN Git Service

Model::Base: remove deprecated / unused functions
[newslash/newslash.git] / src / newslash_web / lib / Newslash / Model / Timeline.pm
1 package Newslash::Model::Timeline;
2 use Newslash::Model::Base -base;
3
4 use Data::Dumper;
5
6
7 sub select {
8     my $self = shift;
9     my $params = {@_};
10
11     my $uid = $params->{uid};
12     my $type = $params->{type} || "global";
13     if ($uid) {
14         $type = $params->{type} || "user";
15     }
16
17     if ($type eq "user") {
18         return $self->_select_user($uid, $params);
19     }
20     elsif ($type eq "tags") {
21         return $self->_select_tags($uid, $params);
22     }
23     elsif ($type eq "user") {
24         return $self->_select_user($uid, $params);
25     }
26     elsif ($type eq "friends") {
27         return $self->_select_friends($uid, $params);
28     }
29     return $self->_select_global($params);
30 }
31
32 sub _select_tags {
33     my ($self, $items) = @_;
34     return if !$items;
35
36     return 1 if !@$items;
37
38     my @globj_ids = map { $_->{globjid} } @$items;
39     my @placeholders = map { "?" } @globj_ids;
40     my $placeholder = join(", ", @placeholders);
41
42     my $dbh = $self->connect_db;
43     my $sql = <<"EOSQL";
44 SELECT * FROM tags
45   LEFT JOIN tagnames ON tags.tagnameid = tagnames.tagnameid
46   WHERE tags.globjid IN ($placeholder)
47 EOSQL
48
49     my $sth = $dbh->prepare($sql);
50     $sth->execute(@globj_ids);
51     my $rs = $sth->fetchall_arrayref({});
52     $self->disconnect_db;
53
54     return if !$rs;
55
56     my $tags_of = {};
57     for my $tag (@$rs) {
58         my $id = $tag->{globjid};
59         $tags_of->{$id} = [] if !$tags_of->{$id};
60         push @{$tags_of->{$id}}, $tag;
61     }
62
63     for my $item (@$items) {
64         my $globj_id = $item->{globjid};
65         $item->{tags} = $tags_of->{$globj_id} || [];
66     }
67
68     return 1;
69 }
70
71 # select global timeline
72 sub _select_global {
73     my ($self, $params) = @_;
74
75     my $unique_keys = { id => "id" };
76     my $keys = { popularity => "popularity",
77                };
78     my $datetime_keys = { create_time => "createtime",
79                           update_time => "last_update",
80                         };
81     my $timestamp = "createtime";
82
83     my ($where_clause, $where_values, $unique) = $self->build_where_clause(unique_keys => $unique_keys,
84                                                                            keys => $keys,
85                                                                            datetime_keys => $datetime_keys,
86                                                                            timestamp => $timestamp,
87                                                                            params => $params);
88     my ($limit_clause, $limit_values) = $self->build_limit_clause(params => $params);
89     my ($orderby_clause, $orderby_values) = $self->build_order_by_clause(keys => $keys,
90                                                                          params => $params);
91
92     if ($where_clause) {
93         $where_clause = $where_clause . " AND public = 'yes' AND createtime <= NOW()";
94     }
95     else {
96         $where_clause = "WHERE public = 'yes' AND createtime <= NOW()";
97     }
98
99     if ($orderby_clause) {
100         $orderby_clause = $orderby_clause . ", createtime DESC";
101     }
102     else {
103         $orderby_clause = " ORDER BY createtime DESC";
104     }
105
106     my $sql = <<"EOSQL";
107 SELECT fh.*,
108        firehose_text.*,
109        topics.*,
110        stories.sid,
111        users.nickname AS author
112   FROM (SELECT * FROM firehose
113           $where_clause
114           $orderby_clause
115           $limit_clause)
116     AS fh
117   JOIN firehose_text ON fh.id = firehose_text.id
118   JOIN users ON fh.uid = users.uid
119   LEFT JOIN stories ON fh.type = "story" AND fh.srcid = stories.stoid
120   LEFT JOIN topics ON fh.tid = topics.tid
121 EOSQL
122     my @params;
123     push @params, @$where_values;
124     push @params, @$orderby_values;
125     push @params, @$limit_values;
126
127     #warn $sql;
128     #warn Dumper @params;
129
130     my $dbh = $self->connect_db;
131     my $sth = $dbh->prepare($sql);
132     $sth->execute(@params);
133     my $rs = $sth->fetchall_arrayref({});
134     $self->disconnect_db;
135
136     my $urls = $self->new_instance_of("Urls");
137     if ($rs) {
138         for my $item (@$rs) {
139             $self->_generalize($item);
140             # check if feed?
141             if ($item->{type} eq "feed") {
142                 my $url = $urls->select(id => $item->{srcid});
143                 if ($url) {
144                     $item->{url} = $url->{url};
145                 }
146             }
147         }
148     }
149
150     $self->_select_tags($rs);
151     return $rs->[0] if ($rs && $unique);
152     return $rs;
153 }
154
155 # select user's timelime
156 sub _select_user {
157     my ($self, $uid, $params) = @_;
158     my ($limit_clause, $limit_values) = $self->build_limit_clause(params => $params);
159
160     # show future story?
161     my $extra_where_clause = "";
162     if (!$params->{show_future}) {
163         $extra_where_clause = $extra_where_clause . " AND firehose.createtime <= NOW() ";
164     }
165
166     # show non-public story?
167     if (!$params->{show_nonpublic} && !$params->{public}) {
168         $extra_where_clause = $extra_where_clause . " AND firehose.public != 'no' ";
169     }
170
171     my $sql = <<"EOSQL";
172 SELECT firehose.*,
173        firehose_text.*,
174        topics.*,
175        users.nickname AS author
176   FROM firehose
177   JOIN firehose_text ON firehose.id = firehose_text.id
178   JOIN users ON firehose.uid = users.uid
179   JOIN topics ON firehose.tid = topics.tid
180   WHERE firehose.uid = ?
181   $extra_where_clause
182   ORDER BY createtime DESC $limit_clause
183 EOSQL
184     my @params;
185     push @params, $uid;
186     push @params, @$limit_values;
187
188     my $dbh = $self->connect_db;
189     my $sth = $dbh->prepare($sql);
190     $sth->execute(@params);
191     my $rs = $sth->fetchall_arrayref({});
192     $self->disconnect_db;
193
194     if ($rs) {
195         for my $item (@$rs) {
196             $self->_generalize($item);
197         }
198     }
199     $self->_select_tags($rs);
200     return $rs;
201 }
202
203 # select user's friends' timelime
204 sub _select_friends {
205     my ($self, $uid, $params) = @_;
206     my ($limit_clause, $limit_values) = $self->build_limit_clause(params => $params);
207
208     my $sql = <<"EOSQL";
209 SELECT firehose.*,
210        firehose_text.*,
211        topics.*,
212        users.nickname AS author
213   FROM firehose
214   JOIN firehose_text ON firehose.id = firehose_text.id
215   JOIN users ON firehose.uid = users.uid
216   JOIN topics ON firehose.tid = topics.tid
217   WHERE firehose.uid IN (SELECT person FROM people
218                            WHERE uid = ? AND type = "friend")
219   ORDER BY firehose.createtime DESC $limit_clause;
220 EOSQL
221
222     my @params;
223     push @params, $uid;
224     push @params, @$limit_values;
225
226     my $dbh = $self->connect_db;
227     my $sth = $dbh->prepare($sql);
228     $sth->execute(@params);
229     my $rs = $sth->fetchall_arrayref({});
230     $self->disconnect_db;
231
232     if ($rs) {
233         for my $item (@$rs) {
234             $self->_generalize($item);
235         }
236     }
237     $self->_select_tags($rs);
238     return $rs;
239 }
240
241 sub _generalize {
242     my ($self, $item) = @_;
243
244     # NTO-nized
245     $item->{id} = $item->{srcid};
246     $item->{create_time} = $item->{createtime};
247     $item->{update_time} = $item->{last_update};
248
249     my $topic = {};
250     for my $k (qw(tid keyword textname series
251                   image witdh height submittable
252                   searchable storypickable usersprite)) {
253         $topic->{$k} = $item->{$k};
254         delete $item->{$k};
255     }
256     $item->{topics} = [$topic];
257     $item->{primary_topic} = $topic;
258
259     $item->{content_type} = $item->{type};
260     $item->{intro_text} = $item->{introtext};
261     $item->{body_text} = $item->{bodytext};
262     $item->{discussion_id} = $item->{discussion};
263
264     if ($item->{type} eq 'comment') {
265         $item->{cid} = $item->{srcid};
266         $item->{comment_id} = $item->{srcid};
267     }
268     elsif ($item->{type} eq 'story') {
269         $item->{stoid} = $item->{srcid};
270         $item->{story_id} = $item->{srcid};
271     }
272     elsif ($item->{type} eq 'journal') {
273         $item->{journal_id} = $item->{srcid};
274     }
275     elsif ($item->{type} eq 'submission') {
276         $item->{subid} = $item->{srcid};
277         $item->{submission_id} = $item->{srcid};
278     }
279
280     # no public flag given, public is 'yes'
281     $item->{public} = 'yes' if !$item->{public};
282
283     #my $max_weight = 0;
284     #for my $t (@{$item->{topics}}) {
285     #    if ($t->{tid} == $item->{tid}) {
286     #        $item->{primary_topic} = $t;
287      #   }
288         #if ($t->{weight} && $t->{weight} > $max_weight) {
289         #    $max_weight = $t->{weight};
290         #}
291     #}
292 }
293
294
295 1;