2020-06-23 15:11:01 -05:00
|
|
|
package chat
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
2021-07-19 19:22:29 -07:00
|
|
|
"net/http"
|
|
|
|
"sort"
|
2020-06-23 15:11:01 -05:00
|
|
|
|
2021-07-19 19:22:29 -07:00
|
|
|
"github.com/owncast/owncast/core/chat/events"
|
2020-10-06 01:07:09 +08:00
|
|
|
"github.com/owncast/owncast/models"
|
2021-07-19 19:22:29 -07:00
|
|
|
log "github.com/sirupsen/logrus"
|
2020-06-23 15:11:01 -05:00
|
|
|
)
|
|
|
|
|
2021-07-19 19:22:29 -07:00
|
|
|
var getStatus func() models.Status
|
|
|
|
|
2021-09-12 00:18:15 -07:00
|
|
|
// Start begins the chat server.
|
2021-07-19 19:22:29 -07:00
|
|
|
func Start(getStatusFunc func() models.Status) error {
|
2020-07-15 17:20:47 -07:00
|
|
|
setupPersistence()
|
|
|
|
|
2021-07-19 19:22:29 -07:00
|
|
|
getStatus = getStatusFunc
|
|
|
|
_server = NewChat()
|
|
|
|
|
|
|
|
go _server.Run()
|
|
|
|
|
2021-08-13 15:26:44 -07:00
|
|
|
log.Traceln("Chat server started with max connection count of", _server.maxSocketConnectionLimit)
|
2021-07-19 19:22:29 -07:00
|
|
|
|
|
|
|
return nil
|
2020-06-23 15:11:01 -05:00
|
|
|
}
|
|
|
|
|
2021-07-19 19:22:29 -07:00
|
|
|
// GetClientsForUser will return chat connections that are owned by a specific user.
|
2021-09-12 00:18:15 -07:00
|
|
|
func GetClientsForUser(userID string) ([]*Client, error) {
|
|
|
|
clients := map[string][]*Client{}
|
2020-06-23 15:11:01 -05:00
|
|
|
|
2021-07-19 19:22:29 -07:00
|
|
|
for _, client := range _server.clients {
|
2021-09-12 00:18:15 -07:00
|
|
|
clients[client.User.ID] = append(clients[client.User.ID], client)
|
2021-07-19 19:22:29 -07:00
|
|
|
}
|
2020-06-23 15:11:01 -05:00
|
|
|
|
2021-07-19 19:22:29 -07:00
|
|
|
if _, exists := clients[userID]; !exists {
|
|
|
|
return nil, errors.New("no connections for user found")
|
|
|
|
}
|
2020-06-23 15:11:01 -05:00
|
|
|
|
2021-07-19 19:22:29 -07:00
|
|
|
return clients[userID], nil
|
2020-06-23 15:11:01 -05:00
|
|
|
}
|
|
|
|
|
2021-09-13 10:26:28 +02:00
|
|
|
// FindClientByID will return a single connected client by ID.
|
|
|
|
func FindClientByID(clientID uint) (*Client, bool) {
|
|
|
|
client, found := _server.clients[clientID]
|
|
|
|
return client, found
|
|
|
|
}
|
|
|
|
|
2021-09-12 00:18:15 -07:00
|
|
|
// GetClients will return all the current chat clients connected.
|
|
|
|
func GetClients() []*Client {
|
|
|
|
clients := []*Client{}
|
2021-07-19 19:22:29 -07:00
|
|
|
|
|
|
|
// Convert the keyed map to a slice.
|
|
|
|
for _, client := range _server.clients {
|
|
|
|
clients = append(clients, client)
|
2020-06-23 15:11:01 -05:00
|
|
|
}
|
|
|
|
|
2021-07-19 19:22:29 -07:00
|
|
|
sort.Slice(clients, func(i, j int) bool {
|
|
|
|
return clients[i].ConnectedAt.Before(clients[j].ConnectedAt)
|
|
|
|
})
|
|
|
|
|
|
|
|
return clients
|
2020-06-23 15:11:01 -05:00
|
|
|
}
|
|
|
|
|
2021-09-12 00:18:15 -07:00
|
|
|
// SendSystemMessage will send a message string as a system message to all clients.
|
2021-07-19 19:22:29 -07:00
|
|
|
func SendSystemMessage(text string, ephemeral bool) error {
|
|
|
|
message := events.SystemMessageEvent{
|
|
|
|
MessageEvent: events.MessageEvent{
|
|
|
|
Body: text,
|
|
|
|
},
|
2020-06-23 15:11:01 -05:00
|
|
|
}
|
2021-07-19 19:22:29 -07:00
|
|
|
message.SetDefaults()
|
|
|
|
message.RenderBody()
|
2020-06-23 15:11:01 -05:00
|
|
|
|
2021-07-19 19:22:29 -07:00
|
|
|
if err := Broadcast(&message); err != nil {
|
|
|
|
log.Errorln("error sending system message", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if !ephemeral {
|
2021-09-12 00:18:15 -07:00
|
|
|
saveEvent(message.ID, "system", message.Body, message.GetMessageType(), nil, message.Timestamp)
|
2021-07-19 19:22:29 -07:00
|
|
|
}
|
2021-02-18 23:05:52 -08:00
|
|
|
|
2021-07-19 19:22:29 -07:00
|
|
|
return nil
|
2020-06-23 15:11:01 -05:00
|
|
|
}
|
2020-10-06 23:14:33 -07:00
|
|
|
|
2021-09-12 00:18:15 -07:00
|
|
|
// SendSystemAction will send a system action string as an action event to all clients.
|
2021-07-19 19:22:29 -07:00
|
|
|
func SendSystemAction(text string, ephemeral bool) error {
|
|
|
|
message := events.ActionEvent{
|
|
|
|
MessageEvent: events.MessageEvent{
|
|
|
|
Body: text,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
message.SetDefaults()
|
|
|
|
message.RenderBody()
|
2021-03-03 20:44:13 -08:00
|
|
|
|
2021-07-19 19:22:29 -07:00
|
|
|
if err := Broadcast(&message); err != nil {
|
|
|
|
log.Errorln("error sending system chat action")
|
2020-10-06 23:14:33 -07:00
|
|
|
}
|
2021-07-19 19:22:29 -07:00
|
|
|
|
|
|
|
if !ephemeral {
|
2021-09-12 00:18:15 -07:00
|
|
|
saveEvent(message.ID, "action", message.Body, message.GetMessageType(), nil, message.Timestamp)
|
2021-07-19 19:22:29 -07:00
|
|
|
}
|
|
|
|
|
2020-10-06 23:14:33 -07:00
|
|
|
return nil
|
|
|
|
}
|
2021-07-19 19:22:29 -07:00
|
|
|
|
2021-09-12 00:18:15 -07:00
|
|
|
// SendAllWelcomeMessage will send the chat message to all connected clients.
|
2021-07-19 19:22:29 -07:00
|
|
|
func SendAllWelcomeMessage() {
|
|
|
|
_server.sendAllWelcomeMessage()
|
|
|
|
}
|
|
|
|
|
2021-09-13 10:26:28 +02:00
|
|
|
// SendSystemMessageToClient will send a single message to a single connected chat client.
|
|
|
|
func SendSystemMessageToClient(clientID uint, text string) {
|
|
|
|
if client, foundClient := FindClientByID(clientID); foundClient {
|
|
|
|
_server.sendSystemMessageToClient(client, text)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-09-12 00:18:15 -07:00
|
|
|
// Broadcast will send all connected clients the outbound object provided.
|
2021-07-19 19:22:29 -07:00
|
|
|
func Broadcast(event events.OutboundEvent) error {
|
|
|
|
return _server.Broadcast(event.GetBroadcastPayload())
|
|
|
|
}
|
|
|
|
|
2021-09-12 00:18:15 -07:00
|
|
|
// HandleClientConnection handles a single inbound websocket connection.
|
2021-07-19 19:22:29 -07:00
|
|
|
func HandleClientConnection(w http.ResponseWriter, r *http.Request) {
|
|
|
|
_server.HandleClientConnection(w, r)
|
|
|
|
}
|
|
|
|
|
|
|
|
// DisconnectUser will forcefully disconnect all clients belonging to a user by ID.
|
|
|
|
func DisconnectUser(userID string) {
|
|
|
|
_server.DisconnectUser(userID)
|
|
|
|
}
|