🔀 Merge branch 'release/v82'
[Sone.git] / src / main / java / net / pterodactylus / sone / template / SoneAccessor.java
index beb7f2a..b072dc9 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Sone - SoneAccessor.java - Copyright © 2010 David Roden
+ * Sone - SoneAccessor.java - Copyright © 2010–2020 David Roden
  *
  * This program is free software: you can redistribute it and/or modify
  * it under the terms of the GNU General Public License as published by
 
 package net.pterodactylus.sone.template;
 
+import static java.util.logging.Logger.getLogger;
+
+import java.util.logging.Level;
+import java.util.logging.Logger;
+
 import net.pterodactylus.sone.core.Core;
-import net.pterodactylus.sone.core.Core.SoneStatus;
 import net.pterodactylus.sone.data.Profile;
 import net.pterodactylus.sone.data.Sone;
+import net.pterodactylus.sone.data.Sone.SoneStatus;
+import net.pterodactylus.sone.data.SoneKt;
+import net.pterodactylus.sone.freenet.wot.OwnIdentity;
 import net.pterodactylus.sone.freenet.wot.Trust;
+import net.pterodactylus.sone.text.TimeTextConverter;
 import net.pterodactylus.util.template.Accessor;
-import net.pterodactylus.util.template.DataProvider;
 import net.pterodactylus.util.template.ReflectionAccessor;
+import net.pterodactylus.util.template.TemplateContext;
 
 /**
  * {@link Accessor} for {@link Sone}s that adds a couple of properties to Sones.
@@ -35,18 +43,20 @@ import net.pterodactylus.util.template.ReflectionAccessor;
  * <dt>friend</dt>
  * <dd>Will return {@code true} if the sone in question is a friend of the
  * currently logged in Sone (as determined by accessing the “currentSone”
- * variable of the given {@link DataProvider}).</dd>
+ * variable of the given {@link TemplateContext}).</dd>
  * <dt>current</dt>
  * <dd>Will return {@code true} if the sone in question is the currently logged
  * in Sone.</dd>
  * </dl>
- *
- * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
  */
 public class SoneAccessor extends ReflectionAccessor {
 
+       /** The logger. */
+       private static final Logger logger = getLogger(SoneAccessor.class.getName());
+
        /** The core. */
        private final Core core;
+       private final TimeTextConverter timeTextConverter;
 
        /**
         * Creates a new Sone accessor.
@@ -54,53 +64,60 @@ public class SoneAccessor extends ReflectionAccessor {
         * @param core
         *            The Sone core
         */
-       public SoneAccessor(Core core) {
+       public SoneAccessor(Core core, TimeTextConverter timeTextConverter) {
                this.core = core;
+               this.timeTextConverter = timeTextConverter;
        }
 
        /**
         * {@inheritDoc}
         */
        @Override
-       public Object get(DataProvider dataProvider, Object object, String member) {
+       public Object get(TemplateContext templateContext, Object object, String member) {
                Sone sone = (Sone) object;
                if (member.equals("niceName")) {
                        return getNiceName(sone);
-               } else if (member.equals("local")) {
-                       return sone.getInsertUri() != null;
                } else if (member.equals("friend")) {
-                       Sone currentSone = (Sone) dataProvider.get("currentSone");
+                       Sone currentSone = (Sone) templateContext.get("currentSone");
                        return (currentSone != null) && currentSone.hasFriend(sone.getId());
                } else if (member.equals("current")) {
-                       Sone currentSone = (Sone) dataProvider.get("currentSone");
+                       Sone currentSone = (Sone) templateContext.get("currentSone");
                        return (currentSone != null) && currentSone.equals(sone);
                } else if (member.equals("modified")) {
                        return core.isModifiedSone(sone);
                } else if (member.equals("status")) {
-                       return core.getSoneStatus(sone);
+                       return sone.getStatus();
                } else if (member.equals("unknown")) {
-                       return core.getSoneStatus(sone) == SoneStatus.unknown;
+                       return sone.getStatus() == SoneStatus.unknown;
                } else if (member.equals("idle")) {
-                       return core.getSoneStatus(sone) == SoneStatus.idle;
+                       return sone.getStatus() == SoneStatus.idle;
                } else if (member.equals("inserting")) {
-                       return core.getSoneStatus(sone) == SoneStatus.inserting;
+                       return sone.getStatus() == SoneStatus.inserting;
                } else if (member.equals("downloading")) {
-                       return core.getSoneStatus(sone) == SoneStatus.downloading;
+                       return sone.getStatus() == SoneStatus.downloading;
                } else if (member.equals("new")) {
-                       return core.isNewSone(sone);
+                       return !sone.isKnown();
                } else if (member.equals("locked")) {
                        return core.isLocked(sone);
+               } else if (member.equals("lastUpdatedText")) {
+                       return timeTextConverter.getTimeText(sone.getTime()).getL10nText();
                } else if (member.equals("trust")) {
-                       Sone currentSone = (Sone) dataProvider.get("currentSone");
+                       Sone currentSone = (Sone) templateContext.get("currentSone");
                        if (currentSone == null) {
                                return null;
                        }
-                       Trust trust = core.getTrust(currentSone, sone);
+                       Trust trust = sone.getIdentity().getTrust((OwnIdentity) currentSone.getIdentity());
+                       logger.log(Level.FINEST, String.format("Trust for %s by %s: %s", sone, currentSone, trust));
                        if (trust == null) {
                                return new Trust(null, null, null);
                        }
+                       return trust;
+               } else if (member.equals("allImages")) {
+                       return SoneKt.getAllImages(sone);
+               } else if (member.equals("albums")) {
+                       return sone.getRootAlbum().getAlbums();
                }
-               return super.get(dataProvider, object, member);
+               return super.get(templateContext, object, member);
        }
 
        //