Replace "loadDraftAndPublishedComments()" with a Query

Summary: Ref T13513. Continue marching toward coherent query pathways for all access to inline comments.

Test Plan:
  - Viewed a commit and a path within that commit, as a user with unpublished inlines and a different user.
  - Saw appropriate inlines in all cases (published inlines, plus undeleted unpublished inlines authored by the current viewer).
  - Grepped for "loadDraftAndPublishedComments()".

Maniphest Tasks: T13513

Differential Revision: https://secure.phabricator.com/D21228
This commit is contained in:
epriestley
2020-05-07 11:24:08 -07:00
parent c1f1345cc0
commit d0593a5a78
4 changed files with 93 additions and 30 deletions

View File

@@ -5,6 +5,8 @@ abstract class PhabricatorDiffInlineCommentQuery
private $fixedStates;
private $needReplyToComments;
private $visibleComments;
private $publishableComments;
abstract protected function buildInlineCommentWhereClauseParts(
AphrontDatabaseConnection $conn);
@@ -20,6 +22,16 @@ abstract class PhabricatorDiffInlineCommentQuery
return $this;
}
public function withVisibleComments($with_visible) {
$this->visibleComments = $with_visible;
return $this;
}
public function withPublishableComments($with_publishable) {
$this->publishableComments = $with_publishable;
return $this;
}
protected function buildWhereClauseParts(AphrontDatabaseConnection $conn) {
$where = parent::buildWhereClauseParts($conn);
$alias = $this->getPrimaryTableAlias();
@@ -36,6 +48,72 @@ abstract class PhabricatorDiffInlineCommentQuery
$this->fixedStates);
}
$show_published = false;
$show_publishable = false;
if ($this->visibleComments !== null) {
if (!$this->visibleComments) {
throw new Exception(
pht(
'Querying for comments that are not visible is '.
'not supported.'));
}
$show_published = true;
$show_publishable = true;
}
if ($this->publishableComments !== null) {
if (!$this->publishableComments) {
throw new Exception(
pht(
'Querying for comments that are not publishable is '.
'not supported.'));
}
$show_publishable = true;
}
if ($show_publishable || $show_published) {
$clauses = array();
if ($show_published) {
// Published comments are always visible.
$clauses[] = qsprintf(
$conn,
'%T.transactionPHID IS NOT NULL',
$alias);
}
if ($show_publishable) {
$viewer = $this->getViewer();
$viewer_phid = $viewer->getPHID();
// If the viewer has a PHID, unpublished comments they authored and
// have not deleted are visible.
if ($viewer_phid) {
$clauses[] = qsprintf(
$conn,
'%T.authorPHID = %s
AND %T.isDeleted = 0
AND %T.transactionPHID IS NULL ',
$alias,
$viewer_phid,
$alias,
$alias);
}
}
// We can end up with a known-empty query if we (for example) query for
// publishable comments and the viewer is logged-out.
if (!$clauses) {
throw new PhabricatorEmptyQueryException();
}
$where[] = qsprintf(
$conn,
'%LO',
$clauses);
}
return $where;
}