🔀 Merge branch 'release/v82'
[Sone.git] / src / main / java / net / pterodactylus / sone / template / SoneAccessor.java
1 /*
2  * Sone - SoneAccessor.java - Copyright © 2010–2020 David Roden
3  *
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.
8  *
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.
13  *
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/>.
16  */
17
18 package net.pterodactylus.sone.template;
19
20 import static java.util.logging.Logger.getLogger;
21
22 import java.util.logging.Level;
23 import java.util.logging.Logger;
24
25 import net.pterodactylus.sone.core.Core;
26 import net.pterodactylus.sone.data.Profile;
27 import net.pterodactylus.sone.data.Sone;
28 import net.pterodactylus.sone.data.Sone.SoneStatus;
29 import net.pterodactylus.sone.data.SoneKt;
30 import net.pterodactylus.sone.freenet.wot.OwnIdentity;
31 import net.pterodactylus.sone.freenet.wot.Trust;
32 import net.pterodactylus.sone.text.TimeTextConverter;
33 import net.pterodactylus.util.template.Accessor;
34 import net.pterodactylus.util.template.ReflectionAccessor;
35 import net.pterodactylus.util.template.TemplateContext;
36
37 /**
38  * {@link Accessor} for {@link Sone}s that adds a couple of properties to Sones.
39  * <dl>
40  * <dt>niceName</dt>
41  * <dd>Will show a combination of first name, middle name, and last name, if
42  * available, otherwise the username of the Sone is returned.</dd>
43  * <dt>friend</dt>
44  * <dd>Will return {@code true} if the sone in question is a friend of the
45  * currently logged in Sone (as determined by accessing the “currentSone”
46  * variable of the given {@link TemplateContext}).</dd>
47  * <dt>current</dt>
48  * <dd>Will return {@code true} if the sone in question is the currently logged
49  * in Sone.</dd>
50  * </dl>
51  */
52 public class SoneAccessor extends ReflectionAccessor {
53
54         /** The logger. */
55         private static final Logger logger = getLogger(SoneAccessor.class.getName());
56
57         /** The core. */
58         private final Core core;
59         private final TimeTextConverter timeTextConverter;
60
61         /**
62          * Creates a new Sone accessor.
63          *
64          * @param core
65          *            The Sone core
66          */
67         public SoneAccessor(Core core, TimeTextConverter timeTextConverter) {
68                 this.core = core;
69                 this.timeTextConverter = timeTextConverter;
70         }
71
72         /**
73          * {@inheritDoc}
74          */
75         @Override
76         public Object get(TemplateContext templateContext, Object object, String member) {
77                 Sone sone = (Sone) object;
78                 if (member.equals("niceName")) {
79                         return getNiceName(sone);
80                 } else if (member.equals("friend")) {
81                         Sone currentSone = (Sone) templateContext.get("currentSone");
82                         return (currentSone != null) && currentSone.hasFriend(sone.getId());
83                 } else if (member.equals("current")) {
84                         Sone currentSone = (Sone) templateContext.get("currentSone");
85                         return (currentSone != null) && currentSone.equals(sone);
86                 } else if (member.equals("modified")) {
87                         return core.isModifiedSone(sone);
88                 } else if (member.equals("status")) {
89                         return sone.getStatus();
90                 } else if (member.equals("unknown")) {
91                         return sone.getStatus() == SoneStatus.unknown;
92                 } else if (member.equals("idle")) {
93                         return sone.getStatus() == SoneStatus.idle;
94                 } else if (member.equals("inserting")) {
95                         return sone.getStatus() == SoneStatus.inserting;
96                 } else if (member.equals("downloading")) {
97                         return sone.getStatus() == SoneStatus.downloading;
98                 } else if (member.equals("new")) {
99                         return !sone.isKnown();
100                 } else if (member.equals("locked")) {
101                         return core.isLocked(sone);
102                 } else if (member.equals("lastUpdatedText")) {
103                         return timeTextConverter.getTimeText(sone.getTime()).getL10nText();
104                 } else if (member.equals("trust")) {
105                         Sone currentSone = (Sone) templateContext.get("currentSone");
106                         if (currentSone == null) {
107                                 return null;
108                         }
109                         Trust trust = sone.getIdentity().getTrust((OwnIdentity) currentSone.getIdentity());
110                         logger.log(Level.FINEST, String.format("Trust for %s by %s: %s", sone, currentSone, trust));
111                         if (trust == null) {
112                                 return new Trust(null, null, null);
113                         }
114                         return trust;
115                 } else if (member.equals("allImages")) {
116                         return SoneKt.getAllImages(sone);
117                 } else if (member.equals("albums")) {
118                         return sone.getRootAlbum().getAlbums();
119                 }
120                 return super.get(templateContext, object, member);
121         }
122
123         //
124         // STATIC METHODS
125         //
126
127         /**
128          * Returns the nice name of the given Sone.
129          *
130          * @param sone
131          *            The Sone to get the nice name for
132          * @return The nice name of the Sone
133          */
134         public static String getNiceName(Sone sone) {
135                 Profile profile = sone.getProfile();
136                 String firstName = profile.getFirstName();
137                 String middleName = profile.getMiddleName();
138                 String lastName = profile.getLastName();
139                 if (firstName == null) {
140                         if (lastName == null) {
141                                 return String.valueOf(sone.getName());
142                         }
143                         return lastName;
144                 }
145                 return firstName + ((middleName != null) ? " " + middleName : "") + ((lastName != null) ? " " + lastName : "");
146         }
147
148 }