Skip to content

Commit

Permalink
Adjust fetching nodebb user to plugin adjustments for Nodebb v3 api
Browse files Browse the repository at this point in the history
  • Loading branch information
Brutus5000 committed Nov 6, 2024
1 parent 5959c77 commit d6a81b6
Showing 1 changed file with 9 additions and 4 deletions.
13 changes: 9 additions & 4 deletions src/main/java/com/faforever/api/forum/NodebbService.java
Original file line number Diff line number Diff line change
Expand Up @@ -55,12 +55,13 @@ public void userDataChanged(UserUpdatedEvent event) {
private Optional<Integer> getNodebbUserId(int userId) {
URI uri = UriComponentsBuilder.fromHttpUrl(properties.getNodebb().getBaseUrl())
// This is not an official NodeBB api url, it's coming from our own sso plugin
.pathSegment("api", "user", "oauth", String.valueOf(userId))
.pathSegment("api", "v3", "plugins", "sso", "user", String.valueOf(userId))
.queryParam("_uid", getAdminUserId())
.build()
.toUri();

try {
ResponseEntity<UserResponse> result = restTemplate.exchange(uri, HttpMethod.GET, null, UserResponse.class);
ResponseEntity<UserResponse> result = restTemplate.exchange(uri, HttpMethod.GET, buildAuthorizedRequest(null), UserResponse.class);
return Optional.ofNullable(result.getBody()).map(UserResponse::uid);
} catch (HttpClientErrorException e) {
if (e.getStatusCode() == HttpStatus.NOT_FOUND) {
Expand All @@ -78,7 +79,7 @@ private void updateUsernameData(int nodebbUserId, UserUpdatedEvent event) {
.build()
.toUri();

var usernameUpdate = new UsernameUpdate(String.valueOf(properties.getNodebb().getAdminUserId()), event.getUsername());
var usernameUpdate = new UsernameUpdate(getAdminUserId(), event.getUsername());
restTemplate.exchange(uri, HttpMethod.PUT, buildAuthorizedRequest(usernameUpdate), Void.class);
log.debug("Username updated in NodeBB: {}", event);
}
Expand All @@ -89,11 +90,15 @@ private void updateEmailData(int nodebbUserId, UserUpdatedEvent event) {
.build()
.toUri();

var usernameUpdate = new EmailUpdate(String.valueOf(properties.getNodebb().getAdminUserId()), event.getEmail(), 1);
var usernameUpdate = new EmailUpdate(getAdminUserId(), event.getEmail(), 1);
restTemplate.exchange(uri, HttpMethod.PUT, buildAuthorizedRequest(usernameUpdate), Void.class);
log.debug("Email updated in NodeBB: {}", event);
}

private String getAdminUserId() {
return String.valueOf(properties.getNodebb().getAdminUserId());
}

private <T> HttpEntity<T> buildAuthorizedRequest(T payload) {
LinkedMultiValueMap<String, String> headers = new LinkedMultiValueMap<>();
headers.add(HttpHeaders.AUTHORIZATION, "Bearer " + properties.getNodebb().getMasterToken());
Expand Down

0 comments on commit d6a81b6

Please sign in to comment.