Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix default bookmarks activelinks #873

Open
wants to merge 8 commits into
base: next
Choose a base branch
from
Open
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
45 changes: 44 additions & 1 deletion src/freenet/clients/http/WelcomeToadlet.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
import java.net.URI;
import java.util.HashSet;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import freenet.client.ClientMetadata;
import freenet.client.HighLevelSimpleClient;
Expand All @@ -40,6 +42,12 @@ public class WelcomeToadlet extends Toadlet {
/** Suffix {@link #path()} with "#" + BOOKMARKS_ANCHOR to deep link to the bookmark list */
public static final String BOOKMARKS_ANCHOR = "bookmarks";

// Regex Patterns to use when generating keys for activelinks.
// For USK and SSK we want to match from the start of the key to the first instance of -#/ or /#/.
protected static final Pattern PATTERN_USK_SSK = Pattern.compile("^[^/]*/[^/]+/?[/-]?[0-9]+/");
// For bare SSK, CHK, and KSK we match from the start of the key to the first slash and append the activelink there.
protected static final Pattern PATTERN_BARE_SSK_CHK_KSK = Pattern.compile("^[^/]*/");

final Node node;

private static volatile boolean logMINOR;
Expand Down Expand Up @@ -80,7 +88,42 @@ private void addCategoryToList(BookmarkCategory cat, HTMLNode list, boolean noAc
HTMLNode cell = row.addChild("td", "style", "border: none;");
if (item.hasAnActivelink() && !noActiveLinks) {
String initialKey = item.getKey();
String key = '/' + initialKey + (initialKey.endsWith("/") ? "" : "/") + "activelink.png";
// extract the key type
String keyType = initialKey.substring(1,3);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this has to go from 0 to 3 — Java is zero-indexed.

String key = '/' + initialKey + (initialKey.endsWith("/") ? "" : "/");
Matcher match = null;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Seeing that match is not used anywhere outside the two branches within the switch statement, I believe “fix scope issue” and this change are diametrally opposed. 😀

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, but the switch branches share the scope, so that’s how it has to be to avoid inventing new names for the different branches.

switch (keyType) {
case "USK":
case "SSK":
match = PATTERN_USK_SSK.matcher(key);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this only matches when the key is already well-formed ⇒ ideally extract to a static method (uses no this; just put static before the function name to compiler-check that), and write a test that takes the existing bookmarks as input (and maybe some bad but valid examples) and returns the correct key for the activelink.

if (match.matches()) {
Bombe marked this conversation as resolved.
Show resolved Hide resolved
key = match.group(1) + "activelink.png";
}
else { // If no edition # exists, assume bare SSK and just append the activelink.
match = PATTERN_BARE_SSK_CHK_KSK.matcher(key);
if (match.matches()) {
key = match.group(1) + "activelink.png";
}
else {
Logger.minor(this, "Impossible: Regex for USK/SSK didn't match. Key: "+ key);
}
}
break;
case "CHK":
case "KSK": // This assumes the activelink is in the root of any one-shot directory upload.
match = PATTERN_BARE_SSK_CHK_KSK.matcher(key);
if (match.matches()) {
key = match.group(1) + "activelink.png";
}
else { // we append '/' to key at the start if it isn't already there, so this should never be reachable unless the regex is broken.
Logger.minor(this, "Impossible: Regex for CHK/KSK didn't match. Key: "+ key);
}
break;
default:
Logger.minor(this, "Unknown key type: " + keyType + "! Activelink regex needs updating.");
break;
}

cell.addChild("div", "style", "height: 36px; width: 108px;").addChild("a", "href", '/' + item.getKey()).addChild("img", new String[]{"src", "alt", "style", "title"},
new String[]{ key, "activelink", "height: 36px; width: 108px", item.getDescription()});
} else {
Expand Down
Loading