diff --git a/API.md b/API.md index a337736..68880ec 100644 --- a/API.md +++ b/API.md @@ -6,7 +6,7 @@ - [x] 评论楼中楼 - [x] 联合搜索,当本地数据库搜索不到数据时,会返回extra字段 - [ ] 评论通知(浏览器通知) -- [ ] 最新评论(id为-1) +- [ ] 最新评论 - [ ] 公告 # BE @@ -490,6 +490,58 @@ } ``` +## 最新评论 + +* GET `api/comment/newest` + page size参数同上 + +```json +{ + "data": [ + { + "username": "111", + "date": "2021-07-11 10:22:59", + "browser": "Mozi0.31.0", + "content": "1111?", + "resource_id": 233, + "type": "parent", + "id": "60ea53113178773", + "group": [ + "user" + ], + "cnname": "留言板" + }, + { + "username": "11111222", + "date": "2021-07-10 23:54:43", + "browser": "Mozi3322.64", + "content": "@abcd怎么下载啊\n", + "resource_id": 37552, + "type": "child", + "id": "60e9c2c222111397e", + "group": [ + "user" + ], + "cnname": "黑寡妇" + }, + { + "username": "1111", + "date": "2021-07-10 23:41:06", + "browser": "Moz) Chrom.864.67", + "content": "我是1精彩", + "resource_id": 41382, + "type": "parent", + "id": "60e9bf924ad7f2077381111", + "group": [ + "user" + ], + "cnname": "洛基" + } + ], + "count": 294 +} +``` + # metrics ## 添加metrics diff --git a/yyetsweb/Mongo.py b/yyetsweb/Mongo.py index 6cd7492..ceb6d18 100644 --- a/yyetsweb/Mongo.py +++ b/yyetsweb/Mongo.py @@ -26,7 +26,8 @@ from passlib.handlers.pbkdf2 import pbkdf2_sha256 from database import (AnnouncementResource, BlacklistResource, CommentResource, ResourceResource, GrafanaQueryResource, MetricsResource, NameResource, OtherResource, DoubanResource, - TopResource, UserLikeResource, UserResource, CaptchaResource, Redis, CommentChildResource) + TopResource, UserLikeResource, UserResource, CaptchaResource, Redis, + CommentChildResource, CommentNewestResource) from utils import ts_date lib_path = pathlib.Path(__file__).parent.parent.joinpath("yyetsbot").resolve().as_posix() @@ -159,8 +160,6 @@ class CommentMongoResource(CommentResource, Mongo): self.inner_page = kwargs.get("inner_page", 1) self.inner_size = kwargs.get("inner_size", 5) condition = {"resource_id": resource_id, "deleted_at": {"$exists": False}, "type": {"$ne": "child"}} - if resource_id == -1: - condition.pop("resource_id") count = self.db["comment"].count_documents(condition) data = self.db["comment"].find(condition, self.projection) \ @@ -271,6 +270,33 @@ class CommentChildMongoResource(CommentChildResource, CommentMongoResource, Mong } +class CommentNewestMongoResource(CommentNewestResource, CommentMongoResource, Mongo): + def __init__(self): + super().__init__() + self.page = 1 + self.size = 5 + self.projection = {"ip": False, "parent_id": False, "children": False} + self.condition = {"deleted_at": {"$exists": False}} + + def get_comment(self, page: int, size: int) -> dict: + # ID,时间,用户名,用户组,资源名,资源id + condition = {"deleted_at": {"$exists": False}} + 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) + for i in data: + resource_id = i.get("resource_id", 233) + res = self.db["yyets"].find_one({"data.info.id": resource_id}) + i["cnname"] = res["data"]["info"]["cnname"] + return { + "data": data, + "count": count, + } + + class GrafanaQueryMongoResource(GrafanaQueryResource, Mongo): def get_grafana_data(self, date_series) -> str: condition = {"date": {"$in": date_series}} diff --git a/yyetsweb/database.py b/yyetsweb/database.py index 634be61..d212376 100644 --- a/yyetsweb/database.py +++ b/yyetsweb/database.py @@ -161,6 +161,11 @@ class CommentChildResource: pass +class CommentNewestResource: + def get_comment(self, page: int, size: int) -> dict: + pass + + class CaptchaResource: redis = Redis() diff --git a/yyetsweb/handler.py b/yyetsweb/handler.py index 7a2467d..2410d3f 100644 --- a/yyetsweb/handler.py +++ b/yyetsweb/handler.py @@ -352,6 +352,27 @@ class CommentChildHandler(CommentHandler): self.write(resp) +class CommentNewestHandler(CommentHandler): + class_name = f"CommentNewest{adapter}Resource" + + # from Mongo import CommentNewestResource + # instance = CommentNewestResource() + + @run_on_executor() + def get_comment(self): + size = int(self.get_argument("size", "5")) + page = int(self.get_argument("page", "1")) + + comment_data = self.instance.get_comment(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" diff --git a/yyetsweb/server.py b/yyetsweb/server.py index 42a1593..96f5fd3 100644 --- a/yyetsweb/server.py +++ b/yyetsweb/server.py @@ -20,7 +20,8 @@ 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, CommentChildHandler, DoubanHandler + GrafanaQueryHandler, BlacklistHandler, NotFoundHandler, DBDumpHandler, CommentChildHandler, DoubanHandler, \ + CommentNewestHandler enable_pretty_logging() @@ -39,6 +40,7 @@ class RunServer: (r'/api/name', NameHandler), (r'/api/comment', CommentHandler), (r'/api/comment/child', CommentChildHandler), + (r'/api/comment/newest', CommentNewestHandler), (r'/api/captcha', CaptchaHandler), (r'/api/metrics', MetricsHandler), (r'/api/grafana/', GrafanaIndexHandler),