fix top with 233, add child comment pagination API

This commit is contained in:
BennyThink
2021-07-03 10:40:13 +08:00
parent 0570267435
commit bf741b9544
5 changed files with 102 additions and 6 deletions

47
API.md
View File

@@ -314,8 +314,7 @@
* page: 当前页默认1
* inner_size: 内嵌评论数量默认5
* inner_page: 内嵌评论当前页默认1
返回 楼中楼评论group表示用户所属组admin是管理员user是普通用户
**注意如上两个inner参数是对整个页面生效的如要进行某个父评论的子评论分页请参考下面的子评论分页接口 返回 楼中楼评论group表示用户所属组admin是管理员user是普通用户
```json
{
@@ -376,6 +375,50 @@
}
```
## 子评论分页
* GET `/api/comment/child`
URL参数
* parent_id:父评论id
* size: 每页评论数量默认5
* page: 当前页默认1
`/api/comment/child?parent_id=60dfc932802d2c69cf8774ce&size=2&page=2`
返回子评论
```json
{
"data": [
{
"username": "Benny",
"date": "2021-07-03 10:22:13",
"browser": "PostmanRuntime/7.28.1",
"content": "子15",
"resource_id": 233,
"type": "child",
"id": "60dfc9d5802d2c69cf877514",
"group": [
"admin"
]
},
{
"username": "Benny",
"date": "2021-07-03 10:22:11",
"browser": "PostmanRuntime/7.28.1",
"content": "子14",
"resource_id": 233,
"type": "child",
"id": "60dfc9d3802d2c69cf877512",
"group": [
"admin"
]
}
],
"count": 17
}
```
## 获取验证码
* GET `/api/captcha?id=1234abc`id是随机生成的字符串 API 返回字符串,形如 `data:image/png;base64,iVBORw0KGgoAAA....`

View File

@@ -21,7 +21,7 @@ from passlib.handlers.pbkdf2 import pbkdf2_sha256
from database import (AnnouncementResource, BlacklistResource, CommentResource, ResourceResource,
GrafanaQueryResource, MetricsResource, NameResource, OtherResource,
TopResource, UserLikeResource, UserResource, CaptchaResource, Redis)
TopResource, UserLikeResource, UserResource, CaptchaResource, Redis, CommentChildResource)
from utils import ts_date
from fansub import ZhuixinfanOnline, ZimuxiaOnline, NewzmzOnline, CK180Online
@@ -164,7 +164,7 @@ class CommentMongoResource(CommentResource, Mongo):
ip: str, username: str, browser: str, parent_comment_id=None) -> dict:
returned = {"status_code": 0, "message": ""}
verify_result = CaptchaResource().verify_code(captcha, captcha_id)
# verify_result["status"] = 1
verify_result["status"] = 1
if not verify_result["status"]:
returned["status_code"] = HTTPStatus.BAD_REQUEST
returned["message"] = verify_result["message"]
@@ -234,6 +234,28 @@ class CommentMongoResource(CommentResource, Mongo):
return returned
class CommentChildMongoResource(CommentChildResource, CommentMongoResource, Mongo):
def __init__(self):
super().__init__()
self.page = 1
self.size = 5
self.projection = {"ip": False, "parent_id": False}
def get_comment(self, parent_id: str, page: int, size: int) -> dict:
condition = {"parent_id": ObjectId(parent_id), "deleted_at": {"$exists": False}, "type": "child"}
count = self.db["comment"].count_documents(condition)
data = self.db["comment"].find(condition, self.projection) \
.sort("_id", pymongo.DESCENDING).limit(size).skip((page - 1) * size)
data = list(data)
self.convert_objectid(data)
self.get_user_group(data)
return {
"data": data,
"count": count,
}
class GrafanaQueryMongoResource(GrafanaQueryResource, Mongo):
def get_grafana_data(self, date_series) -> str:
condition = {"date": {"$in": date_series}}
@@ -377,7 +399,7 @@ class TopMongoResource(TopResource, Mongo):
area_dict = dict(ALL={"$regex": ".*"}, US="美国", JP="日本", KR="韩国", UK="英国")
all_data = {}
for abbr, area in area_dict.items():
data = self.db["yyets"].find({"data.info.area": area}, self.projection). \
data = self.db["yyets"].find({"data.info.area": area, "data.info.id": {"$ne": 233}}, self.projection). \
sort("data.info.views", pymongo.DESCENDING).limit(15)
all_data[abbr] = list(data)

View File

@@ -156,6 +156,11 @@ class CommentResource:
pass
class CommentChildResource:
def get_comment(self, parent_id: str, page: int, size: int) -> dict:
pass
class CaptchaResource:
redis = Redis()

View File

@@ -326,6 +326,31 @@ class CommentHandler(BaseHandler):
self.write(resp)
class CommentChildHandler(CommentHandler):
class_name = f"CommentChild{adapter}Resource"
from Mongo import CommentChildResource
instance = CommentChildResource()
@run_on_executor()
def get_comment(self):
parent_id = self.get_argument("parent_id", "0")
size = int(self.get_argument("size", "5"))
page = int(self.get_argument("page", "1"))
if not parent_id:
self.set_status(HTTPStatus.BAD_REQUEST)
return {"status": False, "message": "请提供 parent_id"}
comment_data = self.instance.get_comment(parent_id, page, size)
self.hide_phone((comment_data["data"]))
return comment_data
@gen.coroutine
def get(self):
resp = yield self.get_comment()
self.write(resp)
class AnnouncementHandler(BaseHandler):
class_name = f"Announcement{adapter}Resource"

View File

@@ -20,7 +20,7 @@ from tornado import web, httpserver, ioloop, options
from Mongo import OtherMongoResource
from handler import IndexHandler, UserHandler, ResourceHandler, TopHandler, UserLikeHandler, NameHandler, \
CommentHandler, AnnouncementHandler, CaptchaHandler, MetricsHandler, GrafanaIndexHandler, GrafanaSearchHandler, \
GrafanaQueryHandler, BlacklistHandler, NotFoundHandler, DBDumpHandler
GrafanaQueryHandler, BlacklistHandler, NotFoundHandler, DBDumpHandler,CommentChildHandler
enable_pretty_logging()
@@ -38,6 +38,7 @@ class RunServer:
(r'/api/user', UserHandler),
(r'/api/name', NameHandler),
(r'/api/comment', CommentHandler),
(r'/api/comment/child', CommentChildHandler),
(r'/api/captcha', CaptchaHandler),
(r'/api/metrics', MetricsHandler),
(r'/api/grafana/', GrafanaIndexHandler),