[PLUGINS][Repeat] Added onNoteDeleteRelated event

Using DB::merge to increment attachment lives when repeating a note,
since it's getting deprecated in the future, an alternative needs to
replace it here
This commit is contained in:
Eliseu Amaro 2022-01-02 16:17:11 +00:00 committed by Diogo Peralta Cordeiro
parent d5080890ac
commit 7ad39fdc83
Signed by: diogo
GPG Key ID: 18D2D35001FBFAB0

View File

@ -23,7 +23,6 @@ namespace Plugin\RepeatNote;
use App\Core\DB\DB;
use App\Core\Event;
use function App\Core\I18n\_m;
use App\Core\Modules\NoteHandlerPlugin;
use App\Core\Router\RouteLoader;
use App\Core\Router\Router;
@ -35,13 +34,13 @@ use App\Util\Exception\BugFoundException;
use App\Util\Exception\ClientException;
use App\Util\Exception\DuplicateFoundException;
use App\Util\Exception\ServerException;
use App\Util\Formatting;
use Component\Language\Entity\Language;
use Component\Posting\Posting;
use DateTime;
use Plugin\RepeatNote\Entity\NoteRepeat;
use const SORT_REGULAR;
use Plugin\RepeatNote\Entity\NoteRepeat as RepeatEntity;
use Symfony\Component\HttpFoundation\Request;
use function App\Core\I18n\_m;
use const SORT_REGULAR;
class RepeatNote extends NoteHandlerPlugin
{
@ -65,17 +64,24 @@ class RepeatNote extends NoteHandlerPlugin
public static function repeatNote(Note $note, int $actor_id, string $source = 'web'): ?Activity
{
$repeat_entity = DB::findBy('note_repeat', [
'actor_id' => $actor_id,
'note_id' => $note->getId(),
])[0] ?? null;
'actor_id' => $actor_id,
'note_id' => $note->getId(),
])[ 0 ] ?? null;
if (!\is_null($repeat_entity)) {
return null;
}
// If it's a repeat, the reply_to should be to the original, conversation ought to be the same
$og_id = $note->getId();
$extra_args['reply_to'] = $og_id;
$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
$repeat = Posting::storeLocalNote(
@ -88,19 +94,19 @@ class RepeatNote extends NoteHandlerPlugin
notify: false,
);
DB::persist(NoteRepeat::create([
'note_id' => $repeat->getId(),
'actor_id' => $actor_id,
'repeat_of' => $og_id,
DB::persist(RepeatEntity::create([
'note_id' => $repeat->getId(),
'actor_id' => $actor_id,
'repeat_of' => $original_note_id,
]));
// Log an activity
$repeat_activity = Activity::create([
'actor_id' => $actor_id,
'verb' => 'repeat',
'actor_id' => $actor_id,
'verb' => 'repeat',
'object_type' => 'note',
'object_id' => $note->getId(),
'source' => $source,
'object_id' => $note->getId(),
'source' => $source,
]);
DB::persist($repeat_activity);
@ -122,30 +128,31 @@ class RepeatNote extends NoteHandlerPlugin
*/
public static function unrepeatNote(int $note_id, int $actor_id, string $source = 'web'): ?Activity
{
$already_repeated = DB::findBy(NoteRepeat::class, ['actor_id' => $actor_id, 'repeat_of' => $note_id])[0] ?? null;
$already_repeated = DB::findBy(RepeatEntity::class, ['actor_id' => $actor_id, 'repeat_of' => $note_id])[ 0 ] ?? null;
if (!\is_null($already_repeated)) { // If it was repeated, then we can undo it
// Find previous repeat activity
$already_repeated_activity = DB::findBy(Activity::class, [
'actor_id' => $actor_id,
'verb' => 'repeat',
'object_type' => 'note',
'object_id' => $already_repeated->getRepeatOf(),
])[0] ?? null;
'actor_id' => $actor_id,
'verb' => 'repeat',
'object_type' => 'note',
'object_id' => $already_repeated->getRepeatOf(),
])[ 0 ] ?? null;
// Remove the clone note
DB::findBy(Note::class, ['id' => $already_repeated->getNoteId()])[0]->delete(actor: Actor::getById($actor_id));
DB::findBy(Note::class, ['id' => $already_repeated->getNoteId()])[ 0 ]->delete(actor: Actor::getById($actor_id));
DB::flush();
// Remove from the note_repeat table
DB::remove(DB::findBy(NoteRepeat::class, ['note_id' => $already_repeated->getNoteId()])[0]);
DB::remove(DB::findBy(RepeatEntity::class, ['note_id' => $already_repeated->getNoteId()])[ 0 ]);
// Log an activity
$undo_repeat_activity = Activity::create([
'actor_id' => $actor_id,
'verb' => 'undo',
'actor_id' => $actor_id,
'verb' => 'undo',
'object_type' => 'activity',
'object_id' => $already_repeated_activity->getId(),
'source' => $source,
'object_id' => $already_repeated_activity->getId(),
'source' => $source,
]);
DB::persist($undo_repeat_activity);
@ -155,17 +162,17 @@ class RepeatNote extends NoteHandlerPlugin
} else {
// Either was undoed already
if (!\is_null($already_repeated_activity = DB::findBy('activity', [
'actor_id' => $actor_id,
'verb' => 'repeat',
'object_type' => 'note',
'object_id' => $note_id,
])[0] ?? null)) {
'actor_id' => $actor_id,
'verb' => 'repeat',
'object_type' => 'note',
'object_id' => $note_id,
])[ 0 ] ?? null)) {
return DB::findBy('activity', [
'actor_id' => $actor_id,
'verb' => 'undo',
'object_type' => 'activity',
'object_id' => $already_repeated_activity->getId(),
])[0] ?? null; // null if not undoed
'actor_id' => $actor_id,
'verb' => 'undo',
'object_type' => 'activity',
'object_id' => $already_repeated_activity->getId(),
])[ 0 ] ?? null; // null if not undoed
} else {
// or it's an attempt to undo something that wasn't repeated in the first place,
return null;
@ -183,8 +190,8 @@ class RepeatNote extends NoteHandlerPlugin
// it's pretty cool
if (str_starts_with($request->get('_route'), 'actor_view_')) {
$notes = array_map(
fn (Note $note) => NoteRepeat::isNoteRepeat($note)
? Note::getById(NoteRepeat::getByPK($note->getId())->getRepeatOf())
fn(Note $note) => RepeatEntity::isNoteRepeat($note)
? Note::getById(RepeatEntity::getByPK($note->getId())->getRepeatOf())
: $note,
$notes,
);
@ -192,7 +199,7 @@ class RepeatNote extends NoteHandlerPlugin
}
// Filter out repeats altogether
$notes = array_filter($notes, fn (Note $note) => !NoteRepeat::isNoteRepeat($note));
$notes = array_filter($notes, fn(Note $note) => !RepeatEntity::isNoteRepeat($note));
return Event::next;
}
@ -211,13 +218,13 @@ class RepeatNote extends NoteHandlerPlugin
// If note is repeated, "is_repeated" is 1, 0 otherwise.
$is_repeat = ($note_repeat = DB::findBy('note_repeat', [
'actor_id' => $user->getId(),
'actor_id' => $user->getId(),
'repeat_of' => $note->getId(),
])) !== [] ? 1 : 0;
// Generating URL for repeat action route
$args = ['note_id' => $is_repeat === 0 ? $note->getId() : $note_repeat[0]->getRepeatOf()];
$type = Router::ABSOLUTE_PATH;
$args = ['note_id' => $is_repeat === 0 ? $note->getId() : $note_repeat[ 0 ]->getRepeatOf()];
$type = Router::ABSOLUTE_PATH;
$repeat_action_url = $is_repeat
? Router::url('repeat_remove', $args, $type)
: Router::url('repeat_add', $args, $type);
@ -227,8 +234,8 @@ class RepeatNote extends NoteHandlerPlugin
$extra_classes = $is_repeat ? 'note-actions-set' : 'note-actions-unset';
$repeat_action = [
'url' => $repeat_action_url,
'title' => $is_repeat ? 'Remove this repeat' : 'Repeat this note!',
'url' => $repeat_action_url,
'title' => $is_repeat ? 'Remove this repeat' : 'Repeat this note!',
'classes' => "button-container repeat-button-container {$extra_classes}",
'note_id' => 'repeat-button-container-' . $note->getId(),
];
@ -254,12 +261,12 @@ class RepeatNote extends NoteHandlerPlugin
$check_user = !\is_null(Common::user());
// The current Note being rendered
$note = $vars['note'];
$note = $vars[ 'note' ];
// Will have actors array, and action string
// Actors are the subjects, action is the verb (in the final phrase)
$repeat_actors = [];
$note_repeats = NoteRepeat::getNoteRepeats($note);
$repeat_actors = [];
$note_repeats = RepeatEntity::getNoteRepeats($note);
// Get actors who repeated the note
foreach ($note_repeats as $repeat) {
@ -276,6 +283,25 @@ class RepeatNote extends NoteHandlerPlugin
return Event::next;
}
/**
* Deletes every repeat entity that is related to a deleted Note in its
* respective table
*
* @param \App\Entity\Note $note
* @param \App\Entity\Actor $actor
*
* @return bool
*/
public function onNoteDeleteRelated(Note &$note, Actor $actor): bool
{
$note_repeats_list = RepeatEntity::getNoteRepeats($note);
foreach ($note_repeats_list as $repeat_entity) {
DB::remove($repeat_entity);
}
return Event::next;
}
/**
* Connects the following Routes to their respective Controllers:
*