Path: blob/master/src/applications/conpherence/controller/ConpherenceUpdateController.php
12256 views
<?php12final class ConpherenceUpdateController3extends ConpherenceController {45public function handleRequest(AphrontRequest $request) {6$user = $request->getUser();7$conpherence_id = $request->getURIData('id');8if (!$conpherence_id) {9return new Aphront404Response();10}1112$need_participants = false;13$needed_capabilities = array(PhabricatorPolicyCapability::CAN_VIEW);14$action = $request->getStr('action');15switch ($action) {16case ConpherenceUpdateActions::REMOVE_PERSON:17$person_phid = $request->getStr('remove_person');18if ($person_phid != $user->getPHID()) {19$needed_capabilities[] = PhabricatorPolicyCapability::CAN_EDIT;20}21break;22case ConpherenceUpdateActions::ADD_PERSON:23$needed_capabilities[] = PhabricatorPolicyCapability::CAN_EDIT;24break;25case ConpherenceUpdateActions::LOAD:26break;27}28$conpherence = id(new ConpherenceThreadQuery())29->setViewer($user)30->withIDs(array($conpherence_id))31->needParticipants($need_participants)32->requireCapabilities($needed_capabilities)33->executeOne();3435$latest_transaction_id = null;36$response_mode = $request->isAjax() ? 'ajax' : 'redirect';37$error_view = null;38$e_file = array();39$errors = array();40$delete_draft = false;41$xactions = array();42if ($request->isFormPost() || ($action == ConpherenceUpdateActions::LOAD)) {43$editor = id(new ConpherenceEditor())44->setContinueOnNoEffect($request->isContinueRequest())45->setContentSourceFromRequest($request)46->setActor($user);4748switch ($action) {49case ConpherenceUpdateActions::DRAFT:50$draft = PhabricatorDraft::newFromUserAndKey(51$user,52$conpherence->getPHID());53$draft->setDraft($request->getStr('text'));54$draft->replaceOrDelete();55return new AphrontAjaxResponse();56case ConpherenceUpdateActions::JOIN_ROOM:57$xactions[] = id(new ConpherenceTransaction())58->setTransactionType(59ConpherenceThreadParticipantsTransaction::TRANSACTIONTYPE)60->setNewValue(array('+' => array($user->getPHID())));61$delete_draft = true;62$message = $request->getStr('text');63if ($message) {64$message_xactions = $editor->generateTransactionsFromText(65$user,66$conpherence,67$message);68$xactions = array_merge($xactions, $message_xactions);69}70// for now, just redirect back to the conpherence so everything71// will work okay...!72$response_mode = 'redirect';73break;74case ConpherenceUpdateActions::MESSAGE:75$message = $request->getStr('text');76if (strlen($message)) {77$xactions = $editor->generateTransactionsFromText(78$user,79$conpherence,80$message);81$delete_draft = true;82} else {83$action = ConpherenceUpdateActions::LOAD;84$updated = false;85$response_mode = 'ajax';86}87break;88case ConpherenceUpdateActions::ADD_PERSON:89$person_phids = $request->getArr('add_person');90if (!empty($person_phids)) {91$xactions[] = id(new ConpherenceTransaction())92->setTransactionType(93ConpherenceThreadParticipantsTransaction::TRANSACTIONTYPE)94->setNewValue(array('+' => $person_phids));95}96break;97case ConpherenceUpdateActions::REMOVE_PERSON:98if (!$request->isContinueRequest()) {99// do nothing; we'll display a confirmation dialog instead100break;101}102$person_phid = $request->getStr('remove_person');103if ($person_phid) {104$xactions[] = id(new ConpherenceTransaction())105->setTransactionType(106ConpherenceThreadParticipantsTransaction::TRANSACTIONTYPE)107->setNewValue(array('-' => array($person_phid)));108$response_mode = 'go-home';109}110break;111case ConpherenceUpdateActions::LOAD:112$updated = false;113$response_mode = 'ajax';114break;115default:116throw new Exception(pht('Unknown action: %s', $action));117break;118}119120if ($xactions) {121try {122$xactions = $editor->applyTransactions($conpherence, $xactions);123if ($delete_draft) {124$draft = PhabricatorDraft::newFromUserAndKey(125$user,126$conpherence->getPHID());127$draft->delete();128}129} catch (PhabricatorApplicationTransactionNoEffectException $ex) {130return id(new PhabricatorApplicationTransactionNoEffectResponse())131->setCancelURI($this->getApplicationURI($conpherence_id.'/'))132->setException($ex);133}134// xactions had no effect...!135if (empty($xactions)) {136$errors[] = pht(137'That was a non-update. Try cancel.');138}139}140141if ($xactions || ($action == ConpherenceUpdateActions::LOAD)) {142switch ($response_mode) {143case 'ajax':144$latest_transaction_id = $request->getInt('latest_transaction_id');145$content = $this->loadAndRenderUpdates(146$action,147$conpherence_id,148$latest_transaction_id);149return id(new AphrontAjaxResponse())150->setContent($content);151break;152case 'go-home':153$content = array(154'href' => $this->getApplicationURI(),155);156return id(new AphrontAjaxResponse())157->setContent($content);158break;159case 'redirect':160default:161return id(new AphrontRedirectResponse())162->setURI('/'.$conpherence->getMonogram());163break;164}165}166}167168if ($errors) {169$error_view = id(new PHUIInfoView())170->setErrors($errors);171}172173switch ($action) {174case ConpherenceUpdateActions::ADD_PERSON:175$dialog = $this->renderAddPersonDialog($conpherence);176break;177case ConpherenceUpdateActions::REMOVE_PERSON:178$dialog = $this->renderRemovePersonDialog($conpherence);179break;180}181182return183$dialog184->setUser($user)185->setWidth(AphrontDialogView::WIDTH_FORM)186->setSubmitURI($this->getApplicationURI('update/'.$conpherence_id.'/'))187->addSubmitButton()188->addCancelButton($this->getApplicationURI($conpherence->getID().'/'));189190}191192private function renderAddPersonDialog(193ConpherenceThread $conpherence) {194195$request = $this->getRequest();196$user = $request->getUser();197$add_person = $request->getStr('add_person');198199$form = id(new AphrontFormView())200->setUser($user)201->setFullWidth(true)202->appendControl(203id(new AphrontFormTokenizerControl())204->setName('add_person')205->setUser($user)206->setDatasource(new PhabricatorPeopleDatasource()));207208$view = id(new AphrontDialogView())209->setTitle(pht('Add Participants'))210->addHiddenInput('action', 'add_person')211->addHiddenInput(212'latest_transaction_id',213$request->getInt('latest_transaction_id'))214->appendForm($form);215216return $view;217}218219private function renderRemovePersonDialog(220ConpherenceThread $conpherence) {221222$request = $this->getRequest();223$viewer = $request->getUser();224$remove_person = $request->getStr('remove_person');225$participants = $conpherence->getParticipants();226227$removed_user = id(new PhabricatorPeopleQuery())228->setViewer($viewer)229->withPHIDs(array($remove_person))230->executeOne();231if (!$removed_user) {232return new Aphront404Response();233}234235$is_self = ($viewer->getPHID() == $removed_user->getPHID());236$is_last = (count($participants) == 1);237238$test_conpherence = clone $conpherence;239$test_conpherence->attachParticipants(array());240$still_visible = PhabricatorPolicyFilter::hasCapability(241$removed_user,242$test_conpherence,243PhabricatorPolicyCapability::CAN_VIEW);244245$body = array();246247if ($is_self) {248$title = pht('Leave Room');249$body[] = pht(250'Are you sure you want to leave this room?');251} else {252$title = pht('Remove Participant');253$body[] = pht(254'Remove %s from this room?',255phutil_tag('strong', array(), $removed_user->getUsername()));256}257258if ($still_visible) {259if ($is_self) {260$body[] = pht(261'You will be able to rejoin the room later.');262} else {263$body[] = pht(264'They will be able to rejoin the room later.');265}266} else {267if ($is_self) {268if ($is_last) {269$body[] = pht(270'You are the last member, so you will never be able to rejoin '.271'the room.');272} else {273$body[] = pht(274'You will not be able to rejoin the room on your own, but '.275'someone else can invite you later.');276}277} else {278$body[] = pht(279'They will not be able to rejoin the room unless invited '.280'again.');281}282}283284$dialog = id(new AphrontDialogView())285->setTitle($title)286->addHiddenInput('action', 'remove_person')287->addHiddenInput('remove_person', $remove_person)288->addHiddenInput(289'latest_transaction_id',290$request->getInt('latest_transaction_id'))291->addHiddenInput('__continue__', true);292293foreach ($body as $paragraph) {294$dialog->appendParagraph($paragraph);295}296297return $dialog;298}299300private function loadAndRenderUpdates(301$action,302$conpherence_id,303$latest_transaction_id) {304305$need_transactions = false;306switch ($action) {307case ConpherenceUpdateActions::LOAD:308$need_transactions = true;309break;310case ConpherenceUpdateActions::MESSAGE:311case ConpherenceUpdateActions::ADD_PERSON:312$need_transactions = true;313break;314case ConpherenceUpdateActions::REMOVE_PERSON:315default:316break;317318}319$user = $this->getRequest()->getUser();320$conpherence = id(new ConpherenceThreadQuery())321->setViewer($user)322->setAfterTransactionID($latest_transaction_id)323->needProfileImage(true)324->needParticipants(true)325->needTransactions($need_transactions)326->withIDs(array($conpherence_id))327->executeOne();328329$non_update = false;330$participant = $conpherence->getParticipant($user->getPHID());331332if ($need_transactions && $conpherence->getTransactions()) {333$data = ConpherenceTransactionRenderer::renderTransactions(334$user,335$conpherence);336$key = PhabricatorConpherenceColumnMinimizeSetting::SETTINGKEY;337$minimized = $user->getUserSetting($key);338if (!$minimized) {339$participant->markUpToDate($conpherence);340}341} else if ($need_transactions) {342$non_update = true;343$data = array();344} else {345$data = array();346}347$rendered_transactions = idx($data, 'transactions');348$new_latest_transaction_id = idx($data, 'latest_transaction_id');349350$update_uri = $this->getApplicationURI('update/'.$conpherence->getID().'/');351$nav_item = null;352$header = null;353$people_widget = null;354switch ($action) {355case ConpherenceUpdateActions::ADD_PERSON:356$people_widget = id(new ConpherenceParticipantView())357->setUser($user)358->setConpherence($conpherence)359->setUpdateURI($update_uri);360$people_widget = hsprintf('%s', $people_widget->render());361break;362case ConpherenceUpdateActions::REMOVE_PERSON:363default:364break;365}366$data = $conpherence->getDisplayData($user);367$dropdown_query = id(new AphlictDropdownDataQuery())368->setViewer($user);369$dropdown_query->execute();370371$map = ConpherenceRoomSettings::getSoundMap();372$default_receive = ConpherenceRoomSettings::DEFAULT_RECEIVE_SOUND;373$receive_sound = $map[$default_receive]['rsrc'];374$mention_sound = null;375376// Get the user's defaults if logged in377if ($participant) {378$sounds = $this->getSoundForParticipant($user, $participant);379$receive_sound = $sounds[ConpherenceRoomSettings::SOUND_RECEIVE];380$mention_sound = $sounds[ConpherenceRoomSettings::SOUND_MENTION];381}382383$content = array(384'non_update' => $non_update,385'transactions' => hsprintf('%s', $rendered_transactions),386'conpherence_title' => (string)$data['title'],387'latest_transaction_id' => $new_latest_transaction_id,388'nav_item' => $nav_item,389'conpherence_phid' => $conpherence->getPHID(),390'header' => $header,391'people_widget' => $people_widget,392'aphlictDropdownData' => array(393$dropdown_query->getNotificationData(),394$dropdown_query->getConpherenceData(),395),396'sound' => array(397'receive' => $receive_sound,398'mention' => $mention_sound,399),400);401402return $content;403}404405protected function getSoundForParticipant(406PhabricatorUser $user,407ConpherenceParticipant $participant) {408409$sound_key = PhabricatorConpherenceSoundSetting::SETTINGKEY;410$sound_default = $user->getUserSetting($sound_key);411412$settings = $participant->getSettings();413$sounds = idx($settings, 'sounds', array());414$map = PhabricatorConpherenceSoundSetting::getDefaultSound($sound_default);415416$receive = idx($sounds,417ConpherenceRoomSettings::SOUND_RECEIVE,418$map[ConpherenceRoomSettings::SOUND_RECEIVE]);419$mention = idx($sounds,420ConpherenceRoomSettings::SOUND_MENTION,421$map[ConpherenceRoomSettings::SOUND_MENTION]);422423$sound_map = ConpherenceRoomSettings::getSoundMap();424425return array(426ConpherenceRoomSettings::SOUND_RECEIVE => $sound_map[$receive]['rsrc'],427ConpherenceRoomSettings::SOUND_MENTION => $sound_map[$mention]['rsrc'],428);429430}431432}433434435