Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
freebsd
GitHub Repository: freebsd/phabricator
Path: blob/master/src/applications/feed/conduit/FeedQueryConduitAPIMethod.php
12241 views
1
<?php
2
3
final class FeedQueryConduitAPIMethod extends FeedConduitAPIMethod {
4
5
public function getAPIMethodName() {
6
return 'feed.query';
7
}
8
9
public function getMethodStatus() {
10
return self::METHOD_STATUS_UNSTABLE;
11
}
12
13
public function getMethodDescription() {
14
return pht('Query the feed for stories');
15
}
16
17
private function getDefaultLimit() {
18
return 100;
19
}
20
21
protected function defineParamTypes() {
22
return array(
23
'filterPHIDs' => 'optional list <phid>',
24
'limit' => 'optional int (default '.$this->getDefaultLimit().')',
25
'after' => 'optional int',
26
'before' => 'optional int',
27
'view' => 'optional string (data, html, html-summary, text)',
28
);
29
}
30
31
private function getSupportedViewTypes() {
32
return array(
33
'html' => pht('Full HTML presentation of story'),
34
'data' => pht('Dictionary with various data of the story'),
35
'html-summary' => pht('Story contains only the title of the story'),
36
'text' => pht('Simple one-line plain text representation of story'),
37
);
38
}
39
40
protected function defineErrorTypes() {
41
42
$view_types = array_keys($this->getSupportedViewTypes());
43
$view_types = implode(', ', $view_types);
44
45
return array(
46
'ERR-UNKNOWN-TYPE' =>
47
pht(
48
'Unsupported view type, possibles are: %s',
49
$view_types),
50
);
51
}
52
53
protected function defineReturnType() {
54
return 'nonempty dict';
55
}
56
57
protected function execute(ConduitAPIRequest $request) {
58
$results = array();
59
$user = $request->getUser();
60
61
$view_type = $request->getValue('view');
62
if (!$view_type) {
63
$view_type = 'data';
64
}
65
66
$query = id(new PhabricatorFeedQuery())
67
->setViewer($user);
68
69
$filter_phids = $request->getValue('filterPHIDs');
70
if ($filter_phids) {
71
$query->withFilterPHIDs($filter_phids);
72
}
73
74
$limit = $request->getValue('limit');
75
if (!$limit) {
76
$limit = $this->getDefaultLimit();
77
}
78
79
$pager = id(new AphrontCursorPagerView())
80
->setPageSize($limit);
81
82
$after = $request->getValue('after');
83
if (strlen($after)) {
84
$pager->setAfterID($after);
85
}
86
87
$before = $request->getValue('before');
88
if (strlen($before)) {
89
$pager->setBeforeID($before);
90
}
91
92
$stories = $query->executeWithCursorPager($pager);
93
94
if ($stories) {
95
foreach ($stories as $story) {
96
97
$story_data = $story->getStoryData();
98
99
$data = null;
100
101
try {
102
$view = $story->renderView();
103
} catch (Exception $ex) {
104
// When stories fail to render, just fail that story.
105
phlog($ex);
106
continue;
107
}
108
109
$view->setEpoch($story->getEpoch());
110
$view->setUser($user);
111
112
switch ($view_type) {
113
case 'html':
114
$data = $view->render();
115
break;
116
case 'html-summary':
117
$data = $view->render();
118
break;
119
case 'data':
120
$data = array(
121
'class' => $story_data->getStoryType(),
122
'epoch' => $story_data->getEpoch(),
123
'authorPHID' => $story_data->getAuthorPHID(),
124
'chronologicalKey' => $story_data->getChronologicalKey(),
125
'data' => $story_data->getStoryData(),
126
);
127
break;
128
case 'text':
129
$data = array(
130
'class' => $story_data->getStoryType(),
131
'epoch' => $story_data->getEpoch(),
132
'authorPHID' => $story_data->getAuthorPHID(),
133
'chronologicalKey' => $story_data->getChronologicalKey(),
134
'objectPHID' => $story->getPrimaryObjectPHID(),
135
'text' => $story->renderText(),
136
);
137
break;
138
default:
139
throw new ConduitException('ERR-UNKNOWN-TYPE');
140
}
141
142
$results[$story_data->getPHID()] = $data;
143
}
144
}
145
146
return $results;
147
}
148
149
}
150
151