[ Index ]

PHP Cross Reference of Phabricator

title

Body

[close]

/src/applications/maniphest/mail/ -> ManiphestReplyHandler.php (source)

   1  <?php
   2  
   3  final class ManiphestReplyHandler extends PhabricatorMailReplyHandler {
   4  
   5    public function validateMailReceiver($mail_receiver) {
   6      if (!($mail_receiver instanceof ManiphestTask)) {
   7        throw new Exception('Mail receiver is not a ManiphestTask!');
   8      }
   9    }
  10  
  11    public function getPrivateReplyHandlerEmailAddress(
  12      PhabricatorObjectHandle $handle) {
  13      return $this->getDefaultPrivateReplyHandlerEmailAddress($handle, 'T');
  14    }
  15  
  16    public function getPublicReplyHandlerEmailAddress() {
  17      return $this->getDefaultPublicReplyHandlerEmailAddress('T');
  18    }
  19  
  20    public function getReplyHandlerDomain() {
  21      return PhabricatorEnv::getEnvConfig(
  22        'metamta.maniphest.reply-handler-domain');
  23    }
  24  
  25    public function getReplyHandlerInstructions() {
  26      if ($this->supportsReplies()) {
  27        return pht(
  28          'Reply to comment or attach files, or !close, !claim, '.
  29          '!unsubscribe or !assign <username>.');
  30      } else {
  31        return null;
  32      }
  33    }
  34  
  35    protected function receiveEmail(PhabricatorMetaMTAReceivedMail $mail) {
  36      // NOTE: We'll drop in here on both the "reply to a task" and "create a
  37      // new task" workflows! Make sure you test both if you make changes!
  38  
  39      $task = $this->getMailReceiver();
  40  
  41      $is_new_task = !$task->getID();
  42  
  43      $user = $this->getActor();
  44  
  45      $body_data = $mail->parseBody();
  46      $body = $body_data['body'];
  47      $body = $this->enhanceBodyWithAttachments($body, $mail->getAttachments());
  48  
  49      $xactions = array();
  50      $content_source = PhabricatorContentSource::newForSource(
  51        PhabricatorContentSource::SOURCE_EMAIL,
  52        array(
  53          'id' => $mail->getID(),
  54        ));
  55  
  56      $template = new ManiphestTransaction();
  57  
  58      $is_unsub = false;
  59      if ($is_new_task) {
  60        $task = ManiphestTask::initializeNewTask($user);
  61  
  62        $xactions[] = id(new ManiphestTransaction())
  63          ->setTransactionType(ManiphestTransaction::TYPE_STATUS)
  64          ->setNewValue(ManiphestTaskStatus::getDefaultStatus());
  65  
  66        $xactions[] = id(new ManiphestTransaction())
  67          ->setTransactionType(ManiphestTransaction::TYPE_TITLE)
  68          ->setNewValue(nonempty($mail->getSubject(), pht('Untitled Task')));
  69  
  70        $xactions[] = id(new ManiphestTransaction())
  71          ->setTransactionType(ManiphestTransaction::TYPE_DESCRIPTION)
  72          ->setNewValue($body);
  73  
  74      } else {
  75  
  76        $command = $body_data['command'];
  77        $command_value = $body_data['command_value'];
  78  
  79        $ttype = PhabricatorTransactions::TYPE_COMMENT;
  80        $new_value = null;
  81        switch ($command) {
  82          case 'close':
  83            $ttype = ManiphestTransaction::TYPE_STATUS;
  84            $new_value = ManiphestTaskStatus::getDefaultClosedStatus();
  85            break;
  86          case 'claim':
  87            $ttype = ManiphestTransaction::TYPE_OWNER;
  88            $new_value = $user->getPHID();
  89            break;
  90          case 'assign':
  91            $ttype = ManiphestTransaction::TYPE_OWNER;
  92            if ($command_value) {
  93              $assign_users = id(new PhabricatorPeopleQuery())
  94                ->setViewer($user)
  95                ->withUsernames(array($command_value))
  96                ->execute();
  97              if ($assign_users) {
  98                $assign_user = head($assign_users);
  99                $new_value = $assign_user->getPHID();
 100              }
 101            }
 102            // assign to the user by default
 103            if (!$new_value) {
 104              $new_value = $user->getPHID();
 105            }
 106            break;
 107          case 'unsubscribe':
 108            $is_unsub = true;
 109            $ttype = ManiphestTransaction::TYPE_CCS;
 110            $ccs = $task->getCCPHIDs();
 111            foreach ($ccs as $k => $phid) {
 112              if ($phid == $user->getPHID()) {
 113                unset($ccs[$k]);
 114              }
 115            }
 116            $new_value = array_values($ccs);
 117            break;
 118        }
 119  
 120        if ($ttype != PhabricatorTransactions::TYPE_COMMENT) {
 121          $xaction = clone $template;
 122          $xaction->setTransactionType($ttype);
 123          $xaction->setNewValue($new_value);
 124          $xactions[] = $xaction;
 125        }
 126  
 127        if (strlen($body)) {
 128          $xaction = clone $template;
 129          $xaction->setTransactionType(PhabricatorTransactions::TYPE_COMMENT);
 130          $xaction->attachComment(
 131            id(new ManiphestTransactionComment())
 132              ->setContent($body));
 133          $xactions[] = $xaction;
 134        }
 135      }
 136  
 137      $ccs = $mail->loadCCPHIDs();
 138      $old_ccs = $task->getCCPHIDs();
 139      $new_ccs = array_merge($old_ccs, $ccs);
 140      if (!$is_unsub) {
 141        $new_ccs[] = $user->getPHID();
 142      }
 143      $new_ccs = array_unique($new_ccs);
 144  
 145      if (array_diff($new_ccs, $old_ccs)) {
 146        $cc_xaction = clone $template;
 147        $cc_xaction->setTransactionType(ManiphestTransaction::TYPE_CCS);
 148        $cc_xaction->setNewValue($new_ccs);
 149        $xactions[] = $cc_xaction;
 150      }
 151  
 152      $event = new PhabricatorEvent(
 153        PhabricatorEventType::TYPE_MANIPHEST_WILLEDITTASK,
 154        array(
 155          'task'          => $task,
 156          'mail'          => $mail,
 157          'new'           => $is_new_task,
 158          'transactions'  => $xactions,
 159        ));
 160      $event->setUser($user);
 161      PhutilEventEngine::dispatchEvent($event);
 162  
 163      $task = $event->getValue('task');
 164      $xactions = $event->getValue('transactions');
 165  
 166      $editor = id(new ManiphestTransactionEditor())
 167        ->setActor($user)
 168        ->setParentMessageID($mail->getMessageID())
 169        ->setExcludeMailRecipientPHIDs($this->getExcludeMailRecipientPHIDs())
 170        ->setContinueOnNoEffect(true)
 171        ->setContinueOnMissingFields(true)
 172        ->setContentSource($content_source)
 173        ->applyTransactions($task, $xactions);
 174  
 175      $event = new PhabricatorEvent(
 176        PhabricatorEventType::TYPE_MANIPHEST_DIDEDITTASK,
 177        array(
 178          'task'          => $task,
 179          'new'           => $is_new_task,
 180          'transactions'  => $xactions,
 181        ));
 182      $event->setUser($user);
 183      PhutilEventEngine::dispatchEvent($event);
 184    }
 185  
 186  }


Generated: Sun Nov 30 09:20:46 2014 Cross-referenced by PHPXref 0.7.1