[PLUGIN][RepeatNote][COMPONENTS][Posting] Review and fix RepeatNote. Handle attachment lives in Posting

This commit is contained in:
Hugo Sales 2022-01-17 20:56:14 +00:00
parent 9bd1f42843
commit 1856af68b3
Signed by untrusted user: someonewithpc
GPG Key ID: 7D0C7EAFC9D835A0
4 changed files with 43 additions and 58 deletions

View File

@ -210,11 +210,12 @@ class Posting extends Component
array $processed_attachments = [], array $processed_attachments = [],
array $process_note_content_extra_args = [], array $process_note_content_extra_args = [],
bool $notify = true, bool $notify = true,
?string $rendered = null,
string $source = 'web',
): Note { ): Note {
$scope ??= VisibilityScope::EVERYWHERE; // TODO: If site is private, default to LOCAL $scope ??= VisibilityScope::EVERYWHERE; // TODO: If site is private, default to LOCAL
$rendered = null;
$mentions = []; $mentions = [];
if (!empty($content)) { if (\is_null($rendered) && !empty($content)) {
Event::handle('RenderNoteContent', [$content, $content_type, &$rendered, $actor, $locale, &$mentions]); Event::handle('RenderNoteContent', [$content, $content_type, &$rendered, $actor, $locale, &$mentions]);
} }
@ -227,6 +228,7 @@ class Posting extends Component
'is_local' => true, 'is_local' => true,
'scope' => $scope, 'scope' => $scope,
'reply_to' => $reply_to_id, 'reply_to' => $reply_to_id,
'source' => $source,
]); ]);
/** @var UploadedFile[] $attachments */ /** @var UploadedFile[] $attachments */
@ -255,6 +257,7 @@ class Posting extends Component
DB::persist(ActorToAttachment::create($args)); DB::persist(ActorToAttachment::create($args));
} }
DB::persist(AttachmentToNote::create(['attachment_id' => $a->getId(), 'note_id' => $note->getId(), 'title' => $fname])); DB::persist(AttachmentToNote::create(['attachment_id' => $a->getId(), 'note_id' => $note->getId(), 'title' => $fname]));
$a->livesIncrementAndGet();
} }
} }
@ -265,9 +268,10 @@ class Posting extends Component
'verb' => 'create', 'verb' => 'create',
'object_type' => 'note', 'object_type' => 'note',
'object_id' => $note->getId(), 'object_id' => $note->getId(),
'source' => 'web', 'source' => $source,
]); ]);
DB::persist($activity); DB::persist($activity);
if (!\is_null($target)) { if (!\is_null($target)) {
$target = \is_int($target) ? Actor::getById($target) : $target; $target = \is_int($target) ? Actor::getById($target) : $target;
$mentions[] = [ $mentions[] = [

View File

@ -26,6 +26,8 @@ namespace Plugin\RepeatNote\Entity;
use App\Core\Cache; use App\Core\Cache;
use App\Core\DB\DB; use App\Core\DB\DB;
use App\Core\Entity; use App\Core\Entity;
use App\Entity\Actor;
use App\Entity\LocalUser;
use App\Entity\Note; use App\Entity\Note;
/** /**
@ -81,15 +83,29 @@ class NoteRepeat extends Entity
// @codeCoverageIgnoreEnd // @codeCoverageIgnoreEnd
// }}} Autocode // }}} Autocode
public static function cacheKeys(int|Note $note_id): array public static function cacheKeys(int|Note $note_id, null|int|Actor|LocalUser $actor_id = null): array
{ {
$note_id = \is_int($note_id) ? $note_id : $note_id->getId(); $note_id = \is_int($note_id) ? $note_id : $note_id->getId();
$actor_id = \is_null($actor_id) ? null : (\is_int($actor_id) ? $actor_id : $actor_id->getId());
return [ return [
'is_repeat' => "note-repeat-is-{$note_id}", 'is_repeat' => "note-repeat-is-{$note_id}",
'repeats' => "note-repeats-{$note_id}", 'repeats' => "note-repeats-{$note_id}",
'actor_repeat' => "note-repeat-{$note_id}-{$actor_id}",
]; ];
} }
public static function getNoteActorRepeat(int|Note $note_id, int $actor_id): ?self
{
$note_id = \is_int($note_id) ? $note_id : $note_id->getId();
return Cache::get(
self::cacheKeys($note_id, $actor_id)['actor_repeat'],
fn () => DB::findOneBy('note_repeat', [
'actor_id' => $actor_id,
'repeat_of' => $note_id,
], return_null: true),
);
}
/** /**
* @return bool Returns true if Note provided is a repeat of another Note * @return bool Returns true if Note provided is a repeat of another Note
*/ */

View File

@ -30,7 +30,6 @@ use App\Core\Router\RouteLoader;
use App\Core\Router\Router; use App\Core\Router\Router;
use App\Entity\Activity; use App\Entity\Activity;
use App\Entity\Actor; use App\Entity\Actor;
use App\Entity\LocalUser;
use App\Entity\Note; use App\Entity\Note;
use App\Util\Common; use App\Util\Common;
use App\Util\Exception\BugFoundException; use App\Util\Exception\BugFoundException;
@ -46,15 +45,6 @@ use Symfony\Component\HttpFoundation\Request;
class RepeatNote extends NoteHandlerPlugin class RepeatNote extends NoteHandlerPlugin
{ {
public static function cacheKeys(int|Note $note_id, int|Actor|LocalUser $actor_id): array
{
$note_id = \is_int($note_id) ? $note_id : $note_id->getId();
$actor_id = \is_int($actor_id) ? $actor_id : $actor_id->getId();
return [
'repeat' => "note-repeat-{$note_id}-{$actor_id}",
];
}
/** /**
* **Repeats a Note** * **Repeats a Note**
* *
@ -74,28 +64,12 @@ class RepeatNote extends NoteHandlerPlugin
*/ */
public static function repeatNote(Note $note, int $actor_id, string $source = 'web'): ?Activity public static function repeatNote(Note $note, int $actor_id, string $source = 'web'): ?Activity
{ {
$note_repeat = Cache::get( $note_repeat = RepeatEntity::getNoteActorRepeat($note, $actor_id);
self::cacheKeys($note->getId(), $actor_id)['repeat'],
fn () => DB::findOneBy('note_repeat', [
'actor_id' => $actor_id,
'note_id' => $note->getId(),
], return_null: true),
);
if (!\is_null($note_repeat)) { if (!\is_null($note_repeat)) {
return null; return null;
} }
// If it's a repeat, the reply_to should be to the original, conversation ought to be the same
$original_note_id = $note->getId(); $original_note_id = $note->getId();
$extra_args['reply_to'] = $original_note_id;
$attachments = $note->getAttachmentsWithTitle();
foreach ($attachments as $attachment) {
// TODO: merge is going be deprecated in doctrine 3
$attachment[0]->livesIncrementAndGet();
DB::merge($attachment[0]);
}
// Create a new note with the same content as the original // Create a new note with the same content as the original
$repeat = Posting::storeLocalNote( $repeat = Posting::storeLocalNote(
@ -103,9 +77,11 @@ class RepeatNote extends NoteHandlerPlugin
content: $note->getContent(), content: $note->getContent(),
content_type: $note->getContentType(), content_type: $note->getContentType(),
locale: \is_null($lang_id = $note->getLanguageId()) ? null : Language::getById($lang_id)->getLocale(), locale: \is_null($lang_id = $note->getLanguageId()) ? null : Language::getById($lang_id)->getLocale(),
// If it's a repeat, the reply_to should be to the original, conversation ought to be the same
reply_to_id: $note->getReplyTo(),
processed_attachments: $note->getAttachmentsWithTitle(), processed_attachments: $note->getAttachmentsWithTitle(),
process_note_content_extra_args: $extra_args,
notify: false, notify: false,
rendered: $note->getRendered(),
); );
DB::persist(RepeatEntity::create([ DB::persist(RepeatEntity::create([
@ -113,7 +89,8 @@ class RepeatNote extends NoteHandlerPlugin
'actor_id' => $actor_id, 'actor_id' => $actor_id,
'repeat_of' => $original_note_id, 'repeat_of' => $original_note_id,
])); ]));
Cache::delete(self::cacheKeys($note->getId(), $actor_id)['repeat']); Cache::delete(RepeatEntity::cacheKeys($note->getId(), $actor_id)['actor_repeat']);
Cache::delete(RepeatEntity::cacheKeys($note->getId())['repeats']);
// Log an activity // Log an activity
$repeat_activity = Activity::create([ $repeat_activity = Activity::create([
@ -125,7 +102,7 @@ class RepeatNote extends NoteHandlerPlugin
]); ]);
DB::persist($repeat_activity); DB::persist($repeat_activity);
Event::handle('NewNotification', [$actor = Actor::getById($actor_id), $repeat_activity, [], _m('{nickname} repeated note {note_id}.', ['nickname' => $actor->getNickname(), 'note_id' => $repeat_activity->getObjectId()])]); Event::handle('NewNotification', [$actor = Actor::getById($actor_id), $repeat_activity, [], _m('{nickname} repeated note {note_id}.', ['{nickname}' => $actor->getNickname(), '{note_id}' => $repeat_activity->getObjectId()])]);
return $repeat_activity; return $repeat_activity;
} }
@ -143,13 +120,7 @@ class RepeatNote extends NoteHandlerPlugin
*/ */
public static function unrepeatNote(int $note_id, int $actor_id, string $source = 'web'): ?Activity public static function unrepeatNote(int $note_id, int $actor_id, string $source = 'web'): ?Activity
{ {
$note_repeat = Cache::get( $note_repeat = RepeatEntity::getNoteActorRepeat($note_id, $actor_id);
self::cacheKeys($note_id, $actor_id)['repeat'],
fn () => DB::findOneBy('note_repeat', [
'actor_id' => $actor_id,
'note_id' => $note_id,
], return_null: true),
);
if (!\is_null($note_repeat)) { // If it was repeated, then we can undo it if (!\is_null($note_repeat)) { // If it was repeated, then we can undo it
// Find previous repeat activity // Find previous repeat activity
@ -162,11 +133,11 @@ class RepeatNote extends NoteHandlerPlugin
// Remove the clone note // Remove the clone note
DB::findOneBy(Note::class, ['id' => $note_repeat->getNoteId()])->delete(actor: Actor::getById($actor_id)); DB::findOneBy(Note::class, ['id' => $note_repeat->getNoteId()])->delete(actor: Actor::getById($actor_id));
DB::flush();
// Remove from the note_repeat table // Remove from the note_repeat table
DB::removeBy(RepeatEntity::class, ['note_id' => $note_repeat->getNoteId()]); DB::removeBy(RepeatEntity::class, ['note_id' => $note_repeat->getNoteId()]);
Cache::delete(self::cacheKeys($note_id, $actor_id)['repeat']); Cache::delete(RepeatEntity::cacheKeys($note_id, $actor_id)['actor_repeat']);
Cache::delete(RepeatEntity::cacheKeys($note_repeat->getNoteId())['repeats']);
// Log an activity // Log an activity
$undo_repeat_activity = Activity::create([ $undo_repeat_activity = Activity::create([
@ -178,7 +149,7 @@ class RepeatNote extends NoteHandlerPlugin
]); ]);
DB::persist($undo_repeat_activity); DB::persist($undo_repeat_activity);
Event::handle('NewNotification', [$actor = Actor::getById($actor_id), $undo_repeat_activity, [], _m('{nickname} unrepeated note {note_id}.', ['nickname' => $actor->getNickname(), 'note_id' => $note_id])]); Event::handle('NewNotification', [$actor = Actor::getById($actor_id), $undo_repeat_activity, [], _m('{nickname} unrepeated note {note_id}.', ['{nickname}' => $actor->getNickname(), '{note_id}' => $note_id])]);
return $undo_repeat_activity; return $undo_repeat_activity;
} else { } else {
@ -238,13 +209,7 @@ class RepeatNote extends NoteHandlerPlugin
return Event::next; return Event::next;
} }
$note_repeat = Cache::get( $note_repeat = RepeatEntity::getNoteActorRepeat($note, $user->getId());
self::cacheKeys($note->getId(), $user->getId())['repeat'],
fn () => DB::findOneBy('note_repeat', [
'actor_id' => $user->getId(),
'note_id' => $note->getId(),
], return_null: true),
);
// If note is repeated, "is_repeated" is 1, 0 otherwise. // If note is repeated, "is_repeated" is 1, 0 otherwise.
$is_repeat = !\is_null($note_repeat); $is_repeat = !\is_null($note_repeat);

View File

@ -54,7 +54,7 @@ use Symfony\Component\Mime\MimeTypes;
class GSFile class GSFile
{ {
/** /**
* Perform file validation (checks and normalization), store the given file if needed and increment lives * Perform file validation (checks and normalization), store the given file if needed
* *
* @throws DuplicateFoundException * @throws DuplicateFoundException
*/ */
@ -65,7 +65,6 @@ class GSFile
try { try {
$attachment = DB::findOneBy('attachment', ['filehash' => $hash]); $attachment = DB::findOneBy('attachment', ['filehash' => $hash]);
// Attachment Exists // Attachment Exists
$attachment->livesIncrementAndGet();
// We had this attachment, but not the file, thus no filename, update meta // We had this attachment, but not the file, thus no filename, update meta
if (\is_null($attachment->getFilename())) { if (\is_null($attachment->getFilename())) {
$mimetype = $attachment->getMimetype() ?? $file->getMimeType(); $mimetype = $attachment->getMimetype() ?? $file->getMimeType();
@ -128,6 +127,7 @@ class GSFile
'size' => $file->getSize(), 'size' => $file->getSize(),
'width' => $width, 'width' => $width,
'height' => $height, 'height' => $height,
'lives' => 0,
]); ]);
if (!$check_is_supported_mimetype || self::isMimetypeAllowed($mimetype)) { if (!$check_is_supported_mimetype || self::isMimetypeAllowed($mimetype)) {
$file->move(Common::config('attachments', 'dir'), $hash); $file->move(Common::config('attachments', 'dir'), $hash);