15 Replies Latest reply on Nov 9, 2015 11:25 AM by mpetrosky

    Change avatar with REST API 3.9 not possible

    jgoldhammer

      Hello,

       

      I want to change the avatar of my own user with the REST API.

      The resource json does not provide the allowed method put.

       

      method_not_allowed_for_post_avatar.png

      When I try to change the avatar with the put or post method, Jive says me that this method is not allowed.

       

      json_response_for_people.png

       

      The upload of avatars is available to all users as you can see in the following screenshot.

       

      avatar_upload_allowed.png

       

      As long as avatars cannot be synced from the active directory, we wanted to implement a webhook which updates the user avatar each time a new user is created or a user updates the profile image.

       

      Thanks,

      Jens

        • Re: Change avatar with REST API 3.9 not possible

          This is a known oversight in the API and it is slated to be fixed in either this upcoming cloud release or the next.   Aron, can you confirm?

          • Re: Change avatar with REST API 3.9 not possible
            whoiskevin

            Unfortunately you cannot do this with the Rest API.   I had the same requirement.  Which for those watching should be an option for the LDAP synchronization since if you are bringing over photos from an official source you just might want to do the same with the avatar which is actually seen on the site way more than the profile photo.  Also with that option to be able to lock the changing of the profile photo since more than likely an official sync should not leave the user able to change the photo as it currently behaves.

             

            I modified the com.jivesoftware.community.user.sync.DirectoryProfileSynchronizer to use the profile photo as an avatar as well.  Currently I just have a quick resize to avatar size.  Of course this requires an overlay or plugin type of deployment but pretty much every time I have a requirement that is the case.

            1 person found this helpful
              • Re: Change avatar with REST API 3.9 not possible
                jgoldhammer

                Hi Kevin,

                 

                thanks for the pointer. Can you share your changes as a patch or something? I would really appreciate it!

                 

                Thanks,

                Jens

                  • Re: Change avatar with REST API 3.9 not possible
                    whoiskevin

                    I'll pull it up a bit later.  I don't have that as a single diff from my source control but I can send you the method modifications and the spring.xml entries you will need.

                    • Re: Change avatar with REST API 3.9 not possible
                      whoiskevin

                      Here is the relevant method from DirectoryProfileSynchronizer.

                      private AvatarManager avatarManagerImpl;
                      
                      public void setAvatarManagerImpl(AvatarManager avatarManagerImpl) {
                        this.avatarManagerImpl = avatarManagerImpl;
                      }
                      

                      And the one method I changed:

                      @Transactional(readOnly = false, propagation = Propagation.REQUIRED)
                        protected boolean syncProfilePhoto(DirectoryUser userAdapter, User localUser) {
                            if (!initialized) {
                              return false;
                            }
                            if (isSyncPhoto()) {
                              boolean syncAvatar = JiveGlobals.getJiveBooleanProperty("jive.sync.profile.ldap.avatar", false);  // depends on isSyncPhoto for now.
                              boolean deleteImagesWhenLdapIsBlank = JiveGlobals.getJiveBooleanProperty("jive.sync.profile.ldap.remove.images", false);
                              String photoAttribute = directoryProfileSynchronizerConfiguration.getPhotoField();
                              if (StringUtils.isNotBlank(photoAttribute)) {
                                  Object photo = userAdapter.getAttributeData().get(photoAttribute);
                                  if (photo != null) {
                                    try {
                                        BufferedImage bufferedImage = ProfileImageUtils.readImage(photo);
                                        if (bufferedImage != null) {
                                          bufferedImage = ProfileImageUtils.adjustBackground(bufferedImage);
                                          if (ProfileImageUtils.doesImageExceedMaxDimensions(bufferedImage,
                                                ProfileImageUtils.maxWidth(), ProfileImageUtils.maxHeight()))
                                          {
                                              try {
                                                bufferedImage = ProfileImageUtils.resizeImage(bufferedImage,
                                                      ProfileImageUtils.maxWidth(), ProfileImageUtils.maxHeight());
                                              }
                                              catch (Exception e) {
                                                log.error(e.getMessage());
                                              }
                                          }
                                          ByteArrayOutputStream out = ProfileImageUtils.asPNG(bufferedImage);
                                          InputStream in = new ByteArrayInputStream(out.toByteArray());
                                          profileManager.setProfileImage(localUser, in);
                                          // Begin Avatar sync customization
                                          if ( syncAvatar ) {
                                              long activeAvatarID = avatarManagerImpl.getActiveAvatar(localUser).getID();
                                              if (activeAvatarID > 0) {
                                                avatarManagerImpl.deleteUserAvatar(localUser);
                                              }
                                              if (ProfileImageUtils.doesImageExceedMaxDimensions(bufferedImage,
                                                    avatarManagerImpl.getMaxAllowableWidth(), avatarManagerImpl.getMaxAllowableHeight())) {
                                                try {
                                                    bufferedImage = ProfileImageUtils.resizeImage(bufferedImage,
                                                          avatarManagerImpl.getMaxAllowableWidth(), avatarManagerImpl.getMaxAllowableHeight());
                                                    ByteArrayOutputStream avatarOut = ProfileImageUtils.asPNG(bufferedImage);
                                                    Avatar newAvatar = avatarManagerImpl.createAvatar(localUser, "ldap-avatar-" + localUser.getID(), "image/png", new ByteArrayInputStream(avatarOut.toByteArray()));
                                                    avatarManagerImpl.setActiveAvatar(localUser, newAvatar);
                                                    log.debug("Synchronized avatar for user " + localUser.getUsername());
                                                }
                                                catch (Exception avatarSyncException) {
                                                    log.warn("Unable to resize and apply avatar from profile image.", avatarSyncException);
                                                }
                                              }
                                          }
                                          // End Avatar sync customization
                                          log.info("Synchronized profile photo for user " + userAdapter);
                                          return true;
                                        }
                                        else {
                                          log.warn("Unable to process remote image for " + userAdapter);
                                        }
                                    }
                                    catch (Exception e) {
                                        log.error("Unable to set profile image from photo for " + userAdapter, e);
                                    }
                                  }
                              }
                              if ( deleteImagesWhenLdapIsBlank ) {
                                  profileManager.deleteProfileImage(localUser);  // They don't have a photo in LDAP or elected not to publish it.
                                  if ( syncAvatar ) {
                                    avatarManagerImpl.deleteUserAvatar(localUser);    // Same for avatar we have to remove if LDAP does not have usable photo.
                                  }
                              }
                            }
                            return false;
                      
                      
                        }
                      

                       

                      The avatar manager is added so you have to override this definition in spring to add the avatarManagerImpl to this class so that it is properly injected.  You'll notice at the top of the method that it is dependent on two system properties.  One controls the deletion of avatars and photos since the requirement may be that a user can choose not to publish their "corporate" photo.  In that case it will be removed and they will be left with a default photo.  The other simply specifies if you are synching avatars or not.

                      This does not include other changes.  For instance you will want to modify the UI to eliminate the primary photo drag and drop as well as reduce the profile to 1 uploaded photo instead of 10.  In that case you might want to change the profile layout completely.  Sorry I don't have those things separately to share but they are needed since you want to control a single photo from the LDAP sync instead of having so many per user which I always thought was kinda un-necessary fluff anyway...seriously I don't want to scroll through your 10 profile photos :-p

                      1 person found this helpful