- * @param name
- * The name of the Sone
- * @param requestUri
- * The request URI of the Sone, or {@link NullPointerException}
- * to create a Sone at a random location
- * @param insertUri
- * The insert URI of the Sone, or {@code null} to create a Sone
- * at a random location
- * @return The created Sone
- * @throws SoneException
- * if a Sone error occurs
- */
- public Sone createSone(String name, String requestUri, String insertUri) throws SoneException {
- if ((name == null) || (name.trim().length() == 0)) {
- throw new SoneException(Type.INVALID_SONE_NAME);
- }
- String finalRequestUri;
- String finalInsertUri;
- if ((requestUri == null) || (insertUri == null)) {
- String[] keyPair = freenetInterface.generateKeyPair();
- finalRequestUri = keyPair[0];
- finalInsertUri = keyPair[1];
- } else {
- finalRequestUri = requestUri;
- finalInsertUri = insertUri;
- }
- Sone sone;
+ * @param identity
+ * The identity whose Sone to add
+ * @return The added or already existing Sone
+ */
+ public Sone addRemoteSone(Identity identity) {
+ if (identity == null) {
+ logger.log(Level.WARNING, "Given Identity is null!");
+ return null;
+ }
+ synchronized (remoteSones) {
+ boolean newSone = !isRemoteSone(identity.getId());
+ final Sone sone = getRemoteSone(identity.getId()).setIdentity(identity);
+ sone.setRequestUri(getSoneUri(identity.getRequestUri()));
+ sone.setLatestEdition(Numbers.safeParseLong(identity.getProperty("Sone.LatestEdition"), (long) 0));
+ if (newSone) {
+ synchronized (newSones) {
+ newSones.add(sone);
+ }
+ }
+ remoteSones.put(identity.getId(), sone);
+ soneDownloader.addSone(sone);
+ setSoneStatus(sone, SoneStatus.unknown);
+ new Thread(new Runnable() {
+
+ @Override
+ @SuppressWarnings("synthetic-access")
+ public void run() {
+ soneDownloader.fetchSone(sone);
+ }
+
+ }, "Sone Downloader").start();
+ return sone;
+ }
+ }
+
+ /**
+ * Updates the stores Sone with the given Sone.
+ *
+ * @param sone
+ * The updated Sone
+ */
+ public void updateSone(Sone sone) {
+ if (isRemoteSone(sone)) {
+ Sone storedSone = getRemoteSone(sone.getId());
+ if (!(sone.getTime() > storedSone.getTime())) {
+ logger.log(Level.FINE, "Downloaded Sone %s is not newer than stored Sone %s.", new Object[] { sone, storedSone });
+ return;
+ }
+ synchronized (posts) {
+ for (Post post : storedSone.getPosts()) {
+ posts.remove(post.getId());
+ }
+ for (Post post : sone.getPosts()) {
+ posts.put(post.getId(), post);
+ }
+ }
+ synchronized (replies) {
+ for (Reply reply : storedSone.getReplies()) {
+ replies.remove(reply.getId());
+ }
+ for (Reply reply : sone.getReplies()) {
+ replies.put(reply.getId(), reply);
+ }
+ }
+ synchronized (storedSone) {
+ storedSone.setTime(sone.getTime());
+ storedSone.setProfile(sone.getProfile());
+ storedSone.setPosts(sone.getPosts());
+ storedSone.setReplies(sone.getReplies());
+ storedSone.setLikePostIds(sone.getLikedPostIds());
+ storedSone.setLikeReplyIds(sone.getLikedReplyIds());
+ storedSone.setLatestEdition(sone.getRequestUri().getEdition());
+ storedSone.setModificationCounter(0);
+ }
+ }
+ }
+
+ /**
+ * Deletes the given Sone. This will remove the Sone from the
+ * {@link #getLocalSone(String) local Sones}, stops its {@link SoneInserter}
+ * and remove the context from its identity.
+ *
+ * @param sone
+ * The Sone to delete
+ */
+ public void deleteSone(Sone sone) {
+ if (!(sone.getIdentity() instanceof OwnIdentity)) {
+ logger.log(Level.WARNING, "Tried to delete Sone of non-own identity: %s", sone);
+ return;
+ }
+ synchronized (localSones) {
+ if (!localSones.containsKey(sone.getId())) {
+ logger.log(Level.WARNING, "Tried to delete non-local Sone: %s", sone);
+ return;
+ }
+ localSones.remove(sone.getId());
+ soneInserters.remove(sone).stop();
+ }
+ identityManager.removeContext((OwnIdentity) sone.getIdentity(), "Sone");
+ identityManager.removeProperty((OwnIdentity) sone.getIdentity(), "Sone.LatestEdition");