Skip to content

Commit a1090d1

Browse files
committed
API & Client - display comments and workouts likes
1 parent fe7422f commit a1090d1

File tree

26 files changed

+1204
-60
lines changed

26 files changed

+1204
-60
lines changed

fittrackee/comments/comments.py

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@
2424

2525
comments_blueprint = Blueprint('comments', __name__)
2626

27+
DEFAULT_COMMENT_LIKES_PER_PAGE = 10
28+
2729

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

615617

618+
@comments_blueprint.route(
619+
"/comments/<string:comment_short_id>/likes", methods=["GET"]
620+
)
621+
@require_auth(scopes=["workouts:read"], optional_auth_user=True)
622+
@check_workout_comment(only_owner=False)
623+
def get_comment_likes(
624+
auth_user: User, comment: Comment
625+
) -> Union[Dict, HttpResponse]:
626+
"""
627+
Get users who like comment.
628+
629+
**Scope**: ``workouts:read``
630+
631+
**Example request**:
632+
633+
.. sourcecode:: http
634+
635+
POST /api/comments/WJgTwtqFpnPrHYAK5eX9Pw/likes HTTP/1.1
636+
Content-Type: application/json
637+
638+
**Example response**:
639+
640+
.. sourcecode:: http
641+
642+
HTTP/1.1 200 OK
643+
Content-Type: application/json
644+
645+
{
646+
"data": {
647+
"likes": [
648+
{
649+
"created_at": "Sun, 31 Dec 2017 09:00:00 GMT",
650+
"followers": 0,
651+
"following": 0,
652+
"nb_workouts": 1,
653+
"picture": false,
654+
"role": "user",
655+
"suspended_at": null,
656+
"username": "Sam"
657+
}
658+
]
659+
},
660+
"status": "success"
661+
}
662+
663+
:param string comment_short_id: comment short id
664+
665+
:query integer page: page if using pagination (default: 1)
666+
667+
:reqheader Authorization: OAuth 2.0 Bearer Token for comment with
668+
``private`` and ``followers_only`` visibility
669+
670+
:statuscode 200: ``success``
671+
:statuscode 401:
672+
- ``provide a valid auth token``
673+
- ``signature expired, please log in again``
674+
- ``invalid token, please log in again``
675+
:statuscode 403:
676+
- ``you do not have permissions``
677+
- ``you do not have permissions, your account is suspended``
678+
:statuscode 404: ``comment not found``
679+
"""
680+
params = request.args.copy()
681+
page = int(params.get('page', 1))
682+
likes_pagination = (
683+
User.query.join(CommentLike, User.id == CommentLike.user_id)
684+
.filter(CommentLike.comment_id == comment.id)
685+
.order_by(CommentLike.created_at.desc())
686+
.paginate(
687+
page=page, per_page=DEFAULT_COMMENT_LIKES_PER_PAGE, error_out=False
688+
)
689+
)
690+
users = likes_pagination.items
691+
return {
692+
'status': 'success',
693+
'data': {
694+
'likes': [user.serialize(current_user=auth_user) for user in users]
695+
},
696+
'pagination': {
697+
'has_next': likes_pagination.has_next,
698+
'has_prev': likes_pagination.has_prev,
699+
'page': likes_pagination.page,
700+
'pages': likes_pagination.pages,
701+
'total': likes_pagination.total,
702+
},
703+
}
704+
705+
616706
@comments_blueprint.route(
617707
"/comments/<string:comment_short_id>/suspension/appeal",
618708
methods=["POST"],

fittrackee/comments/models.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -460,6 +460,7 @@ class CommentLike(BaseModel):
460460
db.Integer,
461461
db.ForeignKey('comments.id', ondelete="CASCADE"),
462462
nullable=False,
463+
index=True,
463464
)
464465

465466
user = db.relationship("User", lazy=True)
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
"""add index on likes tables
2+
3+
Revision ID: d3c5338bedff
4+
Revises: d3e40c2bda80
5+
Create Date: 2025-01-04 17:17:55.637816
6+
7+
"""
8+
from alembic import op
9+
import sqlalchemy as sa
10+
11+
12+
# revision identifiers, used by Alembic.
13+
revision = 'd3c5338bedff'
14+
down_revision = 'd3e40c2bda80'
15+
branch_labels = None
16+
depends_on = None
17+
18+
19+
def upgrade():
20+
# ### commands auto generated by Alembic - please adjust! ###
21+
with op.batch_alter_table('comment_likes', schema=None) as batch_op:
22+
batch_op.create_index(batch_op.f('ix_comment_likes_comment_id'), ['comment_id'], unique=False)
23+
24+
with op.batch_alter_table('workout_likes', schema=None) as batch_op:
25+
batch_op.create_index(batch_op.f('ix_workout_likes_workout_id'), ['workout_id'], unique=False)
26+
27+
# ### end Alembic commands ###
28+
29+
30+
def downgrade():
31+
# ### commands auto generated by Alembic - please adjust! ###
32+
with op.batch_alter_table('workout_likes', schema=None) as batch_op:
33+
batch_op.drop_index(batch_op.f('ix_workout_likes_workout_id'))
34+
35+
with op.batch_alter_table('comment_likes', schema=None) as batch_op:
36+
batch_op.drop_index(batch_op.f('ix_comment_likes_comment_id'))
37+
38+
# ### end Alembic commands ###

0 commit comments

Comments
 (0)