继续补齐核心模块 Context 超时控制
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
package chat
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"net/url"
|
||||
"strings"
|
||||
@@ -21,45 +22,45 @@ func NewService(repo *Repository) *Service {
|
||||
return &Service{repo: repo}
|
||||
}
|
||||
|
||||
func (s *Service) ListConversations(principal Principal, page, pageSize int) (*PaginatedResult, error) {
|
||||
func (s *Service) ListConversations(ctx context.Context, principal Principal, page, pageSize int) (*PaginatedResult, error) {
|
||||
if s.repo == nil {
|
||||
return nil, ErrDependencyUnavailable
|
||||
}
|
||||
return s.repo.ListConversations(principal, page, pageSize)
|
||||
return s.repo.ListConversations(ctx, principal, page, pageSize)
|
||||
}
|
||||
|
||||
func (s *Service) FindConversation(principal Principal, id uint64) (*ConversationDTO, error) {
|
||||
func (s *Service) FindConversation(ctx context.Context, principal Principal, id uint64) (*ConversationDTO, error) {
|
||||
if s.repo == nil {
|
||||
return nil, ErrDependencyUnavailable
|
||||
}
|
||||
return s.repo.FindConversation(principal, id)
|
||||
return s.repo.FindConversation(ctx, principal, id)
|
||||
}
|
||||
|
||||
func (s *Service) FindOrderConversation(userID uint64, orderID uint64) (*ConversationDTO, error) {
|
||||
func (s *Service) FindOrderConversation(ctx context.Context, userID uint64, orderID uint64) (*ConversationDTO, error) {
|
||||
if s.repo == nil {
|
||||
return nil, ErrDependencyUnavailable
|
||||
}
|
||||
return s.repo.FindOrderConversation(userID, orderID)
|
||||
return s.repo.FindOrderConversation(ctx, userID, orderID)
|
||||
}
|
||||
|
||||
func (s *Service) EnsureSupportConversation(userID uint64) (*ConversationDTO, error) {
|
||||
func (s *Service) EnsureSupportConversation(ctx context.Context, userID uint64) (*ConversationDTO, error) {
|
||||
if s.repo == nil {
|
||||
return nil, ErrDependencyUnavailable
|
||||
}
|
||||
if userID == 0 {
|
||||
return nil, ErrPermissionDenied
|
||||
}
|
||||
return s.repo.EnsureSupportConversation(userID)
|
||||
return s.repo.EnsureSupportConversation(ctx, userID)
|
||||
}
|
||||
|
||||
func (s *Service) Messages(principal Principal, conversationID uint64, page, pageSize int) (*PaginatedResult, error) {
|
||||
func (s *Service) Messages(ctx context.Context, principal Principal, conversationID uint64, page, pageSize int) (*PaginatedResult, error) {
|
||||
if s.repo == nil {
|
||||
return nil, ErrDependencyUnavailable
|
||||
}
|
||||
return s.repo.Messages(principal, conversationID, page, pageSize)
|
||||
return s.repo.Messages(ctx, principal, conversationID, page, pageSize)
|
||||
}
|
||||
|
||||
func (s *Service) SendMessage(principal Principal, conversationID uint64, req SendMessageRequest) (*MessageDTO, error) {
|
||||
func (s *Service) SendMessage(ctx context.Context, principal Principal, conversationID uint64, req SendMessageRequest) (*MessageDTO, error) {
|
||||
if s.repo == nil {
|
||||
return nil, ErrDependencyUnavailable
|
||||
}
|
||||
@@ -75,7 +76,7 @@ func (s *Service) SendMessage(principal Principal, conversationID uint64, req Se
|
||||
if !ok {
|
||||
return nil, ErrInvalidMessage
|
||||
}
|
||||
return s.repo.SendMessage(principal, conversationID, req)
|
||||
return s.repo.SendMessage(ctx, principal, conversationID, req)
|
||||
}
|
||||
|
||||
func normalizeAttachmentURLS(items []string) ([]string, bool) {
|
||||
@@ -112,52 +113,52 @@ func normalizeAttachmentURLS(items []string) ([]string, bool) {
|
||||
return result, true
|
||||
}
|
||||
|
||||
func (s *Service) MarkRead(principal Principal, conversationID uint64) error {
|
||||
func (s *Service) MarkRead(ctx context.Context, principal Principal, conversationID uint64) error {
|
||||
if s.repo == nil {
|
||||
return ErrDependencyUnavailable
|
||||
}
|
||||
return s.repo.MarkRead(principal, conversationID)
|
||||
return s.repo.MarkRead(ctx, principal, conversationID)
|
||||
}
|
||||
|
||||
func (s *Service) TransferConversation(principal Principal, conversationID uint64, req TransferRequest) error {
|
||||
func (s *Service) TransferConversation(ctx context.Context, principal Principal, conversationID uint64, req TransferRequest) error {
|
||||
if s.repo == nil {
|
||||
return ErrDependencyUnavailable
|
||||
}
|
||||
if conversationID == 0 || req.ToAdminID == 0 {
|
||||
return ErrInvalidMessage
|
||||
}
|
||||
return s.repo.TransferConversation(principal, conversationID, req.ToAdminID)
|
||||
return s.repo.TransferConversation(ctx, principal, conversationID, req.ToAdminID)
|
||||
}
|
||||
|
||||
func (s *Service) GetAvailableSupportAdmins() ([]SupportAdminDTO, error) {
|
||||
func (s *Service) GetAvailableSupportAdmins(ctx context.Context) ([]SupportAdminDTO, error) {
|
||||
if s.repo == nil {
|
||||
return nil, ErrDependencyUnavailable
|
||||
}
|
||||
return s.repo.GetAvailableSupportAdmins()
|
||||
return s.repo.GetAvailableSupportAdmins(ctx)
|
||||
}
|
||||
|
||||
func (s *Service) ListConversationsWithFilter(principal Principal, page, pageSize int, filter string) (*PaginatedResult, error) {
|
||||
func (s *Service) ListConversationsWithFilter(ctx context.Context, principal Principal, page, pageSize int, filter string) (*PaginatedResult, error) {
|
||||
if s.repo == nil {
|
||||
return nil, ErrDependencyUnavailable
|
||||
}
|
||||
return s.repo.ListConversationsWithFilter(principal, page, pageSize, filter)
|
||||
return s.repo.ListConversationsWithFilter(ctx, principal, page, pageSize, filter)
|
||||
}
|
||||
|
||||
func (s *Service) UpdateRemark(principal Principal, conversationID uint64, req UpdateRemarkRequest) error {
|
||||
func (s *Service) UpdateRemark(ctx context.Context, principal Principal, conversationID uint64, req UpdateRemarkRequest) error {
|
||||
if s.repo == nil {
|
||||
return ErrDependencyUnavailable
|
||||
}
|
||||
return s.repo.UpdateRemark(principal, conversationID, req.Remark)
|
||||
return s.repo.UpdateRemark(ctx, principal, conversationID, req.Remark)
|
||||
}
|
||||
|
||||
func (s *Service) ListQuickReplies(adminID uint64) ([]QuickReplyDTO, error) {
|
||||
func (s *Service) ListQuickReplies(ctx context.Context, adminID uint64) ([]QuickReplyDTO, error) {
|
||||
if s.repo == nil {
|
||||
return nil, ErrDependencyUnavailable
|
||||
}
|
||||
return s.repo.ListQuickReplies(adminID)
|
||||
return s.repo.ListQuickReplies(ctx, adminID)
|
||||
}
|
||||
|
||||
func (s *Service) CreateQuickReply(adminID uint64, req CreateQuickReplyRequest) (*QuickReplyDTO, error) {
|
||||
func (s *Service) CreateQuickReply(ctx context.Context, adminID uint64, req CreateQuickReplyRequest) (*QuickReplyDTO, error) {
|
||||
if s.repo == nil {
|
||||
return nil, ErrDependencyUnavailable
|
||||
}
|
||||
@@ -166,33 +167,33 @@ func (s *Service) CreateQuickReply(adminID uint64, req CreateQuickReplyRequest)
|
||||
if req.Title == "" || req.Content == "" {
|
||||
return nil, ErrInvalidMessage
|
||||
}
|
||||
return s.repo.CreateQuickReply(adminID, req)
|
||||
return s.repo.CreateQuickReply(ctx, adminID, req)
|
||||
}
|
||||
|
||||
func (s *Service) UpdateQuickReply(adminID uint64, replyID uint64, req UpdateQuickReplyRequest) error {
|
||||
func (s *Service) UpdateQuickReply(ctx context.Context, adminID uint64, replyID uint64, req UpdateQuickReplyRequest) error {
|
||||
if s.repo == nil {
|
||||
return ErrDependencyUnavailable
|
||||
}
|
||||
return s.repo.UpdateQuickReply(adminID, replyID, req)
|
||||
return s.repo.UpdateQuickReply(ctx, adminID, replyID, req)
|
||||
}
|
||||
|
||||
func (s *Service) DeleteQuickReply(adminID uint64, replyID uint64) error {
|
||||
func (s *Service) DeleteQuickReply(ctx context.Context, adminID uint64, replyID uint64) error {
|
||||
if s.repo == nil {
|
||||
return ErrDependencyUnavailable
|
||||
}
|
||||
return s.repo.DeleteQuickReply(adminID, replyID)
|
||||
return s.repo.DeleteQuickReply(ctx, adminID, replyID)
|
||||
}
|
||||
|
||||
func (s *Service) GetAutoWelcomeMessage() string {
|
||||
func (s *Service) GetAutoWelcomeMessage(ctx context.Context) string {
|
||||
if s.repo == nil {
|
||||
return ""
|
||||
}
|
||||
return s.repo.GetAutoWelcomeMessage()
|
||||
return s.repo.GetAutoWelcomeMessage(ctx)
|
||||
}
|
||||
|
||||
func (s *Service) UpdateAutoWelcomeMessage(message string) error {
|
||||
func (s *Service) UpdateAutoWelcomeMessage(ctx context.Context, message string) error {
|
||||
if s.repo == nil {
|
||||
return ErrDependencyUnavailable
|
||||
}
|
||||
return s.repo.UpdateAutoWelcomeMessage(message)
|
||||
return s.repo.UpdateAutoWelcomeMessage(ctx, message)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user