2023-05-25 15:54:45 +08:00
|
|
|
from typing import Optional, Union
|
2023-05-15 08:51:32 +08:00
|
|
|
|
|
|
|
from extensions.ext_database import db
|
2024-01-12 12:34:01 +08:00
|
|
|
from libs.infinite_scroll_pagination import InfiniteScrollPagination
|
2023-05-25 15:54:45 +08:00
|
|
|
from models.account import Account
|
2023-05-15 08:51:32 +08:00
|
|
|
from models.model import App, EndUser
|
|
|
|
from models.web import SavedMessage
|
|
|
|
from services.message_service import MessageService
|
|
|
|
|
|
|
|
|
|
|
|
class SavedMessageService:
|
|
|
|
@classmethod
|
2024-08-26 13:43:57 +08:00
|
|
|
def pagination_by_last_id(
|
|
|
|
cls, app_model: App, user: Optional[Union[Account, EndUser]], last_id: Optional[str], limit: int
|
|
|
|
) -> InfiniteScrollPagination:
|
|
|
|
saved_messages = (
|
|
|
|
db.session.query(SavedMessage)
|
|
|
|
.filter(
|
|
|
|
SavedMessage.app_id == app_model.id,
|
|
|
|
SavedMessage.created_by_role == ("account" if isinstance(user, Account) else "end_user"),
|
|
|
|
SavedMessage.created_by == user.id,
|
|
|
|
)
|
|
|
|
.order_by(SavedMessage.created_at.desc())
|
|
|
|
.all()
|
|
|
|
)
|
2023-05-15 08:51:32 +08:00
|
|
|
message_ids = [sm.message_id for sm in saved_messages]
|
|
|
|
|
|
|
|
return MessageService.pagination_by_last_id(
|
2024-08-26 13:43:57 +08:00
|
|
|
app_model=app_model, user=user, last_id=last_id, limit=limit, include_ids=message_ids
|
2023-05-15 08:51:32 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
@classmethod
|
2023-12-03 20:59:13 +08:00
|
|
|
def save(cls, app_model: App, user: Optional[Union[Account, EndUser]], message_id: str):
|
2024-08-26 13:43:57 +08:00
|
|
|
saved_message = (
|
|
|
|
db.session.query(SavedMessage)
|
|
|
|
.filter(
|
|
|
|
SavedMessage.app_id == app_model.id,
|
|
|
|
SavedMessage.message_id == message_id,
|
|
|
|
SavedMessage.created_by_role == ("account" if isinstance(user, Account) else "end_user"),
|
|
|
|
SavedMessage.created_by == user.id,
|
|
|
|
)
|
|
|
|
.first()
|
|
|
|
)
|
2023-05-15 08:51:32 +08:00
|
|
|
|
|
|
|
if saved_message:
|
|
|
|
return
|
|
|
|
|
2024-08-26 13:43:57 +08:00
|
|
|
message = MessageService.get_message(app_model=app_model, user=user, message_id=message_id)
|
2023-05-15 08:51:32 +08:00
|
|
|
|
|
|
|
saved_message = SavedMessage(
|
|
|
|
app_id=app_model.id,
|
|
|
|
message_id=message.id,
|
2024-08-26 13:43:57 +08:00
|
|
|
created_by_role="account" if isinstance(user, Account) else "end_user",
|
|
|
|
created_by=user.id,
|
2023-05-15 08:51:32 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
db.session.add(saved_message)
|
|
|
|
db.session.commit()
|
|
|
|
|
|
|
|
@classmethod
|
2023-12-03 20:59:13 +08:00
|
|
|
def delete(cls, app_model: App, user: Optional[Union[Account, EndUser]], message_id: str):
|
2024-08-26 13:43:57 +08:00
|
|
|
saved_message = (
|
|
|
|
db.session.query(SavedMessage)
|
|
|
|
.filter(
|
|
|
|
SavedMessage.app_id == app_model.id,
|
|
|
|
SavedMessage.message_id == message_id,
|
|
|
|
SavedMessage.created_by_role == ("account" if isinstance(user, Account) else "end_user"),
|
|
|
|
SavedMessage.created_by == user.id,
|
|
|
|
)
|
|
|
|
.first()
|
|
|
|
)
|
2023-05-15 08:51:32 +08:00
|
|
|
|
|
|
|
if not saved_message:
|
|
|
|
return
|
|
|
|
|
|
|
|
db.session.delete(saved_message)
|
|
|
|
db.session.commit()
|