Skip to content

Commit

Permalink
API & Client - display comments and workouts likes
Browse files Browse the repository at this point in the history
  • Loading branch information
SamR1 committed Jan 4, 2025
1 parent fe7422f commit a1090d1
Show file tree
Hide file tree
Showing 26 changed files with 1,204 additions and 60 deletions.
90 changes: 90 additions & 0 deletions fittrackee/comments/comments.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@

comments_blueprint = Blueprint('comments', __name__)

DEFAULT_COMMENT_LIKES_PER_PAGE = 10


@comments_blueprint.route(
"/workouts/<string:workout_short_id>/comments", methods=["POST"]
Expand Down Expand Up @@ -613,6 +615,94 @@ def undo_comment_like(
}, 200


@comments_blueprint.route(
"/comments/<string:comment_short_id>/likes", methods=["GET"]
)
@require_auth(scopes=["workouts:read"], optional_auth_user=True)
@check_workout_comment(only_owner=False)
def get_comment_likes(
auth_user: User, comment: Comment
) -> Union[Dict, HttpResponse]:
"""
Get users who like comment.
**Scope**: ``workouts:read``
**Example request**:
.. sourcecode:: http
POST /api/comments/WJgTwtqFpnPrHYAK5eX9Pw/likes HTTP/1.1
Content-Type: application/json
**Example response**:
.. sourcecode:: http
HTTP/1.1 200 OK
Content-Type: application/json
{
"data": {
"likes": [
{
"created_at": "Sun, 31 Dec 2017 09:00:00 GMT",
"followers": 0,
"following": 0,
"nb_workouts": 1,
"picture": false,
"role": "user",
"suspended_at": null,
"username": "Sam"
}
]
},
"status": "success"
}
:param string comment_short_id: comment short id
:query integer page: page if using pagination (default: 1)
:reqheader Authorization: OAuth 2.0 Bearer Token for comment with
``private`` and ``followers_only`` visibility
:statuscode 200: ``success``
:statuscode 401:
- ``provide a valid auth token``
- ``signature expired, please log in again``
- ``invalid token, please log in again``
:statuscode 403:
- ``you do not have permissions``
- ``you do not have permissions, your account is suspended``
:statuscode 404: ``comment not found``
"""
params = request.args.copy()
page = int(params.get('page', 1))
likes_pagination = (
User.query.join(CommentLike, User.id == CommentLike.user_id)
.filter(CommentLike.comment_id == comment.id)
.order_by(CommentLike.created_at.desc())
.paginate(
page=page, per_page=DEFAULT_COMMENT_LIKES_PER_PAGE, error_out=False
)
)
users = likes_pagination.items
return {
'status': 'success',
'data': {
'likes': [user.serialize(current_user=auth_user) for user in users]
},
'pagination': {
'has_next': likes_pagination.has_next,
'has_prev': likes_pagination.has_prev,
'page': likes_pagination.page,
'pages': likes_pagination.pages,
'total': likes_pagination.total,
},
}


@comments_blueprint.route(
"/comments/<string:comment_short_id>/suspension/appeal",
methods=["POST"],
Expand Down
1 change: 1 addition & 0 deletions fittrackee/comments/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -460,6 +460,7 @@ class CommentLike(BaseModel):
db.Integer,
db.ForeignKey('comments.id', ondelete="CASCADE"),
nullable=False,
index=True,
)

user = db.relationship("User", lazy=True)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
"""add index on likes tables
Revision ID: d3c5338bedff
Revises: d3e40c2bda80
Create Date: 2025-01-04 17:17:55.637816
"""
from alembic import op
import sqlalchemy as sa


# revision identifiers, used by Alembic.
revision = 'd3c5338bedff'
down_revision = 'd3e40c2bda80'
branch_labels = None
depends_on = None


def upgrade():
# ### commands auto generated by Alembic - please adjust! ###
with op.batch_alter_table('comment_likes', schema=None) as batch_op:
batch_op.create_index(batch_op.f('ix_comment_likes_comment_id'), ['comment_id'], unique=False)

with op.batch_alter_table('workout_likes', schema=None) as batch_op:
batch_op.create_index(batch_op.f('ix_workout_likes_workout_id'), ['workout_id'], unique=False)

# ### end Alembic commands ###


def downgrade():
# ### commands auto generated by Alembic - please adjust! ###
with op.batch_alter_table('workout_likes', schema=None) as batch_op:
batch_op.drop_index(batch_op.f('ix_workout_likes_workout_id'))

with op.batch_alter_table('comment_likes', schema=None) as batch_op:
batch_op.drop_index(batch_op.f('ix_comment_likes_comment_id'))

# ### end Alembic commands ###
Loading

0 comments on commit a1090d1

Please sign in to comment.