Subscription stream functions

Made two new functions, Subscription::bySubscriber() and
Subscription::bySubscribed(), to get streams of Subscription objects.

Converted Profile::getSubscribers() and Profile::getSubscriptions() to
use these functions.
This commit is contained in:
Evan Prodromou 2010-12-11 10:24:46 -05:00
parent baae319aef
commit 7285bbc93b
2 changed files with 80 additions and 38 deletions

View File

@ -380,54 +380,32 @@ class Profile extends Memcached_DataObject
function getSubscriptions($offset=0, $limit=null) function getSubscriptions($offset=0, $limit=null)
{ {
$qry = $subs = Subscription::bySubscriber($this->id,
'SELECT profile.* ' . $offset,
'FROM profile JOIN subscription ' . $limit);
'ON profile.id = subscription.subscribed ' .
'WHERE subscription.subscriber = %d ' .
'AND subscription.subscribed != subscription.subscriber ' .
'ORDER BY subscription.created DESC ';
if ($offset>0 && !is_null($limit)){ $profiles = array();
if (common_config('db','type') == 'pgsql') {
$qry .= ' LIMIT ' . $limit . ' OFFSET ' . $offset; while ($subs->fetch()) {
} else { $profiles[] = Profile::staticGet($subs->subscribed);
$qry .= ' LIMIT ' . $offset . ', ' . $limit;
}
} }
$profile = new Profile(); return new ArrayWrapper($profiles);
$profile->query(sprintf($qry, $this->id));
return $profile;
} }
function getSubscribers($offset=0, $limit=null) function getSubscribers($offset=0, $limit=null)
{ {
$qry = $subs = Subscription::bySubscribed($this->id,
'SELECT profile.* ' . $offset,
'FROM profile JOIN subscription ' . $limit);
'ON profile.id = subscription.subscriber ' .
'WHERE subscription.subscribed = %d ' .
'AND subscription.subscribed != subscription.subscriber ' .
'ORDER BY subscription.created DESC ';
if ($offset>0 && !is_null($limit)){ $profiles = array();
if ($offset) {
if (common_config('db','type') == 'pgsql') { while ($subs->fetch()) {
$qry .= ' LIMIT ' . $limit . ' OFFSET ' . $offset; $profiles[] = Profile::staticGet($subs->subscriber);
} else {
$qry .= ' LIMIT ' . $offset . ', ' . $limit;
}
}
} }
$profile = new Profile(); return new ArrayWrapper($profiles);
$cnt = $profile->query(sprintf($qry, $this->id));
return $profile;
} }
function getConnectedApps($offset = 0, $limit = null) function getConnectedApps($offset = 0, $limit = null)

View File

@ -264,4 +264,68 @@ class Subscription extends Memcached_DataObject
return $act; return $act;
} }
/**
* Stream of subscriptions with the same subscriber
*
* Useful for showing pages that list subscriptions in reverse
* chronological order. Has offset & limit to make paging
* easy.
*
* @param integer $subscriberId Profile ID of the subscriber
* @param integer $offset Offset from latest
* @param integer $limit Maximum number to fetch
*
* @return Subscription stream of subscriptions; use fetch() to iterate
*/
static function bySubscriber($subscriberId,
$offset = 0,
$limit = PROFILES_PER_PAGE)
{
$sub = new Subscription();
$sub->subscriber = $subscriberId;
$sub->whereAdd('subscribed != ' . $subscriberId);
$sub->orderBy('created DESC');
$sub->limit($offset, $limit);
$sub->find();
return $sub;
}
/**
* Stream of subscriptions with the same subscribed profile
*
* Useful for showing pages that list subscribers in reverse
* chronological order. Has offset & limit to make paging
* easy.
*
* @param integer $subscribedId Profile ID of the subscribed
* @param integer $offset Offset from latest
* @param integer $limit Maximum number to fetch
*
* @return Subscription stream of subscriptions; use fetch() to iterate
*/
static function bySubscribed($subscribedId,
$offset = 0,
$limit = PROFILES_PER_PAGE)
{
$sub = new Subscription();
$sub->subscribed = $subscribedId;
$sub->whereAdd('subscriber != ' . $subscribedId);
$sub->orderBy('created DESC');
$sub->limit($offset, $limit);
$sub->find();
return $sub;
}
} }