in server/src/main/java/com/epam/aidial/core/server/service/ShareService.java [177:229]
public void acceptSharedResources(String bucket, String location, String invitationId) {
Invitation invitation = invitationService.getInvitation(invitationId);
if (invitation == null) {
throw new ResourceNotFoundException("No invitation found with id: " + invitationId);
}
List<SharedResource> resourceLinks = invitation.getResources();
for (SharedResource link : resourceLinks) {
String url = link.url();
if (ResourceDescriptorFactory.fromPrivateUrl(url, encryptionService).getBucketName().equals(bucket)) {
throw new IllegalArgumentException("Resource %s already belong to you".formatted(url));
}
}
// group resources with the same type to reduce resource transformations
Map<ResourceTypes, List<SharedResource>> resourceGroups = resourceLinks.stream()
.collect(Collectors.groupingBy(sharedResource -> getResourceType(sharedResource.url())));
resourceGroups.forEach((resourceType, links) -> {
String ownerBucket = getBucket(links.get(0).url());
String ownerLocation = encryptionService.decrypt(ownerBucket);
// write user location to the resource owner
ResourceDescriptor sharedByMe = getShareResource(ResourceTypes.SHARED_BY_ME, resourceType, ownerBucket, ownerLocation);
resourceService.computeResource(sharedByMe, state -> {
SharedByMeDto dto = ProxyUtil.convertToObject(state, SharedByMeDto.class);
if (dto == null) {
dto = new SharedByMeDto(new HashMap<>(), new HashMap<>());
}
// add user location for each link
for (SharedResource resource : links) {
dto.addUserToResource(resource, location);
}
return ProxyUtil.convertToString(dto);
});
ResourceDescriptor sharedWithMe = getShareResource(ResourceTypes.SHARED_WITH_ME, resourceType, bucket, location);
resourceService.computeResource(sharedWithMe, state -> {
SharedResources sharedResources = ProxyUtil.convertToObject(state, SharedResources.class);
if (sharedResources == null) {
sharedResources = new SharedResources(new ArrayList<>());
}
// add all links to the user
sharedResources.addSharedResources(sharedResourcesToMap(links));
return ProxyUtil.convertToString(sharedResources);
});
});
}