Path: blob/master/src/applications/feed/conduit/FeedQueryConduitAPIMethod.php
12241 views
<?php12final class FeedQueryConduitAPIMethod extends FeedConduitAPIMethod {34public function getAPIMethodName() {5return 'feed.query';6}78public function getMethodStatus() {9return self::METHOD_STATUS_UNSTABLE;10}1112public function getMethodDescription() {13return pht('Query the feed for stories');14}1516private function getDefaultLimit() {17return 100;18}1920protected function defineParamTypes() {21return array(22'filterPHIDs' => 'optional list <phid>',23'limit' => 'optional int (default '.$this->getDefaultLimit().')',24'after' => 'optional int',25'before' => 'optional int',26'view' => 'optional string (data, html, html-summary, text)',27);28}2930private function getSupportedViewTypes() {31return array(32'html' => pht('Full HTML presentation of story'),33'data' => pht('Dictionary with various data of the story'),34'html-summary' => pht('Story contains only the title of the story'),35'text' => pht('Simple one-line plain text representation of story'),36);37}3839protected function defineErrorTypes() {4041$view_types = array_keys($this->getSupportedViewTypes());42$view_types = implode(', ', $view_types);4344return array(45'ERR-UNKNOWN-TYPE' =>46pht(47'Unsupported view type, possibles are: %s',48$view_types),49);50}5152protected function defineReturnType() {53return 'nonempty dict';54}5556protected function execute(ConduitAPIRequest $request) {57$results = array();58$user = $request->getUser();5960$view_type = $request->getValue('view');61if (!$view_type) {62$view_type = 'data';63}6465$query = id(new PhabricatorFeedQuery())66->setViewer($user);6768$filter_phids = $request->getValue('filterPHIDs');69if ($filter_phids) {70$query->withFilterPHIDs($filter_phids);71}7273$limit = $request->getValue('limit');74if (!$limit) {75$limit = $this->getDefaultLimit();76}7778$pager = id(new AphrontCursorPagerView())79->setPageSize($limit);8081$after = $request->getValue('after');82if (strlen($after)) {83$pager->setAfterID($after);84}8586$before = $request->getValue('before');87if (strlen($before)) {88$pager->setBeforeID($before);89}9091$stories = $query->executeWithCursorPager($pager);9293if ($stories) {94foreach ($stories as $story) {9596$story_data = $story->getStoryData();9798$data = null;99100try {101$view = $story->renderView();102} catch (Exception $ex) {103// When stories fail to render, just fail that story.104phlog($ex);105continue;106}107108$view->setEpoch($story->getEpoch());109$view->setUser($user);110111switch ($view_type) {112case 'html':113$data = $view->render();114break;115case 'html-summary':116$data = $view->render();117break;118case 'data':119$data = array(120'class' => $story_data->getStoryType(),121'epoch' => $story_data->getEpoch(),122'authorPHID' => $story_data->getAuthorPHID(),123'chronologicalKey' => $story_data->getChronologicalKey(),124'data' => $story_data->getStoryData(),125);126break;127case 'text':128$data = array(129'class' => $story_data->getStoryType(),130'epoch' => $story_data->getEpoch(),131'authorPHID' => $story_data->getAuthorPHID(),132'chronologicalKey' => $story_data->getChronologicalKey(),133'objectPHID' => $story->getPrimaryObjectPHID(),134'text' => $story->renderText(),135);136break;137default:138throw new ConduitException('ERR-UNKNOWN-TYPE');139}140141$results[$story_data->getPHID()] = $data;142}143}144145return $results;146}147148}149150151