def report_error(self, captcha: str, captcha_id: int, content: str, resource_id: int) -> dict: returned = {"status_code": 0, "message": ""} verify_result = CaptchaResource().verify_code(captcha, captcha_id) if not verify_result["status"]: returned["status_code"] = HTTPStatus.BAD_REQUEST returned["message"] = verify_result["message"] return returned count = self.db["douban_error"].update_one( {"resource_id": resource_id}, {"$push": {"content": content}}, upsert=True).matched_count return dict(count=count)
def add_comment(self, captcha: str, captcha_id: int, content: str, resource_id: int, 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 if not verify_result["status"]: returned["status_code"] = HTTPStatus.BAD_REQUEST returned["message"] = verify_result["message"] return returned exists = self.db["yyets"].find_one({"data.info.id": resource_id}) if not exists: returned["status_code"] = HTTPStatus.NOT_FOUND returned["message"] = "资源不存在" return returned if parent_comment_id: exists = self.db["comment"].find_one( {"_id": ObjectId(parent_comment_id)}) if not exists: returned["status_code"] = HTTPStatus.NOT_FOUND returned["message"] = "评论不存在" return returned basic_comment = { "username": username, "ip": ip, "date": ts_date(), "browser": browser, "content": content, "resource_id": resource_id } if parent_comment_id is None: basic_comment["type"] = "parent" else: basic_comment["type"] = "child" # 无论什么评论,都要插入一个新的document inserted_id: str = self.db["comment"].insert_one( basic_comment).inserted_id if parent_comment_id is not None: # 对父评论的子评论,需要给父评论加children id self.db["comment"].find_one_and_update( {"_id": ObjectId(parent_comment_id)}, {"$push": { "children": inserted_id }}) self.db["comment"].find_one_and_update( {"_id": ObjectId(inserted_id)}, {"$set": { "parent_id": ObjectId(parent_comment_id) }}) returned["status_code"] = HTTPStatus.CREATED returned["message"] = "评论成功" return returned
def add_comment(self, captcha: str, captcha_id: int, content: str, resource_id: int, ip: str, username: str, browser: str, parent_comment_id=None) -> dict: returned = {"status_code": 0, "message": ""} # check if this user is blocked reason = self.is_user_blocked(username) if reason: return {"status_code": HTTPStatus.FORBIDDEN, "message": reason} if check_spam(ip, browser, username, content) != 0: document = { "username": username, "ip": ip, "date": ts_date(), "browser": browser, "content": content, "resource_id": resource_id } inserted_id = self.db["spam"].insert_one(document).inserted_id document["_id"] = str(inserted_id) SpamProcessMongoResource.request_approval(document) return {"status_code": HTTPStatus.FORBIDDEN, "message": f"possible spam, reference id: {inserted_id}"} user_group = self.db["users"].find_one( {"username": username}, projection={"group": True, "_id": False} ) if not user_group: # admin don't have to verify code verify_result = CaptchaResource().verify_code(captcha, captcha_id) if not verify_result["status"]: returned["status_code"] = HTTPStatus.BAD_REQUEST returned["message"] = verify_result["message"] return returned exists = self.db["yyets"].find_one({"data.info.id": resource_id}) if not exists: returned["status_code"] = HTTPStatus.NOT_FOUND returned["message"] = "资源不存在" return returned if parent_comment_id: exists = self.db["comment"].find_one({"_id": ObjectId(parent_comment_id)}) if not exists: returned["status_code"] = HTTPStatus.NOT_FOUND returned["message"] = "评论不存在" return returned basic_comment = { "username": username, "ip": ip, "date": ts_date(), "browser": browser, "content": content, "resource_id": resource_id } if parent_comment_id is None: basic_comment["type"] = "parent" else: basic_comment["type"] = "child" # 无论什么评论,都要插入一个新的document inserted_id: str = self.db["comment"].insert_one(basic_comment).inserted_id if parent_comment_id is not None: # 对父评论的子评论,需要给父评论加children id self.db["comment"].find_one_and_update({"_id": ObjectId(parent_comment_id)}, {"$push": {"children": inserted_id}} ) self.db["comment"].find_one_and_update({"_id": ObjectId(inserted_id)}, {"$set": {"parent_id": ObjectId(parent_comment_id)}} ) returned["status_code"] = HTTPStatus.CREATED returned["message"] = "评论成功" # notification if parent_comment_id: # find username self.db["notification"].find_one_and_update( {"username": exists["username"]}, {"$push": {"unread": inserted_id}}, upsert=True ) # send email # TODO unsubscribe parent_comment = self.db["comment"].find_one({"_id": ObjectId(parent_comment_id)}) if resource_id == 233: link = f"https://yyets.dmesg.app/discuss#{parent_comment_id}" else: link = f"https://yyets.dmesg.app/resource?id={resource_id}#{parent_comment_id}" user_info = self.db["users"].find_one({"username": parent_comment["username"], "email.verified": True}) if user_info: subject = "[人人影视下载分享站] 你的评论有了新的回复" pt_content = content.split("</reply>")[-1] body = f"{username} 您好,<br>你的评论 {parent_comment['content']} 有了新的回复:<br>{pt_content}" \ f"<br>你可以<a href='{link}'>点此链接</a>查看<br><br>请勿回复此邮件" send_mail(user_info["email"]["address"], subject, body) return returned