Skip to content

Commit

Permalink
1.4.0-SNAPSHOT: Parallel Nextcloud user fetching
Browse files Browse the repository at this point in the history
  • Loading branch information
derBobby committed Jun 7, 2024
1 parent b42bdc6 commit 8a6d1f6
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 41 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

<groupId>eu.planlos</groupId>
<artifactId>java-nextcloud-connector</artifactId>
<version>1.3.0-SNAPSHOT</version>
<version>1.4.0-SNAPSHOT</version>

<parent>
<groupId>org.springframework.boot</groupId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ public boolean isAPIDisabled() {
log.info("Nextcloud API is not enabled. Returning empty list or null");
return true;
}
log.debug("Nextcloud API is enabled");
return false;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -40,35 +40,8 @@ public NextcloudApiUserService(NextcloudApiConfig config, @Qualifier("NextcloudW
super(config, webClient);
}

public List<String> getAllUserIdsFromNextcloud() {

if(isAPIDisabled()) {
return Collections.emptyList();
}

NextcloudApiResponse<NextcloudUserList> apiResponse = webClient
.get()
.uri(NC_API_USERLIST_JSON_URL)
.retrieve()
.bodyToMono(new ParameterizedTypeReference<NextcloudApiResponse<NextcloudUserList>>() {
})
.retryWhen(Retry
.fixedDelay(config.retryCount(), Duration.ofSeconds(config.retryInterval()))
.filter(WebClientRetryFilter::shouldRetry)
)
.doOnError(error -> log.error("{}: {}", FAIL_MESSAGE_GET_USERS, error.getMessage()))
.block();

if (apiResponse == null) {
throw new NextcloudException(NextcloudException.IS_NULL);
}

NextcloudUserList nextcloudUseridList = apiResponse.getData();
return nextcloudUseridList.getUsers();
}

private NextcloudUser getUser(String username) {

log.debug("Getting user from Nextcloud");
if(isAPIDisabled()) {
return null;
}
Expand All @@ -89,17 +62,6 @@ private NextcloudUser getUser(String username) {
return apiResponse.getData();
}

public Map<String, String> getAllUsersAsUseridEmailMap() {
List<String> useridList = getAllUserIdsFromNextcloud();
return useridList.stream().collect(Collectors.toMap(
userid -> userid,
userid -> {
NextcloudUser user = getUser(userid);
return user == null ? "" : user.email();
}
));
}

/**
* Checks, if email is already in use, generates username from given parameters. tries to create account in Nextcloud
* @param email to use for account
Expand All @@ -109,13 +71,16 @@ public Map<String, String> getAllUsersAsUseridEmailMap() {
*/
public Optional<String> createUser(String email, String firstName, String lastName) {

log.debug("Starting Nextcloud user creation");

if (isAPIDisabled()) {
log.info(SUCCESS_API_INACTIVE);
return Optional.of("<none, API inactive>");
}

Map<String, String> allUsersMap = getAllUsersAsUseridEmailMap();
if(isMailAddressAlreadyInUse(email, allUsersMap)) {
log.info("Mail address already in use");
return Optional.empty();
}

Expand Down Expand Up @@ -151,6 +116,46 @@ public Optional<String> createUser(String email, String firstName, String lastNa
return Optional.of(userid);
}

private Map<String, String> getAllUsersAsUseridEmailMap() {
log.debug("Creating id/email map for all users");

List<String> useridList = fetchAllUserIdsFromNextcloud();
return useridList.parallelStream().collect(Collectors.toConcurrentMap(
userid -> userid,
userid -> {
NextcloudUser user = getUser(userid);
return user == null ? "" : user.email();
}
));
}

private List<String> fetchAllUserIdsFromNextcloud() {
log.debug("Fetching all users from Nextcloud");
if(isAPIDisabled()) {
return Collections.emptyList();
}

NextcloudApiResponse<NextcloudUserList> apiResponse = webClient
.get()
.uri(NC_API_USERLIST_JSON_URL)
.retrieve()
.bodyToMono(new ParameterizedTypeReference<NextcloudApiResponse<NextcloudUserList>>() {
})
.retryWhen(Retry
.fixedDelay(config.retryCount(), Duration.ofSeconds(config.retryInterval()))
.filter(WebClientRetryFilter::shouldRetry)
)
.doOnError(error -> log.error("{}: {}", FAIL_MESSAGE_GET_USERS, error.getMessage()))
.block();

if (apiResponse == null) {
throw new NextcloudException(NextcloudException.IS_NULL);
}

NextcloudUserList nextcloudUseridList = apiResponse.getData();
return nextcloudUseridList.getUsers();
}

/*
* Username generators
*/
Expand All @@ -161,7 +166,6 @@ private String generateUserId(Map<String, String> allUsersMap, String firstNameP
}

private String generateUserId(Map<String, String> allUsersMap, String firstName, String lastName, int charCount) {

// Assert because <= 0 can only happen for coding errors
assert charCount > 0;

Expand Down

0 comments on commit 8a6d1f6

Please sign in to comment.