Merge branch 'next' into edit-wot-trust
[Sone.git] / src / main / java / net / pterodactylus / sone / core / Core.java
index 1a92901..f7b56a1 100644 (file)
@@ -40,6 +40,8 @@ import net.pterodactylus.sone.freenet.wot.Identity;
 import net.pterodactylus.sone.freenet.wot.IdentityListener;
 import net.pterodactylus.sone.freenet.wot.IdentityManager;
 import net.pterodactylus.sone.freenet.wot.OwnIdentity;
+import net.pterodactylus.sone.freenet.wot.Trust;
+import net.pterodactylus.sone.freenet.wot.WebOfTrustException;
 import net.pterodactylus.sone.main.SonePlugin;
 import net.pterodactylus.util.config.Configuration;
 import net.pterodactylus.util.config.ConfigurationException;
@@ -147,6 +149,9 @@ public class Core implements IdentityListener {
        /** All known replies. */
        private Set<String> knownReplies = new HashSet<String>();
 
+       /** Trusted identities, sorted by own identities. */
+       private Map<OwnIdentity, Set<Identity>> trustedIdentities = Collections.synchronizedMap(new HashMap<OwnIdentity, Set<Identity>>());
+
        /**
         * Creates a new core.
         *
@@ -503,6 +508,19 @@ public class Core implements IdentityListener {
        }
 
        /**
+        * Returns whether the target Sone is trusted by the origin Sone.
+        *
+        * @param origin
+        *            The origin Sone
+        * @param target
+        *            The target Sone
+        * @return {@code true} if the target Sone is trusted by the origin Sone
+        */
+       public boolean isSoneTrusted(Sone origin, Sone target) {
+               return trustedIdentities.containsKey(origin) && trustedIdentities.get(origin.getIdentity()).contains(target);
+       }
+
+       /**
         * Returns the post with the given ID.
         *
         * @param postId
@@ -825,7 +843,12 @@ public class Core implements IdentityListener {
         * @return The created Sone
         */
        public Sone createSone(OwnIdentity ownIdentity) {
-               identityManager.addContext(ownIdentity, "Sone");
+               try {
+                       ownIdentity.addContext("Sone");
+               } catch (WebOfTrustException wote1) {
+                       logger.log(Level.SEVERE, "Could not add “Sone” context to own identity: " + ownIdentity, wote1);
+                       return null;
+               }
                Sone sone = addLocalSone(ownIdentity);
                return sone;
        }
@@ -875,6 +898,28 @@ public class Core implements IdentityListener {
        }
 
        /**
+        * Retrieves the trust relationship from the origin to the target.
+        *
+        * @param origin
+        *            The origin of the trust tree
+        * @param target
+        *            The target of the trust
+        * @return The trust relationship
+        */
+       public Trust getTrust(Sone origin, Sone target) {
+               if (!isLocalSone(origin)) {
+                       logger.log(Level.WARNING, "Tried to get trust from remote Sone: %s", origin);
+                       return null;
+               }
+               try {
+                       return target.getIdentity().getTrust((OwnIdentity) origin.getIdentity());
+               } catch (WebOfTrustException wote1) {
+                       logger.log(Level.WARNING, "Could not get trust for Sone: " + target, wote1);
+                       return null;
+               }
+       }
+
+       /**
         * Updates the stores Sone with the given Sone.
         *
         * @param sone
@@ -979,8 +1024,12 @@ public class Core implements IdentityListener {
                        localSones.remove(sone.getId());
                        soneInserters.remove(sone).stop();
                }
-               identityManager.removeContext((OwnIdentity) sone.getIdentity(), "Sone");
-               identityManager.removeProperty((OwnIdentity) sone.getIdentity(), "Sone.LatestEdition");
+               try {
+                       ((OwnIdentity) sone.getIdentity()).removeContext("Sone");
+                       ((OwnIdentity) sone.getIdentity()).removeProperty("Sone.LatestEdition");
+               } catch (WebOfTrustException wote1) {
+                       logger.log(Level.WARNING, "Could not remove context and properties from Sone: " + sone, wote1);
+               }
                try {
                        configuration.getLongValue("Sone/" + sone.getId() + "/Time").setValue(null);
                } catch (ConfigurationException ce1) {
@@ -1135,8 +1184,9 @@ public class Core implements IdentityListener {
                }
 
                logger.log(Level.INFO, "Saving Sone: %s", sone);
-               identityManager.setProperty((OwnIdentity) sone.getIdentity(), "Sone.LatestEdition", String.valueOf(sone.getLatestEdition()));
                try {
+                       ((OwnIdentity) sone.getIdentity()).setProperty("Sone.LatestEdition", String.valueOf(sone.getLatestEdition()));
+
                        /* save Sone into configuration. */
                        String sonePrefix = "Sone/" + sone.getId();
                        configuration.getLongValue(sonePrefix + "/Time").setValue(sone.getTime());
@@ -1198,6 +1248,8 @@ public class Core implements IdentityListener {
                        logger.log(Level.INFO, "Sone %s saved.", sone);
                } catch (ConfigurationException ce1) {
                        logger.log(Level.WARNING, "Could not save Sone: " + sone, ce1);
+               } catch (WebOfTrustException wote1) {
+                       logger.log(Level.WARNING, "Could not set WoT property for Sone: " + sone, wote1);
                }
        }
 
@@ -1576,6 +1628,7 @@ public class Core implements IdentityListener {
        public void ownIdentityAdded(OwnIdentity ownIdentity) {
                logger.log(Level.FINEST, "Adding OwnIdentity: " + ownIdentity);
                if (ownIdentity.hasContext("Sone")) {
+                       trustedIdentities.put(ownIdentity, Collections.synchronizedSet(new HashSet<Identity>()));
                        addLocalSone(ownIdentity);
                }
        }
@@ -1586,14 +1639,16 @@ public class Core implements IdentityListener {
        @Override
        public void ownIdentityRemoved(OwnIdentity ownIdentity) {
                logger.log(Level.FINEST, "Removing OwnIdentity: " + ownIdentity);
+               trustedIdentities.remove(ownIdentity);
        }
 
        /**
         * {@inheritDoc}
         */
        @Override
-       public void identityAdded(Identity identity) {
+       public void identityAdded(OwnIdentity ownIdentity, Identity identity) {
                logger.log(Level.FINEST, "Adding Identity: " + identity);
+               trustedIdentities.get(ownIdentity).add(identity);
                addRemoteSone(identity);
        }
 
@@ -1601,7 +1656,7 @@ public class Core implements IdentityListener {
         * {@inheritDoc}
         */
        @Override
-       public void identityUpdated(final Identity identity) {
+       public void identityUpdated(OwnIdentity ownIdentity, final Identity identity) {
                new Thread(new Runnable() {
 
                        @Override
@@ -1617,8 +1672,8 @@ public class Core implements IdentityListener {
         * {@inheritDoc}
         */
        @Override
-       public void identityRemoved(Identity identity) {
-               /* TODO */
+       public void identityRemoved(OwnIdentity ownIdentity, Identity identity) {
+               trustedIdentities.get(ownIdentity).remove(identity);
        }
 
 }