Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,7 @@ YetAnotherForum.NET Changelog
* [FIXED] Security: the html of a name was not encoded where a link is rendered as text (the user link BB code [userlink] in posts and the BB code help, the last post popovers of the forums and topics, the notifications, the moderator list): a display name with html ran as a script; the links are encoded like the pages now (an unrelated "]]" in such a text was removed as well)
* [CHANGES] Admin > Dashboard: the list of the not approved accounts shows the newest registrations first (they were in the order of the database, a new registration was on the last page)
* [CHANGES] Admin > BBCode Extensions: the extensions the forum itself needs ([ATTACH] attachments, [MEDIA] the media button of the editor, [USERLINK] mentions, [ALBUMIMG] album images) are marked "Required" and can't be deleted or renamed (posts showed the raw BB code until the next upgrade added them again); every extension of the forum has "Restore Default", which replaces it with the version of BBCodeExtensions.xml (the upgrade keeps the extensions an admin changed); all languages
* [FIXED] Short URLs: with a PageRouteTransformerConvention (e.g. kebab-case urls) the pages kept their old routes (e.g. /forums/posts/1/name) and the redirect to the short url returned 404; the convention replaces the page name in the page route metadata too

# YetAnotherForum.NET v4.0.11
* Upgraded to .NET 11
Expand Down
30 changes: 30 additions & 0 deletions yafsrc/Tests/YAFNET.Tests/Helpers/ShortUrlTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,36 @@ public void Convention_Test(string page, string area, string template, string ex
model.Selectors[0].AttributeRouteModel!.Template.Should().Be(expected);
}

[TestCase("/Posts", null, "/Posts", "{t:int?}/{name}/{handler?}", "/topic")]
[TestCase("/UserProfile", null, "/UserProfile", "{u:int}/{name}/{handler?}", "/user")]
[TestCase("/Posts", "Forums", "/Forums/Posts", "{t:int?}/{name}/{handler?}", "/Forums/topic")]
[TestCase("/Posts", "Forums", "Forums/Posts", "{t:int?}/{name}/{handler?}", "Forums/topic")]
[TestCase("/PostMessage", null, "/PostMessage", null, "/PostMessage")]
[Description("The convention also replaces the page name in the page route metadata, a PageRouteTransformerConvention builds the route from it")]
public void Convention_PageRouteMetadata_Test(string page, string area, string pageRoute, string routeTemplate, string expected)
{
var model = new PageRouteModel($"/Pages{page}.cshtml", page, area);

var selector = new SelectorModel
{
AttributeRouteModel = new AttributeRouteModel
{
Template = AttributeRouteModel.CombineTemplates(pageRoute.TrimStart('/'), routeTemplate)
}
};

selector.EndpointMetadata.Add(new PageRouteMetadata(pageRoute, routeTemplate));

model.Selectors.Add(selector);

new ShortRouteConvention().Apply(model);

var metadata = model.Selectors[0].EndpointMetadata.OfType<PageRouteMetadata>().Single();

metadata.PageRoute.Should().Be(expected);
metadata.RouteTemplate.Should().Be(routeTemplate);
}

[TestCase("/Posts/5/hello", null, "/topic/5/hello")]
[TestCase("/posts/5/hello", null, "/topic/5/hello")]
[TestCase("/Topics/5/general/2", null, "/forum/5/general/2")]
Expand Down
16 changes: 15 additions & 1 deletion yafsrc/YAFNET.Core/Helpers/ShortRouteConvention.cs
Original file line number Diff line number Diff line change
Expand Up @@ -53,14 +53,28 @@ public void Apply(PageRouteModel model)
var areaPrefix = model.AreaName.IsSet() ? $"{model.AreaName}/" : string.Empty;
var pagePrefix = $"{areaPrefix}{page}";

foreach (var route in model.Selectors.Select(selector => selector.AttributeRouteModel))
foreach (var selector in model.Selectors)
{
var route = selector.AttributeRouteModel;

if (route?.Template is null || !route.Template.StartsWith(pagePrefix, StringComparison.OrdinalIgnoreCase))
{
continue;
}

route.Template = $"{areaPrefix}{segment}{route.Template[pagePrefix.Length..]}";

// a PageRouteTransformerConvention (e.g. kebab-case urls) builds the route again from the
// page route of the metadata (e.g. /Forums/Posts) instead of the template, so it gets the short url segment too
var metadata = selector.EndpointMetadata.OfType<PageRouteMetadata>().FirstOrDefault();

if (metadata is null || !metadata.PageRoute.TrimStart('/').Equals(pagePrefix, StringComparison.OrdinalIgnoreCase))
{
continue;
}

selector.EndpointMetadata[selector.EndpointMetadata.IndexOf(metadata)] =
new PageRouteMetadata($"{metadata.PageRoute[..^pagePrefix.Length]}{areaPrefix}{segment}", metadata.RouteTemplate);
}

if (!page.Equals(nameof(ForumPages.Posts), StringComparison.OrdinalIgnoreCase))
Expand Down