-
-
Notifications
You must be signed in to change notification settings - Fork 79
Support API-driven avatar changes: login.avatar_id sync + refresh consumer #1095
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
Merged
Merged
Changes from 4 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
7603cb3
Read & write login.avatar_id alongside legacy avatars.selected
Brutus5000 c506bea
Bump FAF_DB_VERSION to v143 for login.avatar_id column
Brutus5000 ae53aba
Enforce avatar ownership via grant table; cover login.avatar_id in tests
Brutus5000 99eda4a
Add AvatarChangeQueueService consumer for API-driven avatar updates
Brutus5000 d151dd6
Fix lint/type checks and address review comments
Brutus5000 6ba21e8
Address Codacy pydocstyle findings
Brutus5000 fe3c002
Log avatar changes from both write paths
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -12,7 +12,7 @@ on: | |
| - cron: '0 0 * * *' | ||
|
|
||
| env: | ||
| FAF_DB_VERSION: v138 | ||
| FAF_DB_VERSION: v143 | ||
| FLYWAY_VERSION: 7.5.4 | ||
|
|
||
| jobs: | ||
|
|
||
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,109 @@ | ||
| """ | ||
| Consume "player avatar changed" events from trusted microservices. | ||
|
|
||
| # Wire contract | ||
| Publishers post to the `MQ_EXCHANGE_NAME` topic exchange with routing key | ||
| `success.player_avatar.update`. The body is a UTF-8 JSON object: | ||
|
|
||
| - `player_id` (int, required): the player whose selected avatar changed. | ||
| - `avatar_id` (int or null, optional): the newly selected avatar id, or | ||
| null if the player cleared their avatar. The lobby itself ignores | ||
| this field — it always re-reads the DB so it gets the matching | ||
| `url`/`tooltip` and applies the ownership check. The field is | ||
| shipped for the benefit of *other* subscribers that may want to act | ||
| on the change without an extra DB roundtrip. | ||
|
|
||
| On receipt the lobby re-reads the affected player's avatar from the DB | ||
| and marks them dirty so the existing `BroadcastService` emits a | ||
| `player_info` to every connected client on its next tick. | ||
| """ | ||
|
|
||
| import json | ||
| import logging | ||
| import socket | ||
| from typing import ClassVar, Optional | ||
|
|
||
| from aio_pika.abc import AbstractIncomingMessage, AbstractQueue | ||
|
|
||
| from .config import config | ||
| from .core import Service | ||
| from .decorators import with_logger | ||
| from .message_queue_service import MessageQueueService | ||
| from .player_service import PlayerService | ||
|
|
||
|
|
||
| PLAYER_AVATAR_UPDATE_ROUTING_KEY = "success.player_avatar.update" | ||
|
|
||
|
|
||
| @with_logger | ||
| class AvatarChangeQueueService(Service): | ||
| """Consume `success.player_avatar.update` messages and refresh players.""" | ||
|
|
||
| _logger: ClassVar[logging.Logger] | ||
|
|
||
| def __init__( | ||
| self, | ||
| message_queue_service: MessageQueueService, | ||
| player_service: PlayerService, | ||
| ): | ||
| self.message_queue_service = message_queue_service | ||
| self.player_service = player_service | ||
| self._queue: Optional[AbstractQueue] = None | ||
| self._consumer_tag: Optional[str] = None | ||
|
|
||
| async def initialize(self) -> None: | ||
| # Per-instance queue: every lobby pod must process every event so | ||
| # whichever pod is hosting the player can refresh its in-memory | ||
| # state. Naming follows `<exchange>.<service>.<routing-key>.<host>`, | ||
| # matching `ClientMessageQueueService`. | ||
| queue_name = ( | ||
| f"{config.MQ_EXCHANGE_NAME}.lobby.player_avatar.update" | ||
| f".{socket.gethostname()}" | ||
| ) | ||
| result = await self.message_queue_service.declare_queue_and_consume( | ||
| exchange_name=config.MQ_EXCHANGE_NAME, | ||
| routing_key=PLAYER_AVATAR_UPDATE_ROUTING_KEY, | ||
| callback=self._on_message, | ||
| queue_name=queue_name, | ||
| ) | ||
| if result is not None: | ||
| self._queue, self._consumer_tag = result | ||
|
|
||
| async def shutdown(self) -> None: | ||
| if self._queue is not None and self._consumer_tag is not None: | ||
| await self._queue.cancel(self._consumer_tag) | ||
| self._queue = None | ||
| self._consumer_tag = None | ||
|
|
||
| async def _on_message(self, message: AbstractIncomingMessage) -> None: | ||
| async with message.process(requeue=False): | ||
| try: | ||
| payload = json.loads(message.body) | ||
| except (ValueError, UnicodeDecodeError): | ||
| self._logger.warning( | ||
| "Dropping avatar-update message with non-JSON body" | ||
| ) | ||
| return | ||
|
|
||
| if not isinstance(payload, dict): | ||
| self._logger.warning( | ||
| "Dropping avatar-update message: payload is not a JSON object" | ||
| ) | ||
| return | ||
|
|
||
| raw_player_id = payload.get("player_id") | ||
| try: | ||
| player_id = int(raw_player_id) | ||
| except (TypeError, ValueError): | ||
| self._logger.warning( | ||
| "Dropping avatar-update message: invalid player_id %r", | ||
| raw_player_id, | ||
| ) | ||
|
coderabbitai[bot] marked this conversation as resolved.
Outdated
|
||
| return | ||
|
|
||
| refreshed = await self.player_service.refresh_player_avatar(player_id) | ||
| if not refreshed: | ||
| self._logger.debug( | ||
| "avatar-update for player %s ignored: not connected here", | ||
| player_id, | ||
| ) | ||
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
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.