Skip to content

Instantly share code, notes, and snippets.

@jagmohansingh
Created September 30, 2023 10:01
Show Gist options
  • Save jagmohansingh/108f739344e1cf918fdcedc59d4a861a to your computer and use it in GitHub Desktop.
Save jagmohansingh/108f739344e1cf918fdcedc59d4a861a to your computer and use it in GitHub Desktop.
Community User Photo Methods
public with sharing class DeletePhotoCtrl {
@AuraEnabled @RemoteAction
public static Map<String, Object> deleteCommunityUserPhoto() {
Map<String, Object> result = new Map<String, Object>();
try {
// get logged in user id
String userId = UserInfo.getUserId();
// get community id
String networkId = Network.getNetworkId();
if (networkId != null) {
// call deletePhoto method from UserProfiles class
ConnectApi.UserProfiles.deletePhoto(networkId, userId);
result.put('status', true);
} else {
result.put('status', false);
result.put('error', 'Invalid Network Id');
}
} catch (Exception ex) {
result.put('status', false);
result.put('error', ex.getMessage());
}
return result;
}
}
public with sharing class GetPhotoCtrl {
@AuraEnabled @RemoteAction
public static Map<String, Object> getCommunityUserPhoto() {
Map<String, Object> result = new Map<String, Object>();
try {
// get logged in user id
String userId = UserInfo.getUserId();
// get community id
String networkId = Network.getNetworkId();
if (networkId != null) {
// call getPhoto method to retrieve user photo
ConnectApi.Photo profilePhoto = ConnectApi.UserProfiles.getPhoto(networkId, userId);
result.put('photo', profilePhoto);
result.put('status', true);
} else {
result.put('status', false);
result.put('error', 'Invalid Network Id');
}
} catch (Exception ex) {
result.put('status', false);
result.put('error', ex.getMessage());
}
return result;
}
}
public with sharing class SetPhotoCtrl {
@AuraEnabled @RemoteAction
public static Map<String, Object> setCommunityUserPhoto(String base64Photo, String contentType) {
Map<String, Object> result = new Map<String, Object>();
try {
// get logged in user id
String userId = UserInfo.getUserId();
// get the community id
String networkId = Network.getNetworkId();
if (networkId != null) {
String photoName = '';
if (contentType == 'image/png') {
photoName = 'userImage.png';
} else if (contentType == 'image/jpeg') {
photoName = 'userImage.jpg';
} else {
// TODO throw exception
}
// convert base64 encoded string to blob
Blob photoBlob = EncodingUtil.base64Decode(base64Photo);
// instantiate ConnectApi.BinaryInput object with image data
ConnectApi.BinaryInput binaryPhoto = new ConnectApi.BinaryInput(photoBlob, contentType, photoName);
// set the user profile photo
ConnectApi.Photo profilePhoto = ConnectApi.UserProfiles.setPhoto(networkId, userId, binaryPhoto);
result.put('status', true);
} else {
result.put('status', false);
result.put('error', 'Invalid Network Id');
}
} catch (Exception ex) {
result.put('status', false);
result.put('error', ex.getMessage());
}
return result;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment