[2.x] chore(flags): dispatch model events when flags are deleted#4818
[2.x] chore(flags): dispatch model events when flags are deleted#4818DavideIadeluca wants to merge 4 commits into
Conversation
imorland
left a comment
There was a problem hiding this comment.
The direction here is good — replacing the global HasMany::macro('delete') with an explicit Dismissed event is a real improvement and takes some fragile complexity out of AuditIntegration. A few things need addressing before it can go in, though.
Flags are deleted in two places, and only one is covered. DeleteFlagsHandler dismisses via the UI, but Listener/DeleteFlags also deletes a post's flags in response to Post\Event\Deleted (when a flagged post is deleted). The old macro hooked HasMany::delete globally, so it caught both paths; the new Dismissed event is only dispatched from the command handler. As it stands, deleting a flagged post no longer logs post.dismissed_flags — that's a regression from the current behaviour.
The stated goal isn't fully met for the same reason. The point of the change is to fire model events on flag deletion so observers can hook in, but Listener/DeleteFlags still uses a bulk ->delete(), which fires no model events. So an observer would see UI dismissals but silently miss the post-deletion case.
Needs test coverage for the post-deletion path. AuditTest::delete() only exercises the command path (which still works), so this gap passes CI unnoticed. Please add a test that deletes a flagged post and asserts the dismissal is handled (audit log entry / events fired), so the second path is actually covered.
So: apply the same treatment to Listener/DeleteFlags so both deletion paths behave consistently, and add the post-deletion test. Happy to look again once that's in.
|
@imorland Thanks for the review — I dug into the post-deletion path and the regression turns out not to exist.
That's also why the macro was semantically questionable: it hooked For model events: hooking Deleted can never fire them (the cascade already removed the rows), so I moved Listener/DeleteFlags to One trade-off with listening to Deleting: the flags are removed before the post delete is final, so if the deletion were to abort after the event, the flags would already be gone. Previously the cascade removed them atomically with the post row. The window is a single delete statement on the API path, so I think it's acceptable — but if we want to avoid it entirely, we could snapshot the flags on Deleting (loadMissing('flags')) and run their Eloquent deletes on Deleted: the rows are already cascade-deleted by then, but Model::delete() still fires the model events. Happy to go that route if you prefer. Post-deletion path is now covered: DeleteFlagsOnPostDeletionTest asserts model events fire per flag, and |
Fixes #0000
No eloquent model events being dispatched when a flag gets deleted
Changes proposed in this pull request:
Reviewers should focus on:
Screenshot
Necessity
Confirmed
composer test).Required changes: