Add support for since_id and before_id to Twitter API. Ticket #540.

darcs-hash:20081001001233-e558a-3fcc269985050021ec9b44c052206c731cc4689d.gz
This commit is contained in:
mac65 2008-09-30 20:12:33 -04:00
parent 1c4f7722da
commit c08a67094c
3 changed files with 131 additions and 15 deletions

View File

@ -57,7 +57,22 @@ class TwitapistatusesAction extends TwitterapiAction {
// FIXME: To really live up to the spec we need to build a list // FIXME: To really live up to the spec we need to build a list
// of notices by users who have custom avatars, so fix this SQL -- Zach // of notices by users who have custom avatars, so fix this SQL -- Zach
$notice = Notice::publicStream(0, $MAX_PUBSTATUSES); $page = $this->arg('page');
$since_id = $this->arg('since_id');
$before_id = $this->arg('before_id');
// NOTE: page, since_id, and before_id are extensions to Twitter API -- TB
if (!$page) {
$page = 1;
}
if (!$since_id) {
$since_id = 0;
}
if (!$before_id) {
$before_id = 0;
}
$notice = Notice::publicStream((($page-1)*$MAX_PUBSTATUSES), $MAX_PUBSTATUSES, $since_id, $before_id);
if ($notice) { if ($notice) {
@ -112,16 +127,26 @@ class TwitapistatusesAction extends TwitterapiAction {
$since = $this->arg('since'); $since = $this->arg('since');
$since_id = $this->arg('since_id'); $since_id = $this->arg('since_id');
$count = $this->arg('count'); $count = $this->arg('count');
$page = $this->arg('page'); $page = $this->arg('page');
$before_id = $this->arg('before_id');
if (!$page) { if (!$page) {
$page = 1; $page = 1;
} }
if (!$count) { if (!$count) {
$count = 20; $count = 20;
} }
if (!$since_id) {
$since_id = 0;
}
// NOTE: before_id is an extensions to Twitter API -- TB
if (!$before_id) {
$before_id = 0;
}
$user = $this->get_user($id, $apidata); $user = $this->get_user($id, $apidata);
$profile = $user->getProfile(); $profile = $user->getProfile();
@ -133,7 +158,7 @@ class TwitapistatusesAction extends TwitterapiAction {
$link = common_local_url('all', array('nickname' => $user->nickname)); $link = common_local_url('all', array('nickname' => $user->nickname));
$subtitle = sprintf(_('Updates from %1$s and friends on %2$s!'), $user->nickname, $sitename); $subtitle = sprintf(_('Updates from %1$s and friends on %2$s!'), $user->nickname, $sitename);
$notice = $user->noticesWithFriends(($page-1)*20, $count); $notice = $user->noticesWithFriends(($page-1)*20, $count, $since_id, $before_id);
switch($apidata['content-type']) { switch($apidata['content-type']) {
case 'xml': case 'xml':
@ -215,8 +240,9 @@ class TwitapistatusesAction extends TwitterapiAction {
$count = $this->arg('count'); $count = $this->arg('count');
$since = $this->arg('since'); $since = $this->arg('since');
$since_id = $this->arg('since_id'); $since_id = $this->arg('since_id');
$page = $this->arg('page'); $page = $this->arg('page');
$before_id = $this->arg('before_id');
if (!$page) { if (!$page) {
$page = 1; $page = 1;
@ -226,6 +252,15 @@ class TwitapistatusesAction extends TwitterapiAction {
$count = 20; $count = 20;
} }
if (!$since_id) {
$since_id = 0;
}
// NOTE: before_id is an extensions to Twitter API -- TB
if (!$before_id) {
$before_id = 0;
}
$sitename = common_config('site', 'name'); $sitename = common_config('site', 'name');
$siteserver = common_config('site', 'server'); $siteserver = common_config('site', 'server');
@ -235,9 +270,8 @@ class TwitapistatusesAction extends TwitterapiAction {
$subtitle = sprintf(_('Updates from %1$s on %2$s!'), $user->nickname, $sitename); $subtitle = sprintf(_('Updates from %1$s on %2$s!'), $user->nickname, $sitename);
# XXX: since # XXX: since
# XXX: since_id
$notice = $user->getNotices((($page-1)*20), $count); $notice = $user->getNotices((($page-1)*20), $count, $since_id, $before_id);
switch($apidata['content-type']) { switch($apidata['content-type']) {
case 'xml': case 'xml':
@ -354,6 +388,8 @@ class TwitapistatusesAction extends TwitterapiAction {
$count = $this->arg('count'); $count = $this->arg('count');
$page = $this->arg('page'); $page = $this->arg('page');
$since_id = $this->arg('since_id');
$before_id = $this->arg('before_id');
$user = $apidata['user']; $user = $apidata['user'];
$profile = $user->getProfile(); $profile = $user->getProfile();
@ -374,7 +410,15 @@ class TwitapistatusesAction extends TwitterapiAction {
$count = 20; $count = 20;
} }
$notice = $user->getReplies((($page-1)*20), $count); if (!$since_id) {
$since_id = 0;
}
// NOTE: before_id is an extensions to Twitter API -- TB
if (!$before_id) {
$before_id = 0;
}
$notice = $user->getReplies((($page-1)*20), $count, $since_id, $before_id);
$notices = array(); $notices = array();
while ($notice->fetch()) { while ($notice->fetch()) {

View File

@ -308,12 +308,38 @@ class Notice extends Memcached_DataObject
return $wrapper; return $wrapper;
} }
function publicStream($offset=0, $limit=20) { function publicStream($offset=0, $limit=20, $since_id=0, $before_id=0) {
$needAnd = FALSE;
$needWhere = TRUE;
$qry = 'SELECT * FROM notice '; $qry = 'SELECT * FROM notice ';
if (common_config('public', 'localonly')) { if (common_config('public', 'localonly')) {
$qry .= ' WHERE is_local = 1 '; $qry .= ' WHERE is_local = 1 ';
$needWhere = FALSE;
$needAnd = TRUE;
}
// NOTE: since_id and before_id are extensions to Twitter API
if ($since_id > 0) {
if ($needWhere)
$qry .= ' WHERE ';
if ($needAnd)
$qry .= ' AND ';
$qry .= ' notice.id > ' . $since_id . ' ';
$needAnd = FALSE;
$needWhere = FALSE;
}
if ($before_id > 0) {
if ($needWhere)
$qry .= ' WHERE ';
if ($needAnd)
$qry .= ' AND ';
$qry .= ' notice.id < ' . $before_id . ' ';
$needAnd = FALSE;
$needWhere = FALSE;
} }
return Notice::getStream($qry, return Notice::getStream($qry,

View File

@ -141,6 +141,19 @@ class User extends Memcached_DataObject
return true; return true;
} }
function noticesWithFriendsWindow() {
$notice = new Notice();
$notice->query('SELECT notice.* ' .
'FROM notice JOIN subscription on notice.profile_id = subscription.subscribed ' .
'WHERE subscription.subscriber = ' . $this->id . ' ' .
'ORDER BY created DESC, notice.id DESC ' .
'LIMIT 0, ' . WITHFRIENDS_CACHE_WINDOW);
}
static function register($fields) { static function register($fields) {
# MAGICALLY put fields into current scope # MAGICALLY put fields into current scope
@ -291,23 +304,45 @@ class User extends Memcached_DataObject
return $user; return $user;
} }
function getReplies($offset=0, $limit=NOTICES_PER_PAGE) { function getReplies($offset=0, $limit=NOTICES_PER_PAGE, $since_id=0, $before_id=0) {
$qry = $qry =
'SELECT notice.* ' . 'SELECT notice.* ' .
'FROM notice JOIN reply ON notice.id = reply.notice_id ' . 'FROM notice JOIN reply ON notice.id = reply.notice_id ' .
'WHERE reply.profile_id = %d '; 'WHERE reply.profile_id = %d ';
if ($since_id > 0) {
$qry .= ' AND notice.id > ' . $since_id . ' ';
$needAnd = FALSE;
}
// NOTE: before_id is an extension to Twitter API
if ($before_id > 0) {
$qry .= ' AND notice.id < ' . $before_id . ' ';
$needAnd = FALSE;
}
return Notice::getStream(sprintf($qry, $this->id), return Notice::getStream(sprintf($qry, $this->id),
'user:replies:'.$this->id, 'user:replies:'.$this->id,
$offset, $limit); $offset, $limit);
} }
function getNotices($offset=0, $limit=NOTICES_PER_PAGE) { function getNotices($offset=0, $limit=NOTICES_PER_PAGE, $since_id=0, $before_id=0) {
$qry = $qry =
'SELECT * ' . 'SELECT * ' .
'FROM notice ' . 'FROM notice ' .
'WHERE profile_id = %d '; 'WHERE profile_id = %d ';
if ($since_id > 0) {
$qry .= ' AND notice.id > ' . $since_id . ' ';
$needAnd = FALSE;
}
// NOTE: before_id is an extension to Twitter API
if ($before_id > 0) {
$qry .= ' AND notice.id < ' . $before_id . ' ';
$needAnd = FALSE;
}
return Notice::getStream(sprintf($qry, $this->id), return Notice::getStream(sprintf($qry, $this->id),
'user:notices:'.$this->id, 'user:notices:'.$this->id,
$offset, $limit); $offset, $limit);
@ -324,12 +359,23 @@ class User extends Memcached_DataObject
$offset, $limit); $offset, $limit);
} }
function noticesWithFriends($offset=0, $limit=NOTICES_PER_PAGE) { function noticesWithFriends($offset=0, $limit=NOTICES_PER_PAGE, $since_id=0, $before_id=0) {
$qry = $qry =
'SELECT notice.* ' . 'SELECT notice.* ' .
'FROM notice JOIN subscription ON notice.profile_id = subscription.subscribed ' . 'FROM notice JOIN subscription ON notice.profile_id = subscription.subscribed ' .
'WHERE subscription.subscriber = %d '; 'WHERE subscription.subscriber = %d ';
if ($since_id > 0) {
$qry .= ' AND notice.id > ' . $since_id . ' ';
$needAnd = FALSE;
}
// NOTE: before_id is an extension to Twitter API
if ($before_id > 0) {
$qry .= ' AND notice.id < ' . $before_id . ' ';
$needAnd = FALSE;
}
return Notice::getStream(sprintf($qry, $this->id), return Notice::getStream(sprintf($qry, $this->id),
'user:notices_with_friends:' . $this->id, 'user:notices_with_friends:' . $this->id,
$offset, $limit); $offset, $limit);