Changeset View
Changeset View
Standalone View
Standalone View
src/applications/paste/editor/PhabricatorPasteEditor.php
<?php | <?php | ||||
final class PhabricatorPasteEditor | final class PhabricatorPasteEditor | ||||
extends PhabricatorApplicationTransactionEditor { | extends PhabricatorApplicationTransactionEditor { | ||||
private $fileName; | |||||
public function getEditorApplicationClass() { | public function getEditorApplicationClass() { | ||||
return 'PhabricatorPasteApplication'; | return 'PhabricatorPasteApplication'; | ||||
} | } | ||||
public function getEditorObjectsDescription() { | public function getEditorObjectsDescription() { | ||||
return pht('Pastes'); | return pht('Pastes'); | ||||
} | } | ||||
public static function initializeFileForPaste( | public function getCreateObjectTitle($author, $object) { | ||||
PhabricatorUser $actor, | return pht('%s created this paste.', $author); | ||||
$name, | } | ||||
$data) { | |||||
public function getCreateObjectTitleForFeed($author, $object) { | |||||
return PhabricatorFile::newFromFileData( | return pht('%s created %s.', $author, $object); | ||||
$data, | |||||
array( | |||||
'name' => $name, | |||||
'mime-type' => 'text/plain; charset=utf-8', | |||||
'authorPHID' => $actor->getPHID(), | |||||
'viewPolicy' => PhabricatorPolicies::POLICY_NOONE, | |||||
'editPolicy' => PhabricatorPolicies::POLICY_NOONE, | |||||
)); | |||||
} | } | ||||
public function getTransactionTypes() { | public function getTransactionTypes() { | ||||
$types = parent::getTransactionTypes(); | $types = parent::getTransactionTypes(); | ||||
$types[] = PhabricatorPasteTransaction::TYPE_CONTENT; | |||||
$types[] = PhabricatorPasteTransaction::TYPE_TITLE; | |||||
$types[] = PhabricatorPasteTransaction::TYPE_LANGUAGE; | |||||
$types[] = PhabricatorPasteTransaction::TYPE_STATUS; | |||||
$types[] = PhabricatorTransactions::TYPE_VIEW_POLICY; | $types[] = PhabricatorTransactions::TYPE_VIEW_POLICY; | ||||
$types[] = PhabricatorTransactions::TYPE_EDIT_POLICY; | $types[] = PhabricatorTransactions::TYPE_EDIT_POLICY; | ||||
$types[] = PhabricatorTransactions::TYPE_COMMENT; | $types[] = PhabricatorTransactions::TYPE_COMMENT; | ||||
return $types; | return $types; | ||||
} | } | ||||
protected function shouldApplyInitialEffects( | |||||
PhabricatorLiskDAO $object, | |||||
array $xactions) { | |||||
return true; | |||||
} | |||||
protected function applyInitialEffects( | |||||
PhabricatorLiskDAO $object, | |||||
array $xactions) { | |||||
// Find the most user-friendly filename we can by examining the title of | |||||
// the paste and the pending transactions. We'll use this if we create a | |||||
// new file to store raw content later. | |||||
$name = $object->getTitle(); | |||||
if (!strlen($name)) { | |||||
$name = 'paste.raw'; | |||||
} | |||||
$type_title = PhabricatorPasteTransaction::TYPE_TITLE; | |||||
foreach ($xactions as $xaction) { | |||||
if ($xaction->getTransactionType() == $type_title) { | |||||
$name = $xaction->getNewValue(); | |||||
} | |||||
} | |||||
$this->fileName = $name; | |||||
} | |||||
protected function validateTransaction( | |||||
PhabricatorLiskDAO $object, | |||||
$type, | |||||
array $xactions) { | |||||
$errors = parent::validateTransaction($object, $type, $xactions); | |||||
switch ($type) { | |||||
case PhabricatorPasteTransaction::TYPE_CONTENT: | |||||
if (!$object->getFilePHID() && !$xactions) { | |||||
$error = new PhabricatorApplicationTransactionValidationError( | |||||
$type, | |||||
pht('Required'), | |||||
pht('You must provide content to create a paste.'), | |||||
null); | |||||
$error->setIsMissingFieldError(true); | |||||
$errors[] = $error; | |||||
} | |||||
break; | |||||
} | |||||
return $errors; | |||||
} | |||||
protected function getCustomTransactionOldValue( | |||||
PhabricatorLiskDAO $object, | |||||
PhabricatorApplicationTransaction $xaction) { | |||||
switch ($xaction->getTransactionType()) { | |||||
case PhabricatorPasteTransaction::TYPE_CONTENT: | |||||
return $object->getFilePHID(); | |||||
case PhabricatorPasteTransaction::TYPE_TITLE: | |||||
return $object->getTitle(); | |||||
case PhabricatorPasteTransaction::TYPE_LANGUAGE: | |||||
return $object->getLanguage(); | |||||
case PhabricatorPasteTransaction::TYPE_STATUS: | |||||
return $object->getStatus(); | |||||
} | |||||
} | |||||
protected function getCustomTransactionNewValue( | |||||
PhabricatorLiskDAO $object, | |||||
PhabricatorApplicationTransaction $xaction) { | |||||
switch ($xaction->getTransactionType()) { | |||||
case PhabricatorPasteTransaction::TYPE_TITLE: | |||||
case PhabricatorPasteTransaction::TYPE_LANGUAGE: | |||||
case PhabricatorPasteTransaction::TYPE_STATUS: | |||||
return $xaction->getNewValue(); | |||||
case PhabricatorPasteTransaction::TYPE_CONTENT: | |||||
// If this transaction does not really change the paste content, return | |||||
// the current file PHID so this transaction no-ops. | |||||
$new_content = $xaction->getNewValue(); | |||||
$old_content = $object->getRawContent(); | |||||
$file_phid = $object->getFilePHID(); | |||||
if (($new_content === $old_content) && $file_phid) { | |||||
return $file_phid; | |||||
} | |||||
$file = self::initializeFileForPaste( | |||||
$this->getActor(), | |||||
$this->fileName, | |||||
$xaction->getNewValue()); | |||||
return $file->getPHID(); | |||||
} | |||||
} | |||||
protected function applyCustomInternalTransaction( | |||||
PhabricatorLiskDAO $object, | |||||
PhabricatorApplicationTransaction $xaction) { | |||||
switch ($xaction->getTransactionType()) { | |||||
case PhabricatorPasteTransaction::TYPE_CONTENT: | |||||
$object->setFilePHID($xaction->getNewValue()); | |||||
return; | |||||
case PhabricatorPasteTransaction::TYPE_TITLE: | |||||
$object->setTitle($xaction->getNewValue()); | |||||
return; | |||||
case PhabricatorPasteTransaction::TYPE_LANGUAGE: | |||||
$object->setLanguage($xaction->getNewValue()); | |||||
return; | |||||
case PhabricatorPasteTransaction::TYPE_STATUS: | |||||
$object->setStatus($xaction->getNewValue()); | |||||
return; | |||||
} | |||||
return parent::applyCustomInternalTransaction($object, $xaction); | |||||
} | |||||
protected function applyCustomExternalTransaction( | |||||
PhabricatorLiskDAO $object, | |||||
PhabricatorApplicationTransaction $xaction) { | |||||
switch ($xaction->getTransactionType()) { | |||||
case PhabricatorPasteTransaction::TYPE_CONTENT: | |||||
case PhabricatorPasteTransaction::TYPE_TITLE: | |||||
case PhabricatorPasteTransaction::TYPE_LANGUAGE: | |||||
case PhabricatorPasteTransaction::TYPE_STATUS: | |||||
return; | |||||
} | |||||
return parent::applyCustomExternalTransaction($object, $xaction); | |||||
} | |||||
protected function extractFilePHIDsFromCustomTransaction( | |||||
PhabricatorLiskDAO $object, | |||||
PhabricatorApplicationTransaction $xaction) { | |||||
switch ($xaction->getTransactionType()) { | |||||
case PhabricatorPasteTransaction::TYPE_CONTENT: | |||||
return array($xaction->getNewValue()); | |||||
} | |||||
return parent::extractFilePHIDsFromCustomTransaction($object, $xaction); | |||||
} | |||||
protected function shouldSendMail( | protected function shouldSendMail( | ||||
PhabricatorLiskDAO $object, | PhabricatorLiskDAO $object, | ||||
array $xactions) { | array $xactions) { | ||||
foreach ($xactions as $xaction) { | |||||
switch ($xaction->getTransactionType()) { | if ($this->getIsNewObject()) { | ||||
case PhabricatorPasteTransaction::TYPE_CONTENT: | |||||
return false; | return false; | ||||
default: | |||||
break; | |||||
} | |||||
} | } | ||||
epriestley: The old version of this was weird. I blamed it and it looks like it got sort-of-broken at some… | |||||
return true; | return true; | ||||
} | } | ||||
protected function getMailSubjectPrefix() { | protected function getMailSubjectPrefix() { | ||||
return PhabricatorEnv::getEnvConfig('metamta.paste.subject-prefix'); | return PhabricatorEnv::getEnvConfig('metamta.paste.subject-prefix'); | ||||
} | } | ||||
protected function getMailTo(PhabricatorLiskDAO $object) { | protected function getMailTo(PhabricatorLiskDAO $object) { | ||||
▲ Show 20 Lines • Show All 42 Lines • ▼ Show 20 Lines | final class PhabricatorPasteEditor | ||||
} | } | ||||
protected function shouldPublishFeedStory( | protected function shouldPublishFeedStory( | ||||
PhabricatorLiskDAO $object, | PhabricatorLiskDAO $object, | ||||
array $xactions) { | array $xactions) { | ||||
return true; | return true; | ||||
} | } | ||||
protected function supportsSearch() { | |||||
return false; | |||||
} | |||||
} | } |
The old version of this was weird. I blamed it and it looks like it got sort-of-broken at some point. The original intent was this (only send mail for updates, not for creation). We could take another look at this eventually, not sure if this rule makes sense in modern apps.