35 lines
603 B
Go
35 lines
603 B
Go
package repositories
|
|
|
|
import (
|
|
"chat/internal/models"
|
|
"fmt"
|
|
"time"
|
|
)
|
|
|
|
func SendChatMessage(msg string, user *models.User, channel *models.Channel) error {
|
|
// Handle empty message
|
|
if len(msg) < 1 {
|
|
return fmt.Errorf("message cannot be empty")
|
|
}
|
|
|
|
// Handle long message
|
|
if len(msg) > 500 {
|
|
return fmt.Errorf("message too long")
|
|
}
|
|
|
|
// Check if user is banned
|
|
banned := user.IsBanned
|
|
if banned {
|
|
return fmt.Errorf("user: %s, is banned", user.ID)
|
|
}
|
|
|
|
// Send message to channel
|
|
channel.Send(models.Message{
|
|
Content: msg,
|
|
Sender: user,
|
|
Timestamp: time.Now(),
|
|
})
|
|
|
|
return nil
|
|
}
|