From 3485037fd7cdba174b1098a1c9e06de63cf3d111 Mon Sep 17 00:00:00 2001
From: zzz <zzz@mail.i2p>
Date: Mon, 17 Feb 2025 18:50:24 +0000
Subject: [PATCH] SusiDNS: Implement sort-by-added-date

---
 .../java/src/i2p/susi/dns/AddressBean.java    |  9 ++-
 .../src/i2p/susi/dns/AddressByDateSorter.java | 56 +++++++++++++++++++
 .../src/i2p/susi/dns/AddressbookBean.java     |  2 +-
 .../src/i2p/susi/dns/NamingServiceBean.java   | 21 +++++--
 apps/susidns/src/jsp/addressbook.jsp          | 13 ++++-
 5 files changed, 91 insertions(+), 10 deletions(-)
 create mode 100644 apps/susidns/src/java/src/i2p/susi/dns/AddressByDateSorter.java

diff --git a/apps/susidns/src/java/src/i2p/susi/dns/AddressBean.java b/apps/susidns/src/java/src/i2p/susi/dns/AddressBean.java
index 9ae5d17197..5dc619050f 100644
--- a/apps/susidns/src/java/src/i2p/susi/dns/AddressBean.java
+++ b/apps/susidns/src/java/src/i2p/susi/dns/AddressBean.java
@@ -266,10 +266,13 @@ public class AddressBean
                 return stype.toString();
 	}
 
-	/** @since 0.8.7 */
-	private String getProp(String p) {
+	/**
+	 * @return non-null, "" if not found
+	 * @since 0.8.7, package private since 0.9.66
+	 */
+	String getProp(String p) {
 		if (props == null)
-                    return "";
+			return "";
 		String rv = props.getProperty(p);
 		return rv != null ? rv : "";
 	}
diff --git a/apps/susidns/src/java/src/i2p/susi/dns/AddressByDateSorter.java b/apps/susidns/src/java/src/i2p/susi/dns/AddressByDateSorter.java
new file mode 100644
index 0000000000..c0962b01db
--- /dev/null
+++ b/apps/susidns/src/java/src/i2p/susi/dns/AddressByDateSorter.java
@@ -0,0 +1,56 @@
+/*
+ * Created on Sep 02, 2005
+ * 
+ *  This file is part of susidns project, see http://susi.i2p/
+ *  
+ *  Copyright (C) 2005 <susi23@mail.i2p>
+ *
+ *  This program is free software; you can redistribute it and/or modify
+ *  it under the terms of the GNU General Public License as published by
+ *  the Free Software Foundation; either version 2 of the License, or
+ *  (at your option) any later version.
+ *
+ *  This program is distributed in the hope that it will be useful,
+ *  but WITHOUT ANY WARRANTY; without even the implied warranty of
+ *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ *  GNU General Public License for more details.
+ *
+ *  You should have received a copy of the GNU General Public License
+ *  along with this program; if not, write to the Free Software
+ *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
+ *  
+ * $Revision: 1.2 $
+ */
+
+package i2p.susi.dns;
+
+import java.io.Serializable;
+import java.util.Comparator;
+
+/**
+ *  Newest first, then alphabetical
+ *  @since 0.9.66
+ */
+public class AddressByDateSorter implements Comparator<AddressBean>, Serializable
+{
+	public int compare(AddressBean a, AddressBean b)
+	{
+		String ad = a.getProp("a");
+		String bd = b.getProp("a");
+		long al;
+		long bl;
+		if (ad.length() > 0)
+			al = Long.parseLong(ad);
+		else
+			al = 0;
+		if (bd.length() > 0)
+			bl = Long.parseLong(bd);
+		else
+			bl = 0;
+		if (al < bl)
+			return 1;
+		if (al > bl)
+			return -1;
+		return a.getDisplayName().compareToIgnoreCase(b.getDisplayName());
+	}	
+}
diff --git a/apps/susidns/src/java/src/i2p/susi/dns/AddressbookBean.java b/apps/susidns/src/java/src/i2p/susi/dns/AddressbookBean.java
index 572c79dee9..28f86771c6 100644
--- a/apps/susidns/src/java/src/i2p/susi/dns/AddressbookBean.java
+++ b/apps/susidns/src/java/src/i2p/susi/dns/AddressbookBean.java
@@ -149,7 +149,7 @@ public class AddressbookBean extends BaseBean
 				String name = (String) entry.getKey();
 				String destination = (String) entry.getValue();
 				if( filter != null && filter.length() > 0 ) {
-					if( filter.compareTo( "0-9" ) == 0 ) {
+					if( filter.equals("0-9")) {
 						char first = name.charAt(0);
 						if( first < '0' || first > '9' )
 							continue;
diff --git a/apps/susidns/src/java/src/i2p/susi/dns/NamingServiceBean.java b/apps/susidns/src/java/src/i2p/susi/dns/NamingServiceBean.java
index a52fb337e3..91c73e4ed2 100644
--- a/apps/susidns/src/java/src/i2p/susi/dns/NamingServiceBean.java
+++ b/apps/susidns/src/java/src/i2p/susi/dns/NamingServiceBean.java
@@ -167,8 +167,9 @@ public class NamingServiceBean extends AddressbookBean
 			Map<String, Destination> results;
 			Properties searchProps = new Properties();
 			// only blockfile needs this
+			boolean sortByDate = "latest".equals(filter);
 			searchProps.setProperty("list", getFileName());
-			if (filter != null) {
+			if (filter != null && !sortByDate) {
 				String startsAt = filter.equals("0-9") ? "[0-9]" : filter;
 				searchProps.setProperty("startsWith", startsAt);
 			}
@@ -188,7 +189,7 @@ public class NamingServiceBean extends AddressbookBean
 			debug("Result count: " + results.size());
 			for (Map.Entry<String, Destination> entry : results.entrySet()) {
 				String name = entry.getKey();
-				if( filter != null && filter.length() > 0 ) {
+				if (filter != null && filter.length() > 0 && !sortByDate) {
 					if (filter.equals("0-9")) {
 						char first = name.charAt(0);
 						if( first < '0' || first > '9' )
@@ -205,15 +206,25 @@ public class NamingServiceBean extends AddressbookBean
 				}
 				String destination = entry.getValue().toBase64();
 				if (destination != null) {
-					list.addLast( new AddressBean( name, destination ) );
+					AddressBean bean = new AddressBean(name, destination);
+					if (sortByDate) {
+						Properties p = new Properties();
+						Destination d = service.lookup(name, searchProps, p);
+						if (d != null && !p.isEmpty())
+							bean.setProperties(p);
+					}
+					list.addLast(bean);
 				} else {
 					// delete it too?
 					System.err.println("Bad entry " + name + " in database " + service.getName());
 				}
 			}
 			AddressBean array[] = list.toArray(new AddressBean[list.size()]);
-			if (!(results instanceof SortedMap))
-			    Arrays.sort( array, sorter );
+			if (sortByDate) {
+				Arrays.sort(array, new AddressByDateSorter());
+			} else if (!(results instanceof SortedMap)) {
+				Arrays.sort(array, sorter);
+			}
 			entries = array;
 
 			message = generateLoadMessage();
diff --git a/apps/susidns/src/jsp/addressbook.jsp b/apps/susidns/src/jsp/addressbook.jsp
index f5af148871..e2d1d52f86 100644
--- a/apps/susidns/src/jsp/addressbook.jsp
+++ b/apps/susidns/src/jsp/addressbook.jsp
@@ -151,7 +151,15 @@ ${book.loadBookMessages}
 <c:if test="${book.notEmpty}">
 <div id="filter">
 <c:if test="${book.hasFilter}">
-<span><%=intl._t("Current filter")%>: <b>${book.filter}</b>
+<span><%=intl._t("Current filter")%>:
+<%
+    String f = book.getFilter();
+    if ("latest".equals(f))
+        f = intl._t(f);
+    else if ("xn--".equals(f))
+        f = intl._t("other");
+%>
+<b><%=f%></b>
 <a href="addressbook?filter=none&amp;begin=0&amp;end=49"><%=intl._t("clear filter")%></a></span>
 </c:if>
 <c:if test="${!book.hasFilter}">
@@ -186,6 +194,9 @@ ${book.loadBookMessages}
 <a href="addressbook?filter=z&amp;begin=0&amp;end=49">z</a>
 <a href="addressbook?filter=0-9&amp;begin=0&amp;end=49">0-9</a>
 <a href="addressbook?filter=xn--&amp;begin=0&amp;end=49"><%=intl._t("other")%></a>
+<% if (!book.getBook().equals("published")) { %>
+   <a href="addressbook?filter=latest&amp;begin=0&amp;end=49"><%=intl._t("latest")%></a>
+<% } %>
 <a href="addressbook?filter=none&amp;begin=0&amp;end=49"><%=intl._t("all")%></a>
 </p>
 </div>
-- 
GitLab