1
0
Fork 0
mirror of https://we.phorge.it/source/phorge.git synced 2024-12-21 04:50:55 +01:00

Write "attach" edges when files are attached to objects via comment or other transactions

Summary: Ref T603. When a user comments on an object with an embedded file, write an "attached" edge.

Test Plan: Made a comment on a task with an embedded file, verified the edge was written in Files.

Reviewers: btrahan

Reviewed By: btrahan

CC: aran

Maniphest Tasks: T603

Differential Revision: https://secure.phabricator.com/D7191
This commit is contained in:
epriestley 2013-10-01 16:15:07 -07:00
parent 1d1ecb5629
commit aac490180f

View file

@ -4,6 +4,7 @@
* @task mail Sending Mail * @task mail Sending Mail
* @task feed Publishing Feed Stories * @task feed Publishing Feed Stories
* @task search Search Index * @task search Search Index
* @task files Integration with Files
*/ */
abstract class PhabricatorApplicationTransactionEditor abstract class PhabricatorApplicationTransactionEditor
extends PhabricatorEditor { extends PhabricatorEditor {
@ -407,6 +408,8 @@ abstract class PhabricatorApplicationTransactionEditor
throw new PhabricatorApplicationTransactionValidationException($errors); throw new PhabricatorApplicationTransactionValidationException($errors);
} }
$file_phids = $this->extractFilePHIDs($object, $xactions);
if ($object->getID()) { if ($object->getID()) {
foreach ($xactions as $xaction) { foreach ($xactions as $xaction) {
@ -488,6 +491,10 @@ abstract class PhabricatorApplicationTransactionEditor
} }
} }
if ($file_phids) {
$this->attachFiles($object, $file_phids);
}
foreach ($xactions as $xaction) { foreach ($xactions as $xaction) {
$this->applyExternalEffects($object, $xaction); $this->applyExternalEffects($object, $xaction);
} }
@ -715,7 +722,7 @@ abstract class PhabricatorApplicationTransactionEditor
$texts = array(); $texts = array();
foreach ($xactions as $xaction) { foreach ($xactions as $xaction) {
$texts[] = $this->getMentionableTextsFromTransaction($xaction); $texts[] = $this->getRemarkupBlocksFromTransaction($xaction);
} }
$texts = array_mergev($texts); $texts = array_mergev($texts);
@ -748,7 +755,7 @@ abstract class PhabricatorApplicationTransactionEditor
return $xaction; return $xaction;
} }
protected function getMentionableTextsFromTransaction( protected function getRemarkupBlocksFromTransaction(
PhabricatorApplicationTransaction $transaction) { PhabricatorApplicationTransaction $transaction) {
$texts = array(); $texts = array();
if ($transaction->getComment()) { if ($transaction->getComment()) {
@ -1549,4 +1556,75 @@ abstract class PhabricatorApplicationTransactionEditor
return $field; return $field;
} }
/* -( Files )-------------------------------------------------------------- */
/**
* Extract the PHIDs of any files which these transactions attach.
*
* @task files
*/
private function extractFilePHIDs(
PhabricatorLiskDAO $object,
array $xactions) {
$blocks = array();
foreach ($xactions as $xaction) {
$blocks[] = $this->getRemarkupBlocksFromTransaction($xaction);
}
$blocks = array_mergev($blocks);
if (!$blocks) {
return array();
}
$phids = PhabricatorMarkupEngine::extractFilePHIDsFromEmbeddedFiles(
$blocks);
if (!$phids) {
return array();
}
// Only let a user attach files they can actually see, since this would
// otherwise let you access any file by attaching it to an object you have
// view permission on.
$files = id(new PhabricatorFileQuery())
->setViewer($this->getActor())
->withPHIDs($phids)
->execute();
return mpull($files, 'getPHID');
}
/**
* @task files
*/
private function attachFiles(
PhabricatorLiskDAO $object,
array $file_phids) {
if (!$file_phids) {
return;
}
$editor = id(new PhabricatorEdgeEditor())
->setActor($this->getActor());
// TODO: Edge-based events were almost certainly a terrible idea. If we
// don't suppress this event, the Maniphest listener reenters and adds
// more transactions. Just suppress it until that can get cleaned up.
$editor->setSuppressEvents(true);
$src = $object->getPHID();
$type = PhabricatorEdgeConfig::TYPE_OBJECT_HAS_FILE;
foreach ($file_phids as $dst) {
$editor->addEdge($src, $type, $dst);
}
$editor->save();
}
} }