/** * Get the Nth most recent subscription for this user * * @param User $user The user to get subscriptions for * @param integer $n How far to count back * * @return Subscription a subscription or null */ private function _getNthSub($user, $n) { $sub = new Subscription(); $sub->subscriber = $user->id; $sub->orderBy('created DESC'); $sub->limit($n - 1, 1); if ($sub->find(true)) { return $sub; } else { return null; } }
private static function getSubscriptionIDs($get_type, $profile_id, $offset, $limit) { switch ($get_type) { case 'subscribed': $by_type = 'subscriber'; break; case 'subscriber': $by_type = 'subscribed'; break; default: throw new Exception('Bad type argument to getSubscriptionIDs'); } $cacheKey = 'subscription:by-' . $by_type . ':' . $profile_id; $queryoffset = $offset; $querylimit = $limit; if ($offset + $limit <= self::CACHE_WINDOW) { // Oh, it seems it should be cached $ids = self::cacheGet($cacheKey); if (is_array($ids)) { return array_slice($ids, $offset, $limit); } // Being here indicates we didn't find anything cached // so we'll have to fill it up simultaneously $queryoffset = 0; $querylimit = self::CACHE_WINDOW; } $sub = new Subscription(); $sub->{$by_type} = $profile_id; $sub->selectAdd($get_type); $sub->whereAdd("{$get_type} != {$profile_id}"); $sub->orderBy('created DESC'); $sub->limit($queryoffset, $querylimit); if (!$sub->find()) { return array(); } $ids = $sub->fetchAll($get_type); // If we're simultaneously filling up cache, remember to slice if ($queryoffset === 0 && $querylimit === self::CACHE_WINDOW) { self::cacheSet($cacheKey, $ids); return array_slice($ids, $offset, $limit); } return $ids; }
private static function realBySubscribed($subscribedId, $offset, $limit) { $sub = new Subscription(); $sub->subscribed = $subscribedId; $sub->whereAdd('subscriber != ' . $subscribedId); $sub->orderBy('created DESC'); $sub->limit($offset, $limit); $sub->find(); $subs = array(); while ($sub->fetch()) { $subs[] = clone $sub; } return $subs; }
function subscriptions($apidata, $other_attr, $user_attr, $onlyIDs = false) { $this->auth_user = $apidata['user']; $user = $this->get_user($apidata['api_arg'], $apidata); if (!$user) { $this->clientError('Not Found', 404, $apidata['content-type']); return; } $page = $this->trimmed('page'); if (!$page || !is_numeric($page)) { $page = 1; } $profile = $user->getProfile(); if (!$profile) { $this->serverError(_('User has no profile.')); return; } $sub = new Subscription(); $sub->{$user_attr} = $profile->id; $since = strtotime($this->trimmed('since')); if ($since) { $d = date('Y-m-d H:i:s', $since); $sub->whereAdd("created > '{$d}'"); } $sub->orderBy('created DESC'); if (!$onlyIDs) { $sub->limit(($page - 1) * 100, 100); } $others = array(); if ($sub->find()) { while ($sub->fetch()) { $others[] = Profile::staticGet($sub->{$other_attr}); } } else { // user has no followers } $type = $apidata['content-type']; $this->init_document($type); if ($onlyIDs) { $this->showIDs($others, $type); } else { $this->show_profiles($others, $type); } $this->end_document($type); }