Implement auto-following of new Sones.
authorDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Tue, 5 Apr 2011 13:47:01 +0000 (15:47 +0200)
committerDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Tue, 5 Apr 2011 13:47:01 +0000 (15:47 +0200)
This fixes #88.

src/main/java/net/pterodactylus/sone/core/Core.java
src/main/java/net/pterodactylus/sone/web/OptionsPage.java
src/main/resources/i18n/sone.en.properties
src/main/resources/templates/options.html

index 731fcbd..ad1b1aa 100644 (file)
@@ -905,6 +905,11 @@ public class Core implements IdentityListener, UpdateListener {
                                }
                                if (newSone) {
                                        coreListenerManager.fireNewSoneFound(sone);
+                                       for (Sone localSone : getLocalSones()) {
+                                               if (localSone.getOptions().getBooleanOption("AutoFollow").get()) {
+                                                       localSone.addFriend(sone.getId());
+                                               }
+                                       }
                                }
                        }
                        remoteSones.put(identity.getId(), sone);
@@ -1263,6 +1268,10 @@ public class Core implements IdentityListener, UpdateListener {
                        friends.add(friendId);
                }
 
+               /* load options. */
+               sone.getOptions().addBooleanOption("AutoFollow", new DefaultOption<Boolean>(false));
+               sone.getOptions().getBooleanOption("AutoFollow").set(configuration.getBooleanValue(sonePrefix + "/Options/AutoFollow").getValue(null));
+
                /* if we’re still here, Sone was loaded successfully. */
                synchronized (sone) {
                        sone.setTime(soneTime);
@@ -1378,6 +1387,9 @@ public class Core implements IdentityListener, UpdateListener {
                        }
                        configuration.getStringValue(sonePrefix + "/Friends/" + friendCounter + "/ID").setValue(null);
 
+                       /* save options. */
+                       configuration.getBooleanValue(sonePrefix + "/Options/AutoFollow").setValue(sone.getOptions().getBooleanOption("AutoFollow").getReal());
+
                        configuration.save();
                        logger.log(Level.INFO, "Sone %s saved.", sone);
                } catch (ConfigurationException ce1) {
index 84d7c79..77b4950 100644 (file)
@@ -18,6 +18,7 @@
 package net.pterodactylus.sone.web;
 
 import net.pterodactylus.sone.core.Core.Preferences;
+import net.pterodactylus.sone.data.Sone;
 import net.pterodactylus.sone.web.page.Page.Request.Method;
 import net.pterodactylus.util.number.Numbers;
 import net.pterodactylus.util.template.Template;
@@ -53,7 +54,13 @@ public class OptionsPage extends SoneTemplatePage {
        protected void processTemplate(Request request, TemplateContext templateContext) throws RedirectException {
                super.processTemplate(request, templateContext);
                Preferences preferences = webInterface.getCore().getPreferences();
+               Sone currentSone = webInterface.getCurrentSone(request.getToadletContext(), false);
                if (request.getMethod() == Method.POST) {
+                       if (currentSone != null) {
+                               boolean autoFollow = request.getHttpRequest().isPartSet("auto-follow");
+                               currentSone.getOptions().getBooleanOption("AutoFollow").set(autoFollow);
+                               webInterface.getCore().saveSone(currentSone);
+                       }
                        Integer insertionDelay = Numbers.safeParseInteger(request.getHttpRequest().getPartAsStringFailsafe("insertion-delay", 16));
                        preferences.setInsertionDelay(insertionDelay);
                        Integer positiveTrust = Numbers.safeParseInteger(request.getHttpRequest().getPartAsStringFailsafe("positive-trust", 3));
@@ -74,6 +81,9 @@ public class OptionsPage extends SoneTemplatePage {
                        webInterface.getCore().saveConfiguration();
                        throw new RedirectException(getPath());
                }
+               if (currentSone != null) {
+                       templateContext.set("auto-follow", currentSone.getOptions().getBooleanOption("AutoFollow").get());
+               }
                templateContext.set("insertion-delay", preferences.getInsertionDelay());
                templateContext.set("positive-trust", preferences.getPositiveTrust());
                templateContext.set("negative-trust", preferences.getNegativeTrust());
index 5179709..e8d36a0 100644 (file)
@@ -31,6 +31,9 @@ Page.About.License.Title=License
 Page.Options.Title=Options - Sone
 Page.Options.Page.Title=Options
 Page.Options.Page.Description=These options influence the runtime behaviour of the Sone plugin.
+Page.Options.Section.SoneSpecificOptions.Title=Sone-specific Options
+Page.Options.Section.SoneSpecificOptions.NotLoggedIn=These options are only available if you are {link}logged in{/link}.
+Page.Options.Option.AutoFollow.Description=If a new Sone is discovered, follow it automatically.
 Page.Options.Section.RuntimeOptions.Title=Runtime Behaviour
 Page.Options.Option.InsertionDelay.Description=The number of seconds the Sone inserter waits after a modification of a Sone before it is being inserted.
 Page.Options.Section.TrustOptions.Title=Trust Settings
index 5c47764..4086306 100644 (file)
        <form id="options" method="post">
                <input type="hidden" name="formPassword" value="<% formPassword|html>" />
 
+               <h2><%= Page.Options.Section.SoneSpecificOptions.Title|l10n|html></h2>
+
+               <%ifnull currentSone>
+                       <p><%= Page.Options.Section.SoneSpecificOptions.NotLoggedIn|l10n|html|replace needle="{link}" replacement='<a href="login.html">'|replace needle="{/link}" replacement='</a>'></p>
+               <%/if>
+
+               <p>
+                       <input type="checkbox" name="auto-follow"<%ifnull currentSone> disabled="disabled"<%/if><%if auto-follow> checked="checked"<%/if> />
+                       <%= Page.Options.Option.AutoFollow.Description|l10n|html>
+               </p>
+
                <h2><%= Page.Options.Section.RuntimeOptions.Title|l10n|html></h2>
 
                <p><%= Page.Options.Option.InsertionDelay.Description|l10n|html></p>