Remove all "FileHasObject" edge reads and writes
[phabricator.git] / src / applications / conpherence / editor / ConpherenceEditor.php
bloba1d6431d8c1ebb2673ec71b96f1103671f66f067
1 <?php
3 final class ConpherenceEditor extends PhabricatorApplicationTransactionEditor {
5 const ERROR_EMPTY_PARTICIPANTS = 'error-empty-participants';
6 const ERROR_EMPTY_MESSAGE = 'error-empty-message';
8 public function getEditorApplicationClass() {
9 return 'PhabricatorConpherenceApplication';
12 public function getEditorObjectsDescription() {
13 return pht('Conpherence Rooms');
16 public static function createThread(
17 PhabricatorUser $creator,
18 array $participant_phids,
19 $title,
20 $message,
21 PhabricatorContentSource $source,
22 $topic) {
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);
33 if (empty($message)) {
34 $errors[] = self::ERROR_EMPTY_MESSAGE;
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);
49 if (strlen($topic)) {
50 $xactions[] = id(new ConpherenceTransaction())
51 ->setTransactionType(
52 ConpherenceThreadTopicTransaction::TRANSACTIONTYPE)
53 ->setNewValue($topic);
56 $xactions[] = id(new ConpherenceTransaction())
57 ->setTransactionType(PhabricatorTransactions::TYPE_COMMENT)
58 ->attachComment(
59 id(new ConpherenceTransactionComment())
60 ->setContent($message)
61 ->setConpherencePHID($conpherence->getPHID()));
63 id(new ConpherenceEditor())
64 ->setActor($creator)
65 ->setContentSource($source)
66 ->setContinueOnNoEffect(true)
67 ->applyTransactions($conpherence, $xactions);
70 return array($errors, $conpherence);
73 public function generateTransactionsFromText(
74 PhabricatorUser $viewer,
75 ConpherenceThread $conpherence,
76 $text) {
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;
88 public function getTransactionTypes() {
89 $types = parent::getTransactionTypes();
91 $types[] = PhabricatorTransactions::TYPE_COMMENT;
92 $types[] = PhabricatorTransactions::TYPE_VIEW_POLICY;
93 $types[] = PhabricatorTransactions::TYPE_EDIT_POLICY;
95 return $types;
98 public function getCreateObjectTitle($author, $object) {
99 return pht('%s created this room.', $author);
103 protected function applyBuiltinInternalTransaction(
104 PhabricatorLiskDAO $object,
105 PhabricatorApplicationTransaction $xaction) {
107 switch ($xaction->getTransactionType()) {
108 case PhabricatorTransactions::TYPE_COMMENT:
109 $object->setMessageCount((int)$object->getMessageCount() + 1);
110 break;
113 return parent::applyBuiltinInternalTransaction($object, $xaction);
117 protected function applyFinalEffects(
118 PhabricatorLiskDAO $object,
119 array $xactions) {
121 $acting_phid = $this->getActingAsPHID();
122 $participants = $object->getParticipants();
123 foreach ($participants as $participant) {
124 if ($participant->getParticipantPHID() == $acting_phid) {
125 $participant->markUpToDate($object);
129 if ($participants) {
130 PhabricatorUserCache::clearCaches(
131 PhabricatorUserMessageCountCacheType::KEY_COUNT,
132 array_keys($participants));
135 if ($xactions) {
136 $data = array(
137 'type' => 'message',
138 'threadPHID' => $object->getPHID(),
139 'messageID' => last($xactions)->getID(),
140 'subscribers' => array($object->getPHID()),
143 PhabricatorNotificationClient::tryToPostMessage($data);
146 return $xactions;
149 protected function shouldSendMail(
150 PhabricatorLiskDAO $object,
151 array $xactions) {
152 return true;
155 protected function buildReplyHandler(PhabricatorLiskDAO $object) {
156 return id(new ConpherenceReplyHandler())
157 ->setActor($this->getActor())
158 ->setMailReceiver($object);
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());
170 return id(new PhabricatorMetaMTAMail())
171 ->setSubject("Z{$id}: {$title}");
174 protected function getMailTo(PhabricatorLiskDAO $object) {
175 $to_phids = array();
177 $participants = $object->getParticipants();
178 if (!$participants) {
179 return $to_phids;
182 $participant_phids = mpull($participants, 'getParticipantPHID');
184 $users = id(new PhabricatorPeopleQuery())
185 ->setViewer(PhabricatorUser::getOmnipotentUser())
186 ->withPHIDs($participant_phids)
187 ->needUserSettings(true)
188 ->execute();
189 $users = mpull($users, null, 'getPHID');
191 $notification_key = PhabricatorConpherenceNotificationsSetting::SETTINGKEY;
192 $notification_email =
193 PhabricatorConpherenceNotificationsSetting::VALUE_CONPHERENCE_EMAIL;
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;
203 $settings = $participant->getSettings();
204 $notifications = idx($settings, 'notifications', $default);
206 if ($notifications == $notification_email) {
207 $to_phids[] = $phid;
211 return $to_phids;
214 protected function getMailCC(PhabricatorLiskDAO $object) {
215 return array();
218 protected function buildMailBody(
219 PhabricatorLiskDAO $object,
220 array $xactions) {
222 $body = parent::buildMailBody($object, $xactions);
223 $body->addLinkSection(
224 pht('CONPHERENCE DETAIL'),
225 PhabricatorEnv::getProductionURI('/'.$object->getMonogram()));
227 return $body;
230 protected function addEmailPreferenceSectionToMailBody(
231 PhabricatorMetaMTAMailBody $body,
232 PhabricatorLiskDAO $object,
233 array $xactions) {
235 $href = PhabricatorEnv::getProductionURI(
236 '/'.$object->getMonogram().'?settings');
237 $label = pht('EMAIL PREFERENCES FOR THIS ROOM');
238 $body->addLinkSection($label, $href);
241 protected function getMailSubjectPrefix() {
242 return pht('[Conpherence]');
245 protected function supportsSearch() {
246 return true;