Path: blob/master/src/applications/conpherence/xaction/ConpherenceThreadParticipantsTransaction.php
12256 views
<?php12final class ConpherenceThreadParticipantsTransaction3extends ConpherenceThreadTransactionType {45const TRANSACTIONTYPE = 'participants';67public function generateOldValue($object) {8return $object->getParticipantPHIDs();9}1011public function generateNewValue($object, $value) {12$old = $this->generateOldValue($object);13return $this->getPHIDList($old, $value);14}1516public function applyExternalEffects($object, $value) {17$participants = $object->getParticipants();1819$old = array_keys($participants);20$new = $value;2122$add_map = array_fuse(array_diff($new, $old));23$rem_map = array_fuse(array_diff($old, $new));2425foreach ($rem_map as $phid) {26$remove_participant = $participants[$phid];27$remove_participant->delete();28unset($participants[$phid]);29}3031foreach ($add_map as $phid) {32if (isset($participants[$phid])) {33continue;34}3536$participants[$phid] = id(new ConpherenceParticipant())37->setConpherencePHID($object->getPHID())38->setParticipantPHID($phid)39->setSeenMessageCount(0)40->save();41}4243$object->attachParticipants($participants);44}4546public function getTitle() {47$old = $this->getOldValue();48$new = $this->getNewValue();4950$add = array_diff($new, $old);51$rem = array_diff($old, $new);5253$author_phid = $this->getAuthorPHID();5455if ($add && $rem) {56return pht(57'%s edited participant(s), added %d: %s; removed %d: %s.',58$this->renderAuthor(),59count($add),60$this->renderHandleList($add),61count($rem),62$this->renderHandleList($rem));63} else if ((in_array($author_phid, $add)) && (count($add) == 1)) {64return pht(65'%s joined the room.',66$this->renderAuthor());67} else if ((in_array($author_phid, $rem)) && (count($rem) == 1)) {68return pht(69'%s left the room.',70$this->renderAuthor());71} else if ($add) {72return pht(73'%s added %d participant(s): %s.',74$this->renderAuthor(),75count($add),76$this->renderHandleList($add));77} else {78return pht(79'%s removed %d participant(s): %s.',80$this->renderAuthor(),81count($rem),82$this->renderHandleList($rem));83}84}8586public function validateTransactions($object, array $xactions) {87$errors = array();8889foreach ($xactions as $xaction) {90$old = $object->getParticipantPHIDs();9192$new = $xaction->getNewValue();93$new = $this->getPHIDList($old, $new);9495$add_map = array_fuse(array_diff($new, $old));96$rem_map = array_fuse(array_diff($old, $new));9798foreach ($add_map as $user_phid) {99$user = id(new PhabricatorPeopleQuery())100->setViewer($this->getActor())101->withPHIDs(array($user_phid))102->executeOne();103if (!$user) {104$errors[] = $this->newInvalidError(105pht(106'Participant PHID "%s" is not a valid user PHID.',107$user_phid));108continue;109}110}111}112113return $errors;114}115116public function getRequiredCapabilities(117$object,118PhabricatorApplicationTransaction $xaction) {119120$old_map = array_fuse($xaction->getOldValue());121$new_map = array_fuse($xaction->getNewValue());122123$add = array_keys(array_diff_key($new_map, $old_map));124$rem = array_keys(array_diff_key($old_map, $new_map));125126$actor_phid = $this->getActingAsPHID();127128$is_join = (($add === array($actor_phid)) && !$rem);129$is_leave = (($rem === array($actor_phid)) && !$add);130131if ($is_join) {132// Anyone can join a thread they can see.133return null;134}135136if ($is_leave) {137// Anyone can leave a thread.138return null;139}140141// You need CAN_EDIT to add or remove participants. For additional142// discussion, see D17696 and T4411.143return PhabricatorPolicyCapability::CAN_EDIT;144}145146}147148149