matteocontrini there's probably room for improvement, but I don't see anything fundamentally wrong with the current approach.
I'm not sure why exactly we dispatch the events before that last save. Maybe it's so we reduce the number of database calls in case the event modifies the discussion object itself?
I think the post does exist in the database. What isn't saved is the discussion first and last post relationships, but they should be available from the model during the event nonetheless.
It's true that if there is an error from an extension during the event, those relationships might end up not being saved to the database. However I don't think there will be any loss of data. The post itself should already be saved and visible. Posting a new reply should fix the last post relationship. There's just the first post relationship that might not fix itself automatically.
In general, any event handler for a [...]ed
event (Saved
, Posted
, Replied
, ...) should have proper error management, because not only an error can lead to an incorrect state in Flarum due to this, it will likely also result in incorrect state in other extensions that use the same event.