X-Git-Url: https://git.pterodactylus.net/?p=Sone.git;a=blobdiff_plain;f=src%2Fmain%2Fjava%2Fnet%2Fpterodactylus%2Fsone%2Ftemplate%2FProfileAccessor.java;h=30d1dc205d6062a525da5cdbf6c158853d95fdb2;hp=52f39dda959def54c2c562ccce9ebb0fe562debb;hb=62573c314957b1851f4fbe693b8746686caa940a;hpb=2cee36b47bf421ab43be6ca5a6be7fdb67441418 diff --git a/src/main/java/net/pterodactylus/sone/template/ProfileAccessor.java b/src/main/java/net/pterodactylus/sone/template/ProfileAccessor.java index 52f39dd..30d1dc2 100644 --- a/src/main/java/net/pterodactylus/sone/template/ProfileAccessor.java +++ b/src/main/java/net/pterodactylus/sone/template/ProfileAccessor.java @@ -1,5 +1,5 @@ /* - * Sone - ProfileAccessor.java - Copyright © 2011 David Roden + * Sone - ProfileAccessor.java - Copyright © 2011–2016 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 @@ -17,35 +17,84 @@ package net.pterodactylus.sone.template; -import java.util.LinkedHashMap; -import java.util.Map; - +import net.pterodactylus.sone.core.Core; import net.pterodactylus.sone.data.Profile; +import net.pterodactylus.sone.data.Sone; +import net.pterodactylus.sone.data.SoneOptions.LoadExternalContent; +import net.pterodactylus.sone.freenet.wot.OwnIdentity; +import net.pterodactylus.sone.freenet.wot.Trust; 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 Profile}s, especially for the profile fields. - * - * @author David ‘Bombe’ Roden + * {@link Accessor} for {@link Profile} objects that overwrites the original + * “avatar” member to include checks for whether the custom avatar should + * actually be shown. */ 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(DataProvider dataProvider, Object object, String member) { + public Object get(TemplateContext templateContext, Object object, String member) { Profile profile = (Profile) object; - if ("fields".equals(member)) { - Map fields = new LinkedHashMap(); - for (String field : profile.getFields()) { - fields.put(field, profile.getField(field)); + if ("avatar".equals(member)) { + Sone currentSone = (Sone) templateContext.get("currentSone"); + if (currentSone == null) { + /* not logged in? don’t show custom avatars, then. */ + return null; + } + String avatarId = profile.getAvatar(); + if (avatarId == null) { + return null; + } + if (core.getImage(avatarId, false) == null) { + /* avatar ID but no matching image? show nothing. */ + return null; + } + Sone remoteSone = profile.getSone(); + if (remoteSone.isLocal()) { + /* always show your own avatars. */ + return avatarId; + } + LoadExternalContent showCustomAvatars = currentSone.getOptions().getShowCustomAvatars(); + if (showCustomAvatars == LoadExternalContent.NEVER) { + return null; + } + if (showCustomAvatars == LoadExternalContent.ALWAYS) { + return avatarId; + } + if (showCustomAvatars == LoadExternalContent.FOLLOWED) { + return currentSone.hasFriend(remoteSone.getId()) ? avatarId : null; + } + Trust trust = remoteSone.getIdentity().getTrust((OwnIdentity) currentSone.getIdentity()); + if (trust == null) { + return null; + } + if ((showCustomAvatars == LoadExternalContent.MANUALLY_TRUSTED) && (trust.getExplicit() != null) && (trust.getExplicit() > 0)) { + return avatarId; + } + if ((showCustomAvatars == LoadExternalContent.TRUSTED) && (((trust.getExplicit() != null) && (trust.getExplicit() > 0)) || ((trust.getImplicit() != null) && (trust.getImplicit() > 0)))) { + return avatarId; } - return fields; + return null; } - return super.get(dataProvider, object, member); + return super.get(templateContext, object, member); } }