2024-02-06 13:21:13 +08:00
|
|
|
from flask_restful import fields, marshal_with, reqparse
|
|
|
|
from flask_restful.inputs import int_range
|
|
|
|
from werkzeug.exceptions import NotFound
|
|
|
|
|
2023-05-15 08:51:32 +08:00
|
|
|
from controllers.web import api
|
|
|
|
from controllers.web.error import NotCompletionAppError
|
|
|
|
from controllers.web.wraps import WebApiResource
|
2024-01-12 12:34:01 +08:00
|
|
|
from fields.conversation_fields import message_file_fields
|
|
|
|
from libs.helper import TimestampField, uuid_value
|
2023-05-15 08:51:32 +08:00
|
|
|
from services.errors.message import MessageNotExistsError
|
|
|
|
from services.saved_message_service import SavedMessageService
|
|
|
|
|
2024-08-26 15:29:10 +08:00
|
|
|
feedback_fields = {"rating": fields.String}
|
2023-05-15 08:51:32 +08:00
|
|
|
|
|
|
|
message_fields = {
|
2024-08-26 15:29:10 +08:00
|
|
|
"id": fields.String,
|
|
|
|
"inputs": fields.Raw,
|
|
|
|
"query": fields.String,
|
|
|
|
"answer": fields.String,
|
2024-10-21 10:43:49 +08:00
|
|
|
"message_files": fields.List(fields.Nested(message_file_fields)),
|
2024-08-26 15:29:10 +08:00
|
|
|
"feedback": fields.Nested(feedback_fields, attribute="user_feedback", allow_null=True),
|
|
|
|
"created_at": TimestampField,
|
2023-05-15 08:51:32 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
class SavedMessageListApi(WebApiResource):
|
|
|
|
saved_message_infinite_scroll_pagination_fields = {
|
2024-08-26 15:29:10 +08:00
|
|
|
"limit": fields.Integer,
|
|
|
|
"has_more": fields.Boolean,
|
|
|
|
"data": fields.List(fields.Nested(message_fields)),
|
2023-05-15 08:51:32 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
@marshal_with(saved_message_infinite_scroll_pagination_fields)
|
|
|
|
def get(self, app_model, end_user):
|
2024-08-26 15:29:10 +08:00
|
|
|
if app_model.mode != "completion":
|
2023-05-15 08:51:32 +08:00
|
|
|
raise NotCompletionAppError()
|
|
|
|
|
|
|
|
parser = reqparse.RequestParser()
|
2024-08-26 15:29:10 +08:00
|
|
|
parser.add_argument("last_id", type=uuid_value, location="args")
|
|
|
|
parser.add_argument("limit", type=int_range(1, 100), required=False, default=20, location="args")
|
2023-05-15 08:51:32 +08:00
|
|
|
args = parser.parse_args()
|
|
|
|
|
2024-08-26 15:29:10 +08:00
|
|
|
return SavedMessageService.pagination_by_last_id(app_model, end_user, args["last_id"], args["limit"])
|
2023-05-15 08:51:32 +08:00
|
|
|
|
|
|
|
def post(self, app_model, end_user):
|
2024-08-26 15:29:10 +08:00
|
|
|
if app_model.mode != "completion":
|
2023-05-15 08:51:32 +08:00
|
|
|
raise NotCompletionAppError()
|
|
|
|
|
|
|
|
parser = reqparse.RequestParser()
|
2024-08-26 15:29:10 +08:00
|
|
|
parser.add_argument("message_id", type=uuid_value, required=True, location="json")
|
2023-05-15 08:51:32 +08:00
|
|
|
args = parser.parse_args()
|
|
|
|
|
|
|
|
try:
|
2024-08-26 15:29:10 +08:00
|
|
|
SavedMessageService.save(app_model, end_user, args["message_id"])
|
2023-05-15 08:51:32 +08:00
|
|
|
except MessageNotExistsError:
|
|
|
|
raise NotFound("Message Not Exists.")
|
|
|
|
|
2024-08-26 15:29:10 +08:00
|
|
|
return {"result": "success"}
|
2023-05-15 08:51:32 +08:00
|
|
|
|
|
|
|
|
|
|
|
class SavedMessageApi(WebApiResource):
|
|
|
|
def delete(self, app_model, end_user, message_id):
|
|
|
|
message_id = str(message_id)
|
|
|
|
|
2024-08-26 15:29:10 +08:00
|
|
|
if app_model.mode != "completion":
|
2023-05-15 08:51:32 +08:00
|
|
|
raise NotCompletionAppError()
|
|
|
|
|
|
|
|
SavedMessageService.delete(app_model, end_user, message_id)
|
|
|
|
|
2024-08-26 15:29:10 +08:00
|
|
|
return {"result": "success"}
|
2023-05-15 08:51:32 +08:00
|
|
|
|
|
|
|
|
2024-08-26 15:29:10 +08:00
|
|
|
api.add_resource(SavedMessageListApi, "/saved-messages")
|
|
|
|
api.add_resource(SavedMessageApi, "/saved-messages/<uuid:message_id>")
|