@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 recaptime-dev/main 120 lines 3.5 kB view raw
1<?php 2 3final class DifferentialCreateMailReceiver 4 extends PhabricatorApplicationMailReceiver { 5 6 protected function newApplication() { 7 return new PhabricatorDifferentialApplication(); 8 } 9 10 protected function processReceivedMail( 11 PhabricatorMetaMTAReceivedMail $mail, 12 PhutilEmailAddress $target) { 13 14 $author = $this->getAuthor(); 15 16 $attachments = $mail->getAttachments(); 17 $files = array(); 18 $errors = array(); 19 if ($attachments) { 20 $files = id(new PhabricatorFileQuery()) 21 ->setViewer($author) 22 ->withPHIDs($attachments) 23 ->execute(); 24 foreach ($files as $index => $file) { 25 if ($file->getMimeType() != 'text/plain') { 26 $errors[] = pht( 27 'Could not parse file %s; only files with mimetype text/plain '. 28 'can be parsed via email.', 29 $file->getName()); 30 unset($files[$index]); 31 } 32 } 33 } 34 35 $diffs = array(); 36 foreach ($files as $file) { 37 $call = new ConduitCall( 38 'differential.createrawdiff', 39 array( 40 'diff' => $file->loadFileData(), 41 )); 42 $call->setUser($author); 43 try { 44 $result = $call->execute(); 45 $diffs[$file->getName()] = $result['uri']; 46 } catch (Exception $e) { 47 $errors[] = pht( 48 'Could not parse attachment %s; only attachments (and mail bodies) '. 49 'generated via "diff" commands can be parsed.', 50 $file->getName()); 51 } 52 } 53 54 $body = $mail->getCleanTextBody(); 55 if ($body) { 56 $call = new ConduitCall( 57 'differential.createrawdiff', 58 array( 59 'diff' => $body, 60 )); 61 $call->setUser($author); 62 try { 63 $result = $call->execute(); 64 $diffs[pht('Mail Body')] = $result['uri']; 65 } catch (Exception $e) { 66 $errors[] = pht( 67 'Could not parse mail body; only mail bodies (and attachments) '. 68 'generated via "diff" commands can be parsed.'); 69 } 70 } 71 72 $subject_prefix = pht('[Differential]'); 73 if (count($diffs)) { 74 $subject = pht( 75 'You successfully created %d diff(s).', 76 count($diffs)); 77 } else { 78 $subject = pht( 79 'Diff creation failed; see body for %s error(s).', 80 phutil_count($errors)); 81 } 82 $body = new PhabricatorMetaMTAMailBody(); 83 $body->addRawSection($subject); 84 if (count($diffs)) { 85 $text_body = ''; 86 $html_body = array(); 87 $body_label = pht('%s DIFF LINK(S)', phutil_count($diffs)); 88 foreach ($diffs as $filename => $diff_uri) { 89 $text_body .= $filename.': '.$diff_uri."\n"; 90 $html_body[] = phutil_tag( 91 'a', 92 array( 93 'href' => $diff_uri, 94 ), 95 $filename); 96 $html_body[] = phutil_tag('br'); 97 } 98 $body->addTextSection($body_label, $text_body); 99 $body->addHTMLSection($body_label, $html_body); 100 } 101 102 if (count($errors)) { 103 $body_section = new PhabricatorMetaMTAMailSection(); 104 $body_label = pht('%s ERROR(S)', phutil_count($errors)); 105 foreach ($errors as $error) { 106 $body_section->addFragment($error); 107 } 108 $body->addTextSection($body_label, $body_section); 109 } 110 111 id(new PhabricatorMetaMTAMail()) 112 ->addTos(array($author->getPHID())) 113 ->setSubject($subject) 114 ->setSubjectPrefix($subject_prefix) 115 ->setFrom($author->getPHID()) 116 ->setBody($body->render()) 117 ->saveAndSend(); 118 } 119 120}