[2.x] feat(core, mentions, likes): implement IdWithDisplayNameSlugDriver#4470
[2.x] feat(core, mentions, likes): implement IdWithDisplayNameSlugDriver#4470imorland merged 10 commits intoflarum:2.xfrom
IdWithDisplayNameSlugDriver#4470Conversation
imorland
left a comment
There was a problem hiding this comment.
The approach is clean and consistent with how DiscussionPageResolver works. A few things to address before merging:
Bug: strpos falsiness in fromSlug
if (strpos($slug, '-')) {This is falsy when the slug starts with - (returns 0). Should be:
if (strpos($slug, '-') !== false) {Empty display name edge case
Str::slug('') on a null/empty display_name produces a trailing -, e.g. 2-. Worth guarding:
$suffix = Str::slug($instance->display_name);
return $suffix ? $instance->id . '-' . $suffix : (string) $instance->id;Missing fromSlug tests
Only slug generation is tested. Would be good to add cases for:
fromSlug('2-normal', $actor)→ user 2fromSlug('2', $actor)→ user 2 (bare ID fallback)fromSlug('2-wrong-slug', $actor)→ user 2 (ID takes precedence)
FlarumGenericRoute type
Routes.ts sets route.resolverClass but the diff doesn't show the type being updated. If resolverClass isn't already on FlarumGenericRoute, this will be an implicit any. Can you confirm?
URL canonicalization timing
On first page load, app.store.getById returns null (user not yet loaded), so replaceState won't fire until a re-render. Same behavior as DiscussionPageResolver so it's acceptable — just flagging it as a known limitation in case it comes up.
Followed up with quite a bit of manual testing and research and it seems like this is more of a general PHP gotcha then an issue in this implementation. Technically it's not needed here at this stage but for future robustness I've added it anyways in 44d4e60.
I actually had a similar fallback to whats suggested here in place at the early stages of developing this PR but removed it as
Double checked this one:
Yeah good shout! Why not.. Added in 9df5e67
In most cases this doesn't apply as the user in question is already preloaded by the backend. If a route is resolved for a user which is not yet in the store then yes there is a tiny delay until the state is replaced. As you said the behaviour is similar for the |
Documentation PR at flarum/docs#514
Fixes #0000
Changes proposed in this pull request:
Implements a new slug driver for
User. This slug driver works very similarly to theDiscussionSlug driver in that that the id is used as the main mechanism for routing, but the models slug added for cosmetics.Modifies the frontend routes extender to support adding a custom resolver class. The new
UserPageResolveris needed to patch the URL in case the user has changed their username/nickname.Example:
http://localhost/u/1-nonsense-slug-which-is-not-the-actual-user-or-nickname/likesis replaced withhttp://localhost/u/1-admin-username/likesReviewers should focus on:
Should in theory be backwards compatible. Needs a accompanying documentation PR for extension developers to start to consume the
UserPageResolverwhen adding routes like this:Screenshot
Necessity
Confirmed
composer test).Required changes: