Fix own identities without trust identities from disappearing.
[Sone.git] / src / main / java / net / pterodactylus / sone / freenet / wot / IdentityLoader.java
1 /*
2  * Sone - IdentityLoader.java - Copyright © 2013 David Roden
3  *
4  * This program is free software: you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation, either version 3 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
16  */
17
18 package net.pterodactylus.sone.freenet.wot;
19
20 import static net.pterodactylus.sone.freenet.wot.Context.extractContext;
21
22 import java.util.Collection;
23 import java.util.HashMap;
24 import java.util.Map;
25 import java.util.Set;
26
27 import net.pterodactylus.sone.freenet.plugin.PluginException;
28
29 import com.google.common.base.Optional;
30 import com.google.inject.Inject;
31
32 /**
33  * Loads {@link OwnIdentity}s and the {@link Identity}s they trust.
34  *
35  * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
36  */
37 public class IdentityLoader {
38
39         private final WebOfTrustConnector webOfTrustConnector;
40         private final Optional<Context> context;
41
42         public IdentityLoader(WebOfTrustConnector webOfTrustConnector) {
43                 this(webOfTrustConnector, Optional.<Context>absent());
44         }
45
46         @Inject
47         public IdentityLoader(WebOfTrustConnector webOfTrustConnector, Optional<Context> context) {
48                 this.webOfTrustConnector = webOfTrustConnector;
49                 this.context = context;
50         }
51
52         public Map<OwnIdentity, Collection<Identity>> loadIdentities() throws WebOfTrustException {
53                 Collection<OwnIdentity> currentOwnIdentities = webOfTrustConnector.loadAllOwnIdentities();
54                 return loadTrustedIdentitiesForOwnIdentities(currentOwnIdentities);
55         }
56
57         private Map<OwnIdentity, Collection<Identity>> loadTrustedIdentitiesForOwnIdentities(Collection<OwnIdentity> ownIdentities) throws PluginException {
58                 Map<OwnIdentity, Collection<Identity>> currentIdentities = new HashMap<OwnIdentity, Collection<Identity>>();
59
60                 for (OwnIdentity ownIdentity : ownIdentities) {
61                         if (identityDoesNotHaveTheCorrectContext(ownIdentity)) {
62                                 continue;
63                         }
64
65                         Set<Identity> trustedIdentities = webOfTrustConnector.loadTrustedIdentities(ownIdentity, context.transform(extractContext));
66                         currentIdentities.put(ownIdentity, trustedIdentities);
67                 }
68
69                 return currentIdentities;
70         }
71
72         private boolean identityDoesNotHaveTheCorrectContext(OwnIdentity ownIdentity) {
73                 return context.isPresent() && !ownIdentity.hasContext(context.transform(extractContext).get());
74         }
75
76 }