mirror of
https://we.phorge.it/source/phorge.git
synced 2024-12-20 20:40:56 +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:
parent
1d1ecb5629
commit
aac490180f
1 changed files with 80 additions and 2 deletions
|
@ -4,6 +4,7 @@
|
|||
* @task mail Sending Mail
|
||||
* @task feed Publishing Feed Stories
|
||||
* @task search Search Index
|
||||
* @task files Integration with Files
|
||||
*/
|
||||
abstract class PhabricatorApplicationTransactionEditor
|
||||
extends PhabricatorEditor {
|
||||
|
@ -407,6 +408,8 @@ abstract class PhabricatorApplicationTransactionEditor
|
|||
throw new PhabricatorApplicationTransactionValidationException($errors);
|
||||
}
|
||||
|
||||
$file_phids = $this->extractFilePHIDs($object, $xactions);
|
||||
|
||||
if ($object->getID()) {
|
||||
foreach ($xactions as $xaction) {
|
||||
|
||||
|
@ -488,6 +491,10 @@ abstract class PhabricatorApplicationTransactionEditor
|
|||
}
|
||||
}
|
||||
|
||||
if ($file_phids) {
|
||||
$this->attachFiles($object, $file_phids);
|
||||
}
|
||||
|
||||
foreach ($xactions as $xaction) {
|
||||
$this->applyExternalEffects($object, $xaction);
|
||||
}
|
||||
|
@ -715,7 +722,7 @@ abstract class PhabricatorApplicationTransactionEditor
|
|||
|
||||
$texts = array();
|
||||
foreach ($xactions as $xaction) {
|
||||
$texts[] = $this->getMentionableTextsFromTransaction($xaction);
|
||||
$texts[] = $this->getRemarkupBlocksFromTransaction($xaction);
|
||||
}
|
||||
$texts = array_mergev($texts);
|
||||
|
||||
|
@ -748,7 +755,7 @@ abstract class PhabricatorApplicationTransactionEditor
|
|||
return $xaction;
|
||||
}
|
||||
|
||||
protected function getMentionableTextsFromTransaction(
|
||||
protected function getRemarkupBlocksFromTransaction(
|
||||
PhabricatorApplicationTransaction $transaction) {
|
||||
$texts = array();
|
||||
if ($transaction->getComment()) {
|
||||
|
@ -1549,4 +1556,75 @@ abstract class PhabricatorApplicationTransactionEditor
|
|||
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();
|
||||
}
|
||||
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue