1 package Newslash::Model::Timeline;
2 use Newslash::Model::Base -base;
11 my $uid = $params->{uid};
12 my $type = $params->{type} || "global";
14 $type = $params->{type} || "user";
17 if ($type eq "user") {
18 return $self->_select_user($uid, $params);
20 elsif ($type eq "tags") {
21 return $self->_select_tags($uid, $params);
23 elsif ($type eq "user") {
24 return $self->_select_user($uid, $params);
26 elsif ($type eq "friends") {
27 return $self->_select_friends($uid, $params);
29 return $self->_select_global($params);
33 my ($self, $items) = @_;
38 my @globj_ids = map { $_->{globjid} } @$items;
39 my @placeholders = map { "?" } @globj_ids;
40 my $placeholder = join(", ", @placeholders);
42 my $dbh = $self->connect_db;
45 LEFT JOIN tagnames ON tags.tagnameid = tagnames.tagnameid
46 WHERE tags.globjid IN ($placeholder)
49 my $sth = $dbh->prepare($sql);
50 $sth->execute(@globj_ids);
51 my $rs = $sth->fetchall_arrayref({});
58 my $id = $tag->{globjid};
59 $tags_of->{$id} = [] if !$tags_of->{$id};
60 push @{$tags_of->{$id}}, $tag;
63 for my $item (@$items) {
64 my $globj_id = $item->{globjid};
65 $item->{tags} = $tags_of->{$globj_id} || [];
71 # select global timeline
73 my ($self, $params) = @_;
75 my $unique_keys = { id => "id" };
76 my $keys = { popularity => "popularity",
78 my $datetime_keys = { create_time => "createtime",
79 update_time => "last_update",
81 my $timestamp = "createtime";
83 my ($where_clause, $where_values, $unique) = $self->build_where_clause(unique_keys => $unique_keys,
85 datetime_keys => $datetime_keys,
86 timestamp => $timestamp,
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,
93 $where_clause = $where_clause . " AND public = 'yes' AND createtime <= NOW()";
96 $where_clause = "WHERE public = 'yes' AND createtime <= NOW()";
99 if ($orderby_clause) {
100 $orderby_clause = $orderby_clause . ", createtime DESC";
103 $orderby_clause = " ORDER BY createtime DESC";
111 users.nickname AS author
112 FROM (SELECT * FROM firehose
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
123 push @params, @$where_values;
124 push @params, @$orderby_values;
125 push @params, @$limit_values;
128 #warn Dumper @params;
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;
136 my $urls = $self->new_instance_of("Urls");
138 for my $item (@$rs) {
139 $self->_generalize($item);
141 if ($item->{type} eq "feed") {
142 my $url = $urls->select(id => $item->{srcid});
144 $item->{url} = $url->{url};
150 $self->_select_tags($rs);
151 return $rs->[0] if ($rs && $unique);
155 # select user's timelime
157 my ($self, $uid, $params) = @_;
158 my ($limit_clause, $limit_values) = $self->build_limit_clause(params => $params);
161 my $extra_where_clause = "";
162 if (!$params->{show_future}) {
163 $extra_where_clause = $extra_where_clause . " AND firehose.createtime <= NOW() ";
166 # show non-public story?
167 if (!$params->{show_nonpublic} && !$params->{public}) {
168 $extra_where_clause = $extra_where_clause . " AND firehose.public != 'no' ";
175 users.nickname AS author
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 = ?
182 ORDER BY createtime DESC $limit_clause
186 push @params, @$limit_values;
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;
195 for my $item (@$rs) {
196 $self->_generalize($item);
199 $self->_select_tags($rs);
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);
212 users.nickname AS author
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;
224 push @params, @$limit_values;
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;
233 for my $item (@$rs) {
234 $self->_generalize($item);
237 $self->_select_tags($rs);
242 my ($self, $item) = @_;
245 $item->{id} = $item->{srcid};
246 $item->{create_time} = $item->{createtime};
247 $item->{update_time} = $item->{last_update};
250 for my $k (qw(tid keyword textname series
251 image witdh height submittable
252 searchable storypickable usersprite)) {
253 $topic->{$k} = $item->{$k};
256 $item->{topics} = [$topic];
257 $item->{primary_topic} = $topic;
259 $item->{content_type} = $item->{type};
260 $item->{intro_text} = $item->{introtext};
261 $item->{body_text} = $item->{bodytext};
262 $item->{discussion_id} = $item->{discussion};
264 if ($item->{type} eq 'comment') {
265 $item->{cid} = $item->{srcid};
266 $item->{comment_id} = $item->{srcid};
268 elsif ($item->{type} eq 'story') {
269 $item->{stoid} = $item->{srcid};
270 $item->{story_id} = $item->{srcid};
272 elsif ($item->{type} eq 'journal') {
273 $item->{journal_id} = $item->{srcid};
275 elsif ($item->{type} eq 'submission') {
276 $item->{subid} = $item->{srcid};
277 $item->{submission_id} = $item->{srcid};
280 # no public flag given, public is 'yes'
281 $item->{public} = 'yes' if !$item->{public};
284 #for my $t (@{$item->{topics}}) {
285 # if ($t->{tid} == $item->{tid}) {
286 # $item->{primary_topic} = $t;
288 #if ($t->{weight} && $t->{weight} > $max_weight) {
289 # $max_weight = $t->{weight};