From: David ‘Bombe’ Roden Date: Mon, 21 Oct 2013 04:35:23 +0000 (+0200) Subject: Store the avatar ID differently in profile. X-Git-Url: https://git.pterodactylus.net/?a=commitdiff_plain;h=e42eee396d1646bf97316696f1cd7b836aabd8c6;p=Sone.git Store the avatar ID differently in profile. --- diff --git a/src/main/java/net/pterodactylus/sone/core/Core.java b/src/main/java/net/pterodactylus/sone/core/Core.java index 0f22b81..8edb14f 100644 --- a/src/main/java/net/pterodactylus/sone/core/Core.java +++ b/src/main/java/net/pterodactylus/sone/core/Core.java @@ -1094,7 +1094,7 @@ public class Core extends AbstractService implements SoneProvider { /* load avatar. */ String avatarId = configuration.getStringValue(sonePrefix + "/Profile/Avatar").getValue(null); if (avatarId != null) { - profile.setAvatar(getImage(avatarId).orNull()); + profile.setAvatar(getImage(avatarId).transform(GET_ID)); } /* load options. */ diff --git a/src/main/java/net/pterodactylus/sone/core/SoneParser.java b/src/main/java/net/pterodactylus/sone/core/SoneParser.java index 4ce596e..a4946db 100644 --- a/src/main/java/net/pterodactylus/sone/core/SoneParser.java +++ b/src/main/java/net/pterodactylus/sone/core/SoneParser.java @@ -303,9 +303,7 @@ public class SoneParser { } /* process avatar. */ - if (avatarId != null) { - profile.setAvatar(database.getImage(avatarId).orNull()); - } + profile.setAvatar(fromNullable(avatarId)); /* okay, apparently everything was parsed correctly. Now import. */ sone.setProfile(profile); diff --git a/src/main/java/net/pterodactylus/sone/data/Profile.java b/src/main/java/net/pterodactylus/sone/data/Profile.java index db55227..3e287d7 100644 --- a/src/main/java/net/pterodactylus/sone/data/Profile.java +++ b/src/main/java/net/pterodactylus/sone/data/Profile.java @@ -155,18 +155,12 @@ public class Profile implements Fingerprintable { /** * Sets the avatar image. * - * @param avatar - * The new avatar image, or {@code null} to not select an avatar - * image. - * @return This Sone + * @param avatarId + * The ID of the new avatar image + * @return This profile */ - public Profile setAvatar(Image avatar) { - if (avatar == null) { - this.avatar = null; - return this; - } - checkArgument(avatar.getSone().equals(sone), "avatar must belong to Sone"); - this.avatar = avatar.getId(); + public Profile setAvatar(Optional avatarId) { + this.avatar = avatarId.orNull(); return this; } diff --git a/src/main/java/net/pterodactylus/sone/template/ProfileAccessor.java b/src/main/java/net/pterodactylus/sone/template/ProfileAccessor.java index d215c82..9b0a7fc 100644 --- a/src/main/java/net/pterodactylus/sone/template/ProfileAccessor.java +++ b/src/main/java/net/pterodactylus/sone/template/ProfileAccessor.java @@ -66,6 +66,9 @@ public class ProfileAccessor extends ReflectionAccessor { /* avatar ID but no matching image? show nothing. */ return null; } + if (!avatarImageBelongsToTheSameSoneAsTheProfile(profile, avatarId)) { + return null; + } Sone remoteSone = profile.getSone(); if (remoteSone.isLocal()) { /* always show your own avatars. */ @@ -96,4 +99,8 @@ public class ProfileAccessor extends ReflectionAccessor { return super.get(templateContext, object, member); } + private boolean avatarImageBelongsToTheSameSoneAsTheProfile(Profile profile, String avatarId) { + return core.getImage(avatarId).get().getSone().equals(profile.getSone()); + } + } diff --git a/src/main/java/net/pterodactylus/sone/web/EditProfilePage.java b/src/main/java/net/pterodactylus/sone/web/EditProfilePage.java index 069e732..0d1f7d3 100644 --- a/src/main/java/net/pterodactylus/sone/web/EditProfilePage.java +++ b/src/main/java/net/pterodactylus/sone/web/EditProfilePage.java @@ -17,6 +17,8 @@ package net.pterodactylus.sone.web; +import static net.pterodactylus.sone.data.Identified.GET_ID; + import java.util.List; import net.pterodactylus.sone.data.Profile; @@ -77,7 +79,7 @@ public class EditProfilePage extends SoneTemplatePage { avatarId = request.getHttpRequest().getPartAsStringFailsafe("avatarId", 36); profile.modify().setFirstName(getNameFromFormField(firstName)).setMiddleName(getNameFromFormField(middleName)).setLastName(getNameFromFormField(lastName)).update(); profile.modify().setBirthDay(birthDay).setBirthMonth(birthMonth).setBirthYear(birthYear).update(); - profile.setAvatar(webInterface.getCore().getImage(avatarId).orNull()); + profile.setAvatar(webInterface.getCore().getImage(avatarId).transform(GET_ID)); for (Field field : fields) { String value = request.getHttpRequest().getPartAsStringFailsafe("field-" + field.getId(), 400); field.setValue(value);