mirror of
https://github.com/tgbot-collection/YYeTsBot.git
synced 2025-11-25 03:15:05 +08:00
like and dislike
This commit is contained in:
44
API.md
44
API.md
@@ -224,17 +224,22 @@
|
||||
```json
|
||||
{
|
||||
"username": "Benny",
|
||||
"date": "2021-03-12 11:11:11",
|
||||
"lastDate": "2021-03-15 13:11:18",
|
||||
"ip": "1.1.1.1",
|
||||
"lastIP": "2.2.2.2",
|
||||
"browser": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.85 Safari/537.36",
|
||||
"date": "2021-06-12 13:55:50",
|
||||
"ip": "172.70.122.84",
|
||||
"browser": "Mozilla/5.0 (X11; Gentoo; rv:84.0) Gecko/20100101 Firefox/84.0",
|
||||
"like": [
|
||||
11133
|
||||
31346,
|
||||
39894,
|
||||
41382
|
||||
],
|
||||
"group": [
|
||||
"admin",
|
||||
"user"
|
||||
"admin"
|
||||
],
|
||||
"comments_like": [
|
||||
"60c46d6a6d7c5dd22d69fd3b"
|
||||
],
|
||||
"comments_dislike": [
|
||||
"60c46d6a6d7c5dd22d69fd3b"
|
||||
]
|
||||
}
|
||||
```
|
||||
@@ -542,6 +547,29 @@
|
||||
}
|
||||
```
|
||||
|
||||
## 点赞或踩评论
|
||||
|
||||
* PATCH `/api/comment`
|
||||
|
||||
verb 为`like` 或 `dislike`
|
||||
|
||||
```json
|
||||
{
|
||||
"comment_id": "60c46d6a6d7c5dd22d69fd3b",
|
||||
"verb": "dislike/like"
|
||||
}
|
||||
|
||||
```
|
||||
|
||||
返回:
|
||||
|
||||
* 201 成功
|
||||
* 404 评论没找到
|
||||
* 422 已经赞/踩过了
|
||||
* 400 请求参数错误
|
||||
|
||||
用户曾经点赞的记录会在 `GET /api/user` 返回
|
||||
|
||||
# metrics
|
||||
|
||||
## 添加metrics
|
||||
|
||||
@@ -252,6 +252,30 @@ class CommentMongoResource(CommentResource, Mongo):
|
||||
|
||||
return returned
|
||||
|
||||
def react_comment(self, username, comment_id, verb):
|
||||
if verb not in ("like", "dislike"):
|
||||
return {"status": False,
|
||||
"message": "verb could only be like or dislike",
|
||||
"status_code": HTTPStatus.BAD_REQUEST}
|
||||
|
||||
result = self.db["users"].find_one({"username": username, f"comments_{verb}": {"$in": [comment_id]}})
|
||||
if result:
|
||||
return {"status": False, "message": "too many reactions", "status_code": HTTPStatus.UNPROCESSABLE_ENTITY}
|
||||
|
||||
if not self.db["comment"].find_one({"_id": ObjectId(comment_id)}):
|
||||
return {"status": False, "message": "Where is your comments?", "status_code": HTTPStatus.NOT_FOUND}
|
||||
|
||||
self.db["users"].update_one({"username": username},
|
||||
{"$push": {f"comments_{verb}": comment_id}}
|
||||
)
|
||||
|
||||
self.db["comment"].update_one({"_id": ObjectId(comment_id)},
|
||||
{"$inc": {verb: 1}}
|
||||
)
|
||||
|
||||
return {"status": True, "message": "success",
|
||||
"status_code": HTTPStatus.CREATED}
|
||||
|
||||
|
||||
class CommentChildMongoResource(CommentChildResource, CommentMongoResource, Mongo):
|
||||
def __init__(self):
|
||||
|
||||
@@ -157,6 +157,9 @@ class CommentResource:
|
||||
def delete_comment(self, comment_id: str):
|
||||
pass
|
||||
|
||||
def react_comment(self, username, comment_id, verb):
|
||||
pass
|
||||
|
||||
|
||||
class CommentChildResource:
|
||||
def get_comment(self, parent_id: str, page: int, size: int) -> dict:
|
||||
|
||||
@@ -313,6 +313,16 @@ class CommentHandler(BaseHandler):
|
||||
self.set_status(HTTPStatus.UNAUTHORIZED)
|
||||
return {"count": 0, "message": "You're unauthorized to delete comment."}
|
||||
|
||||
@run_on_executor()
|
||||
def comment_reaction(self):
|
||||
payload = json.loads(self.request.body)
|
||||
username = self.get_current_user()
|
||||
comment_id = payload["comment_id"]
|
||||
verb = payload["verb"]
|
||||
result = self.instance.react_comment(username, comment_id, verb)
|
||||
self.set_status(result.get("status_code") or HTTPStatus.IM_A_TEAPOT)
|
||||
return result
|
||||
|
||||
@gen.coroutine
|
||||
def get(self):
|
||||
resp = yield self.get_comment()
|
||||
@@ -330,6 +340,12 @@ class CommentHandler(BaseHandler):
|
||||
resp = yield self.delete_comment()
|
||||
self.write(resp)
|
||||
|
||||
@gen.coroutine
|
||||
@web.authenticated
|
||||
def patch(self):
|
||||
resp = yield self.comment_reaction()
|
||||
self.write(resp)
|
||||
|
||||
|
||||
class CommentChildHandler(CommentHandler):
|
||||
class_name = f"CommentChild{adapter}Resource"
|
||||
|
||||
Reference in New Issue
Block a user