🚧 Add contexts to WoT identities
authorDavid ‘Bombe’ Roden <bombe@freenetproject.org>
Wed, 8 Jan 2025 07:29:20 +0000 (08:29 +0100)
committerDavid ‘Bombe’ Roden <bombe@freenetproject.org>
Wed, 8 Jan 2025 07:39:12 +0000 (08:39 +0100)
src/main/java/net/pterodactylus/fcp/plugin/Identity.java
src/main/java/net/pterodactylus/fcp/plugin/OwnIdentity.java
src/main/java/net/pterodactylus/fcp/plugin/WebOfTrustPlugin.java
src/test/java/net/pterodactylus/fcp/test/IdentityMatchers.java

index f05e95f..f04f37b 100644 (file)
 
 package net.pterodactylus.fcp.plugin;
 
+import java.util.Collection;
+import java.util.Collections;
+import java.util.HashSet;
+import java.util.Set;
+
 /**
  * Wrapper around a web-of-trust identity.
  *
@@ -33,6 +38,8 @@ public class Identity {
        /** The identity’s request URI. */
        private final String requestUri;
 
+       private final Set<String> contexts = new HashSet<>();
+
        /**
         * Creates a new identity.
         *
@@ -43,10 +50,11 @@ public class Identity {
         * @param requestUri
         *            The request URI of the identity
         */
-       public Identity(String identifier, String nickname, String requestUri) {
+       public Identity(String identifier, String nickname, String requestUri, Collection<String> contexts) {
                this.identifier = identifier;
                this.nickname = nickname;
                this.requestUri = requestUri;
+               this.contexts.addAll(contexts);
        }
 
        /**
@@ -76,6 +84,10 @@ public class Identity {
                return requestUri;
        }
 
+       public Set<String> getContexts() {
+               return Collections.unmodifiableSet(contexts);
+       }
+
        /**
         * {@inheritDoc}
         */
index 8e66ed2..35c19a8 100644 (file)
@@ -17,6 +17,9 @@
 
 package net.pterodactylus.fcp.plugin;
 
+import java.util.Collection;
+import java.util.StringJoiner;
+
 /**
  * Wrapper around a web-of-trust own identity.
  *
@@ -39,8 +42,8 @@ public class OwnIdentity extends Identity {
         * @param insertUri
         *            The insert URI of the identity
         */
-       public OwnIdentity(String identifier, String nickname, String requestUri, String insertUri) {
-               super(identifier, nickname, requestUri);
+       public OwnIdentity(String identifier, String nickname, String requestUri, String insertUri, Collection<String> contexts) {
+               super(identifier, nickname, requestUri, contexts);
                this.insertUri = insertUri;
        }
 
@@ -53,4 +56,15 @@ public class OwnIdentity extends Identity {
                return insertUri;
        }
 
+       @Override
+       public String toString() {
+               return new StringJoiner(", ", OwnIdentity.class.getSimpleName() + "[", "]")
+                               .add("identifier='" + getIdentifier() + "'")
+                               .add("nickname='" + getNickname() + "'")
+                               .add("requestUri='" + getRequestUri() + "'")
+                               .add("insertUri='" + insertUri + "'")
+                               .add("contexts='" + getContexts() + "'")
+                               .toString();
+       }
+
 }
index d118c46..287769a 100644 (file)
@@ -26,6 +26,8 @@ import java.util.Set;
 import net.pterodactylus.fcp.highlevel.FcpClient;
 import net.pterodactylus.fcp.highlevel.FcpException;
 
+import static java.util.Collections.emptySet;
+
 /**
  * Simplifies handling of the web-of-trust plugin.
  *
@@ -88,7 +90,7 @@ public class WebOfTrustPlugin {
                String identifier = replies.get("ID");
                String newRequestUri = replies.get("RequestURI");
                String newInsertUri = replies.get("InsertURI");
-               return new OwnIdentity(identifier, nickname, newRequestUri, newInsertUri);
+               return new OwnIdentity(identifier, nickname, newRequestUri, newInsertUri, emptySet());
        }
 
        /**
@@ -130,7 +132,7 @@ public class WebOfTrustPlugin {
                        String nickname = replies.get("Nickname" + identityIndex);
                        String requestUri = replies.get("RequestURI" + identityIndex);
                        String insertUri = replies.get("InsertURI" + identityIndex);
-                       ownIdentities.add(new OwnIdentity(identity, nickname, requestUri, insertUri));
+                       ownIdentities.add(new OwnIdentity(identity, nickname, requestUri, insertUri, emptySet()));
                }
                return ownIdentities;
        }
@@ -193,7 +195,7 @@ public class WebOfTrustPlugin {
                }
                String identifier = replies.get("ID");
                String nickname = replies.get("Nickname");
-               return new Identity(identifier, nickname, requestUri);
+               return new Identity(identifier, nickname, requestUri, emptySet());
        }
 
        /**
@@ -223,7 +225,7 @@ public class WebOfTrustPlugin {
                        String identifier = replies.get("Identity" + identityIndex);
                        String nickname = replies.get("Nickname" + identityIndex);
                        String requestUri = replies.get("RequestURI" + identityIndex);
-                       identities.add(new Identity(identifier, nickname, requestUri));
+                       identities.add(new Identity(identifier, nickname, requestUri, emptySet()));
                }
                return identities;
        }
@@ -253,7 +255,7 @@ public class WebOfTrustPlugin {
                        String requestUri = replies.get("RequestURI" + identityIndex);
                        byte trust = Byte.parseByte(replies.get("Value" + identityIndex));
                        String comment = replies.get("Comment" + identityIndex);
-                       identityTrusts.put(new Identity(identifier, nickname, requestUri), new IdentityTrust(trust, comment));
+                       identityTrusts.put(new Identity(identifier, nickname, requestUri, emptySet()), new IdentityTrust(trust, comment));
                }
                return identityTrusts;
        }
@@ -283,7 +285,7 @@ public class WebOfTrustPlugin {
                        String requestUri = replies.get("RequestURI" + identityIndex);
                        byte trust = Byte.parseByte(replies.get("Value" + identityIndex));
                        String comment = replies.get("Comment" + identityIndex);
-                       identityTrusts.put(new Identity(identifier, nickname, requestUri), new IdentityTrust(trust, comment));
+                       identityTrusts.put(new Identity(identifier, nickname, requestUri, emptySet()), new IdentityTrust(trust, comment));
                }
                return identityTrusts;
        }
index 80ddde5..19b7497 100644 (file)
@@ -1,10 +1,13 @@
 package net.pterodactylus.fcp.test;
 
+import net.pterodactylus.fcp.plugin.Identity;
 import net.pterodactylus.fcp.plugin.OwnIdentity;
 import org.hamcrest.Description;
 import org.hamcrest.Matcher;
 import org.hamcrest.TypeSafeDiagnosingMatcher;
 
+import java.util.Set;
+
 public class IdentityMatchers {
 
        public static Matcher<OwnIdentity> isOwnIdentity(Matcher<? super String> identifier, Matcher<? super String> nickname, Matcher<? super String> requestUri, Matcher<? super String> insertUri) {
@@ -39,4 +42,22 @@ public class IdentityMatchers {
                };
        }
 
+       public static Matcher<Identity> hasContexts(Matcher<? super Set<? super String>> contextsMatcher) {
+               return new TypeSafeDiagnosingMatcher<Identity>() {
+                       @Override
+                       protected boolean matchesSafely(Identity identity, Description mismatchDescription) {
+                               if (!contextsMatcher.matches(identity.getContexts())) {
+                                       contextsMatcher.describeMismatch(identity.getContexts(), mismatchDescription);
+                                       return false;
+                               }
+                               return true;
+                       }
+
+                       @Override
+                       public void describeTo(Description description) {
+                               description.appendText("has contexts ").appendValue(contextsMatcher);
+                       }
+               };
+       }
+
 }