2023-05-15 08:51:32 +08:00
|
|
|
from typing import Optional, Union
|
|
|
|
|
2024-04-09 17:04:48 +08:00
|
|
|
from core.app.entities.app_invoke_entities import InvokeFrom
|
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 PinnedConversation
|
|
|
|
from services.conversation_service import ConversationService
|
|
|
|
|
|
|
|
|
|
|
|
class WebConversationService:
|
|
|
|
@classmethod
|
2023-12-03 20:59:13 +08:00
|
|
|
def pagination_by_last_id(cls, app_model: App, user: Optional[Union[Account, EndUser]],
|
2024-04-09 17:04:48 +08:00
|
|
|
last_id: Optional[str], limit: int, invoke_from: InvokeFrom,
|
|
|
|
pinned: Optional[bool] = None) -> InfiniteScrollPagination:
|
2023-05-15 08:51:32 +08:00
|
|
|
include_ids = None
|
|
|
|
exclude_ids = None
|
|
|
|
if pinned is not None:
|
|
|
|
pinned_conversations = db.session.query(PinnedConversation).filter(
|
|
|
|
PinnedConversation.app_id == app_model.id,
|
2023-05-25 15:54:45 +08:00
|
|
|
PinnedConversation.created_by_role == ('account' if isinstance(user, Account) else 'end_user'),
|
|
|
|
PinnedConversation.created_by == user.id
|
2023-05-15 08:51:32 +08:00
|
|
|
).order_by(PinnedConversation.created_at.desc()).all()
|
|
|
|
pinned_conversation_ids = [pc.conversation_id for pc in pinned_conversations]
|
|
|
|
if pinned:
|
|
|
|
include_ids = pinned_conversation_ids
|
|
|
|
else:
|
|
|
|
exclude_ids = pinned_conversation_ids
|
|
|
|
|
|
|
|
return ConversationService.pagination_by_last_id(
|
|
|
|
app_model=app_model,
|
2023-05-25 15:54:45 +08:00
|
|
|
user=user,
|
2023-05-15 08:51:32 +08:00
|
|
|
last_id=last_id,
|
|
|
|
limit=limit,
|
2024-04-09 17:04:48 +08:00
|
|
|
invoke_from=invoke_from,
|
2023-05-15 08:51:32 +08:00
|
|
|
include_ids=include_ids,
|
2023-11-13 22:05:46 +08:00
|
|
|
exclude_ids=exclude_ids,
|
2023-05-15 08:51:32 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
@classmethod
|
2023-12-03 20:59:13 +08:00
|
|
|
def pin(cls, app_model: App, conversation_id: str, user: Optional[Union[Account, EndUser]]):
|
2023-05-15 08:51:32 +08:00
|
|
|
pinned_conversation = db.session.query(PinnedConversation).filter(
|
|
|
|
PinnedConversation.app_id == app_model.id,
|
|
|
|
PinnedConversation.conversation_id == conversation_id,
|
2023-05-25 15:54:45 +08:00
|
|
|
PinnedConversation.created_by_role == ('account' if isinstance(user, Account) else 'end_user'),
|
2023-05-15 08:51:32 +08:00
|
|
|
PinnedConversation.created_by == user.id
|
|
|
|
).first()
|
|
|
|
|
|
|
|
if pinned_conversation:
|
|
|
|
return
|
|
|
|
|
|
|
|
conversation = ConversationService.get_conversation(
|
|
|
|
app_model=app_model,
|
|
|
|
conversation_id=conversation_id,
|
|
|
|
user=user
|
|
|
|
)
|
|
|
|
|
|
|
|
pinned_conversation = PinnedConversation(
|
|
|
|
app_id=app_model.id,
|
|
|
|
conversation_id=conversation.id,
|
2023-05-25 15:54:45 +08:00
|
|
|
created_by_role='account' if isinstance(user, Account) else 'end_user',
|
2023-05-15 08:51:32 +08:00
|
|
|
created_by=user.id
|
|
|
|
)
|
|
|
|
|
|
|
|
db.session.add(pinned_conversation)
|
|
|
|
db.session.commit()
|
|
|
|
|
|
|
|
@classmethod
|
2023-12-03 20:59:13 +08:00
|
|
|
def unpin(cls, app_model: App, conversation_id: str, user: Optional[Union[Account, EndUser]]):
|
2023-05-15 08:51:32 +08:00
|
|
|
pinned_conversation = db.session.query(PinnedConversation).filter(
|
|
|
|
PinnedConversation.app_id == app_model.id,
|
|
|
|
PinnedConversation.conversation_id == conversation_id,
|
2023-05-25 15:54:45 +08:00
|
|
|
PinnedConversation.created_by_role == ('account' if isinstance(user, Account) else 'end_user'),
|
2023-05-15 08:51:32 +08:00
|
|
|
PinnedConversation.created_by == user.id
|
|
|
|
).first()
|
|
|
|
|
|
|
|
if not pinned_conversation:
|
|
|
|
return
|
|
|
|
|
|
|
|
db.session.delete(pinned_conversation)
|
|
|
|
db.session.commit()
|