-
Notifications
You must be signed in to change notification settings - Fork 131
[PAY-175][PAY-173][PAY-146] Discovery: Reaction Notifications #3078
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 all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
5d7982b
Identity: change column names
piazzatron 264b8ea
Discovery: change column names
piazzatron afa8e6a
Discovery: reaction notifications
piazzatron ab0226b
Discovery: bulk get reactions endpoint
piazzatron d66a89d
Service commands: fix seed audio script
piazzatron 958b3d7
Identity lint fix
piazzatron 1f42df9
Discovery: fix lint
piazzatron b8f31ac
PR revisions
piazzatron 5028921
Fix migration idempotency
piazzatron b81e2aa
Fix migrations
piazzatron 4c36525
Use a local fn
piazzatron 80b9d3b
Fix migration again
piazzatron dc412f7
Last migration fix
piazzatron 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
55 changes: 55 additions & 0 deletions
55
discovery-provider/alembic/versions/0d2067242dd5_change_reaction_column_names.py
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,55 @@ | ||
| """Change reaction column names | ||
|
|
||
| Revision ID: 0d2067242dd5 | ||
| Revises: f11f9e83b28b | ||
| Create Date: 2022-05-09 22:03:16.838837 | ||
|
|
||
| """ | ||
| import inspect | ||
|
|
||
| import sqlalchemy as sa | ||
| from alembic import op | ||
|
|
||
| # revision identifiers, used by Alembic. | ||
| revision = "0d2067242dd5" | ||
| down_revision = "f11f9e83b28b" | ||
| branch_labels = None | ||
| depends_on = None | ||
|
|
||
|
|
||
| def column_exists(table_name, column_name): | ||
| bind = op.get_context().bind | ||
| insp = sa.inspect(bind) | ||
| columns = insp.get_columns(table_name) | ||
| return any(c["name"] == column_name for c in columns) | ||
|
|
||
|
|
||
| def upgrade(): | ||
| # Handle lack of idempotency for this migration | ||
| if column_exists("reactions", "entity_id"): | ||
| op.alter_column("reactions", "entity_id", new_column_name="reacted_to") | ||
| if column_exists("reactions", "entity_type"): | ||
| op.alter_column("reactions", "entity_type", new_column_name="reaction_type") | ||
| if column_exists("reactions", "reaction"): | ||
| op.alter_column("reactions", "reaction", new_column_name="reaction_value") | ||
| op.create_index( | ||
| op.f("ix_reactions_reacted_to_reaction_type"), | ||
| "reactions", | ||
| ["reacted_to", "reaction_type"], | ||
| unique=False, | ||
| info={"if_not_exists": True}, | ||
| ) | ||
|
|
||
|
|
||
| def downgrade(): | ||
| if column_exists("reactions", "reacted_to"): | ||
| op.alter_column("reactions", "reacted_to", new_column_name="entity_id") | ||
| if column_exists("reactions", "reaction_type"): | ||
| op.alter_column("reactions", "reaction_type", new_column_name="entity_type") | ||
| if column_exists("reactions", "reaction_value"): | ||
| op.alter_column("reactions", "reaction_value", new_column_name="reaction") | ||
| op.drop_index( | ||
| op.f("ix_reactions_reacted_to_reaction_type"), | ||
| table_name="reaction", | ||
| info={"if_exists": True}, | ||
| ) |
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,13 @@ | ||
| from flask_restx import fields | ||
|
|
||
| from .common import ns | ||
|
|
||
| reaction = ns.model( | ||
| "reaction", | ||
| { | ||
| "reaction_value": fields.String(required=True), | ||
| "reaction_type": fields.String(required=True), | ||
| "sender_user_id": fields.String(required=True), | ||
| "reacted_to": fields.String(required=True), | ||
| }, | ||
| ) |
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,50 @@ | ||
| from flask_restx import Namespace, Resource, fields, marshal_with, reqparse | ||
| from src.api.v1.helpers import ( | ||
| DescriptiveArgument, | ||
| extend_reaction, | ||
| make_response, | ||
| success_response, | ||
| ) | ||
| from src.api.v1.models.reactions import reaction | ||
| from src.queries.reactions import get_reactions | ||
| from src.utils.db_session import get_db_read_replica | ||
| from src.utils.redis_cache import cache | ||
| from src.utils.redis_metrics import record_metrics | ||
|
|
||
| ns = Namespace("reactions", description="Reaction related operations") | ||
|
|
||
| get_reactions_parser = reqparse.RequestParser(argument_class=DescriptiveArgument) | ||
| get_reactions_parser.add_argument( | ||
| "type", required=False, description="The type of reactions for which to query." | ||
| ) | ||
| get_reactions_parser.add_argument( | ||
| "tx_signatures", | ||
| required=True, | ||
| action="split", | ||
| description="The `reacted_to` transaction id(s) of the reactions in question.", | ||
| ) | ||
|
|
||
| get_reactions_response = make_response( | ||
| "reactions", ns, fields.List(fields.Nested(reaction)) | ||
| ) | ||
|
|
||
|
|
||
| @ns.route("") | ||
| class BulkReactions(Resource): | ||
| @record_metrics | ||
| @ns.doc( | ||
| id="Bulk get Reactions", | ||
| description="Gets reactions by transaction_id and type", | ||
| responses={200: "Success", 400: "Bad request", 500: "Server error"}, | ||
| ) | ||
| @ns.expect(get_reactions_parser) | ||
| @marshal_with(get_reactions_response) | ||
| @cache(ttl_sec=5) | ||
| def get(self): | ||
| args = get_reactions_parser.parse_args() | ||
| tx_ids, type = args.get("tx_signatures"), args.get("type") | ||
| db = get_db_read_replica() | ||
| with db.scoped_session() as session: | ||
| reactions = get_reactions(session, tx_ids, type) | ||
| reactions = list(map(extend_reaction, reactions)) | ||
| return success_response(reactions) |
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,39 @@ | ||
| from typing import List, Optional, Tuple, TypedDict | ||
|
|
||
| from sqlalchemy.orm.session import Session | ||
| from src.models.models import User | ||
| from src.models.reaction import Reaction | ||
|
|
||
|
|
||
| class ReactionResponse(TypedDict): | ||
| reaction_value: int | ||
| reaction_type: str | ||
| reacted_to: str | ||
| sender_user_id: int | ||
|
|
||
|
|
||
| def get_reactions( | ||
| session: Session, transaction_ids: List[str], type: Optional[str] | ||
| ) -> List[ReactionResponse]: | ||
| filters = [Reaction.reacted_to.in_(transaction_ids), User.is_current == True] | ||
| if type: | ||
| filters.append(Reaction.reaction_type == type) | ||
|
|
||
| results: List[Tuple[Reaction, int]] = ( | ||
| session.query(Reaction, User.user_id) | ||
| .join(User, User.wallet == Reaction.sender_wallet) | ||
| .filter( | ||
| *filters, | ||
| ) | ||
| .all() | ||
| ) | ||
|
|
||
| return [ | ||
|
piazzatron marked this conversation as resolved.
|
||
| { | ||
| "reaction_value": r.reaction_value, | ||
| "reaction_type": r.reaction_type, | ||
| "reacted_to": r.reacted_to, | ||
| "sender_user_id": user_id, | ||
| } | ||
| for (r, user_id) in results | ||
| ] | ||
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
15 changes: 15 additions & 0 deletions
15
identity-service/sequelize/migrations/20220509223231-change_reaction_columns.js
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,15 @@ | ||
| 'use strict' | ||
|
|
||
| module.exports = { | ||
| up: (queryInterface, Sequelize) => { | ||
| return queryInterface.renameColumn('Reactions', 'entityId', 'reactedTo') | ||
| .then(() => queryInterface.renameColumn('Reactions', 'entityType', 'reactionType')) | ||
| .then(() => queryInterface.renameColumn('Reactions', 'reaction', 'reactionValue')) | ||
| }, | ||
|
|
||
| down: (queryInterface, Sequelize) => { | ||
| return queryInterface.renameColumn('Reactions', 'reactedTo', 'entityId') | ||
| .then(() => queryInterface.renameColumn('Reactions', 'reactionType', 'entityType')) | ||
| .then(() => queryInterface.renameColumn('Reactions', 'reactionValue', 'reaction')) | ||
| } | ||
| } |
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.
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.