Exemplo n.º 1
0
 public function save()
 {
     if (!$this->viewer) {
         throw new Exception("Must set user before saving question");
     }
     if (!$this->question) {
         throw new Exception("Must set question before saving it");
     }
     $viewer = $this->viewer;
     $question = $this->question;
     $question->save();
     // search index
     $question->attachRelated();
     PhabricatorSearchPonderIndexer::indexQuestion($question);
     // subscribe author and @mentions
     $subeditor = id(new PhabricatorSubscriptionsEditor())->setObject($question)->setUser($viewer);
     $subeditor->subscribeExplicit(array($question->getAuthorPHID()));
     $content = $question->getContent();
     $at_mention_phids = PhabricatorMarkupEngine::extractPHIDsFromMentions(array($content));
     $subeditor->subscribeImplicit($at_mention_phids);
     $subeditor->save();
     if ($this->shouldEmail && $at_mention_phids) {
         id(new PonderMentionMail($question, $question, $viewer))->setToPHIDs($at_mention_phids)->send();
     }
 }
Exemplo n.º 2
0
 public function save()
 {
     if (!$this->comment) {
         throw new Exception("Must set comment before saving it");
     }
     if (!$this->question) {
         throw new Exception("Must set question before saving comment");
     }
     if (!$this->targetPHID) {
         throw new Exception("Must set target before saving comment");
     }
     if (!$this->viewer) {
         throw new Exception("Must set viewer before saving comment");
     }
     $comment = $this->comment;
     $question = $this->question;
     $target = $this->targetPHID;
     $viewer = $this->viewer;
     $comment->save();
     $question->attachRelated();
     PhabricatorSearchPonderIndexer::indexQuestion($question);
     // subscribe author and @mentions
     $subeditor = id(new PhabricatorSubscriptionsEditor())->setObject($question)->setUser($viewer);
     $subeditor->subscribeExplicit(array($comment->getAuthorPHID()));
     $content = $comment->getContent();
     $at_mention_phids = PhabricatorMarkupEngine::extractPHIDsFromMentions(array($content));
     $subeditor->subscribeImplicit($at_mention_phids);
     $subeditor->save();
     if ($this->shouldEmail) {
         // now load subscribers, including implicitly-added @mention victims
         $subscribers = PhabricatorSubscribersQuery::loadSubscribersForPHID($question->getPHID());
         // @mention emails (but not for anyone who has explicitly unsubscribed)
         if (array_intersect($at_mention_phids, $subscribers)) {
             id(new PonderMentionMail($question, $comment, $viewer))->setToPHIDs($at_mention_phids)->send();
         }
         if ($target === $question->getPHID()) {
             $target = $question;
         } else {
             $answers_by_phid = mgroup($question->getAnswers(), 'getPHID');
             $target = head($answers_by_phid[$target]);
         }
         // only send emails to others in the same thread
         $thread = mpull($target->getComments(), 'getAuthorPHID');
         $thread[] = $target->getAuthorPHID();
         $thread[] = $question->getAuthorPHID();
         $other_subs = array_diff(array_intersect($thread, $subscribers), $at_mention_phids);
         // 'Comment' emails for subscribers who are in the same comment thread,
         // including the author of the parent question and/or answer, excluding
         // @mentions (and excluding the author, depending on their MetaMTA
         // settings).
         if ($other_subs) {
             id(new PonderCommentMail($question, $comment, $viewer))->setToPHIDs($other_subs)->send();
         }
     }
 }
Exemplo n.º 3
0
 public function saveAnswer()
 {
     if (!$this->viewer) {
         throw new Exception("Must set user before saving question");
     }
     if (!$this->question) {
         throw new Exception("Must set question before saving answer");
     }
     if (!$this->answer) {
         throw new Exception("Must set answer before saving it");
     }
     $question = $this->question;
     $answer = $this->answer;
     $viewer = $this->viewer;
     $conn = $answer->establishConnection('w');
     $trans = $conn->openTransaction();
     $trans->beginReadLocking();
     $question->reload();
     queryfx($conn, 'UPDATE %T as t
     SET t.`answerCount` = t.`answerCount` + 1
     WHERE t.`PHID` = %s', $question->getTableName(), $question->getPHID());
     $answer->setQuestionID($question->getID());
     $answer->save();
     $trans->endReadLocking();
     $trans->saveTransaction();
     $question->attachRelated();
     PhabricatorSearchPonderIndexer::indexQuestion($question);
     // subscribe author and @mentions
     $subeditor = id(new PhabricatorSubscriptionsEditor())->setObject($question)->setUser($viewer);
     $subeditor->subscribeExplicit(array($answer->getAuthorPHID()));
     $content = $answer->getContent();
     $at_mention_phids = PhabricatorMarkupEngine::extractPHIDsFromMentions(array($content));
     $subeditor->subscribeImplicit($at_mention_phids);
     $subeditor->save();
     if ($this->shouldEmail) {
         // now load subscribers, including implicitly-added @mention victims
         $subscribers = PhabricatorSubscribersQuery::loadSubscribersForPHID($question->getPHID());
         // @mention emails (but not for anyone who has explicitly unsubscribed)
         if (array_intersect($at_mention_phids, $subscribers)) {
             id(new PonderMentionMail($question, $answer, $viewer))->setToPHIDs($at_mention_phids)->send();
         }
         $other_subs = array_diff($subscribers, $at_mention_phids);
         // 'Answered' emails for subscribers who are not @mentiond (and excluding
         // author depending on their MetaMTA settings).
         if ($other_subs) {
             id(new PonderAnsweredMail($question, $answer, $viewer))->setToPHIDs($other_subs)->send();
         }
     }
 }
 private function handlePost()
 {
     $request = $this->getRequest();
     $user = $request->getUser();
     $errors = array();
     $title = $request->getStr('title');
     $content = $request->getStr('content');
     // form validation
     if (phutil_utf8_strlen($title) < 1 || phutil_utf8_strlen($title) > 255) {
         $errors[] = "Please enter a title (1-255 characters)";
     }
     if ($errors) {
         return $this->showForm($errors, $title, $content);
     }
     // no validation errors -> save it
     $content_source = PhabricatorContentSource::newForSource(PhabricatorContentSource::SOURCE_WEB, array('ip' => $request->getRemoteAddr()));
     $question = id(new PonderQuestion())->setTitle($title)->setContent($content)->setAuthorPHID($user->getPHID())->setContentSource($content_source)->setVoteCount(0)->setAnswerCount(0)->setHeat(0.0)->save();
     PhabricatorSearchPonderIndexer::indexQuestion($question);
     return id(new AphrontRedirectResponse())->setURI('/Q' . $question->getID());
 }
 public function processRequest()
 {
     $request = $this->getRequest();
     if (!$request->isFormPost()) {
         return new Aphront400Response();
     }
     $user = $request->getUser();
     $question_id = $request->getInt('question_id');
     $question = PonderQuestionQuery::loadSingle($user, $question_id);
     if (!$question) {
         return new Aphront404Response();
     }
     $answer = $request->getStr('answer');
     $content_source = PhabricatorContentSource::newForSource(PhabricatorContentSource::SOURCE_WEB, array('ip' => $request->getRemoteAddr()));
     $res = new PonderAnswer();
     $res->setContent($answer)->setAuthorPHID($user->getPHID())->setVoteCount(0)->setQuestionID($question_id)->setContentSource($content_source);
     id(new PonderAnswerEditor())->setQuestion($question)->setAnswer($res)->saveAnswer();
     PhabricatorSearchPonderIndexer::indexQuestion($question);
     return id(new AphrontRedirectResponse())->setURI(id(new PhutilURI('/Q' . $question->getID()))->setFragment('A' . $res->getID()));
 }