Path: blob/master/src/applications/conpherence/storage/ConpherenceThread.php
12256 views
<?php12final class ConpherenceThread extends ConpherenceDAO3implements4PhabricatorPolicyInterface,5PhabricatorApplicationTransactionInterface,6PhabricatorMentionableInterface,7PhabricatorDestructibleInterface,8PhabricatorNgramsInterface {910protected $title;11protected $topic;12protected $profileImagePHID;13protected $messageCount;14protected $mailKey;15protected $viewPolicy;16protected $editPolicy;17protected $joinPolicy;1819private $participants = self::ATTACHABLE;20private $transactions = self::ATTACHABLE;21private $profileImageFile = self::ATTACHABLE;22private $handles = self::ATTACHABLE;2324public static function initializeNewRoom(PhabricatorUser $sender) {25$default_policy = id(new ConpherenceThreadMembersPolicyRule())26->getObjectPolicyFullKey();27return id(new ConpherenceThread())28->setMessageCount(0)29->setTitle('')30->setTopic('')31->attachParticipants(array())32->setViewPolicy($default_policy)33->setEditPolicy($default_policy)34->setJoinPolicy('');35}3637protected function getConfiguration() {38return array(39self::CONFIG_AUX_PHID => true,40self::CONFIG_COLUMN_SCHEMA => array(41'title' => 'text255?',42'topic' => 'text255',43'messageCount' => 'uint64',44'mailKey' => 'text20',45'joinPolicy' => 'policy',46'profileImagePHID' => 'phid?',47),48self::CONFIG_KEY_SCHEMA => array(49'key_phid' => null,50'phid' => array(51'columns' => array('phid'),52'unique' => true,53),54),55) + parent::getConfiguration();56}5758public function generatePHID() {59return PhabricatorPHID::generateNewPHID(60PhabricatorConpherenceThreadPHIDType::TYPECONST);61}6263public function save() {64if (!$this->getMailKey()) {65$this->setMailKey(Filesystem::readRandomCharacters(20));66}67return parent::save();68}6970public function getMonogram() {71return 'Z'.$this->getID();72}7374public function getURI() {75return '/'.$this->getMonogram();76}7778public function attachParticipants(array $participants) {79assert_instances_of($participants, 'ConpherenceParticipant');80$this->participants = $participants;81return $this;82}8384public function getParticipants() {85return $this->assertAttached($this->participants);86}8788public function getParticipant($phid) {89$participants = $this->getParticipants();90return $participants[$phid];91}9293public function getParticipantIfExists($phid, $default = null) {94$participants = $this->getParticipants();95return idx($participants, $phid, $default);96}9798public function getParticipantPHIDs() {99$participants = $this->getParticipants();100return array_keys($participants);101}102103public function attachHandles(array $handles) {104assert_instances_of($handles, 'PhabricatorObjectHandle');105$this->handles = $handles;106return $this;107}108109public function getHandles() {110return $this->assertAttached($this->handles);111}112113public function attachTransactions(array $transactions) {114assert_instances_of($transactions, 'ConpherenceTransaction');115$this->transactions = $transactions;116return $this;117}118119public function getTransactions($assert_attached = true) {120return $this->assertAttached($this->transactions);121}122123public function hasAttachedTransactions() {124return $this->transactions !== self::ATTACHABLE;125}126127public function getTransactionsFrom($begin = 0, $amount = null) {128$length = count($this->transactions);129130return array_slice(131$this->getTransactions(),132$length - $begin - $amount,133$amount);134}135136public function getProfileImageURI() {137return $this->getProfileImageFile()->getBestURI();138}139140public function attachProfileImageFile(PhabricatorFile $file) {141$this->profileImageFile = $file;142return $this;143}144145public function getProfileImageFile() {146return $this->assertAttached($this->profileImageFile);147}148149/**150* Get a thread title which doesn't require handles to be attached.151*152* This is a less rich title than @{method:getDisplayTitle}, but does not153* require handles to be attached. We use it to build thread handles without154* risking cycles or recursion while querying.155*156* @return string Lower quality human-readable title.157*/158public function getStaticTitle() {159$title = $this->getTitle();160if (strlen($title)) {161return $title;162}163164return pht('Private Room');165}166167public function getDisplayData(PhabricatorUser $viewer) {168$handles = $this->getHandles();169170if ($this->hasAttachedTransactions()) {171$transactions = $this->getTransactions();172} else {173$transactions = array();174}175176$img_src = $this->getProfileImageURI();177178$message_transaction = null;179foreach ($transactions as $transaction) {180if ($message_transaction) {181break;182}183switch ($transaction->getTransactionType()) {184case PhabricatorTransactions::TYPE_COMMENT:185$message_transaction = $transaction;186break;187default:188break;189}190}191if ($message_transaction) {192$message_handle = $handles[$message_transaction->getAuthorPHID()];193$subtitle = sprintf(194'%s: %s',195$message_handle->getName(),196id(new PhutilUTF8StringTruncator())197->setMaximumGlyphs(60)198->truncateString(199$message_transaction->getComment()->getContent()));200} else {201// Kinda lame, but maybe add last message to cache?202$subtitle = pht('No recent messages');203}204205$user_participation = $this->getParticipantIfExists($viewer->getPHID());206$theme = ConpherenceRoomSettings::COLOR_LIGHT;207if ($user_participation) {208$user_seen_count = $user_participation->getSeenMessageCount();209$participant = $this->getParticipant($viewer->getPHID());210$settings = $participant->getSettings();211$theme = idx($settings, 'theme', $theme);212} else {213$user_seen_count = 0;214}215216$unread_count = $this->getMessageCount() - $user_seen_count;217$theme_class = ConpherenceRoomSettings::getThemeClass($theme);218219$title = $this->getTitle();220$topic = $this->getTopic();221222return array(223'title' => $title,224'topic' => $topic,225'subtitle' => $subtitle,226'unread_count' => $unread_count,227'epoch' => $this->getDateModified(),228'image' => $img_src,229'theme' => $theme_class,230);231}232233234/* -( PhabricatorPolicyInterface Implementation )-------------------------- */235236237public function getCapabilities() {238return array(239PhabricatorPolicyCapability::CAN_VIEW,240PhabricatorPolicyCapability::CAN_EDIT,241);242}243244public function getPolicy($capability) {245switch ($capability) {246case PhabricatorPolicyCapability::CAN_VIEW:247return $this->getViewPolicy();248case PhabricatorPolicyCapability::CAN_EDIT:249return $this->getEditPolicy();250}251return PhabricatorPolicies::POLICY_NOONE;252}253254public function hasAutomaticCapability($capability, PhabricatorUser $user) {255// this bad boy isn't even created yet so go nuts $user256if (!$this->getID()) {257return true;258}259260switch ($capability) {261case PhabricatorPolicyCapability::CAN_EDIT:262return false;263}264265$participants = $this->getParticipants();266return isset($participants[$user->getPHID()]);267}268269public function describeAutomaticCapability($capability) {270switch ($capability) {271case PhabricatorPolicyCapability::CAN_VIEW:272return pht('Participants in a room can always view it.');273break;274}275}276277public static function loadViewPolicyObjects(278PhabricatorUser $viewer,279array $conpherences) {280281assert_instances_of($conpherences, __CLASS__);282283$policies = array();284foreach ($conpherences as $room) {285$policies[$room->getViewPolicy()] = 1;286}287$policy_objects = array();288if ($policies) {289$policy_objects = id(new PhabricatorPolicyQuery())290->setViewer($viewer)291->withPHIDs(array_keys($policies))292->execute();293}294295return $policy_objects;296}297298public function getPolicyIconName(array $policy_objects) {299assert_instances_of($policy_objects, 'PhabricatorPolicy');300301$icon = $policy_objects[$this->getViewPolicy()]->getIcon();302return $icon;303}304305306/* -( PhabricatorApplicationTransactionInterface )------------------------- */307308309public function getApplicationTransactionEditor() {310return new ConpherenceEditor();311}312313public function getApplicationTransactionTemplate() {314return new ConpherenceTransaction();315}316317318/* -( PhabricatorNgramInterface )------------------------------------------ */319320321public function newNgrams() {322return array(323id(new ConpherenceThreadTitleNgrams())324->setValue($this->getTitle()),325);326}327328329/* -( PhabricatorDestructibleInterface )----------------------------------- */330331332public function destroyObjectPermanently(333PhabricatorDestructionEngine $engine) {334335$this->openTransaction();336$this->delete();337338$participants = id(new ConpherenceParticipant())339->loadAllWhere('conpherencePHID = %s', $this->getPHID());340foreach ($participants as $participant) {341$participant->delete();342}343344$this->saveTransaction();345346}347}348349350