Correct Aphlict websocket URI construction after PHP8 compatibility changes
[phabricator.git] / src / applications / transactions / feed / PhabricatorApplicationTransactionFeedStory.php
blob0e8531a22dbcb995994c8cc1fb755c8fb10eb919
1 <?php
3 /**
4 * @concrete-extensible
5 */
6 class PhabricatorApplicationTransactionFeedStory
7 extends PhabricatorFeedStory {
9 private $primaryTransactionPHID;
11 public function getPrimaryObjectPHID() {
12 return $this->getValue('objectPHID');
15 public function getRequiredObjectPHIDs() {
16 return $this->getValue('transactionPHIDs');
19 public function getRequiredHandlePHIDs() {
20 $phids = array();
21 $phids[] = $this->getValue('objectPHID');
22 foreach ($this->getValue('transactionPHIDs') as $xaction_phid) {
23 $xaction = $this->getObject($xaction_phid);
24 foreach ($xaction->getRequiredHandlePHIDs() as $handle_phid) {
25 $phids[] = $handle_phid;
28 return $phids;
31 protected function getPrimaryTransactionPHID() {
32 if ($this->primaryTransactionPHID === null) {
33 // Transactions are filtered and sorted before they're stored, but the
34 // rendering logic can change between the time an edit occurs and when
35 // we actually render the story. Recalculate the filtering at display
36 // time because it's cheap and gets us better results when things change
37 // by letting the changes apply retroactively.
39 $xactions = $this->getTransactions();
41 foreach ($xactions as $key => $xaction) {
42 if ($xaction->shouldHideForFeed()) {
43 unset($xactions[$key]);
47 if ($xactions) {
48 $primary_phid = head($xactions)->getPHID();
49 } else {
50 $primary_phid = head($this->getValue('transactionPHIDs'));
53 $this->primaryTransactionPHID = $primary_phid;
56 return $this->primaryTransactionPHID;
59 public function isVisibleInNotifications() {
60 $xactions = $this->getTransactions();
62 foreach ($xactions as $key => $xaction) {
63 if (!$xaction->shouldHideForNotifications()) {
64 return true;
68 return false;
71 public function isVisibleInFeed() {
72 $xactions = $this->getTransactions();
74 foreach ($xactions as $key => $xaction) {
75 if (!$xaction->shouldHideForFeed()) {
76 return true;
80 return false;
83 private function getTransactions() {
84 $xaction_phids = $this->getValue('transactionPHIDs');
86 $xactions = array();
87 foreach ($xaction_phids as $xaction_phid) {
88 $xactions[] = $this->getObject($xaction_phid);
91 return $xactions;
94 public function getPrimaryTransaction() {
95 return $this->getObject($this->getPrimaryTransactionPHID());
98 public function getFieldStoryMarkupFields() {
99 $xaction_phids = $this->getValue('transactionPHIDs');
101 $fields = array();
102 foreach ($xaction_phids as $xaction_phid) {
103 $xaction = $this->getObject($xaction_phid);
104 foreach ($xaction->getMarkupFieldsForFeed($this) as $field) {
105 $fields[] = $field;
109 return $fields;
112 public function getMarkupText($field) {
113 $xaction_phids = $this->getValue('transactionPHIDs');
115 foreach ($xaction_phids as $xaction_phid) {
116 $xaction = $this->getObject($xaction_phid);
117 foreach ($xaction->getMarkupFieldsForFeed($this) as $xaction_field) {
118 if ($xaction_field == $field) {
119 return $xaction->getMarkupTextForFeed($this, $field);
124 return null;
127 public function renderView() {
128 $view = $this->newStoryView();
130 $handle = $this->getHandle($this->getPrimaryObjectPHID());
131 $view->setHref($handle->getURI());
133 $type = phid_get_type($handle->getPHID());
134 $phid_types = PhabricatorPHIDType::getAllTypes();
135 $icon = null;
136 if (!empty($phid_types[$type])) {
137 $phid_type = $phid_types[$type];
138 $class = $phid_type->getPHIDTypeApplicationClass();
139 if ($class) {
140 $application = PhabricatorApplication::getByClass($class);
141 $icon = $application->getIcon();
145 $view->setAppIcon($icon);
147 $xaction_phids = $this->getValue('transactionPHIDs');
148 $xaction = $this->getPrimaryTransaction();
150 $xaction->setHandles($this->getHandles());
151 $view->setTitle($xaction->getTitleForFeed());
153 foreach ($xaction_phids as $xaction_phid) {
154 $secondary_xaction = $this->getObject($xaction_phid);
155 $secondary_xaction->setHandles($this->getHandles());
157 $body = $secondary_xaction->getBodyForFeed($this);
158 if (nonempty($body)) {
159 $view->appendChild($body);
163 $author_phid = $xaction->getAuthorPHID();
164 $author_handle = $this->getHandle($author_phid);
165 $author_image = $author_handle->getImageURI();
167 if ($author_image) {
168 $view->setImage($author_image);
169 $view->setImageHref($author_handle->getURI());
170 } else {
171 $view->setAuthorIcon($author_handle->getIcon());
174 return $view;
177 public function renderText() {
178 $xaction = $this->getPrimaryTransaction();
179 $old_target = $xaction->getRenderingTarget();
180 $new_target = PhabricatorApplicationTransaction::TARGET_TEXT;
181 $xaction->setRenderingTarget($new_target);
182 $xaction->setHandles($this->getHandles());
183 $text = $xaction->getTitleForFeed();
184 $xaction->setRenderingTarget($old_target);
185 return $text;
188 public function renderTextBody() {
189 $all_bodies = '';
190 $new_target = PhabricatorApplicationTransaction::TARGET_TEXT;
191 $xaction_phids = $this->getValue('transactionPHIDs');
192 foreach ($xaction_phids as $xaction_phid) {
193 $secondary_xaction = $this->getObject($xaction_phid);
194 $old_target = $secondary_xaction->getRenderingTarget();
195 $secondary_xaction->setRenderingTarget($new_target);
196 $secondary_xaction->setHandles($this->getHandles());
198 $body = $secondary_xaction->getBodyForMail();
199 if (nonempty($body)) {
200 $all_bodies .= $body."\n";
202 $secondary_xaction->setRenderingTarget($old_target);
204 return trim($all_bodies);
207 public function getImageURI() {
208 $author_phid = $this->getPrimaryTransaction()->getAuthorPHID();
209 return $this->getHandle($author_phid)->getImageURI();
212 public function getURI() {
213 $handle = $this->getHandle($this->getPrimaryObjectPHID());
214 return PhabricatorEnv::getProductionURI($handle->getURI());
217 public function renderAsTextForDoorkeeper(
218 DoorkeeperFeedStoryPublisher $publisher) {
220 $xactions = array();
221 $xaction_phids = $this->getValue('transactionPHIDs');
222 foreach ($xaction_phids as $xaction_phid) {
223 $xaction = $this->getObject($xaction_phid);
224 $xaction->setHandles($this->getHandles());
225 $xactions[] = $xaction;
228 $primary = $this->getPrimaryTransaction();
229 return $primary->renderAsTextForDoorkeeper($publisher, $this, $xactions);