1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556 |
- import html
- from db import db
- from db import Comment, User, Video
- from user import get_authorized_user
- from video import find_video_by_id
- from tools import parse_offset, validate_text
- async def get_comments(video_id, offset=0):
- video = await find_video_by_id(video_id)
- comments = await db.execute(Comment
- .select(Comment, User)
- .join(User)
- .switch(Comment)
- .join(Video)
- .where(
- (Video.id == video.id) &
- (~Comment.is_hidden)
- )
- .order_by(Comment.publish_date.desc())
- .offset(parse_offset(offset))
- .limit(5))
- return [
- {
- 'id': comment.id,
- 'author_id': comment.user.id,
- 'publish_ts': int(comment.publish_date.timestamp()),
- 'text': html.escape(comment.text)
- } for comment in comments
- ]
- async def get_comments_count(video_id):
- video = await find_video_by_id(video_id)
- return await db.count(Comment
- .select()
- .join(Video)
- .where(
- (Video.id == video.id) &
- (~Comment.is_hidden)
- ))
- async def post_comment(session, video_id, text):
- user = await get_authorized_user(session)
- video = await find_video_by_id(video_id)
- text = validate_text(text)
-
- return await db.create(Comment,
- text=text,
- user=user,
- video=video)
|