♻️ Use identity parser for parsing single identities, too
authorDavid ‘Bombe’ Roden <bombe@freenetproject.org>
Wed, 17 Dec 2025 16:58:58 +0000 (17:58 +0100)
committerDavid ‘Bombe’ Roden <bombe@freenetproject.org>
Wed, 17 Dec 2025 16:58:58 +0000 (17:58 +0100)
src/main/java/net/pterodactylus/fcp/plugin/WebOfTrustPlugin.java

index bec9dd3..28cd9eb 100644 (file)
@@ -87,10 +87,7 @@ public class WebOfTrustPlugin {
                if (!replies.get("Message").equals("IdentityCreated")) {
                        throw new FcpException("WebOfTrust Plugin did not reply with “IdentityCreated” message!");
                }
-               String identifier = replies.get("ID");
-               String newRequestUri = replies.get("RequestURI");
-               String newInsertUri = replies.get("InsertURI");
-               return new OwnIdentity(identifier, nickname, newRequestUri, newInsertUri, emptySet(), emptyMap());
+               return parseIdentity(replies, WebOfTrustPlugin::createOwnIdentity);
        }
 
        /**
@@ -142,25 +139,7 @@ public class WebOfTrustPlugin {
                if (!replies.get("Message").equals("OwnIdentities")) {
                        throw new FcpException("WebOfTrust Plugin did not reply with “OwnIdentities” message!");
                }
-               Set<OwnIdentity> ownIdentities = new HashSet<OwnIdentity>();
-               for (int identityIndex = 0; replies.containsKey("Identity" + identityIndex); identityIndex++) {
-                       String identity = replies.get("Identity" + identityIndex);
-                       String nickname = replies.get("Nickname" + identityIndex);
-                       String requestUri = replies.get("RequestURI" + identityIndex);
-                       String insertUri = replies.get("InsertURI" + identityIndex);
-                       Set<String> contexts = new HashSet<>();
-                       for (int contextIndex = 0; replies.containsKey("Contexts" + identityIndex + ".Context" + contextIndex); contextIndex++) {
-                               contexts.add(replies.get("Contexts" + identityIndex + ".Context" + contextIndex));
-                       }
-                       Map<String, String> properties = new HashMap<>();
-                       for (int propertyIndex = 0; replies.containsKey("Properties" + identityIndex + ".Property" + propertyIndex + ".Name"); propertyIndex++) {
-                               String key = replies.get("Properties" + identityIndex + ".Property" + propertyIndex + ".Name");
-                               String value = replies.get("Properties" + identityIndex + ".Property" + propertyIndex + ".Value");
-                               properties.put(key, value);
-                       }
-                       ownIdentities.add(new OwnIdentity(identity, nickname, requestUri, insertUri, contexts, properties));
-               }
-               return ownIdentities;
+               return parseIdentities(replies, WebOfTrustPlugin::createOwnIdentity);
        }
 
        /**
@@ -219,9 +198,7 @@ public class WebOfTrustPlugin {
                if (!replies.get("Message").equals("IdentityAdded")) {
                        throw new FcpException("WebOfTrust Plugin did not reply with “IdentityAdded” message!");
                }
-               String identifier = replies.get("ID");
-               String nickname = replies.get("Nickname");
-               return new Identity(identifier, nickname, requestUri, emptySet(), emptyMap());
+               return parseIdentity(replies, WebOfTrustPlugin::createIdentity);
        }
 
        /**
@@ -453,6 +430,11 @@ public class WebOfTrustPlugin {
                return parameterMap;
        }
 
+       private static <I extends Identity> I parseIdentity(Map<String, String> replies, IdentityGenerator<I> identityGenerator) {
+               IdentityParser parser = v2IdentityParser.canParse(replies, "") ? v2IdentityParser : v1IdentityParser;
+               return parser.parseSingleIdentity(replies, "", identityGenerator);
+       }
+
        private static <I extends Identity> Set<I> parseIdentities(Map<String, String> replies, IdentityGenerator<I> identityGenerator) {
                IdentityParser parser = v2IdentityParser.canParse(replies, "") ? v2IdentityParser : v1IdentityParser;
                return parser.parseMultipleIdentities(replies, "", identityGenerator);