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

Drag & Drop Task Cover Image: also attach

Summary:
After this change, the cover image is finally always visible to others (to people who can see the Task)
instead of being visible only to yourself.

Example conversation before this change:

- Anna: (uploads a nice Cover Image on the Task, using drag & drop from the Workboard)
- Bob: Thanks! But I cannot see that image. Please change visibility.
- Anna: Ouch! How?
- Bob: I don't know. Try Files > your File > Edit > Set Visibility > Something that includes "Bob".
- Anna: Done. Do you see my image now? (Spoiler: it's a green pepper)
- Bob: Yes and no. I mean, I can see the original file, but not from the Workboard.
- Anna: Uh? Have you tried refreshing your cache?
- Bob: Yes. Still invisible from Workboard.
- Anna: Maybe you have to change Defaults Permissions of the "Files" Application, to have "All Users"
- Bob: That is a bit invasive. Let's try. I changed the Files policy globally. Try re-uploading your image again.
- Anna: Done. Do you see it now?
- Bob: Yes! I can finally see it now, also from the Workboard.

Example conversation after this change:

- Anna: (uploads a nice Cover Image on the Task, using drag & drop from the Workboard)

Done! So, after this change, everything works as expected. Every task participant can see that image,
and nobody starts a nonsense conversation, and nobody needs to do random experiments with
default application policies.

So, after this change:

- the original file is attached to the Task
- the cover image is not orphan anymore, but mentioned in the "Transforms" of the original file
- the cover image so now inherits the original file policies

So, Task participants can finally see that cover image (transform), since it comes from a file,
and that file is attached to the Task, and since they can see that Task.

For a micro-optimization, this change refactors a bit PhabricatorFile.

You can still work in object-oriented:

    lang=php
    $file->attachToObject($task_phid);

But now you can also work directly with PHIDs, if necessary:

    lang=php
    PhabricatorFile::attachFileToObject($file_phid, $task_phid)

This patch somehow improves T15768.

Closes T15163
Closes T15703

Test Plan:
Visit a Workboard with a Column and a Task. Then:

1. Drag & Drop GNU.png from your computer to that Task
2. Drag & Drop GNU.png again there
3. Drag & Drop Linux.png to that Task
4. Drag & Drop Linux.png again
5. No nuclear implosion.
6. In all cases Task participants can see your Cover Image.

Reviewers: O1 Blessed Committers, avivey

Reviewed By: O1 Blessed Committers, avivey

Subscribers: avivey, speck, tobiaswiese, Matthew, Cigaryno

Maniphest Tasks: T15163, T15703

Differential Revision: https://we.phorge.it/D25475
This commit is contained in:
Valerio Bozzolan 2024-04-29 17:33:28 +02:00
parent f75c7ce766
commit 3367fe0017
4 changed files with 106 additions and 4 deletions

View file

@ -1414,11 +1414,33 @@ final class PhabricatorFile extends PhabricatorFileDAO
/**
* Write the policy edge between this file and some object.
* This method is successful even if the file is already attached.
*
* @param phid Object PHID to attach to.
* @return this
*/
public function attachToObject($phid) {
self::attachFileToObject($this->getPHID(), $phid);
return $this;
}
/**
* Write the policy edge between a file and some object.
* This method is successful even if the file is already attached.
* NOTE: Please avoid to use this static method directly.
* Instead, use PhabricatorFile#attachToObject(phid).
*
* @param phid File PHID to attach from.
* @param phid Object PHID to attach to.
* @return void
*/
public static function attachFileToObject($file_phid, $object_phid) {
// It can be easy to confuse the two arguments. Be strict.
if (phid_get_type($file_phid) !== PhabricatorFileFilePHIDType::TYPECONST) {
throw new Exception(pht('The first argument must be a phid of a file.'));
}
$attachment_table = new PhabricatorFileAttachment();
$attachment_conn = $attachment_table->establishConnection('w');
@ -1432,14 +1454,12 @@ final class PhabricatorFile extends PhabricatorFileDAO
attacherPHID = VALUES(attacherPHID),
dateModified = VALUES(dateModified)',
$attachment_table,
$phid,
$this->getPHID(),
$object_phid,
$file_phid,
PhabricatorFileAttachment::MODE_ATTACH,
null,
PhabricatorTime::getNow(),
PhabricatorTime::getNow());
return $this;
}

View file

@ -277,6 +277,70 @@ final class PhabricatorFileTestCase extends PhabricatorTestCase {
pht('Attached Thumbnail Visibility'));
}
public function testFileVisibilityManually() {
$author = $this->generateNewTestUser();
$viewer = $this->generateNewTestUser();
$author_and_viewer = array($author, $viewer);
$engine = new PhabricatorTestStorageEngine();
$params = array(
'name' => 'test.dat',
'viewPolicy' => PhabricatorPolicies::POLICY_NOONE,
'authorPHID' => $author->getPHID(),
'storageEngines' => array(
$engine,
),
);
$data = Filesystem::readRandomCharacters(64);
$file = PhabricatorFile::newFromFileData($data, $params);
// Create an object.
$object = ManiphestTask::initializeNewTask($author)
->setTitle(pht('Test Task'))
->setViewPolicy(PhabricatorPolicies::getMostOpenPolicy())
->save();
// Test file's visibility before attachment.
$this->assertEqual(
array(
true,
false,
),
$this->canViewFile($author_and_viewer, $file),
pht('File Visibility Before Being Attached'));
// Manually attach.
$file->attachToObject($object->getPHID());
// Test the referenced file's visibility.
$this->assertEqual(
array(
true,
true,
),
$this->canViewFile($author_and_viewer, $file),
pht('File Visibility After Being Attached'));
// Try again. This should not explode.
$file->attachToObject($object->getPHID());
// Try again with this low-level. Again, this should not explode.
PhabricatorFile::attachFileToObject($file->getPHID(), $object->getPHID());
// Try again but using the wrong low-level usage.
$is_wrong_usage = false;
try {
PhabricatorFile::attachFileToObject($object->getPHID(), $file->getPHID());
} catch (Throwable $e) {
$is_wrong_usage = true;
}
$this->assertEqual(
true,
$is_wrong_usage,
pht('Check Attach Low-Level Validation'));
}
private function canViewFile(array $users, PhabricatorFile $file) {
$results = array();
foreach ($users as $user) {

View file

@ -27,14 +27,31 @@ final class ManiphestTaskCoverImageTransaction
return;
}
// Generate an image transformation, usually smaller (orphan now).
$xform_key = PhabricatorFileThumbnailTransform::TRANSFORM_WORKCARD;
$xform = PhabricatorFileTransform::getTransformByKey($xform_key)
->executeTransform($file);
// Make that image transformation non-orphan.
id(new PhabricatorTransformedFile())
->setOriginalPHID($file_phid)
->setTransformedPHID($xform->getPHID())
->setTransform($xform_key)
->save();
$object->setProperty('cover.filePHID', $file->getPHID());
$object->setProperty('cover.thumbnailPHID', $xform->getPHID());
}
public function applyExternalEffects($object, $value) {
// If the File has a Cover Image, attach that as side-effect.
// Otherwise, the Cover Image may be invisible to participants.
$file_phid = $object->getProperty('cover.filePHID');
if ($file_phid) {
PhabricatorFile::attachFileToObject($file_phid, $object->getPHID());
}
}
public function getTitle() {
$old = $this->getOldValue();
$new = $this->getNewValue();

View file

@ -35,6 +35,7 @@ final class PhabricatorProjectCoverController
$xactions = array();
// Set the new Cover Image.
$xactions[] = id(new ManiphestTransaction())
->setTransactionType(ManiphestTaskCoverImageTransaction::TRANSACTIONTYPE)
->setNewValue($file->getPHID());