2 * Sone - SoneAccessor.java - Copyright © 2010–2020 David Roden
4 * This program is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation, either version 3 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <http://www.gnu.org/licenses/>.
18 package net.pterodactylus.sone.template;
20 import static java.util.logging.Logger.getLogger;
21 import static java.util.stream.Collectors.toList;
22 import static net.pterodactylus.sone.data.Album.IMAGES;
24 import java.util.logging.Level;
25 import java.util.logging.Logger;
27 import net.pterodactylus.sone.core.Core;
28 import net.pterodactylus.sone.data.Profile;
29 import net.pterodactylus.sone.data.Sone;
30 import net.pterodactylus.sone.data.Sone.SoneStatus;
31 import net.pterodactylus.sone.data.SoneKt;
32 import net.pterodactylus.sone.freenet.wot.OwnIdentity;
33 import net.pterodactylus.sone.freenet.wot.Trust;
34 import net.pterodactylus.sone.text.TimeTextConverter;
35 import net.pterodactylus.util.template.Accessor;
36 import net.pterodactylus.util.template.ReflectionAccessor;
37 import net.pterodactylus.util.template.TemplateContext;
40 * {@link Accessor} for {@link Sone}s that adds a couple of properties to Sones.
43 * <dd>Will show a combination of first name, middle name, and last name, if
44 * available, otherwise the username of the Sone is returned.</dd>
46 * <dd>Will return {@code true} if the sone in question is a friend of the
47 * currently logged in Sone (as determined by accessing the “currentSone”
48 * variable of the given {@link TemplateContext}).</dd>
50 * <dd>Will return {@code true} if the sone in question is the currently logged
54 public class SoneAccessor extends ReflectionAccessor {
57 private static final Logger logger = getLogger(SoneAccessor.class.getName());
60 private final Core core;
61 private final TimeTextConverter timeTextConverter;
64 * Creates a new Sone accessor.
69 public SoneAccessor(Core core, TimeTextConverter timeTextConverter) {
71 this.timeTextConverter = timeTextConverter;
78 public Object get(TemplateContext templateContext, Object object, String member) {
79 Sone sone = (Sone) object;
80 if (member.equals("niceName")) {
81 return getNiceName(sone);
82 } else if (member.equals("friend")) {
83 Sone currentSone = (Sone) templateContext.get("currentSone");
84 return (currentSone != null) && currentSone.hasFriend(sone.getId());
85 } else if (member.equals("current")) {
86 Sone currentSone = (Sone) templateContext.get("currentSone");
87 return (currentSone != null) && currentSone.equals(sone);
88 } else if (member.equals("modified")) {
89 return core.isModifiedSone(sone);
90 } else if (member.equals("status")) {
91 return sone.getStatus();
92 } else if (member.equals("unknown")) {
93 return sone.getStatus() == SoneStatus.unknown;
94 } else if (member.equals("idle")) {
95 return sone.getStatus() == SoneStatus.idle;
96 } else if (member.equals("inserting")) {
97 return sone.getStatus() == SoneStatus.inserting;
98 } else if (member.equals("downloading")) {
99 return sone.getStatus() == SoneStatus.downloading;
100 } else if (member.equals("new")) {
101 return !sone.isKnown();
102 } else if (member.equals("locked")) {
103 return core.isLocked(sone);
104 } else if (member.equals("lastUpdatedText")) {
105 return timeTextConverter.getTimeText(sone.getTime()).getL10nText();
106 } else if (member.equals("trust")) {
107 Sone currentSone = (Sone) templateContext.get("currentSone");
108 if (currentSone == null) {
111 Trust trust = sone.getIdentity().getTrust((OwnIdentity) currentSone.getIdentity());
112 logger.log(Level.FINEST, String.format("Trust for %s by %s: %s", sone, currentSone, trust));
114 return new Trust(null, null, null);
117 } else if (member.equals("allImages")) {
118 return SoneKt.getAllAlbums(sone).stream().flatMap(a -> IMAGES.apply(a).stream()).collect(toList());
119 } else if (member.equals("albums")) {
120 return sone.getRootAlbum().getAlbums();
122 return super.get(templateContext, object, member);
130 * Returns the nice name of the given Sone.
133 * The Sone to get the nice name for
134 * @return The nice name of the Sone
136 public static String getNiceName(Sone sone) {
137 Profile profile = sone.getProfile();
138 String firstName = profile.getFirstName();
139 String middleName = profile.getMiddleName();
140 String lastName = profile.getLastName();
141 if (firstName == null) {
142 if (lastName == null) {
143 return String.valueOf(sone.getName());
147 return firstName + ((middleName != null) ? " " + middleName : "") + ((lastName != null) ? " " + lastName : "");