+ public Sone addLocalSone(OwnIdentity ownIdentity) {
+ if (ownIdentity == null) {
+ logger.log(Level.WARNING, "Given OwnIdentity is null!");
+ return null;
+ }
+ synchronized (localSones) {
+ if (localSones.containsKey(ownIdentity.getId())) {
+ logger.log(Level.FINE, "Tried to add known local Sone: %s", ownIdentity);
+ return localSones.get(ownIdentity.getId());
+ }
+ final Sone sone;
+ try {
+ sone = new Sone(ownIdentity).setInsertUri(new FreenetURI(ownIdentity.getInsertUri())).setRequestUri(new FreenetURI(ownIdentity.getRequestUri()));
+ sone.setLatestEdition(Numbers.safeParseLong(ownIdentity.getProperty("Sone.LatestEdition"), (long) 0));
+ } catch (MalformedURLException mue1) {
+ logger.log(Level.SEVERE, "Could not convert the Identity’s URIs to Freenet URIs: " + ownIdentity.getInsertUri() + ", " + ownIdentity.getRequestUri(), mue1);
+ return null;
+ }
+ /* TODO - load posts ’n stuff */
+ localSones.put(ownIdentity.getId(), sone);
+ SoneInserter soneInserter = new SoneInserter(this, freenetInterface, sone);
+ soneInserters.put(sone, soneInserter);
+ soneInserter.start();
+ setSoneStatus(sone, SoneStatus.idle);
+ new Thread(new Runnable() {
+
+ @Override
+ @SuppressWarnings("synthetic-access")
+ public void run() {
+ soneDownloader.fetchSone(sone);
+ }
+
+ }, "Sone Downloader").start();
+ return sone;
+ }
+ }
+
+ /**
+ * Creates a new Sone for the given own identity.
+ *
+ * @param ownIdentity
+ * The own identity to create a Sone for
+ * @return The created Sone
+ */
+ public Sone createSone(OwnIdentity ownIdentity) {
+ identityManager.addContext(ownIdentity, "Sone");
+ Sone sone = addLocalSone(ownIdentity);
+ synchronized (sone) {
+ /* mark as modified so that it gets inserted immediately. */
+ sone.setModificationCounter(sone.getModificationCounter() + 1);
+ }
+ return sone;
+ }
+
+ /**
+ * Adds the Sone of the given identity.
+ *
+ * @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) {
+ if (remoteSones.containsKey(identity.getId())) {
+ logger.log(Level.FINE, "Identity already exists: %s", identity);
+ return remoteSones.get(identity.getId());
+ }
+ Sone sone = new Sone(identity);
+ sone.setRequestUri(getSoneUri(identity.getRequestUri(), identity.getProperty("Sone.LatestEdition")));
+ remoteSones.put(identity.getId(), sone);
+ soneDownloader.addSone(sone);
+ setSoneStatus(sone, SoneStatus.idle);
+ 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());
+ }
+ saveSone(storedSone);
+ }
+ }
+
+ /**
+ * 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.getId()).stop();
+ }
+ identityManager.removeContext((OwnIdentity) sone.getIdentity(), "Sone");
+ }
+
+ /**
+ * Saves the given Sone. This will persist all local settings for the given
+ * Sone, such as the friends list and similar, private options.
+ *
+ * @param sone
+ * The Sone to save
+ */
+ public void saveSone(Sone sone) {
+ if (!isLocalSone(sone)) {
+ logger.log(Level.FINE, "Tried to save non-local Sone: %s", sone);
+ return;
+ }
+ if (!(sone.getIdentity() instanceof OwnIdentity)) {
+ logger.log(Level.WARNING, "Local Sone without OwnIdentity found, refusing to save: %s", sone);
+ return;
+ }
+
+ logger.log(Level.INFO, "Saving Sone: %s", sone);
+ identityManager.setProperty((OwnIdentity) sone.getIdentity(), "Sone.LatestEdition", String.valueOf(sone.getLatestEdition()));
+ try {
+ /* save Sone into configuration. */
+ String sonePrefix = "Sone/" + sone.getId();
+ configuration.getLongValue(sonePrefix + "/Time").setValue(sone.getTime());
+
+ /* save profile. */
+ Profile profile = sone.getProfile();
+ configuration.getStringValue(sonePrefix + "/Profile/FirstName").setValue(profile.getFirstName());
+ configuration.getStringValue(sonePrefix + "/Profile/MiddleName").setValue(profile.getMiddleName());
+ configuration.getStringValue(sonePrefix + "/Profile/LastName").setValue(profile.getLastName());
+ configuration.getIntValue(sonePrefix + "/Profile/BirthDay").setValue(profile.getBirthDay());
+ configuration.getIntValue(sonePrefix + "/Profile/BirthMonth").setValue(profile.getBirthMonth());
+ configuration.getIntValue(sonePrefix + "/Profile/BirthYear").setValue(profile.getBirthYear());
+
+ /* save posts. */
+ int postCounter = 0;
+ for (Post post : sone.getPosts()) {
+ String postPrefix = sonePrefix + "/Posts/" + postCounter++;
+ configuration.getStringValue(postPrefix + "/ID").setValue(post.getId());
+ configuration.getLongValue(postPrefix + "/Time").setValue(post.getTime());
+ configuration.getStringValue(postPrefix + "/Text").setValue(post.getText());
+ }
+ configuration.getStringValue(sonePrefix + "/Posts/" + postCounter + "/ID").setValue(null);
+
+ /* save replies. */
+ int replyCounter = 0;
+ for (Reply reply : sone.getReplies()) {
+ String replyPrefix = sonePrefix + "/Replies/" + replyCounter++;
+ configuration.getStringValue(replyPrefix + "/ID").setValue(reply.getId());
+ configuration.getStringValue(replyPrefix + "/Post/ID").setValue(reply.getPost().getId());
+ configuration.getLongValue(replyPrefix + "/Time").setValue(reply.getTime());
+ configuration.getStringValue(replyPrefix + "/Text").setValue(reply.getText());
+ }
+ configuration.getStringValue(sonePrefix + "/Replies/" + replyCounter + "/ID").setValue(null);
+
+ /* save post likes. */
+ int postLikeCounter = 0;
+ for (String postId : sone.getLikedPostIds()) {
+ configuration.getStringValue(sonePrefix + "/Likes/Post/" + postLikeCounter++ + "/ID").setValue(postId);
+ }
+ configuration.getStringValue(sonePrefix + "/Likes/Post/" + postLikeCounter + "/ID").setValue(null);
+
+ /* save reply likes. */
+ int replyLikeCounter = 0;
+ for (String replyId : sone.getLikedReplyIds()) {
+ configuration.getStringValue(sonePrefix + "/Likes/Reply/" + replyLikeCounter++ + "/ID").setValue(replyId);
+ }
+ configuration.getStringValue(sonePrefix + "/Likes/Reply/" + replyLikeCounter + "/ID").setValue(null);
+
+ logger.log(Level.INFO, "Sone %s saved.", sone);
+ } catch (ConfigurationException ce1) {
+ logger.log(Level.WARNING, "Could not save Sone: " + sone, ce1);
+ }
+ }
+
+ /**
+ * Creates a new post.
+ *
+ * @param sone
+ * The Sone that creates the post
+ * @param text
+ * The text of the post
+ */
+ public void createPost(Sone sone, String text) {
+ createPost(sone, System.currentTimeMillis(), text);
+ }
+
+ /**
+ * Creates a new post.
+ *
+ * @param sone
+ * The Sone that creates the post
+ * @param time
+ * The time of the post
+ * @param text
+ * The text of the post
+ */
+ public void createPost(Sone sone, long time, String text) {
+ if (!isLocalSone(sone)) {
+ logger.log(Level.FINE, "Tried to create post for non-local Sone: %s", sone);
+ return;
+ }
+ Post post = new Post(sone, time, text);
+ synchronized (posts) {
+ posts.put(post.getId(), post);
+ }
+ sone.addPost(post);
+ saveSone(sone);
+ }
+
+ /**
+ * Deletes the given post.
+ *
+ * @param post
+ * The post to delete
+ */
+ public void deletePost(Post post) {
+ if (!isLocalSone(post.getSone())) {
+ logger.log(Level.WARNING, "Tried to delete post of non-local Sone: %s", post.getSone());
+ return;
+ }
+ post.getSone().removePost(post);
+ synchronized (posts) {
+ posts.remove(post.getId());
+ }
+ saveSone(post.getSone());
+ }
+
+ /**
+ * Creates a new reply.
+ *
+ * @param sone
+ * The Sone that creates the reply
+ * @param post
+ * The post that this reply refers to
+ * @param text
+ * The text of the reply
+ */
+ public void createReply(Sone sone, Post post, String text) {
+ createReply(sone, post, System.currentTimeMillis(), text);
+ }
+
+ /**
+ * Creates a new reply.
+ *
+ * @param sone
+ * The Sone that creates the reply
+ * @param post
+ * The post that this reply refers to
+ * @param time
+ * The time of the reply
+ * @param text
+ * The text of the reply
+ */
+ public void createReply(Sone sone, Post post, long time, String text) {
+ if (!isLocalSone(sone)) {
+ logger.log(Level.FINE, "Tried to create reply for non-local Sone: %s", sone);
+ return;
+ }
+ Reply reply = new Reply(sone, post, System.currentTimeMillis(), text);
+ synchronized (replies) {
+ replies.put(reply.getId(), reply);
+ }
+ sone.addReply(reply);
+ saveSone(sone);
+ }
+
+ /**
+ * Deletes the given reply.
+ *
+ * @param reply
+ * The reply to delete
+ */
+ public void deleteReply(Reply reply) {
+ Sone sone = reply.getSone();
+ if (!isLocalSone(sone)) {
+ logger.log(Level.FINE, "Tried to delete non-local reply: %s", reply);
+ return;
+ }
+ synchronized (replies) {
+ replies.remove(reply.getId());
+ }
+ sone.removeReply(reply);
+ saveSone(sone);
+ }
+
+ /**
+ * Starts the core.
+ */
+ public void start() {