Skip to content

Commit

Permalink
[211] Store profile images retrieved using OAuth2 metadata in our dat…
Browse files Browse the repository at this point in the history
…abase

Bug: #211
Signed-off-by: Stéphane Bégaudeau <[email protected]>
  • Loading branch information
sbegaudeau committed Jul 26, 2023
1 parent 2c14b24 commit 39b7387
Show file tree
Hide file tree
Showing 35 changed files with 448 additions and 102 deletions.
4 changes: 4 additions & 0 deletions backend/svalyn-studio-application/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,10 @@
</properties>

<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-graphql</artifactId>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
/*
* Copyright (c) 2023 Stéphane Bégaudeau.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy of this software and
* associated documentation files (the "Software"), to deal in the Software without restriction,
* including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so,
* subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all copies or substantial
* portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT
* LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/

package com.svalyn.studio.application.controllers.account;

import com.svalyn.studio.domain.account.repositories.IAccountRepository;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.core.io.ClassPathResource;
import org.springframework.http.HttpHeaders;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;

import java.io.IOException;
import java.util.Objects;

/**
* Controller used to retrieve avatar image.
*
* @author sbegaudeau
*/
@Controller
public class AvatarController {

private final IAccountRepository accountRepository;

private final Logger logger = LoggerFactory.getLogger(AvatarController.class);

public AvatarController(IAccountRepository accountRepository) {
this.accountRepository = Objects.requireNonNull(accountRepository);
}

@GetMapping(value = "/api/avatars/{username}")
public ResponseEntity<byte[]> getAvatar(@PathVariable String username) {
return this.accountRepository.findByUsername(username)
.filter(account -> account.getImage() != null && account.getImageContentType() != null)
.map(account -> ResponseEntity.ok()
.header(HttpHeaders.CONTENT_TYPE, account.getImageContentType())
.body(account.getImage()))
.orElse(this.defaultAvatar());
}

public ResponseEntity<byte[]> defaultAvatar() {
byte[] content = new byte[] {};
var avatar = new ClassPathResource("images/avatar.png");
try {
content = avatar.getContentAsByteArray();
} catch (IOException exception) {
logger.warn(exception.getMessage(), exception);
}

return ResponseEntity.ok()
.header(HttpHeaders.CONTENT_TYPE, "image/png")
.body(content);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import com.svalyn.studio.application.controllers.dto.ProfileDTO;
import com.svalyn.studio.application.controllers.viewer.Viewer;
import com.svalyn.studio.application.services.account.api.IAccountService;
import com.svalyn.studio.application.services.account.api.IAvatarUrlService;
import com.svalyn.studio.domain.account.repositories.IAccountRepository;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
Expand All @@ -40,19 +41,22 @@ public class AccountService implements IAccountService {

private final IAccountRepository accountRepository;

public AccountService(IAccountRepository accountRepository) {
private final IAvatarUrlService avatarUrlService;

public AccountService(IAccountRepository accountRepository, IAvatarUrlService avatarUrlService) {
this.accountRepository = Objects.requireNonNull(accountRepository);
this.avatarUrlService = Objects.requireNonNull(avatarUrlService);
}

@Override
@Transactional(readOnly = true)
public Optional<Viewer> findViewerById(UUID id) {
return this.accountRepository.findById(id).map(account -> new Viewer(account.getName(), account.getUsername(), account.getImageUrl()));
return this.accountRepository.findById(id).map(account -> new Viewer(account.getName(), account.getUsername(), this.avatarUrlService.imageUrl(account.getUsername())));
}

@Override
@Transactional(readOnly = true)
public Optional<ProfileDTO> findProfileByUsername(String username) {
return this.accountRepository.findByUsername(username).map(account -> new ProfileDTO(account.getName(), account.getUsername(), account.getImageUrl()));
return this.accountRepository.findByUsername(username).map(account -> new ProfileDTO(account.getName(), account.getUsername(), this.avatarUrlService.imageUrl(account.getUsername())));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/*
* Copyright (c) 2023 Stéphane Bégaudeau.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy of this software and
* associated documentation files (the "Software"), to deal in the Software without restriction,
* including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so,
* subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all copies or substantial
* portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT
* LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
package com.svalyn.studio.application.services.account;

import com.svalyn.studio.application.services.account.api.IAvatarUrlService;
import org.springframework.stereotype.Service;
import org.springframework.web.servlet.support.ServletUriComponentsBuilder;

/**
* Used to compute the url of the avatar of a profile.
*
* @author sbegaudeau
*/
@Service
public class AvatarUrlService implements IAvatarUrlService {
@Override
public String imageUrl(String username) {
var currentUri = ServletUriComponentsBuilder.fromCurrentRequestUri().build().toUri();
var uri = currentUri.getScheme() + "://" + currentUri.getHost();
if (currentUri.getPort() != -1) {
uri = uri + ":" + currentUri.getPort();
}
uri = uri + "/api/avatars/" + username;
return uri;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/*
* Copyright (c) 2023 Stéphane Bégaudeau.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy of this software and
* associated documentation files (the "Software"), to deal in the Software without restriction,
* including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so,
* subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all copies or substantial
* portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT
* LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
package com.svalyn.studio.application.services.account.api;

/**
* Used to compute the url of the avatar of a profile.
*
* @author sbegaudeau
*/
public interface IAvatarUrlService {
String imageUrl(String username);
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@

import com.svalyn.studio.application.controllers.activity.dto.ActivityEntryDTO;
import com.svalyn.studio.application.controllers.dto.ProfileDTO;
import com.svalyn.studio.application.services.account.api.IAvatarUrlService;
import com.svalyn.studio.application.services.activity.api.IActivityService;
import com.svalyn.studio.domain.account.Account;
import com.svalyn.studio.domain.account.repositories.IAccountRepository;
Expand Down Expand Up @@ -49,15 +50,18 @@ public class ActivityService implements IActivityService {

private final IActivityEntryRepository activityEntryRepository;

private final IAvatarUrlService avatarUrlService;

public ActivityService(IAccountRepository accountRepository, IActivityEntryRepository activityEntryRepository) {

public ActivityService(IAccountRepository accountRepository, IActivityEntryRepository activityEntryRepository, IAvatarUrlService avatarUrlService) {
this.accountRepository = Objects.requireNonNull(accountRepository);
this.activityEntryRepository = Objects.requireNonNull(activityEntryRepository);
this.avatarUrlService = Objects.requireNonNull(avatarUrlService);
}

private Optional<ActivityEntryDTO> toDTO(ActivityEntry activityEntry) {
var optionalCreatedByProfile = this.accountRepository.findById(activityEntry.getCreatedBy().getId())
.map(account -> new ProfileDTO(account.getName(), account.getUsername(), account.getImageUrl()));
.map(account -> new ProfileDTO(account.getName(), account.getUsername(), this.avatarUrlService.imageUrl(account.getUsername())));

return optionalCreatedByProfile.map(createdBy -> new ActivityEntryDTO(
activityEntry.getId(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@

import com.svalyn.studio.application.controllers.dto.ProfileDTO;
import com.svalyn.studio.application.controllers.history.dto.BranchDTO;
import com.svalyn.studio.application.services.account.api.IAvatarUrlService;
import com.svalyn.studio.application.services.history.api.IBranchService;
import com.svalyn.studio.domain.account.repositories.IAccountRepository;
import com.svalyn.studio.domain.history.Branch;
Expand All @@ -47,9 +48,12 @@ public class BranchService implements IBranchService {
private final IAccountRepository accountRepository;
private final IBranchRepository branchRepository;

public BranchService(IAccountRepository accountRepository, IBranchRepository branchRepository) {
private final IAvatarUrlService avatarUrlService;

public BranchService(IAccountRepository accountRepository, IBranchRepository branchRepository, IAvatarUrlService avatarUrlService) {
this.accountRepository = Objects.requireNonNull(accountRepository);
this.branchRepository = Objects.requireNonNull(branchRepository);
this.avatarUrlService = Objects.requireNonNull(avatarUrlService);
}

@Override
Expand All @@ -71,9 +75,9 @@ public Optional<BranchDTO> findByProjectIdAndName(UUID projectId, String name) {

private Optional<BranchDTO> toDTO(Branch branch) {
var optionalCreatedByProfile = this.accountRepository.findById(branch.getCreatedBy().getId())
.map(account -> new ProfileDTO(account.getName(), account.getUsername(), account.getImageUrl()));
.map(account -> new ProfileDTO(account.getName(), account.getUsername(), this.avatarUrlService.imageUrl(account.getUsername())));
var optionalLastModifiedByProfile = this.accountRepository.findById(branch.getLastModifiedBy().getId())
.map(account -> new ProfileDTO(account.getName(), account.getUsername(), account.getImageUrl()));
.map(account -> new ProfileDTO(account.getName(), account.getUsername(), this.avatarUrlService.imageUrl(account.getUsername())));
var changeId = Optional.ofNullable(branch.getChange()).map(AggregateReference::getId).orElse(null);

return optionalCreatedByProfile.flatMap(createdBy ->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
import com.svalyn.studio.application.controllers.dto.IPayload;
import com.svalyn.studio.application.controllers.dto.ProfileDTO;
import com.svalyn.studio.application.controllers.dto.SuccessPayload;
import com.svalyn.studio.application.services.account.api.IAvatarUrlService;
import com.svalyn.studio.application.services.history.api.IChangeProposalService;
import com.svalyn.studio.domain.Failure;
import com.svalyn.studio.domain.Success;
Expand All @@ -44,7 +45,6 @@
import com.svalyn.studio.domain.history.services.api.IChangeProposalCreationService;
import com.svalyn.studio.domain.history.services.api.IChangeProposalDeletionService;
import com.svalyn.studio.domain.history.services.api.IChangeProposalUpdateService;
import com.svalyn.studio.domain.resource.repositories.IResourceRepository;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.PageRequest;
Expand Down Expand Up @@ -74,22 +74,22 @@ public class ChangeProposalService implements IChangeProposalService {

private final IChangeProposalDeletionService changeProposalDeletionService;

private final IResourceRepository resourceRepository;
private final IAvatarUrlService avatarUrlService;

public ChangeProposalService(IAccountRepository accountRepository, IChangeProposalRepository changeProposalRepository, IChangeProposalCreationService changeProposalCreationService, IChangeProposalUpdateService changeProposalUpdateService, IChangeProposalDeletionService changeProposalDeletionService, IResourceRepository resourceRepository) {
public ChangeProposalService(IAccountRepository accountRepository, IChangeProposalRepository changeProposalRepository, IChangeProposalCreationService changeProposalCreationService, IChangeProposalUpdateService changeProposalUpdateService, IChangeProposalDeletionService changeProposalDeletionService, IAvatarUrlService avatarUrlService) {
this.accountRepository = Objects.requireNonNull(accountRepository);
this.changeProposalRepository = Objects.requireNonNull(changeProposalRepository);
this.changeProposalCreationService = Objects.requireNonNull(changeProposalCreationService);
this.changeProposalUpdateService = Objects.requireNonNull(changeProposalUpdateService);
this.changeProposalDeletionService = Objects.requireNonNull(changeProposalDeletionService);
this.resourceRepository = Objects.requireNonNull(resourceRepository);
this.avatarUrlService = Objects.requireNonNull(avatarUrlService);
}

private Optional<ChangeProposalDTO> toDTO(ChangeProposal changeProposal) {
var optionalCreatedByProfile = this.accountRepository.findById(changeProposal.getCreatedBy().getId())
.map(account -> new ProfileDTO(account.getName(), account.getUsername(), account.getImageUrl()));
.map(account -> new ProfileDTO(account.getName(), account.getUsername(), this.avatarUrlService.imageUrl(account.getUsername())));
var optionalLastModifiedByProfile = this.accountRepository.findById(changeProposal.getLastModifiedBy().getId())
.map(account -> new ProfileDTO(account.getName(), account.getUsername(), account.getImageUrl()));
.map(account -> new ProfileDTO(account.getName(), account.getUsername(), this.avatarUrlService.imageUrl(account.getUsername())));

return optionalCreatedByProfile.flatMap(createdBy ->
optionalLastModifiedByProfile.map(lastModifiedBy ->
Expand Down Expand Up @@ -144,9 +144,9 @@ public IPayload createChangeProposal(CreateChangeProposalInput input) {

private Optional<ReviewDTO> toDTO(Review review) {
var optionalCreatedByProfile = this.accountRepository.findById(review.getCreatedBy().getId())
.map(account -> new ProfileDTO(account.getName(), account.getUsername(), account.getImageUrl()));
.map(account -> new ProfileDTO(account.getName(), account.getUsername(), this.avatarUrlService.imageUrl(account.getUsername())));
var optionalLastModifiedByProfile = this.accountRepository.findById(review.getLastModifiedBy().getId())
.map(account -> new ProfileDTO(account.getName(), account.getUsername(), account.getImageUrl()));
.map(account -> new ProfileDTO(account.getName(), account.getUsername(), this.avatarUrlService.imageUrl(account.getUsername())));

return optionalCreatedByProfile.flatMap(createdBy ->
optionalLastModifiedByProfile.map(lastModifiedBy ->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import com.fasterxml.jackson.databind.ObjectMapper;
import com.svalyn.studio.application.controllers.dto.ProfileDTO;
import com.svalyn.studio.application.controllers.history.dto.ChangeDTO;
import com.svalyn.studio.application.services.account.api.IAvatarUrlService;
import com.svalyn.studio.application.services.history.api.IChangeService;
import com.svalyn.studio.domain.account.repositories.IAccountRepository;
import com.svalyn.studio.domain.history.Change;
Expand All @@ -50,18 +51,21 @@ public class ChangeService implements IChangeService {

private final IChangeRepository changeRepository;

private final IAvatarUrlService avatarUrlService;

private final Logger logger = LoggerFactory.getLogger(ChangeService.class);

public ChangeService(IAccountRepository accountRepository, IChangeRepository changeRepository) {
public ChangeService(IAccountRepository accountRepository, IChangeRepository changeRepository, IAvatarUrlService avatarUrlService) {
this.accountRepository = Objects.requireNonNull(accountRepository);
this.changeRepository = Objects.requireNonNull(changeRepository);
this.avatarUrlService = Objects.requireNonNull(avatarUrlService);
}

private Optional<ChangeDTO> toDTO(Change change) {
var optionalCreatedByProfile = this.accountRepository.findById(change.getCreatedBy().getId())
.map(account -> new ProfileDTO(account.getName(), account.getUsername(), account.getImageUrl()));
.map(account -> new ProfileDTO(account.getName(), account.getUsername(), this.avatarUrlService.imageUrl(account.getUsername())));
var optionalLastModifiedByProfile = this.accountRepository.findById(change.getLastModifiedBy().getId())
.map(account -> new ProfileDTO(account.getName(), account.getUsername(), account.getImageUrl()));
.map(account -> new ProfileDTO(account.getName(), account.getUsername(), this.avatarUrlService.imageUrl(account.getUsername())));

return optionalCreatedByProfile.flatMap(createdBy ->
optionalLastModifiedByProfile.map(lastModifiedBy ->
Expand Down
Loading

0 comments on commit 39b7387

Please sign in to comment.