Store the avatar ID differently in profile.
authorDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Mon, 21 Oct 2013 04:35:23 +0000 (06:35 +0200)
committerDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Fri, 28 Feb 2014 21:25:32 +0000 (22:25 +0100)
src/main/java/net/pterodactylus/sone/core/Core.java
src/main/java/net/pterodactylus/sone/core/SoneParser.java
src/main/java/net/pterodactylus/sone/data/Profile.java
src/main/java/net/pterodactylus/sone/template/ProfileAccessor.java
src/main/java/net/pterodactylus/sone/web/EditProfilePage.java

index 0f22b81..8edb14f 100644 (file)
@@ -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. */
index 4ce596e..a4946db 100644 (file)
@@ -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);
index db55227..3e287d7 100644 (file)
@@ -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<String> avatarId) {
+               this.avatar = avatarId.orNull();
                return this;
        }
 
index d215c82..9b0a7fc 100644 (file)
@@ -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());
+       }
+
 }
index 069e732..0d1f7d3 100644 (file)
@@ -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);