forked from mtusi2026/mtusi2026lab2
сделана пагинация чатов
This commit is contained in:
+71
-15
@@ -12,8 +12,9 @@ import (
|
||||
)
|
||||
|
||||
var (
|
||||
ErrNotFound = fmt.Errorf("not found")
|
||||
ErrConflict = fmt.Errorf("conflict")
|
||||
ErrNotFound = fmt.Errorf("not found")
|
||||
ErrConflict = fmt.Errorf("conflict")
|
||||
ErrInvalidCursor = fmt.Errorf("invalid cursor")
|
||||
)
|
||||
|
||||
type MemoryStore struct {
|
||||
@@ -122,33 +123,88 @@ func (s *MemoryStore) GetChatByID(chatID, callerID string) (domain.Chat, error)
|
||||
return chat, nil
|
||||
}
|
||||
|
||||
func (s *MemoryStore) GetChatsForUser(userID string, limit int) ([]domain.Chat, error) {
|
||||
func (s *MemoryStore) GetChatsForUser(
|
||||
userID string,
|
||||
limit int,
|
||||
cursorStr string,
|
||||
) (domain.ChatPage, error) {
|
||||
var cursorData *domain.ChatCursorData
|
||||
|
||||
if cursorStr != "" {
|
||||
decodedCursor, err := domain.DecodeChatCursor(cursorStr, userID)
|
||||
if err != nil {
|
||||
return domain.ChatPage{},
|
||||
fmt.Errorf("%w: %v", ErrInvalidCursor, err)
|
||||
}
|
||||
|
||||
cursorData = decodedCursor
|
||||
}
|
||||
|
||||
s.mu.RLock()
|
||||
defer s.mu.RUnlock()
|
||||
|
||||
var result []domain.Chat
|
||||
chats := make([]domain.Chat, 0)
|
||||
|
||||
for _, chat := range s.chatsByID {
|
||||
for _, m := range chat.MemberIDs {
|
||||
if m == userID {
|
||||
result = append(result, chat)
|
||||
for _, memberID := range chat.MemberIDs {
|
||||
if memberID == userID {
|
||||
chats = append(chats, chat)
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
//по контракту: сортировка по created_at, id по возрастанию
|
||||
sort.Slice(result, func(i, j int) bool {
|
||||
if result[i].CreatedAt.Equal(result[j].CreatedAt) {
|
||||
return result[i].ID < result[j].ID
|
||||
sort.Slice(chats, func(i, j int) bool {
|
||||
if chats[i].CreatedAt.Equal(chats[j].CreatedAt) {
|
||||
return chats[i].ID < chats[j].ID
|
||||
}
|
||||
return result[i].CreatedAt.Before(result[j].CreatedAt)
|
||||
|
||||
return chats[i].CreatedAt.Before(chats[j].CreatedAt)
|
||||
})
|
||||
|
||||
if limit > 0 && len(result) > limit {
|
||||
result = result[:limit]
|
||||
start := 0
|
||||
|
||||
if cursorData != nil {
|
||||
start = sort.Search(len(chats), func(index int) bool {
|
||||
chat := chats[index]
|
||||
|
||||
if chat.CreatedAt.After(cursorData.CreatedAt) {
|
||||
return true
|
||||
}
|
||||
|
||||
return chat.CreatedAt.Equal(cursorData.CreatedAt) &&
|
||||
chat.ID > cursorData.ChatID
|
||||
})
|
||||
}
|
||||
|
||||
return result, nil
|
||||
page := domain.ChatPage{
|
||||
Items: make([]domain.Chat, 0),
|
||||
NextCursor: nil,
|
||||
}
|
||||
|
||||
if start >= len(chats) {
|
||||
return page, nil
|
||||
}
|
||||
|
||||
end := start + limit
|
||||
if end > len(chats) {
|
||||
end = len(chats)
|
||||
}
|
||||
|
||||
page.Items = make([]domain.Chat, end-start)
|
||||
copy(page.Items, chats[start:end])
|
||||
|
||||
if end < len(chats) {
|
||||
lastChat := page.Items[len(page.Items)-1]
|
||||
nextCursor := domain.EncodeChatCursor(
|
||||
lastChat.CreatedAt,
|
||||
lastChat.ID,
|
||||
userID,
|
||||
)
|
||||
page.NextCursor = &nextCursor
|
||||
}
|
||||
|
||||
return page, nil
|
||||
}
|
||||
|
||||
// ---Сообщения---
|
||||
|
||||
Reference in New Issue
Block a user