Skip to content
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

Add a user collection #1241

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 34 additions & 0 deletions backend/alembic/versions/a7266940f895_add_user_collection.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
"""Add user collection

Revision ID: a7266940f895
Revises: fb0b29dfa4c3
Create Date: 2023-12-23 22:05:09.903670

"""
from alembic import op
import sqlalchemy as sa


# revision identifiers, used by Alembic.
revision = 'a7266940f895'
down_revision = 'fb0b29dfa4c3'
branch_labels = None
depends_on = None


def upgrade():
# ### commands auto generated by Alembic - please adjust! ###
op.create_table('user_collected_app',
sa.Column('app_id', sa.String(), nullable=False),
sa.Column('account', sa.Integer(), nullable=False),
sa.Column('created', sa.DateTime(), nullable=False),
sa.ForeignKeyConstraint(['account'], ['flathubuser.id'], ondelete='CASCADE'),
sa.PrimaryKeyConstraint('app_id', 'account')
)
# ### end Alembic commands ###


def downgrade():
# ### commands auto generated by Alembic - please adjust! ###
op.drop_table('user_collected_app')
# ### end Alembic commands ###
46 changes: 46 additions & 0 deletions backend/app/logins.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
from collections.abc import Callable
from dataclasses import dataclass
from datetime import datetime, timedelta
from http import HTTPStatus
from urllib.parse import urlencode
from uuid import uuid4

Expand Down Expand Up @@ -846,6 +847,44 @@ def continue_oauth_flow(
}


@router.post("/add-to-collection", tags=["auth"])
def add_to_collection(
app_id: str,
login=Depends(logged_in),
):
"""
Add an app to a users collection. The appid is the ID of the app to add.
"""

try:
models.UserCollectedApp.add_app(db, login["user"].id, app_id)
db.session.commit()

return Response(status_code=HTTPStatus.OK)
except Exception:
db.session.rollback()
return Response(status_code=HTTPStatus.INTERNAL_SERVER_ERROR)


@router.post("/remove-from-collection", tags=["auth"])
def remove_from_collection(
app_id: str,
login=Depends(logged_in),
):
"""
Remove an app from a users collection. The appid is the ID of the app to remove.
"""

try:
models.UserCollectedApp.remove_app(db, login["user"].id, app_id)
db.session.commit()

return Response(status_code=HTTPStatus.OK)
except Exception:
db.session.rollback()
return Response(status_code=HTTPStatus.INTERNAL_SERVER_ERROR)


@router.get("/userinfo", tags=["auth"])
def get_userinfo(login: LoginStatusDep):
"""
Expand Down Expand Up @@ -889,6 +928,7 @@ def get_userinfo(login: LoginStatusDep):
"displayname": default_account.display_name,
"dev-flatpaks": set(),
"owned-flatpaks": set(),
"collected-flatpaks": set(),
"invited-flatpaks": set(),
"invite-code": user.invite_code,
"accepted-publisher-agreement-at": user.accepted_publisher_agreement_at,
Expand All @@ -903,13 +943,19 @@ def get_userinfo(login: LoginStatusDep):
for app in models.UserOwnedApp.all_owned_by_user(db, user)
if app.app_id in appstream
}
collected_flatpaks = {
app.app_id
for app in models.UserCollectedApp.all_collected_by_user(db, user)
if app.app_id in appstream
}
invited_flatpaks = [
app.app_id
for _invite, app in models.DirectUploadAppInvite.by_developer(db, user)
]

ret["dev-flatpaks"] = sorted(ret["dev-flatpaks"].union(dev_flatpaks))
ret["owned-flatpaks"] = sorted(owned_flatpaks)
ret["collected-flatpaks"] = sorted(collected_flatpaks)
ret["invited-flatpaks"] = sorted(invited_flatpaks)

for account in user.connected_accounts(db):
Expand Down
52 changes: 52 additions & 0 deletions backend/app/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -1094,6 +1094,58 @@ def delete_user(db, user: FlathubUser):
FlathubUser.TABLES_FOR_DELETE.append(UserOwnedApp)


class UserCollectedApp(Base):
__tablename__ = "user_collected_app"

app_id = mapped_column(String, nullable=False, primary_key=True)
account = mapped_column(
Integer,
ForeignKey(FlathubUser.id, ondelete="CASCADE"),
nullable=False,
primary_key=True,
)
created = mapped_column(DateTime, nullable=False)

@staticmethod
def add_app(db, user_id: int, app_id: str):
app = UserCollectedApp(app_id=app_id, account=user_id, created=datetime.now())
db.session.add(app)
db.session.flush()

@staticmethod
def remove_app(db, user_id: int, app_id: str):
db.session.execute(
delete(UserCollectedApp)
.where(UserCollectedApp.account == user_id)
.where(UserCollectedApp.app_id == app_id)
)

@staticmethod
def user_collected_app(db, user_id: int, app_id: str):
return (
db.session.query(UserCollectedApp)
.filter_by(account=user_id, app_id=app_id)
.first()
is not None
)

@staticmethod
def all_collected_by_user(db, user: FlathubUser):
return db.session.query(UserCollectedApp).filter_by(account=user.id)

@staticmethod
def delete_user(db, user: FlathubUser):
"""
Delete any app ownerships associated with this user
"""
db.session.execute(
delete(UserCollectedApp).where(UserCollectedApp.account == user.id)
)


FlathubUser.TABLES_FOR_DELETE.append(UserCollectedApp)


# Vending related tables, including Stripe-only stuff


Expand Down
Loading