[NOTE][Posting] Revert regressions introduced with c90efe2c52

Entity Note: It doesn't make sense to handle attachments on Note::create.
Attachments exist out of Notes, they are a thing on their own.
Furthermore, they aren't always handled the same, they most definitely
aren't always uploaded files.

FileQuota: It doesn't make sense to check if a file is greater than max
allowed upload size here. The plugin ensures a user is inside his
allowed quota, it's ignorant to anything else. Whether a file respect
max upload is a core thing that must be handled directly in the Posting
component. TODO: The configuration regarding user and monthly quota
must become FileQuotaPlugin settings and be removed from core.

c90efe2c52 - [UI] Add mechanism for rendering note contents in different formats. Implement plaintext rendering. Use rendered field for note content, rather than the content itself
This commit is contained in:
Diogo Peralta Cordeiro 2021-09-18 03:44:02 +01:00
parent 941cbe6599
commit 2bd19fa087
Signed by: diogo
GPG Key ID: 18D2D35001FBFAB0
3 changed files with 43 additions and 42 deletions

View File

@ -28,7 +28,9 @@ use App\Core\Form;
use function App\Core\I18n\_m;
use App\Core\Modules\Component;
use App\Entity\Actor;
use App\Entity\ActorToAttachment;
use App\Entity\Attachment;
use App\Entity\AttachmentToNote;
use App\Entity\Note;
use App\Util\Common;
use App\Util\Exception\ClientException;
@ -109,6 +111,21 @@ class Posting extends Component
return Event::next;
}
/**
* Store the given note with $content and $attachments, created by
* $actor_id, possibly as a reply to note $reply_to and with flag
* $is_local. Sanitizes $content and $attachments
*
* @param Actor $actor
* @param string $content
* @param string $content_type
* @param array $attachments
* @param null|Note $reply_to
* @param null|Note $repeat_of
*
* @throws ClientException
* @throws ServerException
*/
public static function storeLocalNote(Actor $actor, string $content, string $content_type, array $attachments, ?Note $reply_to = null, ?Note $repeat_of = null)
{
$rendered = null;
@ -118,11 +135,36 @@ class Posting extends Component
'content' => $content,
'content_type' => $content_type,
'rendered' => $rendered,
'attachments' => $attachments, // Not a regular field
'is_local' => true,
]);
$processed_attachments = [];
/** @var \Symfony\Component\HttpFoundation\File\UploadedFile[] $attachments */
foreach ($attachments as $f) {
$filesize = $f->getSize();
$max_file_size = Common::config('attachments', 'file_quota');
if ($max_file_size < $filesize) {
throw new ClientException(_m('No file may be larger than {quota} bytes and the file you sent was {size} bytes. ' .
'Try to upload a smaller version.', ['quota' => $max_file_size, 'size' => $filesize]));
}
Event::handle('EnforceUserFileQuota', [$filesize, $actor->getId()]);
$processed_attachments[] = [GSFile::sanitizeAndStoreFileAsAttachment($f), $f->getClientOriginalName()];
}
DB::persist($note);
// Need file and note ids for the next step
Event::handle('ProcessNoteContent', [$note->getId(), $content, $content_type]);
DB::flush();
if ($processed_attachments != []) {
foreach ($processed_attachments as [$a, $fname]) {
if (DB::count('actor_to_attachment', $args = ['attachment_id' => $a->getId(), 'actor_id' => $args['actor_id']]) === 0) {
DB::persist(ActorToAttachment::create($args));
}
DB::persist(AttachmentToNote::create(['attachment_id' => $a->getId(), 'note_id' => $note->getId(), 'title' => $fname]));
}
}
}
public function onRenderNoteContent(string $content, string $content_type, ?string &$rendered, Actor $author, ?Note $reply_to = null)

View File

@ -69,12 +69,6 @@ select sum(at.size) as total
where ua.actor_id = :actor_id and at.size is not null
END;
$max_file_size = Common::config('attachments', 'file_quota');
if ($max_file_size < $filesize) {
throw new ClientException(_m('No file may be larger than {quota} bytes and the file you sent was {size} bytes. ',
['quota' => $max_file_size, 'size' => $filesize]));
}
$max_user_quota = Common::config('attachments', 'user_quota');
if ($max_user_quota !== false) { // If not disabled
$cache_key_user_total = "FileQuota-total-user-{$user_id}";

View File

@ -25,7 +25,6 @@ use App\Core\Cache;
use App\Core\DB\DB;
use App\Core\Entity;
use App\Core\Event;
use App\Core\GSFile;
use App\Core\VisibilityScope;
use DateTimeInterface;
@ -299,40 +298,6 @@ class Note extends Entity
['note_id' => $this->id, 'actor_id' => $a->getId()]));
}
/**
* Create an instance of NoteToLink or fill in the
* properties of $obj with the associative array $args. Does
* persist the result
*/
public static function create(array $args, mixed $obj = null): self
{
/** @var \Symfony\Component\HttpFoundation\File\UploadedFile[] $attachments */
$attachments = $args['attachments'];
unset($args['attachments']);
$note = parent::create($args, new self());
$processed_attachments = [];
foreach ($attachments as $f) {
Event::handle('EnforceUserFileQuota', [$f->getSize(), $args['actor_id']]);
$processed_attachments[] = [GSFile::sanitizeAndStoreFileAsAttachment($f), $f->getClientOriginalName()];
}
// Need file and note ids for the next step
DB::persist($note);
if ($processed_attachments != []) {
foreach ($processed_attachments as [$a, $fname]) {
if (DB::count('actor_to_attachment', $args = ['attachment_id' => $a->getId(), 'actor_id' => $args['actor_id']]) === 0) {
DB::persist(ActorToAttachment::create($args));
}
DB::persist(AttachmentToNote::create(['attachment_id' => $a->getId(), 'note_id' => $note->getId(), 'title' => $fname]));
}
}
return $note;
}
/**
* @return Actor[]
*/