-
-
Notifications
You must be signed in to change notification settings - Fork 30
Add Player.currentAvatar FK and publish avatar update events #1146
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
Brutus5000
wants to merge
7
commits into
develop
Choose a base branch
from
feat/player-avatar-id-and-rabbit-event
base: develop
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
51e66fc
Add Player.currentAvatar FK and publish avatar update events
Brutus5000 96e58ca
Bump faf-db-migrations to v143 for avatar_id column
Brutus5000 48e6d4b
Deprecate AvatarAssignment.selected in favor of Player.currentAvatar
Brutus5000 68cbf53
Audit Player.currentAvatar changes
Brutus5000 5c356a2
Add push-branch-image.sh for pushing test images
Brutus5000 f8f579b
Validate avatar ownership before assigning currentAvatar
Brutus5000 036e722
Use JUnit Jupiter assertThrows in PlayerAvatarValidationHookTest
Brutus5000 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,49 @@ | ||
| #!/usr/bin/env sh | ||
| # Build the current branch into a Docker image and push it as a tag. | ||
| # Refuses to run on develop/main/master or in a detached HEAD state. | ||
| # | ||
| # Builds for linux/amd64 (x86_64) by default so the image runs on the real | ||
| # servers even when building from an arm64 machine (Apple Silicon). | ||
| # | ||
| # The Dockerfile expects a prebuilt jar in build/libs/, so we run the Gradle | ||
| # bootJar task first (skip with SKIP_BUILD=1 if you already built it). | ||
| # | ||
| # Usage: ./push-branch-image.sh | ||
| # Env: IMAGE override the destination repo (default: faforever/faf-java-api) | ||
| # PLATFORM override the target platform (default: linux/amd64) | ||
| # SKIP_BUILD set to 1 to reuse an existing build/libs jar | ||
| set -eu | ||
|
|
||
| IMAGE="${IMAGE:-faforever/faf-java-api}" | ||
| PLATFORM="${PLATFORM:-linux/amd64}" | ||
|
|
||
| branch=$(git rev-parse --abbrev-ref HEAD) | ||
|
|
||
| case "$branch" in | ||
| develop|main|master|HEAD) | ||
| echo "Refusing to push '$branch' as a Docker tag." >&2 | ||
| exit 1 | ||
| ;; | ||
| esac | ||
|
|
||
| # Docker tags must match [A-Za-z0-9_][A-Za-z0-9_.-]{0,127}. | ||
| # Replace anything else (most commonly '/' from branch prefixes) with '-'. | ||
| tag=$(printf '%s' "$branch" | sed 's/[^A-Za-z0-9_.-]/-/g') | ||
|
|
||
| if [ "${SKIP_BUILD:-0}" != "1" ]; then | ||
| echo "Building jar with ./gradlew bootJar" | ||
| ./gradlew bootJar -x test | ||
| fi | ||
|
|
||
| echo "Building $IMAGE:$tag from branch $branch for $PLATFORM" | ||
| # buildx with --push builds and pushes in one step. --provenance=false keeps | ||
| # the registry tag pointing at a plain image manifest rather than a manifest | ||
| # list, which some deployment tooling expects. | ||
| docker buildx build \ | ||
| --platform "$PLATFORM" \ | ||
| --provenance=false \ | ||
| -t "$IMAGE:$tag" \ | ||
| --push \ | ||
| . | ||
|
|
||
| echo "Done: $IMAGE:$tag" | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
45 changes: 45 additions & 0 deletions
45
src/main/java/com/faforever/api/data/hook/PlayerAvatarUpdateHook.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,45 @@ | ||
| package com.faforever.api.data.hook; | ||
|
|
||
| import com.faforever.api.config.RabbitConfiguration; | ||
| import com.faforever.api.data.domain.Avatar; | ||
| import com.faforever.api.data.domain.Player; | ||
| import com.yahoo.elide.annotation.LifeCycleHookBinding.Operation; | ||
| import com.yahoo.elide.annotation.LifeCycleHookBinding.TransactionPhase; | ||
| import com.yahoo.elide.core.lifecycle.LifeCycleHook; | ||
| import com.yahoo.elide.core.security.ChangeSpec; | ||
| import com.yahoo.elide.core.security.RequestScope; | ||
| import lombok.RequiredArgsConstructor; | ||
| import lombok.extern.slf4j.Slf4j; | ||
| import org.springframework.amqp.rabbit.core.RabbitTemplate; | ||
| import org.springframework.stereotype.Component; | ||
|
|
||
| import java.util.HashMap; | ||
| import java.util.Map; | ||
| import java.util.Optional; | ||
|
|
||
| @Slf4j | ||
| @Component | ||
| @RequiredArgsConstructor | ||
| public class PlayerAvatarUpdateHook implements LifeCycleHook<Player> { | ||
|
|
||
| public static final String ROUTING_KEY_PLAYER_AVATAR_UPDATE = "success.player_avatar.update"; | ||
|
|
||
| private final RabbitTemplate rabbitTemplate; | ||
|
|
||
| @Override | ||
| public void execute(Operation operation, TransactionPhase phase, Player player, RequestScope requestScope, Optional<ChangeSpec> changes) { | ||
| final Avatar avatar = player.getCurrentAvatar(); | ||
| final Integer avatarId = avatar == null ? null : avatar.getId(); | ||
|
|
||
| Map<String, Object> payload = new HashMap<>(); | ||
| payload.put("player_id", player.getId()); | ||
| payload.put("avatar_id", avatarId); | ||
|
|
||
| log.debug("Publishing player_avatar update: {}", payload); | ||
| rabbitTemplate.convertAndSend( | ||
| RabbitConfiguration.EXCHANGE_FAF_LOBBY, | ||
| ROUTING_KEY_PLAYER_AVATAR_UPDATE, | ||
| payload | ||
| ); | ||
| } | ||
| } |
40 changes: 40 additions & 0 deletions
40
src/main/java/com/faforever/api/data/hook/PlayerAvatarValidationHook.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,40 @@ | ||
| package com.faforever.api.data.hook; | ||
|
|
||
| import com.faforever.api.avatar.AvatarAssignmentRepository; | ||
| import com.faforever.api.data.domain.Avatar; | ||
| import com.faforever.api.data.domain.Player; | ||
| import com.faforever.api.error.ApiException; | ||
| import com.faforever.api.error.Error; | ||
| import com.faforever.api.error.ErrorCode; | ||
| import com.yahoo.elide.annotation.LifeCycleHookBinding.Operation; | ||
| import com.yahoo.elide.annotation.LifeCycleHookBinding.TransactionPhase; | ||
| import com.yahoo.elide.core.lifecycle.LifeCycleHook; | ||
| import com.yahoo.elide.core.security.ChangeSpec; | ||
| import com.yahoo.elide.core.security.RequestScope; | ||
| import lombok.RequiredArgsConstructor; | ||
| import org.springframework.stereotype.Component; | ||
|
|
||
| import java.util.Optional; | ||
|
|
||
| /** | ||
| * Rejects assigning a {@code currentAvatar} that is not granted to the player. The | ||
| * {@code IsEntityOwner} update permission only guarantees the caller edits their own Player row; | ||
| * it does not verify the avatar belongs to them. Clearing the avatar (null) is always allowed. | ||
| */ | ||
| @Component | ||
| @RequiredArgsConstructor | ||
| public class PlayerAvatarValidationHook implements LifeCycleHook<Player> { | ||
|
|
||
| private final AvatarAssignmentRepository avatarAssignmentRepository; | ||
|
|
||
| @Override | ||
| public void execute(Operation operation, TransactionPhase phase, Player player, RequestScope requestScope, Optional<ChangeSpec> changes) { | ||
| final Avatar avatar = player.getCurrentAvatar(); | ||
| if (avatar == null) { | ||
| return; | ||
| } | ||
|
|
||
| avatarAssignmentRepository.findOneByAvatarIdAndPlayerId(avatar.getId(), player.getId()) | ||
| .orElseThrow(() -> new ApiException(new Error(ErrorCode.AVATAR_NOT_ASSIGNED, avatar.getId(), player.getId()))); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
77 changes: 77 additions & 0 deletions
77
src/test/java/com/faforever/api/data/hook/PlayerAvatarValidationHookTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,77 @@ | ||
| package com.faforever.api.data.hook; | ||
|
|
||
| import com.faforever.api.avatar.AvatarAssignmentRepository; | ||
| import com.faforever.api.data.domain.Avatar; | ||
| import com.faforever.api.data.domain.AvatarAssignment; | ||
| import com.faforever.api.data.domain.Player; | ||
| import com.faforever.api.error.ApiException; | ||
| import com.faforever.api.error.ErrorCode; | ||
| import com.yahoo.elide.annotation.LifeCycleHookBinding.Operation; | ||
| import com.yahoo.elide.annotation.LifeCycleHookBinding.TransactionPhase; | ||
| import org.junit.jupiter.api.BeforeEach; | ||
| import org.junit.jupiter.api.Test; | ||
| import org.junit.jupiter.api.extension.ExtendWith; | ||
| import org.mockito.Mock; | ||
| import org.mockito.junit.jupiter.MockitoExtension; | ||
|
|
||
| import java.util.Optional; | ||
|
|
||
| import static com.faforever.api.error.ApiExceptionMatcher.hasErrorCode; | ||
| import static org.hamcrest.MatcherAssert.assertThat; | ||
| import static org.junit.jupiter.api.Assertions.assertThrows; | ||
| import static org.mockito.Mockito.verifyNoInteractions; | ||
| import static org.mockito.Mockito.when; | ||
|
|
||
| @ExtendWith(MockitoExtension.class) | ||
| class PlayerAvatarValidationHookTest { | ||
|
|
||
| private static final int PLAYER_ID = 7; | ||
| private static final int AVATAR_ID = 42; | ||
|
|
||
| @Mock | ||
| private AvatarAssignmentRepository avatarAssignmentRepository; | ||
|
|
||
| private PlayerAvatarValidationHook instance; | ||
|
|
||
| @BeforeEach | ||
| void setUp() { | ||
| instance = new PlayerAvatarValidationHook(avatarAssignmentRepository); | ||
| } | ||
|
|
||
| private static Player playerWithAvatar(Integer avatarId) { | ||
| Avatar avatar = new Avatar(); | ||
| avatar.setId(avatarId); | ||
| Player player = new Player(); | ||
| player.setId(PLAYER_ID); | ||
| player.setCurrentAvatar(avatar); | ||
| return player; | ||
| } | ||
|
|
||
| @Test | ||
| void allowsAssignedAvatar() { | ||
| when(avatarAssignmentRepository.findOneByAvatarIdAndPlayerId(AVATAR_ID, PLAYER_ID)) | ||
| .thenReturn(Optional.of(new AvatarAssignment())); | ||
|
|
||
| instance.execute(Operation.UPDATE, TransactionPhase.PRECOMMIT, playerWithAvatar(AVATAR_ID), null, Optional.empty()); | ||
| } | ||
|
|
||
| @Test | ||
| void rejectsUnassignedAvatar() { | ||
| when(avatarAssignmentRepository.findOneByAvatarIdAndPlayerId(AVATAR_ID, PLAYER_ID)) | ||
| .thenReturn(Optional.empty()); | ||
|
|
||
| ApiException result = assertThrows(ApiException.class, () -> | ||
| instance.execute(Operation.UPDATE, TransactionPhase.PRECOMMIT, playerWithAvatar(AVATAR_ID), null, Optional.empty())); | ||
| assertThat(result, hasErrorCode(ErrorCode.AVATAR_NOT_ASSIGNED)); | ||
| } | ||
|
|
||
| @Test | ||
| void allowsClearingAvatar() { | ||
| Player player = new Player(); | ||
| player.setId(PLAYER_ID); | ||
|
|
||
| instance.execute(Operation.UPDATE, TransactionPhase.PRECOMMIT, player, null, Optional.empty()); | ||
|
|
||
| verifyNoInteractions(avatarAssignmentRepository); | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ensure sanitized tag is always Docker-valid before invoking
buildx.Current sanitization replaces invalid chars, but it can still yield an invalid first character (
./-) or exceed 128 chars, causing avoidable late failures indocker buildx.Suggested patch
📝 Committable suggestion
🤖 Prompt for AI Agents