diff --git a/src/applications/conpherence/controller/ConpherenceUpdateController.php b/src/applications/conpherence/controller/ConpherenceUpdateController.php index 3808b75ee8..5f90df92ba 100644 --- a/src/applications/conpherence/controller/ConpherenceUpdateController.php +++ b/src/applications/conpherence/controller/ConpherenceUpdateController.php @@ -1,478 +1,483 @@ getUser(); $conpherence_id = $request->getURIData('id'); if (!$conpherence_id) { return new Aphront404Response(); } + $need_participants = false; $needed_capabilities = array(PhabricatorPolicyCapability::CAN_VIEW); $action = $request->getStr('action', ConpherenceUpdateActions::METADATA); switch ($action) { case ConpherenceUpdateActions::REMOVE_PERSON: $person_phid = $request->getStr('remove_person'); if ($person_phid != $user->getPHID()) { $needed_capabilities[] = PhabricatorPolicyCapability::CAN_EDIT; } break; case ConpherenceUpdateActions::ADD_PERSON: case ConpherenceUpdateActions::METADATA: $needed_capabilities[] = PhabricatorPolicyCapability::CAN_EDIT; break; case ConpherenceUpdateActions::JOIN_ROOM: $needed_capabilities[] = PhabricatorPolicyCapability::CAN_JOIN; break; + case ConpherenceUpdateActions::NOTIFICATIONS: + $need_participants = true; + break; + case ConpherenceUpdateActions::LOAD: + break; } $conpherence = id(new ConpherenceThreadQuery()) ->setViewer($user) ->withIDs(array($conpherence_id)) ->needFilePHIDs(true) - ->needParticipantCache(true) + ->needParticipants($need_participants) ->requireCapabilities($needed_capabilities) ->executeOne(); $latest_transaction_id = null; $response_mode = $request->isAjax() ? 'ajax' : 'redirect'; $error_view = null; $e_file = array(); $errors = array(); $delete_draft = false; $xactions = array(); if ($request->isFormPost() || ($action == ConpherenceUpdateActions::LOAD)) { $editor = id(new ConpherenceEditor()) ->setContinueOnNoEffect($request->isContinueRequest()) ->setContentSourceFromRequest($request) ->setActor($user); switch ($action) { case ConpherenceUpdateActions::DRAFT: $draft = PhabricatorDraft::newFromUserAndKey( $user, $conpherence->getPHID()); $draft->setDraft($request->getStr('text')); $draft->replaceOrDelete(); return new AphrontAjaxResponse(); case ConpherenceUpdateActions::JOIN_ROOM: $xactions[] = id(new ConpherenceTransaction()) ->setTransactionType( ConpherenceTransactionType::TYPE_PARTICIPANTS) ->setNewValue(array('+' => array($user->getPHID()))); $delete_draft = true; $message = $request->getStr('text'); if ($message) { $message_xactions = $editor->generateTransactionsFromText( $user, $conpherence, $message); $xactions = array_merge($xactions, $message_xactions); } // for now, just redirect back to the conpherence so everything // will work okay...! $response_mode = 'redirect'; break; case ConpherenceUpdateActions::MESSAGE: $message = $request->getStr('text'); if (strlen($message)) { $xactions = $editor->generateTransactionsFromText( $user, $conpherence, $message); $delete_draft = true; } else { $action = ConpherenceUpdateActions::LOAD; $updated = false; $response_mode = 'ajax'; } break; case ConpherenceUpdateActions::ADD_PERSON: $person_phids = $request->getArr('add_person'); if (!empty($person_phids)) { $xactions[] = id(new ConpherenceTransaction()) ->setTransactionType( ConpherenceTransactionType::TYPE_PARTICIPANTS) ->setNewValue(array('+' => $person_phids)); } break; case ConpherenceUpdateActions::REMOVE_PERSON: if (!$request->isContinueRequest()) { // do nothing; we'll display a confirmation dialogue instead break; } $person_phid = $request->getStr('remove_person'); if ($person_phid && $person_phid == $user->getPHID()) { $xactions[] = id(new ConpherenceTransaction()) ->setTransactionType( ConpherenceTransactionType::TYPE_PARTICIPANTS) ->setNewValue(array('-' => array($person_phid))); $response_mode = 'go-home'; } break; case ConpherenceUpdateActions::NOTIFICATIONS: $notifications = $request->getStr('notifications'); $participant = $conpherence->getParticipantIfExists($user->getPHID()); if (!$participant) { return id(new Aphront404Response()); } $participant->setSettings(array('notifications' => $notifications)); $participant->save(); $result = pht( 'Updated notification settings to "%s".', ConpherenceSettings::getHumanString($notifications)); return id(new AphrontAjaxResponse()) ->setContent($result); break; case ConpherenceUpdateActions::METADATA: // all metadata updates are continue requests if (!$request->isContinueRequest()) { break; } $title = $request->getStr('title'); $xactions[] = id(new ConpherenceTransaction()) ->setTransactionType(ConpherenceTransactionType::TYPE_TITLE) ->setNewValue($title); if ($conpherence->getIsRoom()) { $xactions[] = id(new ConpherenceTransaction()) ->setTransactionType(PhabricatorTransactions::TYPE_VIEW_POLICY) ->setNewValue($request->getStr('viewPolicy')); $xactions[] = id(new ConpherenceTransaction()) ->setTransactionType(PhabricatorTransactions::TYPE_EDIT_POLICY) ->setNewValue($request->getStr('editPolicy')); $xactions[] = id(new ConpherenceTransaction()) ->setTransactionType(PhabricatorTransactions::TYPE_JOIN_POLICY) ->setNewValue($request->getStr('joinPolicy')); } if (!$request->getExists('force_ajax')) { $response_mode = 'redirect'; } break; case ConpherenceUpdateActions::LOAD: $updated = false; $response_mode = 'ajax'; break; default: throw new Exception('Unknown action: '.$action); break; } if ($xactions) { try { $xactions = $editor->applyTransactions($conpherence, $xactions); if ($delete_draft) { $draft = PhabricatorDraft::newFromUserAndKey( $user, $conpherence->getPHID()); $draft->delete(); } } catch (PhabricatorApplicationTransactionNoEffectException $ex) { return id(new PhabricatorApplicationTransactionNoEffectResponse()) ->setCancelURI($this->getApplicationURI($conpherence_id.'/')) ->setException($ex); } // xactions had no effect...! if (empty($xactions)) { $errors[] = pht( 'That was a non-update. Try cancel.'); } } if ($xactions || ($action == ConpherenceUpdateActions::LOAD)) { switch ($response_mode) { case 'ajax': $latest_transaction_id = $request->getInt('latest_transaction_id'); $content = $this->loadAndRenderUpdates( $action, $conpherence_id, $latest_transaction_id); return id(new AphrontAjaxResponse()) ->setContent($content); break; case 'go-home': return id(new AphrontRedirectResponse()) ->setURI($this->getApplicationURI()); break; case 'redirect': default: return id(new AphrontRedirectResponse()) ->setURI($this->getApplicationURI($conpherence->getID().'/')); break; } } } if ($errors) { $error_view = id(new PHUIInfoView()) ->setErrors($errors); } switch ($action) { case ConpherenceUpdateActions::ADD_PERSON: $dialogue = $this->renderAddPersonDialogue($conpherence); break; case ConpherenceUpdateActions::REMOVE_PERSON: $dialogue = $this->renderRemovePersonDialogue($conpherence); break; case ConpherenceUpdateActions::METADATA: default: $dialogue = $this->renderMetadataDialogue($conpherence, $error_view); break; } return id(new AphrontDialogResponse()) ->setDialog($dialogue ->setUser($user) ->setWidth(AphrontDialogView::WIDTH_FORM) ->setSubmitURI($this->getApplicationURI('update/'.$conpherence_id.'/')) ->addSubmitButton() ->addCancelButton($this->getApplicationURI($conpherence->getID().'/'))); } private function renderAddPersonDialogue( ConpherenceThread $conpherence) { $request = $this->getRequest(); $user = $request->getUser(); $add_person = $request->getStr('add_person'); $form = id(new AphrontFormView()) ->setUser($user) ->setFullWidth(true) ->appendControl( id(new AphrontFormTokenizerControl()) ->setName('add_person') ->setUser($user) ->setDatasource(new PhabricatorPeopleDatasource())); require_celerity_resource('conpherence-update-css'); $view = id(new AphrontDialogView()) ->setTitle(pht('Add Participants')) ->addHiddenInput('action', 'add_person') ->addHiddenInput( 'latest_transaction_id', $request->getInt('latest_transaction_id')) ->appendForm($form); if ($request->getExists('minimal_display')) { $view->addHiddenInput('minimal_display', true); } return $view; } private function renderRemovePersonDialogue( ConpherenceThread $conpherence) { $request = $this->getRequest(); $user = $request->getUser(); $remove_person = $request->getStr('remove_person'); $participants = $conpherence->getParticipants(); if ($conpherence->getIsRoom()) { $message = pht( 'Are you sure you want to remove yourself from this room?'); } else { $message = pht( 'Are you sure you want to remove yourself from this thread?'); if (count($participants) == 1) { $message .= pht( 'The thread will be inaccessible forever and ever.'); } else { $message .= pht( 'Someone else in the thread can add you back later.'); } } $body = phutil_tag( 'p', array( ), $message); require_celerity_resource('conpherence-update-css'); return id(new AphrontDialogView()) ->setTitle(pht('Remove Participants')) ->addHiddenInput('action', 'remove_person') ->addHiddenInput('remove_person', $remove_person) ->addHiddenInput( 'latest_transaction_id', $request->getInt('latest_transaction_id')) ->addHiddenInput('__continue__', true) ->appendChild($body); } private function renderMetadataDialogue( ConpherenceThread $conpherence, $error_view) { $request = $this->getRequest(); $user = $request->getUser(); $form = id(new PHUIFormLayoutView()) ->appendChild($error_view) ->appendChild( id(new AphrontFormTextControl()) ->setLabel(pht('Title')) ->setName('title') ->setValue($conpherence->getTitle())); if ($conpherence->getIsRoom()) { $title = pht('Update Room'); $policies = id(new PhabricatorPolicyQuery()) ->setViewer($user) ->setObject($conpherence) ->execute(); $form->appendChild( id(new AphrontFormPolicyControl()) ->setName('viewPolicy') ->setPolicyObject($conpherence) ->setCapability(PhabricatorPolicyCapability::CAN_VIEW) ->setPolicies($policies)) ->appendChild( id(new AphrontFormPolicyControl()) ->setName('editPolicy') ->setPolicyObject($conpherence) ->setCapability(PhabricatorPolicyCapability::CAN_EDIT) ->setPolicies($policies)) ->appendChild( id(new AphrontFormPolicyControl()) ->setName('joinPolicy') ->setPolicyObject($conpherence) ->setCapability(PhabricatorPolicyCapability::CAN_JOIN) ->setPolicies($policies)); } else { $title = pht('Update Thread'); } require_celerity_resource('conpherence-update-css'); $view = id(new AphrontDialogView()) ->setTitle($title) ->addHiddenInput('action', 'metadata') ->addHiddenInput( 'latest_transaction_id', $request->getInt('latest_transaction_id')) ->addHiddenInput('__continue__', true) ->appendChild($form); if ($request->getExists('minimal_display')) { $view->addHiddenInput('minimal_display', true); } if ($request->getExists('force_ajax')) { $view->addHiddenInput('force_ajax', true); } return $view; } private function loadAndRenderUpdates( $action, $conpherence_id, $latest_transaction_id) { $need_widget_data = false; $need_transactions = false; - $need_participant_cache = false; + $need_participant_cache = true; switch ($action) { case ConpherenceUpdateActions::METADATA: - $need_participant_cache = true; $need_transactions = true; break; case ConpherenceUpdateActions::LOAD: $need_transactions = true; break; case ConpherenceUpdateActions::MESSAGE: case ConpherenceUpdateActions::ADD_PERSON: $need_transactions = true; $need_widget_data = true; break; case ConpherenceUpdateActions::REMOVE_PERSON: case ConpherenceUpdateActions::NOTIFICATIONS: default: break; } $user = $this->getRequest()->getUser(); $conpherence = id(new ConpherenceThreadQuery()) ->setViewer($user) ->setAfterTransactionID($latest_transaction_id) ->needParticipantCache($need_participant_cache) ->needWidgetData($need_widget_data) ->needTransactions($need_transactions) ->withIDs(array($conpherence_id)) ->executeOne(); $non_update = false; if ($need_transactions && $conpherence->getTransactions()) { $data = ConpherenceTransactionView::renderTransactions( $user, $conpherence, !$this->getRequest()->getExists('minimal_display')); $participant_obj = $conpherence->getParticipant($user->getPHID()); $participant_obj->markUpToDate($conpherence, $data['latest_transaction']); } else if ($need_transactions) { $non_update = true; $data = array(); } else { $data = array(); } $rendered_transactions = idx($data, 'transactions'); $new_latest_transaction_id = idx($data, 'latest_transaction_id'); $widget_uri = $this->getApplicationURI('update/'.$conpherence->getID().'/'); $nav_item = null; $header = null; $people_widget = null; $file_widget = null; switch ($action) { case ConpherenceUpdateActions::METADATA: $policy_objects = id(new PhabricatorPolicyQuery()) ->setViewer($user) ->setObject($conpherence) ->execute(); $header = $this->buildHeaderPaneContent($conpherence, $policy_objects); $nav_item = id(new ConpherenceThreadListView()) ->setUser($user) ->setBaseURI($this->getApplicationURI()) ->renderSingleThread($conpherence); break; case ConpherenceUpdateActions::MESSAGE: $file_widget = id(new ConpherenceFileWidgetView()) ->setUser($this->getRequest()->getUser()) ->setConpherence($conpherence) ->setUpdateURI($widget_uri); break; case ConpherenceUpdateActions::ADD_PERSON: $people_widget = id(new ConpherencePeopleWidgetView()) ->setUser($user) ->setConpherence($conpherence) ->setUpdateURI($widget_uri); break; case ConpherenceUpdateActions::REMOVE_PERSON: case ConpherenceUpdateActions::NOTIFICATIONS: default: break; } $people_html = null; if ($people_widget) { $people_html = hsprintf('%s', $people_widget->render()); } $data = $conpherence->getDisplayData($user); $content = array( 'non_update' => $non_update, 'transactions' => hsprintf('%s', $rendered_transactions), 'conpherence_title' => (string) $data['title'], 'latest_transaction_id' => $new_latest_transaction_id, 'nav_item' => hsprintf('%s', $nav_item), 'conpherence_phid' => $conpherence->getPHID(), 'header' => hsprintf('%s', $header), 'file_widget' => $file_widget ? $file_widget->render() : null, 'people_widget' => $people_html, ); return $content; } } diff --git a/src/applications/conpherence/query/ConpherenceThreadQuery.php b/src/applications/conpherence/query/ConpherenceThreadQuery.php index 9418f97052..00c2a21460 100644 --- a/src/applications/conpherence/query/ConpherenceThreadQuery.php +++ b/src/applications/conpherence/query/ConpherenceThreadQuery.php @@ -1,375 +1,377 @@ needFilePHIDs = $need_file_phids; return $this; } public function needParticipantCache($participant_cache) { $this->needParticipantCache = $participant_cache; return $this; } public function needWidgetData($need_widget_data) { $this->needWidgetData = $need_widget_data; return $this; } public function needTransactions($need_transactions) { $this->needTransactions = $need_transactions; return $this; } public function withIDs(array $ids) { $this->ids = $ids; return $this; } public function withPHIDs(array $phids) { $this->phids = $phids; return $this; } public function withParticipantPHIDs(array $phids) { $this->participantPHIDs = $phids; return $this; } public function withIsRoom($bool) { $this->isRoom = $bool; return $this; } 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; return $this; } public function getTransactionLimit() { return $this->transactionLimit; } protected function loadPage() { $table = new ConpherenceThread(); $conn_r = $table->establishConnection('r'); $data = queryfx_all( $conn_r, 'SELECT conpherence_thread.* FROM %T conpherence_thread %Q %Q %Q %Q %Q', $table->getTableName(), $this->buildJoinClause($conn_r), $this->buildWhereClause($conn_r), $this->buildGroupClause($conn_r), $this->buildOrderClause($conn_r), $this->buildLimitClause($conn_r)); $conpherences = $table->loadAllFromArray($data); if ($conpherences) { $conpherences = mpull($conpherences, null, 'getPHID'); $this->loadParticipantsAndInitHandles($conpherences); if ($this->needParticipantCache) { $this->loadCoreHandles($conpherences, 'getRecentParticipantPHIDs'); - } else if ($this->needWidgetData) { + } + if ($this->needWidgetData) { $this->loadCoreHandles($conpherences, 'getParticipantPHIDs'); } if ($this->needTransactions) { $this->loadTransactionsAndHandles($conpherences); } if ($this->needFilePHIDs || $this->needWidgetData) { $this->loadFilePHIDs($conpherences); } if ($this->needWidgetData) { $this->loadWidgetData($conpherences); } } return $conpherences; } private function buildGroupClause($conn_r) { if ($this->participantPHIDs !== null) { return 'GROUP BY conpherence_thread.id'; } else { return $this->buildApplicationSearchGroupClause($conn_r); } } private function buildJoinClause($conn_r) { $joins = array(); if ($this->participantPHIDs !== null) { $joins[] = qsprintf( $conn_r, 'JOIN %T p ON p.conpherencePHID = conpherence_thread.phid', id(new ConpherenceParticipant())->getTableName()); } $viewer = $this->getViewer(); if ($this->shouldJoinForViewer($viewer)) { $joins[] = qsprintf( $conn_r, 'LEFT JOIN %T v ON v.conpherencePHID = conpherence_thread.phid '. 'AND v.participantPHID = %s', id(new ConpherenceParticipant())->getTableName(), $viewer->getPHID()); } $joins[] = $this->buildApplicationSearchJoinClause($conn_r); return implode(' ', $joins); } private function shouldJoinForViewer(PhabricatorUser $viewer) { if ($viewer->isLoggedIn() && $this->ids === null && $this->phids === null) { return true; } return false; } protected function buildWhereClause($conn_r) { $where = array(); $where[] = $this->buildPagingClause($conn_r); if ($this->ids !== null) { $where[] = qsprintf( $conn_r, 'conpherence_thread.id IN (%Ld)', $this->ids); } if ($this->phids !== null) { $where[] = qsprintf( $conn_r, 'conpherence_thread.phid IN (%Ls)', $this->phids); } if ($this->participantPHIDs !== null) { $where[] = qsprintf( $conn_r, 'p.participantPHID IN (%Ls)', $this->participantPHIDs); } if ($this->isRoom !== null) { $where[] = qsprintf( $conn_r, 'conpherence_thread.isRoom = %d', (int)$this->isRoom); } $viewer = $this->getViewer(); if ($this->shouldJoinForViewer($viewer)) { $where[] = qsprintf( $conn_r, 'conpherence_thread.isRoom = 1 OR v.participantPHID IS NOT NULL'); } else if ($this->phids === null && $this->ids === null) { $where[] = qsprintf( $conn_r, 'conpherence_thread.isRoom = 1'); } return $this->formatWhereClause($where); } private function loadParticipantsAndInitHandles(array $conpherences) { $participants = id(new ConpherenceParticipant()) ->loadAllWhere('conpherencePHID IN (%Ls)', array_keys($conpherences)); $map = mgroup($participants, 'getConpherencePHID'); foreach ($conpherences as $current_conpherence) { $conpherence_phid = $current_conpherence->getPHID(); $conpherence_participants = idx( $map, $conpherence_phid, array()); $conpherence_participants = mpull( $conpherence_participants, null, 'getParticipantPHID'); $current_conpherence->attachParticipants($conpherence_participants); $current_conpherence->attachHandles(array()); } return $this; } 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 PhabricatorHandleQuery()) ->setViewer($this->getViewer()) ->withPHIDs($flat_phids) ->execute(); foreach ($handle_phids as $conpherence_phid => $phids) { $conpherence = $conpherences[$conpherence_phid]; - $conpherence->attachHandles(array_select_keys($handles, $phids)); + $conpherence->attachHandles( + $conpherence->getHandles() + array_select_keys($handles, $phids)); } return $this; } private function loadTransactionsAndHandles(array $conpherences) { $query = id(new ConpherenceTransactionQuery()) ->setViewer($this->getViewer()) ->withObjectPHIDs(array_keys($conpherences)) ->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(); $transactions = mgroup($transactions, 'getObjectPHID'); foreach ($conpherences as $phid => $conpherence) { $current_transactions = idx($transactions, $phid, array()); $handles = array(); foreach ($current_transactions as $transaction) { $handles += $transaction->getHandles(); } $conpherence->attachHandles($conpherence->getHandles() + $handles); $conpherence->attachTransactions($current_transactions); } return $this; } private function loadFilePHIDs(array $conpherences) { $edge_type = PhabricatorObjectHasFileEdgeType::EDGECONST; $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); $epochs = CalendarTimeUtil::getCalendarEventEpochs( $this->getViewer()); $start_epoch = $epochs['start_epoch']; $end_epoch = $epochs['end_epoch']; $statuses = id(new PhabricatorCalendarEventQuery()) ->setViewer($this->getViewer()) ->withInvitedPHIDs($participant_phids) ->withDateRange($start_epoch, $end_epoch) ->execute(); $statuses = mgroup($statuses, 'getUserPHID'); // attached files $files = array(); $file_author_phids = array(); $authors = array(); if ($file_phids) { $files = id(new PhabricatorFileQuery()) ->setViewer($this->getViewer()) ->withPHIDs($file_phids) ->execute(); $files = mpull($files, null, 'getPHID'); $file_author_phids = mpull($files, 'getAuthorPHID', 'getPHID'); $authors = id(new PhabricatorHandleQuery()) ->setViewer($this->getViewer()) ->withPHIDs($file_author_phids) ->execute(); $authors = mpull($authors, null, 'getPHID'); } foreach ($conpherences as $phid => $conpherence) { $participant_phids = array_keys($conpherence->getParticipants()); $statuses = array_select_keys($statuses, $participant_phids); $statuses = array_mergev($statuses); $statuses = msort($statuses, 'getDateFrom'); $conpherence_files = array(); $files_authors = array(); foreach ($conpherence->getFilePHIDs() as $curr_phid) { $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; // 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; } $widget_data = array( 'statuses' => $statuses, 'files' => $conpherence_files, 'files_authors' => $files_authors, ); $conpherence->attachWidgetData($widget_data); } return $this; } public function getQueryApplicationClass() { return 'PhabricatorConpherenceApplication'; } }