From: David ‘Bombe’ Roden Date: Wed, 7 Dec 2011 15:03:13 +0000 (+0100) Subject: Add accessor for Profile objects. X-Git-Tag: 0.7.6^2~1^2~8 X-Git-Url: https://git.pterodactylus.net/?p=Sone.git;a=commitdiff_plain;h=ec46afaef0469b4362e0825354f54812531003b8 Add accessor for Profile objects. --- diff --git a/src/main/java/net/pterodactylus/sone/template/ProfileAccessor.java b/src/main/java/net/pterodactylus/sone/template/ProfileAccessor.java new file mode 100644 index 0000000..97972c7 --- /dev/null +++ b/src/main/java/net/pterodactylus/sone/template/ProfileAccessor.java @@ -0,0 +1,90 @@ +/* + * Sone - ProfileAccessor.java - Copyright © 2011 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 + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +package net.pterodactylus.sone.template; + +import net.pterodactylus.sone.core.Core; +import net.pterodactylus.sone.data.Profile; +import net.pterodactylus.sone.data.Sone; +import net.pterodactylus.sone.data.Sone.ShowCustomAvatars; +import net.pterodactylus.sone.freenet.wot.Trust; +import net.pterodactylus.util.template.Accessor; +import net.pterodactylus.util.template.ReflectionAccessor; +import net.pterodactylus.util.template.TemplateContext; + +/** + * {@link Accessor} for {@link Profile} objects that overwrites the original + * “avatar” member to include checks for whether the custom avatar should + * actually be shown. + * + * @author David ‘Bombe’ Roden + */ +public class ProfileAccessor extends ReflectionAccessor { + + /** The core. */ + private final Core core; + + /** + * Creates a new profile accessor. + * + * @param core + * The Sone core + */ + public ProfileAccessor(Core core) { + this.core = core; + } + + /** + * {@inheritDoc} + */ + @Override + public Object get(TemplateContext templateContext, Object object, String member) { + Profile profile = (Profile) object; + if ("avatar".equals(member)) { + Sone currentSone = (Sone) templateContext.get("currentSone"); + if (currentSone == null) { + /* not logged in? don’t show custom avatars, then. */ + return null; + } + ShowCustomAvatars showCustomAvatars = currentSone.getOptions(). getEnumOption("ShowCustomAvatars").get(); + if (showCustomAvatars == ShowCustomAvatars.NEVER) { + return null; + } + String avatarId = profile.getAvatar(); + if ((showCustomAvatars == ShowCustomAvatars.ALWAYS) || (avatarId == null)) { + return avatarId; + } + Sone remoteSone = profile.getSone(); + if ((showCustomAvatars == ShowCustomAvatars.FOLLOWED) && currentSone.hasFriend(remoteSone.getId())) { + return avatarId; + } + Trust trust = core.getTrust(currentSone, remoteSone); + if (trust == null) { + return null; + } + if ((showCustomAvatars == ShowCustomAvatars.MANUALLY_TRUSTED) && (trust.getExplicit() != null) && (trust.getExplicit() > 0)) { + return avatarId; + } + if ((showCustomAvatars == ShowCustomAvatars.TRUSTED) && ((trust.getExplicit() != null) && (trust.getExplicit() > 0)) || ((trust.getImplicit() != null) && (trust.getImplicit() > 0))) { + return avatarId; + } + return null; + } + return super.get(templateContext, object, member); + } + +}