🔀 Merge “release/v81” into “master”
[Sone.git] / src / main / java / net / pterodactylus / sone / template / SoneAccessor.java
index c134fc0..4bc6a5f 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 com.google.common.collect.FluentIterable.from;
+import static java.util.Arrays.asList;
+import static java.util.logging.Logger.getLogger;
+import static net.pterodactylus.sone.data.Album.FLATTENER;
+import static net.pterodactylus.sone.data.Album.IMAGES;
+
 import java.util.logging.Level;
 import java.util.logging.Logger;
 
@@ -24,10 +30,9 @@ import net.pterodactylus.sone.core.Core;
 import net.pterodactylus.sone.data.Profile;
 import net.pterodactylus.sone.data.Sone;
 import net.pterodactylus.sone.data.Sone.SoneStatus;
+import net.pterodactylus.sone.freenet.wot.OwnIdentity;
 import net.pterodactylus.sone.freenet.wot.Trust;
-import net.pterodactylus.sone.web.WebInterface;
-import net.pterodactylus.sone.web.ajax.GetTimesAjaxPage;
-import net.pterodactylus.util.logging.Logging;
+import net.pterodactylus.sone.text.TimeTextConverter;
 import net.pterodactylus.util.template.Accessor;
 import net.pterodactylus.util.template.ReflectionAccessor;
 import net.pterodactylus.util.template.TemplateContext;
@@ -46,16 +51,15 @@ import net.pterodactylus.util.template.TemplateContext;
  * <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 = Logging.getLogger(SoneAccessor.class);
+       private static final Logger logger = getLogger(SoneAccessor.class.getName());
 
        /** The core. */
        private final Core core;
+       private final TimeTextConverter timeTextConverter;
 
        /**
         * Creates a new Sone accessor.
@@ -63,8 +67,9 @@ 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;
        }
 
        /**
@@ -75,8 +80,6 @@ public class SoneAccessor extends ReflectionAccessor {
                Sone sone = (Sone) object;
                if (member.equals("niceName")) {
                        return getNiceName(sone);
-               } else if (member.equals("local")) {
-                       return core.isLocalSone(sone);
                } else if (member.equals("friend")) {
                        Sone currentSone = (Sone) templateContext.get("currentSone");
                        return (currentSone != null) && currentSone.hasFriend(sone.getId());
@@ -100,18 +103,22 @@ public class SoneAccessor extends ReflectionAccessor {
                } else if (member.equals("locked")) {
                        return core.isLocked(sone);
                } else if (member.equals("lastUpdatedText")) {
-                       return GetTimesAjaxPage.getTime((WebInterface) templateContext.get("webInterface"), sone.getTime());
+                       return timeTextConverter.getTimeText(sone.getTime()).getL10nText();
                } else if (member.equals("trust")) {
                        Sone currentSone = (Sone) templateContext.get("currentSone");
                        if (currentSone == null) {
                                return null;
                        }
-                       Trust trust = core.getTrust(currentSone, sone);
-                       logger.log(Level.FINEST, "Trust for %s by %s: %s", new Object[] { sone, currentSone, trust });
+                       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 from(asList(sone.getRootAlbum())).transformAndConcat(FLATTENER).transformAndConcat(IMAGES);
+               } else if (member.equals("albums")) {
+                       return sone.getRootAlbum().getAlbums();
                }
                return super.get(templateContext, object, member);
        }