52 lines
1.2 KiB
Go
52 lines
1.2 KiB
Go
package logic
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
|
|
"spark-server/internal/pkg/errs"
|
|
)
|
|
|
|
type HistoryRequest struct {
|
|
ConversationID int64
|
|
LastMsgID int64
|
|
Limit int
|
|
}
|
|
|
|
type HistoryResponse struct {
|
|
Items []*Item `json:"items"`
|
|
HasMore bool `json:"has_more"`
|
|
}
|
|
|
|
func (s *Service) GetHistory(ctx context.Context, userID int64, req HistoryRequest) (*HistoryResponse, error) {
|
|
if !s.ready() || userID <= 0 {
|
|
return nil, errs.ErrUnauthorized
|
|
}
|
|
if req.ConversationID <= 0 {
|
|
return nil, errs.ErrInvalidArgument
|
|
}
|
|
if _, err := s.chats.GetMember(ctx, req.ConversationID, userID); err != nil {
|
|
if errors.Is(err, errs.ErrNotFound) {
|
|
return nil, errs.ErrForbidden
|
|
}
|
|
return nil, err
|
|
}
|
|
limit := req.Limit
|
|
if limit <= 0 || limit > defaultHistoryLimit {
|
|
limit = defaultHistoryLimit
|
|
}
|
|
rows, err := s.repo.ListByConversation(ctx, req.ConversationID, userID, req.LastMsgID, limit+1)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
hasMore := len(rows) > limit
|
|
if hasMore {
|
|
rows = rows[:limit]
|
|
}
|
|
items := make([]*Item, 0, len(rows))
|
|
for _, row := range rows {
|
|
items = append(items, itemOf(row))
|
|
}
|
|
return &HistoryResponse{Items: items, HasMore: hasMore}, nil
|
|
}
|