@recaptime-dev's working patches + fork for Phorge, a community fork of Phabricator. (Upstream dev and stable branches are at upstream/main and upstream/stable respectively.) hq.recaptime.dev/wiki/Phorge
phorge phabricator
at upstream/main 249 lines 7.0 kB view raw
1<?php 2 3final class ConpherenceEditor extends PhabricatorApplicationTransactionEditor { 4 5 const ERROR_EMPTY_PARTICIPANTS = 'error-empty-participants'; 6 const ERROR_EMPTY_MESSAGE = 'error-empty-message'; 7 8 public function getEditorApplicationClass() { 9 return PhabricatorConpherenceApplication::class; 10 } 11 12 public function getEditorObjectsDescription() { 13 return pht('Conpherence Rooms'); 14 } 15 16 public static function createThread( 17 PhabricatorUser $creator, 18 array $participant_phids, 19 $title, 20 $message, 21 PhabricatorContentSource $source, 22 $topic) { 23 24 $conpherence = ConpherenceThread::initializeNewRoom($creator); 25 $errors = array(); 26 if (empty($participant_phids)) { 27 $errors[] = self::ERROR_EMPTY_PARTICIPANTS; 28 } else { 29 $participant_phids[] = $creator->getPHID(); 30 $participant_phids = array_unique($participant_phids); 31 } 32 33 if (empty($message)) { 34 $errors[] = self::ERROR_EMPTY_MESSAGE; 35 } 36 37 if (!$errors) { 38 $xactions = array(); 39 $xactions[] = id(new ConpherenceTransaction()) 40 ->setTransactionType( 41 ConpherenceThreadParticipantsTransaction::TRANSACTIONTYPE) 42 ->setNewValue(array('+' => $participant_phids)); 43 if ($title) { 44 $xactions[] = id(new ConpherenceTransaction()) 45 ->setTransactionType( 46 ConpherenceThreadTitleTransaction::TRANSACTIONTYPE) 47 ->setNewValue($title); 48 } 49 if (strlen($topic)) { 50 $xactions[] = id(new ConpherenceTransaction()) 51 ->setTransactionType( 52 ConpherenceThreadTopicTransaction::TRANSACTIONTYPE) 53 ->setNewValue($topic); 54 } 55 56 $xactions[] = id(new ConpherenceTransaction()) 57 ->setTransactionType(PhabricatorTransactions::TYPE_COMMENT) 58 ->attachComment( 59 id(new ConpherenceTransactionComment()) 60 ->setContent($message) 61 ->setConpherencePHID($conpherence->getPHID())); 62 63 id(new ConpherenceEditor()) 64 ->setActor($creator) 65 ->setContentSource($source) 66 ->setContinueOnNoEffect(true) 67 ->applyTransactions($conpherence, $xactions); 68 } 69 70 return array($errors, $conpherence); 71 } 72 73 public function generateTransactionsFromText( 74 PhabricatorUser $viewer, 75 ConpherenceThread $conpherence, 76 $text) { 77 78 $xactions = array(); 79 $xactions[] = id(new ConpherenceTransaction()) 80 ->setTransactionType(PhabricatorTransactions::TYPE_COMMENT) 81 ->attachComment( 82 id(new ConpherenceTransactionComment()) 83 ->setContent($text) 84 ->setConpherencePHID($conpherence->getPHID())); 85 return $xactions; 86 } 87 88 public function getTransactionTypes() { 89 $types = parent::getTransactionTypes(); 90 91 $types[] = PhabricatorTransactions::TYPE_COMMENT; 92 $types[] = PhabricatorTransactions::TYPE_VIEW_POLICY; 93 $types[] = PhabricatorTransactions::TYPE_EDIT_POLICY; 94 95 return $types; 96 } 97 98 public function getCreateObjectTitle($author, $object) { 99 return pht('%s created this room.', $author); 100 } 101 102 103 protected function applyBuiltinInternalTransaction( 104 PhabricatorLiskDAO $object, 105 PhabricatorApplicationTransaction $xaction) { 106 107 switch ($xaction->getTransactionType()) { 108 case PhabricatorTransactions::TYPE_COMMENT: 109 $object->setMessageCount((int)$object->getMessageCount() + 1); 110 break; 111 } 112 113 return parent::applyBuiltinInternalTransaction($object, $xaction); 114 } 115 116 117 protected function applyFinalEffects( 118 PhabricatorLiskDAO $object, 119 array $xactions) { 120 121 $acting_phid = $this->getActingAsPHID(); 122 $participants = $object->getParticipants(); 123 foreach ($participants as $participant) { 124 if ($participant->getParticipantPHID() == $acting_phid) { 125 $participant->markUpToDate($object); 126 } 127 } 128 129 if ($participants) { 130 PhabricatorUserCache::clearCaches( 131 PhabricatorUserMessageCountCacheType::KEY_COUNT, 132 array_keys($participants)); 133 } 134 135 if ($xactions) { 136 $data = array( 137 'type' => 'message', 138 'threadPHID' => $object->getPHID(), 139 'messageID' => last($xactions)->getID(), 140 'subscribers' => array($object->getPHID()), 141 ); 142 143 PhabricatorNotificationClient::tryToPostMessage($data); 144 } 145 146 return $xactions; 147 } 148 149 protected function shouldSendMail( 150 PhabricatorLiskDAO $object, 151 array $xactions) { 152 return true; 153 } 154 155 protected function buildReplyHandler(PhabricatorLiskDAO $object) { 156 return id(new ConpherenceReplyHandler()) 157 ->setActor($this->getActor()) 158 ->setMailReceiver($object); 159 } 160 161 protected function buildMailTemplate(PhabricatorLiskDAO $object) { 162 $id = $object->getID(); 163 $title = $object->getTitle(); 164 if (!$title) { 165 $title = pht( 166 '%s sent you a message.', 167 $this->getActor()->getUserName()); 168 } 169 170 return id(new PhabricatorMetaMTAMail()) 171 ->setSubject("Z{$id}: {$title}"); 172 } 173 174 protected function getMailTo(PhabricatorLiskDAO $object) { 175 $to_phids = array(); 176 177 $participants = $object->getParticipants(); 178 if (!$participants) { 179 return $to_phids; 180 } 181 182 $participant_phids = mpull($participants, 'getParticipantPHID'); 183 184 $users = id(new PhabricatorPeopleQuery()) 185 ->setViewer(PhabricatorUser::getOmnipotentUser()) 186 ->withPHIDs($participant_phids) 187 ->needUserSettings(true) 188 ->execute(); 189 $users = mpull($users, null, 'getPHID'); 190 191 $notification_key = PhabricatorConpherenceNotificationsSetting::SETTINGKEY; 192 $notification_email = 193 PhabricatorConpherenceNotificationsSetting::VALUE_CONPHERENCE_EMAIL; 194 195 foreach ($participants as $phid => $participant) { 196 $user = idx($users, $phid); 197 if ($user) { 198 $default = $user->getUserSetting($notification_key); 199 } else { 200 $default = $notification_email; 201 } 202 203 $settings = $participant->getSettings(); 204 $notifications = idx($settings, 'notifications', $default); 205 206 if ($notifications == $notification_email) { 207 $to_phids[] = $phid; 208 } 209 } 210 211 return $to_phids; 212 } 213 214 protected function getMailCC(PhabricatorLiskDAO $object) { 215 return array(); 216 } 217 218 protected function buildMailBody( 219 PhabricatorLiskDAO $object, 220 array $xactions) { 221 222 $body = parent::buildMailBody($object, $xactions); 223 $body->addLinkSection( 224 pht('CONPHERENCE DETAIL'), 225 PhabricatorEnv::getProductionURI('/'.$object->getMonogram())); 226 227 return $body; 228 } 229 230 protected function addEmailPreferenceSectionToMailBody( 231 PhabricatorMetaMTAMailBody $body, 232 PhabricatorLiskDAO $object, 233 array $xactions) { 234 235 $href = PhabricatorEnv::getProductionURI( 236 '/'.$object->getMonogram().'?settings'); 237 $label = pht('EMAIL PREFERENCES FOR THIS ROOM'); 238 $body->addLinkSection($label, $href); 239 } 240 241 protected function getMailSubjectPrefix() { 242 return pht('[Conpherence]'); 243 } 244 245 protected function supportsSearch() { 246 return true; 247 } 248 249}