User repository (#3795)
* It builds with the new user repository * fix(test): fix broken test * fix(api): fix registration endpoint that was broken after the change * fix(test): update test to reflect new user repository * fix: use interface type instead of concrete type * fix: restore commented out code
This commit is contained in:
@@ -14,8 +14,8 @@ import (
|
||||
"github.com/owncast/owncast/config"
|
||||
"github.com/owncast/owncast/core/chat/events"
|
||||
"github.com/owncast/owncast/core/data"
|
||||
"github.com/owncast/owncast/core/user"
|
||||
"github.com/owncast/owncast/geoip"
|
||||
"github.com/owncast/owncast/models"
|
||||
)
|
||||
|
||||
// Client represents a single chat client.
|
||||
@@ -25,7 +25,7 @@ type Client struct {
|
||||
rateLimiter *rate.Limiter
|
||||
messageFilter *ChatMessageFilter
|
||||
conn *websocket.Conn
|
||||
User *user.User `json:"user"`
|
||||
User *models.User `json:"user"`
|
||||
server *Server
|
||||
Geo *geoip.GeoDetails `json:"geo"`
|
||||
// Buffered channel of outbound messages.
|
||||
|
||||
@@ -9,8 +9,8 @@ import (
|
||||
"github.com/owncast/owncast/config"
|
||||
"github.com/owncast/owncast/core/chat/events"
|
||||
"github.com/owncast/owncast/core/data"
|
||||
"github.com/owncast/owncast/core/user"
|
||||
"github.com/owncast/owncast/core/webhooks"
|
||||
"github.com/owncast/owncast/persistence/userrepository"
|
||||
"github.com/owncast/owncast/utils"
|
||||
log "github.com/sirupsen/logrus"
|
||||
)
|
||||
@@ -46,8 +46,10 @@ func (s *Server) userNameChanged(eventData chatClientEvent) {
|
||||
}
|
||||
}
|
||||
|
||||
userRepository := userrepository.Get()
|
||||
|
||||
// Check if the name is not already assigned to a registered user.
|
||||
if available, err := user.IsDisplayNameAvailable(proposedUsername); err != nil {
|
||||
if available, err := userRepository.IsDisplayNameAvailable(proposedUsername); err != nil {
|
||||
log.Errorln("error checking if name is available", err)
|
||||
return
|
||||
} else if !available {
|
||||
@@ -60,7 +62,7 @@ func (s *Server) userNameChanged(eventData chatClientEvent) {
|
||||
return
|
||||
}
|
||||
|
||||
savedUser := user.GetUserByToken(eventData.client.accessToken)
|
||||
savedUser := userRepository.GetUserByToken(eventData.client.accessToken)
|
||||
oldName := savedUser.DisplayName
|
||||
|
||||
// Check that the new name is different from old.
|
||||
@@ -70,7 +72,7 @@ func (s *Server) userNameChanged(eventData chatClientEvent) {
|
||||
}
|
||||
|
||||
// Save the new name
|
||||
if err := user.ChangeUsername(eventData.client.User.ID, proposedUsername); err != nil {
|
||||
if err := userRepository.ChangeUsername(eventData.client.User.ID, proposedUsername); err != nil {
|
||||
log.Errorln("error changing username", err)
|
||||
}
|
||||
|
||||
@@ -103,6 +105,8 @@ func (s *Server) userNameChanged(eventData chatClientEvent) {
|
||||
}
|
||||
|
||||
func (s *Server) userColorChanged(eventData chatClientEvent) {
|
||||
userRepository := userrepository.Get()
|
||||
|
||||
var receivedEvent events.ColorChangeEvent
|
||||
if err := json.Unmarshal(eventData.data, &receivedEvent); err != nil {
|
||||
log.Errorln("error unmarshalling to ColorChangeEvent", err)
|
||||
@@ -116,7 +120,7 @@ func (s *Server) userColorChanged(eventData chatClientEvent) {
|
||||
}
|
||||
|
||||
// Save the new color
|
||||
if err := user.ChangeUserColor(eventData.client.User.ID, receivedEvent.NewColor); err != nil {
|
||||
if err := userRepository.ChangeUserColor(eventData.client.User.ID, receivedEvent.NewColor); err != nil {
|
||||
log.Errorln("error changing user display color", err)
|
||||
}
|
||||
|
||||
@@ -126,6 +130,8 @@ func (s *Server) userColorChanged(eventData chatClientEvent) {
|
||||
}
|
||||
|
||||
func (s *Server) userMessageSent(eventData chatClientEvent) {
|
||||
userRepository := userrepository.Get()
|
||||
|
||||
var event events.UserMessageEvent
|
||||
if err := json.Unmarshal(eventData.data, &event); err != nil {
|
||||
log.Errorln("error unmarshalling to UserMessageEvent", err)
|
||||
@@ -148,7 +154,7 @@ func (s *Server) userMessageSent(eventData chatClientEvent) {
|
||||
}
|
||||
}
|
||||
|
||||
event.User = user.GetUserByToken(eventData.client.accessToken)
|
||||
event.User = userRepository.GetUserByToken(eventData.client.accessToken)
|
||||
|
||||
// Guard against nil users
|
||||
if event.User == nil {
|
||||
|
||||
@@ -1,9 +1,9 @@
|
||||
package events
|
||||
|
||||
import "github.com/owncast/owncast/core/user"
|
||||
import "github.com/owncast/owncast/models"
|
||||
|
||||
// ConnectedClientInfo represents the information about a connected client.
|
||||
type ConnectedClientInfo struct {
|
||||
User *user.User `json:"user"`
|
||||
User *models.User `json:"user"`
|
||||
Event
|
||||
}
|
||||
|
||||
@@ -20,7 +20,7 @@ import (
|
||||
"mvdan.cc/xurls"
|
||||
|
||||
"github.com/owncast/owncast/core/data"
|
||||
"github.com/owncast/owncast/core/user"
|
||||
"github.com/owncast/owncast/models"
|
||||
log "github.com/sirupsen/logrus"
|
||||
)
|
||||
|
||||
@@ -30,21 +30,21 @@ type EventPayload map[string]interface{}
|
||||
// OutboundEvent represents an event that is sent out to all listeners of the chat server.
|
||||
type OutboundEvent interface {
|
||||
GetBroadcastPayload() EventPayload
|
||||
GetMessageType() EventType
|
||||
GetMessageType() models.EventType
|
||||
}
|
||||
|
||||
// Event is any kind of event. A type is required to be specified.
|
||||
type Event struct {
|
||||
Timestamp time.Time `json:"timestamp"`
|
||||
Type EventType `json:"type,omitempty"`
|
||||
ID string `json:"id"`
|
||||
Timestamp time.Time `json:"timestamp"`
|
||||
Type models.EventType `json:"type,omitempty"`
|
||||
ID string `json:"id"`
|
||||
}
|
||||
|
||||
// UserEvent is an event with an associated user.
|
||||
type UserEvent struct {
|
||||
User *user.User `json:"user"`
|
||||
HiddenAt *time.Time `json:"hiddenAt,omitempty"`
|
||||
ClientID uint `json:"clientId,omitempty"`
|
||||
User *models.User `json:"user"`
|
||||
HiddenAt *time.Time `json:"hiddenAt,omitempty"`
|
||||
ClientID uint `json:"clientId,omitempty"`
|
||||
}
|
||||
|
||||
// MessageEvent is an event that has a message body.
|
||||
|
||||
@@ -8,8 +8,9 @@ import (
|
||||
|
||||
"github.com/owncast/owncast/core/chat/events"
|
||||
"github.com/owncast/owncast/core/data"
|
||||
"github.com/owncast/owncast/core/user"
|
||||
"github.com/owncast/owncast/models"
|
||||
"github.com/owncast/owncast/persistence/tables"
|
||||
|
||||
log "github.com/sirupsen/logrus"
|
||||
)
|
||||
|
||||
@@ -22,7 +23,7 @@ const (
|
||||
|
||||
func setupPersistence() {
|
||||
_datastore = data.GetDatastore()
|
||||
data.CreateMessagesTable(_datastore.DB)
|
||||
tables.CreateMessagesTable(_datastore.DB)
|
||||
data.CreateBanIPTable(_datastore.DB)
|
||||
|
||||
chatDataPruner := time.NewTicker(5 * time.Minute)
|
||||
@@ -104,7 +105,7 @@ func makeUserMessageEventFromRowData(row rowData) events.UserMessageEvent {
|
||||
isBot := (row.userType != nil && *row.userType == "API")
|
||||
scopeSlice := strings.Split(scopes, ",")
|
||||
|
||||
u := user.User{
|
||||
u := models.User{
|
||||
ID: *row.userID,
|
||||
DisplayName: displayName,
|
||||
DisplayColor: displayColor,
|
||||
|
||||
@@ -14,9 +14,10 @@ import (
|
||||
"github.com/owncast/owncast/config"
|
||||
"github.com/owncast/owncast/core/chat/events"
|
||||
"github.com/owncast/owncast/core/data"
|
||||
"github.com/owncast/owncast/core/user"
|
||||
"github.com/owncast/owncast/core/webhooks"
|
||||
"github.com/owncast/owncast/geoip"
|
||||
"github.com/owncast/owncast/models"
|
||||
"github.com/owncast/owncast/persistence/userrepository"
|
||||
"github.com/owncast/owncast/utils"
|
||||
)
|
||||
|
||||
@@ -82,7 +83,7 @@ func (s *Server) Run() {
|
||||
}
|
||||
|
||||
// Addclient registers new connection as a User.
|
||||
func (s *Server) Addclient(conn *websocket.Conn, user *user.User, accessToken string, userAgent string, ipAddress string) *Client {
|
||||
func (s *Server) Addclient(conn *websocket.Conn, user *models.User, accessToken string, userAgent string, ipAddress string) *Client {
|
||||
client := &Client{
|
||||
server: s,
|
||||
conn: conn,
|
||||
@@ -239,8 +240,11 @@ func (s *Server) HandleClientConnection(w http.ResponseWriter, r *http.Request)
|
||||
return
|
||||
}
|
||||
|
||||
userRepository := userrepository.Get()
|
||||
|
||||
// A user is required to use the websocket
|
||||
user := user.GetUserByToken(accessToken)
|
||||
user := userRepository.GetUserByToken(accessToken)
|
||||
|
||||
if user == nil {
|
||||
// Send error that registration is required
|
||||
_ = conn.WriteJSON(events.EventPayload{
|
||||
@@ -335,8 +339,10 @@ func SendConnectedClientInfoToUser(userID string) error {
|
||||
return err
|
||||
}
|
||||
|
||||
userRepository := userrepository.Get()
|
||||
|
||||
// Get an updated reference to the user.
|
||||
user := user.GetUserByID(userID)
|
||||
user := userRepository.GetUserByID(userID)
|
||||
if user == nil {
|
||||
return fmt.Errorf("user not found")
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user