2013-01-25 02:23:05 +01:00
|
|
|
<?php
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @group conpherence
|
|
|
|
*/
|
|
|
|
final class ConpherenceThreadQuery
|
|
|
|
extends PhabricatorCursorPagedPolicyAwareQuery {
|
|
|
|
|
2013-04-19 01:56:38 +02:00
|
|
|
const TRANSACTION_LIMIT = 100;
|
2013-04-19 01:54:06 +02:00
|
|
|
|
2013-01-25 02:23:05 +01:00
|
|
|
private $phids;
|
|
|
|
private $ids;
|
|
|
|
private $needWidgetData;
|
2013-04-08 20:13:35 +02:00
|
|
|
private $needTransactions;
|
|
|
|
private $needParticipantCache;
|
|
|
|
private $needFilePHIDs;
|
2013-04-19 01:54:06 +02:00
|
|
|
private $afterTransactionID;
|
|
|
|
private $beforeTransactionID;
|
|
|
|
private $transactionLimit;
|
2013-02-06 23:03:52 +01:00
|
|
|
|
2013-04-08 20:13:35 +02:00
|
|
|
public function needFilePHIDs($need_file_phids) {
|
|
|
|
$this->needFilePHIDs = $need_file_phids;
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function needParticipantCache($participant_cache) {
|
|
|
|
$this->needParticipantCache = $participant_cache;
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
2013-01-25 02:23:05 +01:00
|
|
|
public function needWidgetData($need_widget_data) {
|
|
|
|
$this->needWidgetData = $need_widget_data;
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
2013-04-08 20:13:35 +02:00
|
|
|
public function needTransactions($need_transactions) {
|
|
|
|
$this->needTransactions = $need_transactions;
|
2013-04-02 15:44:55 +02:00
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
2013-01-25 02:23:05 +01:00
|
|
|
public function withIDs(array $ids) {
|
|
|
|
$this->ids = $ids;
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function withPHIDs(array $phids) {
|
|
|
|
$this->phids = $phids;
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
2013-04-19 01:54:06 +02:00
|
|
|
public function setAfterTransactionID($id) {
|
|
|
|
$this->afterTransactionID = $id;
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function setBeforeTransactionID($id) {
|
|
|
|
$this->beforeTransactionID = $id;
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function setTransactionLimit($transaction_limit) {
|
|
|
|
$this->transactionLimit = $transaction_limit;
|
2013-04-02 19:34:24 +02:00
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
2013-04-19 01:54:06 +02:00
|
|
|
public function getTransactionLimit() {
|
|
|
|
return $this->transactionLimit;
|
|
|
|
}
|
|
|
|
|
2013-03-01 20:28:02 +01:00
|
|
|
protected function loadPage() {
|
2013-01-25 02:23:05 +01:00
|
|
|
$table = new ConpherenceThread();
|
|
|
|
$conn_r = $table->establishConnection('r');
|
|
|
|
|
|
|
|
$data = queryfx_all(
|
|
|
|
$conn_r,
|
|
|
|
'SELECT conpherence_thread.* FROM %T conpherence_thread %Q %Q %Q',
|
|
|
|
$table->getTableName(),
|
|
|
|
$this->buildWhereClause($conn_r),
|
|
|
|
$this->buildOrderClause($conn_r),
|
|
|
|
$this->buildLimitClause($conn_r));
|
|
|
|
|
|
|
|
$conpherences = $table->loadAllFromArray($data);
|
|
|
|
|
|
|
|
if ($conpherences) {
|
|
|
|
$conpherences = mpull($conpherences, null, 'getPHID');
|
2013-04-08 20:13:35 +02:00
|
|
|
$this->loadParticipantsAndInitHandles($conpherences);
|
|
|
|
if ($this->needParticipantCache) {
|
|
|
|
$this->loadCoreHandles($conpherences, 'getRecentParticipantPHIDs');
|
|
|
|
} else if ($this->needWidgetData) {
|
|
|
|
$this->loadCoreHandles($conpherences, 'getParticipantPHIDs');
|
|
|
|
}
|
|
|
|
if ($this->needTransactions) {
|
2013-04-02 15:44:55 +02:00
|
|
|
$this->loadTransactionsAndHandles($conpherences);
|
|
|
|
}
|
2013-04-08 20:13:35 +02:00
|
|
|
if ($this->needFilePHIDs || $this->needWidgetData) {
|
|
|
|
$this->loadFilePHIDs($conpherences);
|
|
|
|
}
|
2013-01-25 02:23:05 +01:00
|
|
|
if ($this->needWidgetData) {
|
|
|
|
$this->loadWidgetData($conpherences);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return $conpherences;
|
|
|
|
}
|
|
|
|
|
|
|
|
protected function buildWhereClause($conn_r) {
|
|
|
|
$where = array();
|
|
|
|
|
|
|
|
$where[] = $this->buildPagingClause($conn_r);
|
|
|
|
|
|
|
|
if ($this->ids) {
|
|
|
|
$where[] = qsprintf(
|
|
|
|
$conn_r,
|
|
|
|
'id IN (%Ld)',
|
|
|
|
$this->ids);
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($this->phids) {
|
|
|
|
$where[] = qsprintf(
|
|
|
|
$conn_r,
|
|
|
|
'phid IN (%Ls)',
|
|
|
|
$this->phids);
|
|
|
|
}
|
|
|
|
|
|
|
|
return $this->formatWhereClause($where);
|
|
|
|
}
|
|
|
|
|
2013-04-08 20:13:35 +02:00
|
|
|
private function loadParticipantsAndInitHandles(array $conpherences) {
|
2013-01-25 02:23:05 +01:00
|
|
|
$participants = id(new ConpherenceParticipant())
|
|
|
|
->loadAllWhere('conpherencePHID IN (%Ls)', array_keys($conpherences));
|
|
|
|
$map = mgroup($participants, 'getConpherencePHID');
|
|
|
|
foreach ($map as $conpherence_phid => $conpherence_participants) {
|
|
|
|
$current_conpherence = $conpherences[$conpherence_phid];
|
|
|
|
$conpherence_participants = mpull(
|
|
|
|
$conpherence_participants,
|
|
|
|
null,
|
2013-02-19 22:33:10 +01:00
|
|
|
'getParticipantPHID');
|
2013-01-25 02:23:05 +01:00
|
|
|
$current_conpherence->attachParticipants($conpherence_participants);
|
2013-04-08 20:13:35 +02:00
|
|
|
$current_conpherence->attachHandles(array());
|
2013-01-25 02:23:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
2013-04-08 20:13:35 +02:00
|
|
|
private function loadCoreHandles(
|
|
|
|
array $conpherences,
|
|
|
|
$method) {
|
|
|
|
|
|
|
|
$handle_phids = array();
|
|
|
|
foreach ($conpherences as $conpherence) {
|
|
|
|
$handle_phids[$conpherence->getPHID()] =
|
|
|
|
$conpherence->$method();
|
|
|
|
}
|
|
|
|
$flat_phids = array_mergev($handle_phids);
|
|
|
|
$handles = id(new PhabricatorObjectHandleData($flat_phids))
|
|
|
|
->setViewer($this->getViewer())
|
|
|
|
->loadHandles();
|
|
|
|
foreach ($handle_phids as $conpherence_phid => $phids) {
|
|
|
|
$conpherence = $conpherences[$conpherence_phid];
|
|
|
|
$conpherence->attachHandles(array_select_keys($handles, $phids));
|
|
|
|
}
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
2013-01-25 02:23:05 +01:00
|
|
|
private function loadTransactionsAndHandles(array $conpherences) {
|
2013-04-19 01:54:06 +02:00
|
|
|
$query = id(new ConpherenceTransactionQuery())
|
2013-01-25 02:23:05 +01:00
|
|
|
->setViewer($this->getViewer())
|
|
|
|
->withObjectPHIDs(array_keys($conpherences))
|
2013-04-19 01:54:06 +02:00
|
|
|
->needHandles(true);
|
|
|
|
|
|
|
|
// We have to flip these for the underyling query class. The semantics of
|
|
|
|
// paging are tricky business.
|
|
|
|
if ($this->afterTransactionID) {
|
|
|
|
$query->setBeforeID($this->afterTransactionID);
|
|
|
|
} else if ($this->beforeTransactionID) {
|
|
|
|
$query->setAfterID($this->beforeTransactionID);
|
|
|
|
}
|
|
|
|
if ($this->getTransactionLimit()) {
|
|
|
|
// fetch an extra for "show older" scenarios
|
|
|
|
$query->setLimit($this->getTransactionLimit() + 1);
|
|
|
|
}
|
|
|
|
$transactions = $query->execute();
|
2013-01-25 02:23:05 +01:00
|
|
|
$transactions = mgroup($transactions, 'getObjectPHID');
|
|
|
|
foreach ($conpherences as $phid => $conpherence) {
|
|
|
|
$current_transactions = $transactions[$phid];
|
|
|
|
$handles = array();
|
|
|
|
foreach ($current_transactions as $transaction) {
|
|
|
|
$handles += $transaction->getHandles();
|
|
|
|
}
|
2013-04-08 20:13:35 +02:00
|
|
|
$conpherence->attachHandles($conpherence->getHandles() + $handles);
|
2013-01-25 02:23:05 +01:00
|
|
|
$conpherence->attachTransactions($transactions[$phid]);
|
|
|
|
}
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
private function loadFilePHIDs(array $conpherences) {
|
|
|
|
$edge_type = PhabricatorEdgeConfig::TYPE_OBJECT_HAS_FILE;
|
|
|
|
$file_edges = id(new PhabricatorEdgeQuery())
|
|
|
|
->withSourcePHIDs(array_keys($conpherences))
|
|
|
|
->withEdgeTypes(array($edge_type))
|
|
|
|
->execute();
|
|
|
|
foreach ($file_edges as $conpherence_phid => $data) {
|
|
|
|
$conpherence = $conpherences[$conpherence_phid];
|
|
|
|
$conpherence->attachFilePHIDs(array_keys($data[$edge_type]));
|
|
|
|
}
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
private function loadWidgetData(array $conpherences) {
|
|
|
|
$participant_phids = array();
|
|
|
|
$file_phids = array();
|
|
|
|
foreach ($conpherences as $conpherence) {
|
|
|
|
$participant_phids[] = array_keys($conpherence->getParticipants());
|
|
|
|
$file_phids[] = $conpherence->getFilePHIDs();
|
|
|
|
}
|
|
|
|
$participant_phids = array_mergev($participant_phids);
|
|
|
|
$file_phids = array_mergev($file_phids);
|
|
|
|
|
|
|
|
// statuses of everyone currently in the conpherence
|
2013-02-05 04:01:46 +01:00
|
|
|
// for a rolling one week window
|
|
|
|
$start_of_week = phabricator_format_local_time(
|
2013-04-04 02:30:20 +02:00
|
|
|
strtotime('last monday', strtotime('tomorrow')),
|
2013-02-05 04:01:46 +01:00
|
|
|
$this->getViewer(),
|
2013-02-19 22:33:10 +01:00
|
|
|
'U');
|
2013-01-25 02:23:05 +01:00
|
|
|
$end_of_week = phabricator_format_local_time(
|
2013-04-04 02:30:20 +02:00
|
|
|
strtotime('last monday +1 week', strtotime('tomorrow')),
|
2013-01-25 02:23:05 +01:00
|
|
|
$this->getViewer(),
|
2013-02-19 22:33:10 +01:00
|
|
|
'U');
|
2013-01-25 02:23:05 +01:00
|
|
|
$statuses = id(new PhabricatorUserStatus())
|
|
|
|
->loadAllWhere(
|
2013-02-05 04:01:46 +01:00
|
|
|
'userPHID in (%Ls) AND dateTo >= %d AND dateFrom <= %d',
|
2013-01-25 02:23:05 +01:00
|
|
|
$participant_phids,
|
2013-02-05 04:01:46 +01:00
|
|
|
$start_of_week,
|
2013-02-19 22:33:10 +01:00
|
|
|
$end_of_week);
|
2013-01-25 02:23:05 +01:00
|
|
|
$statuses = mgroup($statuses, 'getUserPHID');
|
|
|
|
|
|
|
|
// attached files
|
|
|
|
$files = array();
|
2013-03-16 07:41:36 +01:00
|
|
|
$file_author_phids = array();
|
|
|
|
$authors = array();
|
2013-01-25 02:23:05 +01:00
|
|
|
if ($file_phids) {
|
|
|
|
$files = id(new PhabricatorFileQuery())
|
|
|
|
->setViewer($this->getViewer())
|
|
|
|
->withPHIDs($file_phids)
|
|
|
|
->execute();
|
|
|
|
$files = mpull($files, null, 'getPHID');
|
2013-03-16 07:41:36 +01:00
|
|
|
$file_author_phids = mpull($files, 'getAuthorPHID', 'getPHID');
|
|
|
|
$authors = id(new PhabricatorObjectHandleData($file_author_phids))
|
|
|
|
->setViewer($this->getViewer())
|
|
|
|
->loadHandles();
|
|
|
|
$authors = mpull($authors, null, 'getPHID');
|
2013-01-25 02:23:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
foreach ($conpherences as $phid => $conpherence) {
|
|
|
|
$participant_phids = array_keys($conpherence->getParticipants());
|
2013-02-05 04:01:46 +01:00
|
|
|
$statuses = array_select_keys($statuses, $participant_phids);
|
|
|
|
$statuses = array_mergev($statuses);
|
|
|
|
$statuses = msort($statuses, 'getDateFrom');
|
2013-03-16 07:41:36 +01:00
|
|
|
|
|
|
|
$conpherence_files = array();
|
|
|
|
$files_authors = array();
|
|
|
|
foreach ($conpherence->getFilePHIDs() as $curr_phid) {
|
2013-03-17 22:36:22 +01:00
|
|
|
$curr_file = idx($files, $curr_phid);
|
|
|
|
if (!$curr_file) {
|
|
|
|
// this file was deleted or user doesn't have permission to see it
|
|
|
|
// this is generally weird
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
$conpherence_files[$curr_phid] = $curr_file;
|
2013-03-16 07:41:36 +01:00
|
|
|
// some files don't have authors so be careful
|
|
|
|
$current_author = null;
|
|
|
|
$current_author_phid = idx($file_author_phids, $curr_phid);
|
|
|
|
if ($current_author_phid) {
|
|
|
|
$current_author = $authors[$current_author_phid];
|
|
|
|
}
|
|
|
|
$files_authors[$curr_phid] = $current_author;
|
|
|
|
}
|
2013-01-25 02:23:05 +01:00
|
|
|
$widget_data = array(
|
2013-02-05 04:01:46 +01:00
|
|
|
'statuses' => $statuses,
|
2013-03-16 07:41:36 +01:00
|
|
|
'files' => $conpherence_files,
|
|
|
|
'files_authors' => $files_authors
|
2013-01-25 02:23:05 +01:00
|
|
|
);
|
|
|
|
$conpherence->attachWidgetData($widget_data);
|
|
|
|
}
|
|
|
|
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|