PhabricatorApplicationTransactionView.php 14.9 KB
Newer Older
epriestley's avatar
epriestley committed
1 2 3 4 5 6 7 8 9
<?php

/**
 * @concrete-extensible
 */
class PhabricatorApplicationTransactionView extends AphrontView {

  private $transactions;
  private $engine;
10
  private $showEditActions = true;
11
  private $isPreview;
12
  private $objectPHID;
Chad Little's avatar
Chad Little committed
13
  private $shouldTerminate = false;
14 15
  private $quoteTargetID;
  private $quoteRef;
16
  private $pager;
Fabian Stelzer's avatar
Fabian Stelzer committed
17
  private $renderAsFeed;
18
  private $hideCommentOptions = false;
19
  private $viewData = array();
20

Fabian Stelzer's avatar
Fabian Stelzer committed
21 22 23 24 25
  public function setRenderAsFeed($feed) {
    $this->renderAsFeed = $feed;
    return $this;
  }

26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42
  public function setQuoteRef($quote_ref) {
    $this->quoteRef = $quote_ref;
    return $this;
  }

  public function getQuoteRef() {
    return $this->quoteRef;
  }

  public function setQuoteTargetID($quote_target_id) {
    $this->quoteTargetID = $quote_target_id;
    return $this;
  }

  public function getQuoteTargetID() {
    return $this->quoteTargetID;
  }
43 44 45 46 47 48 49 50 51

  public function setObjectPHID($object_phid) {
    $this->objectPHID = $object_phid;
    return $this;
  }

  public function getObjectPHID() {
    return $this->objectPHID;
  }
52 53 54 55 56

  public function setIsPreview($is_preview) {
    $this->isPreview = $is_preview;
    return $this;
  }
57

58 59 60 61
  public function getIsPreview() {
    return $this->isPreview;
  }

62 63 64 65 66 67 68 69
  public function setShowEditActions($show_edit_actions) {
    $this->showEditActions = $show_edit_actions;
    return $this;
  }

  public function getShowEditActions() {
    return $this->showEditActions;
  }
epriestley's avatar
epriestley committed
70 71 72 73 74 75 76 77 78 79 80 81

  public function setMarkupEngine(PhabricatorMarkupEngine $engine) {
    $this->engine = $engine;
    return $this;
  }

  public function setTransactions(array $transactions) {
    assert_instances_of($transactions, 'PhabricatorApplicationTransaction');
    $this->transactions = $transactions;
    return $this;
  }

82 83 84 85
  public function getTransactions() {
    return $this->transactions;
  }

Chad Little's avatar
Chad Little committed
86 87 88 89 90
  public function setShouldTerminate($term) {
    $this->shouldTerminate = $term;
    return $this;
  }

91 92 93 94 95 96 97 98 99
  public function setPager(AphrontCursorPagerView $pager) {
    $this->pager = $pager;
    return $this;
  }

  public function getPager() {
    return $this->pager;
  }

100 101
  public function setHideCommentOptions($hide_comment_options) {
    $this->hideCommentOptions = $hide_comment_options;
102 103 104
    return $this;
  }

105 106
  public function getHideCommentOptions() {
    return $this->hideCommentOptions;
107 108
  }

109 110
  public function setViewData(array $view_data) {
    $this->viewData = $view_data;
111 112 113
    return $this;
  }

114 115
  public function getViewData() {
    return $this->viewData;
116 117
  }

118
  public function buildEvents($with_hiding = false) {
119
    $user = $this->getUser();
epriestley's avatar
epriestley committed
120

121
    $xactions = $this->transactions;
122

123 124 125
    $xactions = $this->filterHiddenTransactions($xactions);
    $xactions = $this->groupRelatedTransactions($xactions);
    $groups = $this->groupDisplayTransactions($xactions);
126

127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158
    // If the viewer has interacted with this object, we hide things from
    // before their most recent interaction by default. This tends to make
    // very long threads much more manageable, because you don't have to
    // scroll through a lot of history and can focus on just new stuff.

    $show_group = null;

    if ($with_hiding) {
      // Find the most recent comment by the viewer.
      $group_keys = array_keys($groups);
      $group_keys = array_reverse($group_keys);

      // If we would only hide a small number of transactions, don't hide
      // anything. Just don't examine the last few keys. Also, we always
      // want to show the most recent pieces of activity, so don't examine
      // the first few keys either.
      $group_keys = array_slice($group_keys, 2, -2);

      $type_comment = PhabricatorTransactions::TYPE_COMMENT;
      foreach ($group_keys as $group_key) {
        $group = $groups[$group_key];
        foreach ($group as $xaction) {
          if ($xaction->getAuthorPHID() == $user->getPHID() &&
              $xaction->getTransactionType() == $type_comment) {
            // This is the most recent group where the user commented.
            $show_group = $group_key;
            break 2;
          }
        }
      }
    }

159
    $events = array();
160
    $hide_by_default = ($show_group !== null);
Bob Trahan's avatar
Bob Trahan committed
161
    $set_next_page_id = false;
162 163 164 165

    foreach ($groups as $group_key => $group) {
      if ($hide_by_default && ($show_group === $group_key)) {
        $hide_by_default = false;
Bob Trahan's avatar
Bob Trahan committed
166
        $set_next_page_id = true;
167 168
      }

169 170
      $group_event = null;
      foreach ($group as $xaction) {
171
        $event = $this->renderEvent($xaction, $group);
172
        $event->setHideByDefault($hide_by_default);
173 174 175 176
        if (!$group_event) {
          $group_event = $event;
        } else {
          $group_event->addEventToGroup($event);
177
        }
Bob Trahan's avatar
Bob Trahan committed
178 179
        if ($set_next_page_id) {
          $set_next_page_id = false;
180 181 182 183 184
          $pager = $this->getPager();
          if ($pager) {
            $pager->setNextPageID($xaction->getID());
          }
        }
185
      }
186
      $events[] = $group_event;
187

188 189 190 191 192 193
    }

    return $events;
  }

  public function render() {
194
    if (!$this->getObjectPHID()) {
Joshua Spence's avatar
Joshua Spence committed
195
      throw new PhutilInvalidStateException('setObjectPHID');
196 197
    }

198 199 200 201 202 203 204 205 206 207 208
    $view = $this->buildPHUITimelineView();

    if ($this->getShowEditActions()) {
      Javelin::initBehavior('phabricator-transaction-list');
    }

    return $view->render();
  }

  public function buildPHUITimelineView($with_hiding = true) {
    if (!$this->getObjectPHID()) {
Joshua Spence's avatar
Joshua Spence committed
209
      throw new PhutilInvalidStateException('setObjectPHID');
210 211
    }

epriestley's avatar
epriestley committed
212
    $view = id(new PHUITimelineView())
213
      ->setViewer($this->getViewer())
epriestley's avatar
epriestley committed
214 215
      ->setShouldTerminate($this->shouldTerminate)
      ->setQuoteTargetID($this->getQuoteTargetID())
216 217
      ->setQuoteRef($this->getQuoteRef())
      ->setViewData($this->getViewData());
epriestley's avatar
epriestley committed
218

219
    $events = $this->buildEvents($with_hiding);
220
    foreach ($events as $event) {
epriestley's avatar
epriestley committed
221 222
      $view->addEvent($event);
    }
epriestley's avatar
epriestley committed
223

224 225
    if ($this->getPager()) {
      $view->setPager($this->getPager());
226
    }
epriestley's avatar
epriestley committed
227

228
    return $view;
epriestley's avatar
epriestley committed
229
  }
230

231 232 233 234
  public function isTimelineEmpty() {
    return !count($this->buildEvents(true));
  }

235
  protected function getOrBuildEngine() {
236 237 238 239
    if (!$this->engine) {
      $field = PhabricatorApplicationTransactionComment::MARKUP_FIELD_COMMENT;

      $engine = id(new PhabricatorMarkupEngine())
240
        ->setViewer($this->getViewer());
241 242 243 244 245
      foreach ($this->transactions as $xaction) {
        if (!$xaction->hasComment()) {
          continue;
        }
        $engine->addObject($xaction->getComment(), $field);
246
      }
247 248 249
      $engine->process();

      $this->engine = $engine;
250 251
    }

252
    return $this->engine;
253 254
  }

255 256 257 258 259 260
  private function buildChangeDetailsLink(
    PhabricatorApplicationTransaction $xaction) {

    return javelin_tag(
      'a',
      array(
261
        'href' => $xaction->getChangeDetailsURI(),
262
        'sigil' => 'workflow',
263 264 265 266
      ),
      pht('(Show Details)'));
  }

267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282
  private function buildExtraInformationLink(
    PhabricatorApplicationTransaction $xaction) {

    $link = $xaction->renderExtraInformationLink();
    if (!$link) {
      return null;
    }

    return phutil_tag(
      'span',
      array(
        'class' => 'phui-timeline-extra-information',
      ),
      array(" \xC2\xB7  ", $link));
  }

283 284 285 286 287 288 289 290 291 292 293 294 295
  protected function shouldGroupTransactions(
    PhabricatorApplicationTransaction $u,
    PhabricatorApplicationTransaction $v) {
    return false;
  }

  protected function renderTransactionContent(
    PhabricatorApplicationTransaction $xaction) {

    $field = PhabricatorApplicationTransactionComment::MARKUP_FIELD_COMMENT;
    $engine = $this->getOrBuildEngine();
    $comment = $xaction->getComment();

296
    if ($comment) {
297 298 299 300 301 302 303 304 305 306 307 308
      if ($comment->getIsRemoved()) {
        return javelin_tag(
          'span',
          array(
            'class' => 'comment-deleted',
            'sigil' => 'transaction-comment',
            'meta'  => array('phid' => $comment->getTransactionPHID()),
          ),
          pht(
            'This comment was removed by %s.',
            $xaction->getHandle($comment->getAuthorPHID())->renderLink()));
      } else if ($comment->getIsDeleted()) {
309 310 311 312 313 314 315
        return javelin_tag(
          'span',
          array(
            'class' => 'comment-deleted',
            'sigil' => 'transaction-comment',
            'meta'  => array('phid' => $comment->getTransactionPHID()),
          ),
316
          pht('This comment has been deleted.'));
317
      } else if ($xaction->hasComment()) {
318 319 320
        return javelin_tag(
          'span',
          array(
Chad Little's avatar
Chad Little committed
321
            'class' => 'transaction-comment',
322 323 324 325
            'sigil' => 'transaction-comment',
            'meta'  => array('phid' => $comment->getTransactionPHID()),
          ),
          $engine->getOutput($comment, $field));
326 327 328 329
      } else {
        // This is an empty, non-deleted comment. Usually this happens when
        // rendering previews.
        return null;
330 331 332 333 334
      }
    }

    return null;
  }
335

336 337 338 339
  private function filterHiddenTransactions(array $xactions) {
    foreach ($xactions as $key => $xaction) {
      if ($xaction->shouldHide()) {
        unset($xactions[$key]);
340 341 342 343 344 345 346 347 348 349 350 351 352
	continue;
      }

      $phids = $xaction->getRequiredHandlePHIDs();
      $handles = id(new PhabricatorHandleQuery())
        ->setViewer($this->getUser())
	->withPHIDs($phids)
	->execute();
      foreach ($handles as $handle) {
        if ($handle->getPolicyFiltered()) {
	  unset($xactions[$key]);
	  continue 2;
	}
353 354
      }
    }
355

356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398
    return $xactions;
  }

  private function groupRelatedTransactions(array $xactions) {
    $last = null;
    $last_key = null;
    $groups = array();
    foreach ($xactions as $key => $xaction) {
      if ($last && $this->shouldGroupTransactions($last, $xaction)) {
        $groups[$last_key][] = $xaction;
        unset($xactions[$key]);
      } else {
        $last = $xaction;
        $last_key = $key;
      }
    }

    foreach ($xactions as $key => $xaction) {
      $xaction->attachTransactionGroup(idx($groups, $key, array()));
    }

    return $xactions;
  }

  private function groupDisplayTransactions(array $xactions) {
    $groups = array();
    $group = array();
    foreach ($xactions as $xaction) {
      if ($xaction->shouldDisplayGroupWith($group)) {
        $group[] = $xaction;
      } else {
        if ($group) {
          $groups[] = $group;
        }
        $group = array($xaction);
      }
    }

    if ($group) {
      $groups[] = $group;
    }

    foreach ($groups as $key => $group) {
399 400 401
      $results = array();

      // Sort transactions within the group by action strength, then by
epriestley's avatar
epriestley committed
402 403 404
      // chronological order. This makes sure that multiple actions of the
      // same type (like a close, then a reopen) render in the order they
      // were performed.
405 406 407 408 409 410 411 412 413
      $strength_groups = mgroup($group, 'getActionStrength');
      krsort($strength_groups);
      foreach ($strength_groups as $strength_group) {
        foreach (msort($strength_group, 'getID') as $xaction) {
          $results[] = $xaction;
        }
      }

      $groups[$key] = $results;
414 415 416 417 418 419 420
    }

    return $groups;
  }

  private function renderEvent(
    PhabricatorApplicationTransaction $xaction,
421
    array $group) {
422
    $viewer = $this->getViewer();
423

Chad Little's avatar
Chad Little committed
424
    $event = id(new PHUITimelineEventView())
425
      ->setViewer($viewer)
epriestley's avatar
epriestley committed
426
      ->setAuthorPHID($xaction->getAuthorPHID())
427 428 429
      ->setTransactionPHID($xaction->getPHID())
      ->setUserHandle($xaction->getHandle($xaction->getAuthorPHID()))
      ->setIcon($xaction->getIcon())
430
      ->setColor($xaction->getColor())
431
      ->setHideCommentOptions($this->getHideCommentOptions())
432
      ->setIsSilent($xaction->getIsSilentTransaction())
433 434
      ->setIsMFA($xaction->getIsMFATransaction())
      ->setIsLockOverride($xaction->getIsLockOverrideTransaction());
435

epriestley's avatar
epriestley committed
436 437 438 439 440
    list($token, $token_removed) = $xaction->getToken();
    if ($token) {
      $event->setToken($token, $token_removed);
    }

441
    if (!$this->shouldSuppressTitle($xaction, $group)) {
Fabian Stelzer's avatar
Fabian Stelzer committed
442 443 444 445 446
      if ($this->renderAsFeed) {
        $title = $xaction->getTitleForFeed();
      } else {
        $title = $xaction->getTitle();
      }
447
      if ($xaction->hasChangeDetails()) {
448
        if (!$this->isPreview) {
449
          $details = $this->buildChangeDetailsLink($xaction);
450 451 452 453 454
          $title = array(
            $title,
            ' ',
            $details,
          );
455 456
        }
      }
457 458 459 460 461 462 463 464

      if (!$this->isPreview) {
        $more = $this->buildExtraInformationLink($xaction);
        if ($more) {
          $title = array($title, ' ', $more);
        }
      }

465 466 467 468 469 470 471 472 473
      $event->setTitle($title);
    }

    if ($this->isPreview) {
      $event->setIsPreview(true);
    } else {
      $event
        ->setDateCreated($xaction->getDateCreated())
        ->setContentSource($xaction->getContentSource())
474
        ->setAnchor($xaction->getID());
475 476
    }

477 478 479
    $transaction_type = $xaction->getTransactionType();
    $comment_type = PhabricatorTransactions::TYPE_COMMENT;
    $is_normal_comment = ($transaction_type == $comment_type);
480

481 482 483 484 485 486 487 488 489 490 491
    if ($this->getShowEditActions() &&
        !$this->isPreview &&
        $is_normal_comment) {

      $has_deleted_comment =
        $xaction->getComment() &&
        $xaction->getComment()->getIsDeleted();

      $has_removed_comment =
        $xaction->getComment() &&
        $xaction->getComment()->getIsRemoved();
492 493

      if ($xaction->getCommentVersion() > 1 && !$has_removed_comment) {
494 495 496
        $event->setIsEdited(true);
      }

497 498 499
      if (!$has_removed_comment) {
        $event->setIsNormalComment(true);
      }
500

501 502 503 504 505 506 507 508 509 510 511
      // If we have a place for quoted text to go and this is a quotable
      // comment, pass the quote target ID to the event view.
      if ($this->getQuoteTargetID()) {
        if ($xaction->hasComment()) {
          if (!$has_removed_comment && !$has_deleted_comment) {
            $event->setQuoteTargetID($this->getQuoteTargetID());
            $event->setQuoteRef($this->getQuoteRef());
          }
        }
      }

512 513 514 515 516 517 518
      $can_edit = PhabricatorPolicyCapability::CAN_EDIT;

      if ($xaction->hasComment() || $has_deleted_comment) {
        $has_edit_capability = PhabricatorPolicyFilter::hasCapability(
          $viewer,
          $xaction,
          $can_edit);
519
        if ($has_edit_capability && !$has_removed_comment) {
520 521
          $event->setIsEditable(true);
        }
522

523 524 525 526 527
        if ($has_edit_capability || $viewer->getIsAdmin()) {
          if (!$has_removed_comment) {
            $event->setIsRemovable(true);
          }
        }
528
      }
529

530
      $can_interact = PhabricatorPolicyFilter::canInteract(
531
        $viewer,
532
        $xaction->getObject());
533
      $event->setCanInteract($can_interact);
534 535
    }

536 537 538
    $comment = $this->renderTransactionContent($xaction);
    if ($comment) {
      $event->appendChild($comment);
539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561
    }

    return $event;
  }

  private function shouldSuppressTitle(
    PhabricatorApplicationTransaction $xaction,
    array $group) {

    // This is a little hard-coded, but we don't have any other reasonable
    // cases for now. Suppress "commented on" if there are other actions in
    // the display group.

    if (count($group) > 1) {
      $type_comment = PhabricatorTransactions::TYPE_COMMENT;
      if ($xaction->getTransactionType() == $type_comment) {
        return true;
      }
    }

    return false;
  }

epriestley's avatar
epriestley committed
562
}