forked from mtusi2026/mtusi2026lab3
feat: add 5 experiment
This commit is contained in:
@@ -5,8 +5,6 @@ import (
|
||||
"encoding/base64"
|
||||
"encoding/json"
|
||||
"strconv"
|
||||
"strings"
|
||||
"unicode/utf8"
|
||||
|
||||
"messenger/src/backend/repositories"
|
||||
)
|
||||
@@ -24,31 +22,6 @@ func NewMessageService(messages *repositories.MessageStore) *MessageService {
|
||||
return &MessageService{messages: messages}
|
||||
}
|
||||
|
||||
func (s *MessageService) Create(ctx context.Context, userID string, chat repositories.Chat, text string) (repositories.Message, error) {
|
||||
if !ChatHasMember(chat, userID) {
|
||||
return repositories.Message{}, ErrChatNotFound
|
||||
}
|
||||
if strings.TrimSpace(text) == "" {
|
||||
return repositories.Message{}, InvalidInput{"text must not be blank"}
|
||||
}
|
||||
if utf8.RuneCountInString(text) > 4000 {
|
||||
return repositories.Message{}, InvalidInput{"text is too long"}
|
||||
}
|
||||
return s.messages.Create(ctx, chat.ID, userID, text)
|
||||
}
|
||||
|
||||
func MessageLess(left, right repositories.Message) bool {
|
||||
if left.CreatedAt.Equal(right.CreatedAt) {
|
||||
return left.ID < right.ID
|
||||
}
|
||||
return left.CreatedAt.Before(right.CreatedAt)
|
||||
}
|
||||
|
||||
func MessageAfterCursor(message repositories.Message, cursor repositories.MessageCursor) bool {
|
||||
return message.CreatedAt.After(cursor.CreatedAt) ||
|
||||
(message.CreatedAt.Equal(cursor.CreatedAt) && message.ID > cursor.MessageID)
|
||||
}
|
||||
|
||||
func EncodeMessageCursor(cursor repositories.MessageCursor) (string, error) {
|
||||
data, err := json.Marshal(cursor)
|
||||
if err != nil {
|
||||
|
||||
Reference in New Issue
Block a user