PageRenderTime 47ms CodeModel.GetById 22ms RepoModel.GetById 1ms app.codeStats 0ms

/src/applications/conpherence/editor/ConpherenceEditor.php

http://github.com/facebook/phabricator
PHP | 249 lines | 199 code | 50 blank | 0 comment | 17 complexity | 18d17f2585b94a1ea29f52155faab87d MD5 | raw file
Possible License(s): JSON, MPL-2.0-no-copyleft-exception, Apache-2.0, BSD-3-Clause, LGPL-2.0, MIT, LGPL-2.1, LGPL-3.0
  1. <?php
  2. final class ConpherenceEditor extends PhabricatorApplicationTransactionEditor {
  3. const ERROR_EMPTY_PARTICIPANTS = 'error-empty-participants';
  4. const ERROR_EMPTY_MESSAGE = 'error-empty-message';
  5. public function getEditorApplicationClass() {
  6. return 'PhabricatorConpherenceApplication';
  7. }
  8. public function getEditorObjectsDescription() {
  9. return pht('Conpherence Rooms');
  10. }
  11. public static function createThread(
  12. PhabricatorUser $creator,
  13. array $participant_phids,
  14. $title,
  15. $message,
  16. PhabricatorContentSource $source,
  17. $topic) {
  18. $conpherence = ConpherenceThread::initializeNewRoom($creator);
  19. $errors = array();
  20. if (empty($participant_phids)) {
  21. $errors[] = self::ERROR_EMPTY_PARTICIPANTS;
  22. } else {
  23. $participant_phids[] = $creator->getPHID();
  24. $participant_phids = array_unique($participant_phids);
  25. }
  26. if (empty($message)) {
  27. $errors[] = self::ERROR_EMPTY_MESSAGE;
  28. }
  29. if (!$errors) {
  30. $xactions = array();
  31. $xactions[] = id(new ConpherenceTransaction())
  32. ->setTransactionType(
  33. ConpherenceThreadParticipantsTransaction::TRANSACTIONTYPE)
  34. ->setNewValue(array('+' => $participant_phids));
  35. if ($title) {
  36. $xactions[] = id(new ConpherenceTransaction())
  37. ->setTransactionType(
  38. ConpherenceThreadTitleTransaction::TRANSACTIONTYPE)
  39. ->setNewValue($title);
  40. }
  41. if (strlen($topic)) {
  42. $xactions[] = id(new ConpherenceTransaction())
  43. ->setTransactionType(
  44. ConpherenceThreadTopicTransaction::TRANSACTIONTYPE)
  45. ->setNewValue($topic);
  46. }
  47. $xactions[] = id(new ConpherenceTransaction())
  48. ->setTransactionType(PhabricatorTransactions::TYPE_COMMENT)
  49. ->attachComment(
  50. id(new ConpherenceTransactionComment())
  51. ->setContent($message)
  52. ->setConpherencePHID($conpherence->getPHID()));
  53. id(new ConpherenceEditor())
  54. ->setActor($creator)
  55. ->setContentSource($source)
  56. ->setContinueOnNoEffect(true)
  57. ->applyTransactions($conpherence, $xactions);
  58. }
  59. return array($errors, $conpherence);
  60. }
  61. public function generateTransactionsFromText(
  62. PhabricatorUser $viewer,
  63. ConpherenceThread $conpherence,
  64. $text) {
  65. $xactions = array();
  66. $xactions[] = id(new ConpherenceTransaction())
  67. ->setTransactionType(PhabricatorTransactions::TYPE_COMMENT)
  68. ->attachComment(
  69. id(new ConpherenceTransactionComment())
  70. ->setContent($text)
  71. ->setConpherencePHID($conpherence->getPHID()));
  72. return $xactions;
  73. }
  74. public function getTransactionTypes() {
  75. $types = parent::getTransactionTypes();
  76. $types[] = PhabricatorTransactions::TYPE_COMMENT;
  77. $types[] = PhabricatorTransactions::TYPE_VIEW_POLICY;
  78. $types[] = PhabricatorTransactions::TYPE_EDIT_POLICY;
  79. return $types;
  80. }
  81. public function getCreateObjectTitle($author, $object) {
  82. return pht('%s created this room.', $author);
  83. }
  84. protected function applyBuiltinInternalTransaction(
  85. PhabricatorLiskDAO $object,
  86. PhabricatorApplicationTransaction $xaction) {
  87. switch ($xaction->getTransactionType()) {
  88. case PhabricatorTransactions::TYPE_COMMENT:
  89. $object->setMessageCount((int)$object->getMessageCount() + 1);
  90. break;
  91. }
  92. return parent::applyBuiltinInternalTransaction($object, $xaction);
  93. }
  94. protected function applyFinalEffects(
  95. PhabricatorLiskDAO $object,
  96. array $xactions) {
  97. $acting_phid = $this->getActingAsPHID();
  98. $participants = $object->getParticipants();
  99. foreach ($participants as $participant) {
  100. if ($participant->getParticipantPHID() == $acting_phid) {
  101. $participant->markUpToDate($object);
  102. }
  103. }
  104. if ($participants) {
  105. PhabricatorUserCache::clearCaches(
  106. PhabricatorUserMessageCountCacheType::KEY_COUNT,
  107. array_keys($participants));
  108. }
  109. if ($xactions) {
  110. $data = array(
  111. 'type' => 'message',
  112. 'threadPHID' => $object->getPHID(),
  113. 'messageID' => last($xactions)->getID(),
  114. 'subscribers' => array($object->getPHID()),
  115. );
  116. PhabricatorNotificationClient::tryToPostMessage($data);
  117. }
  118. return $xactions;
  119. }
  120. protected function shouldSendMail(
  121. PhabricatorLiskDAO $object,
  122. array $xactions) {
  123. return true;
  124. }
  125. protected function buildReplyHandler(PhabricatorLiskDAO $object) {
  126. return id(new ConpherenceReplyHandler())
  127. ->setActor($this->getActor())
  128. ->setMailReceiver($object);
  129. }
  130. protected function buildMailTemplate(PhabricatorLiskDAO $object) {
  131. $id = $object->getID();
  132. $title = $object->getTitle();
  133. if (!$title) {
  134. $title = pht(
  135. '%s sent you a message.',
  136. $this->getActor()->getUserName());
  137. }
  138. return id(new PhabricatorMetaMTAMail())
  139. ->setSubject("Z{$id}: {$title}");
  140. }
  141. protected function getMailTo(PhabricatorLiskDAO $object) {
  142. $to_phids = array();
  143. $participants = $object->getParticipants();
  144. if (!$participants) {
  145. return $to_phids;
  146. }
  147. $participant_phids = mpull($participants, 'getParticipantPHID');
  148. $users = id(new PhabricatorPeopleQuery())
  149. ->setViewer(PhabricatorUser::getOmnipotentUser())
  150. ->withPHIDs($participant_phids)
  151. ->needUserSettings(true)
  152. ->execute();
  153. $users = mpull($users, null, 'getPHID');
  154. $notification_key = PhabricatorConpherenceNotificationsSetting::SETTINGKEY;
  155. $notification_email =
  156. PhabricatorConpherenceNotificationsSetting::VALUE_CONPHERENCE_EMAIL;
  157. foreach ($participants as $phid => $participant) {
  158. $user = idx($users, $phid);
  159. if ($user) {
  160. $default = $user->getUserSetting($notification_key);
  161. } else {
  162. $default = $notification_email;
  163. }
  164. $settings = $participant->getSettings();
  165. $notifications = idx($settings, 'notifications', $default);
  166. if ($notifications == $notification_email) {
  167. $to_phids[] = $phid;
  168. }
  169. }
  170. return $to_phids;
  171. }
  172. protected function getMailCC(PhabricatorLiskDAO $object) {
  173. return array();
  174. }
  175. protected function buildMailBody(
  176. PhabricatorLiskDAO $object,
  177. array $xactions) {
  178. $body = parent::buildMailBody($object, $xactions);
  179. $body->addLinkSection(
  180. pht('CONPHERENCE DETAIL'),
  181. PhabricatorEnv::getProductionURI('/'.$object->getMonogram()));
  182. return $body;
  183. }
  184. protected function addEmailPreferenceSectionToMailBody(
  185. PhabricatorMetaMTAMailBody $body,
  186. PhabricatorLiskDAO $object,
  187. array $xactions) {
  188. $href = PhabricatorEnv::getProductionURI(
  189. '/'.$object->getMonogram().'?settings');
  190. $label = pht('EMAIL PREFERENCES FOR THIS ROOM');
  191. $body->addLinkSection($label, $href);
  192. }
  193. protected function getMailSubjectPrefix() {
  194. return pht('[Conpherence]');
  195. }
  196. protected function supportsSearch() {
  197. return true;
  198. }
  199. }