Chat refactor + persistent backing chat users (#1163)
* First pass at chat user registration and validation * Disable chat if the user is disabled/blocked or the server hits max connections * Handle dropping sockets if chat is disabled * Fix origin in automated chat test * Work for updated chat moderation * Chat message markdown rendering and fix tests * Put /api/chat behind a chat user access token. Closes #1085 * Reject blocked username changes * More WIP moderation * Defer configuring chat until we know if it is enabled. Closes #1135 * chat user blocking. Closes #1096 * Add tests around user access for #1096 * Add external integration chat message API + update integration auth middleware to pass along integration name. Closes #1092 * Delete old chat messages from db as to not hold on to excessive data. Closes #1152 * Add schema migration for messages. Closes #1155 * Commit updated API documentation * Add chat load test * Shared db mutex and db optimizations * Simplify past display name handling * Use a new test db for each test run * Wire up the external messages actions + add tests for them * Move access tokens to be actual users * Run message pruning at launch + fix comparison * Do not return API users in disabled users response * Fix incorrect highlighting. Closes #1160 * Consolidate user table statements * Set the max process connection limit to 70% of maximum * Fix wrong old display name being returned in name change event * Delete the old chat server files * Wire back up the webhooks * Remove unused * Invalidate user cache on changes * Do not send rendered body as RawBody * Some cleanup * Standardize names for external API users to ExternalAPIUser * Do not log token * Checkout branch when building admin for testing * Bundle in dev admin for testing * Some cleanup * Cleanup js logs * Cleanup and standardize event names * Clean up some logging * Update API spec. Closes #1133 * Commit updated API documentation * Change paths to be better named * Commit updated API documentation * Update admin bundle * Fix duplicate event name * Rename scope var * Update admin bundle * Move connected clients controller into admin package * Fix collecting usernames for autocomplete purposes * No longer generate username when it is empty * Sort clients and users by timestamp * Move file to admin controller package * Swap, so the comments stay correct Co-authored-by: Jannik <jannik@outlook.com> * Use explicit type alias Co-authored-by: Jannik <jannik@outlook.com> * Remove commented code. Co-authored-by: Jannik <jannik@outlook.com> * Cleanup test * Remove some extra logging * Add some clarity * Update dev instance of admin for testing * Consolidate lines Co-authored-by: Jannik <jannik@outlook.com> * Remove commented unused vars Co-authored-by: Jannik <jannik@outlook.com> * Until needed do not return IP address with client list * Fix typo of wrong var * Typo led to a bad test. Fix typo and fix test. * Guard against the socket reconnecting on error if previously set to shutdown * Do not log access tokens * Return success message on enable/disable user * Clean up some inactionable error messages. Sent ban message. Sort banned users. * fix styling for when chat is completely disabled * Unused * guard against nil clients * Update dev admin bundle * Do not unhide messages when unblocking user just to be safe. Send removal action from the controller * Add convinience function for getting active connections for a single user * Lock db on these mutations * Cleanup force disconnect using GetClientsForUser and capture client reference explicitly * No longer re-showing banned user messages for safety. Removing this test. * Remove no longer needed comment * Tweaks to forbidden username handling. - Standardize naming to not use "block" but "forbidden" instead. - Pass array over the wire instead of string. - Add API test - Fix default list incorrectly being appended to custom list. * Logging cleanup * Update dev admin bundle * Add an artificial delay in order to visually see message being hidden when testing * Remove the user cache as it is a premature optimization * When connected to chat let the user know their current user details to sync the username in the UI * On connected send current display name back to client. - Move name change out of chat component. - Add additional event type constants. * Fix broken workflow due to typo * Troubleshoot workflow * Bump htm from 3.0.4 to 3.1.0 in /build/javascript (#1181) * Bump htm from 3.0.4 to 3.1.0 in /build/javascript Bumps [htm](https://github.com/developit/htm) from 3.0.4 to 3.1.0. - [Release notes](https://github.com/developit/htm/releases) - [Commits](https://github.com/developit/htm/compare/3.0.4...3.1.0) --- updated-dependencies: - dependency-name: htm dependency-type: direct:production update-type: version-update:semver-minor ... Signed-off-by: dependabot[bot] <support@github.com> * Run npm run build and update libraries Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com> Co-authored-by: Gabe Kangas <gabek@real-ity.com> * Commit updated Javascript packages * Re-send current user info when a rejected name change takes place * All socket writes should be through the send chan and not directly * Seed the random generator * Add keys and indexes to users table * a util to generate consistent emoji markup * console clean up * mod tidy * Commit updated API documentation * Handle the max payload size of a socket message. - Only close socket if x2 greater than the max size. - Send the user a message if a message is too large. - Surface the max size in bytes in the config. * Update admin bundle * Force all events to be sent in their own socket message and do not concatinate in a single message * Update chat embed to register for access token * Use a different access token for embed chat * Update the chat message bubble background color to be bolder * add base tag to open links in new window, closes #1220 * Support text input of :emoji: in chat (#1190) * Initial implementation of emoji injection * fix bookkeeping with multiple emoji * make the emoji lookup case-insensitive * try another solution for Caretposition * add title to emojis minor refactoring * bind moji injection to InputKeyUp * simplify the code replace all found emojis * inject emoji if the modifer is released earlier * more efficient emoji tag search * use json emoji.emoji as url * use createEmojiMarkup() * move emojify() to chat.js * emojify on paste * cleanup emoji titles in paste * update inputText in InputKeyup * mark emoji titles with 2*zwnj this way paste cleanup will not interfere with text which include zwnj * emoji should not change the inputText * Do not show join messages when chat is offline. Closes #1224 - Show stream starting/ending messages in chat. - When stream starts show everyone the welcome message. * Force scrolling chat to bottom after history is populated regardless of scroll position. Closes https://github.com/owncast/owncast/issues/1222 * use maxSocketPayloadSize to calculate total bytes of message payload (#1221) * utilize maxSocketPayloadSize from config; update chatInput to calculate based on that value instead of text value; remove usage of inputText for counting * add a buffer to account for entire websocket payload for message char counting; trim nbsp;'s from ends of messages when calculating count Co-authored-by: Gabe Kangas <gabek@real-ity.com> Co-authored-by: Owncast <owncast@owncast.online> Co-authored-by: Jannik <jannik@outlook.com> Co-authored-by: Ginger Wong <omqmail@gmail.com> Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com> Co-authored-by: Meisam <39205857+MFTabriz@users.noreply.github.com>
This commit is contained in:
@@ -2,84 +2,113 @@ package chat
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"time"
|
||||
"net/http"
|
||||
"sort"
|
||||
|
||||
"github.com/owncast/owncast/core/chat/events"
|
||||
"github.com/owncast/owncast/models"
|
||||
log "github.com/sirupsen/logrus"
|
||||
)
|
||||
|
||||
// Setup sets up the chat server.
|
||||
func Setup(listener models.ChatListener) {
|
||||
var getStatus func() models.Status
|
||||
|
||||
func Start(getStatusFunc func() models.Status) error {
|
||||
setupPersistence()
|
||||
|
||||
clients := make(map[string]*Client)
|
||||
addCh := make(chan *Client)
|
||||
delCh := make(chan *Client)
|
||||
sendAllCh := make(chan models.ChatEvent)
|
||||
pingCh := make(chan models.PingMessage)
|
||||
doneCh := make(chan bool)
|
||||
errCh := make(chan error)
|
||||
getStatus = getStatusFunc
|
||||
_server = NewChat()
|
||||
|
||||
_server = &server{
|
||||
clients,
|
||||
"/entry", //hardcoded due to the UI requiring this and it is not configurable
|
||||
listener,
|
||||
addCh,
|
||||
delCh,
|
||||
sendAllCh,
|
||||
pingCh,
|
||||
doneCh,
|
||||
errCh,
|
||||
}
|
||||
}
|
||||
go _server.Run()
|
||||
|
||||
// Start starts the chat server.
|
||||
func Start() error {
|
||||
if _server == nil {
|
||||
return errors.New("chat server is nil")
|
||||
}
|
||||
log.Traceln("Chat server started with max connection count of", _server.maxClientCount)
|
||||
|
||||
ticker := time.NewTicker(30 * time.Second)
|
||||
go func() {
|
||||
for range ticker.C {
|
||||
_server.ping()
|
||||
}
|
||||
}()
|
||||
|
||||
_server.Listen()
|
||||
|
||||
return errors.New("chat server failed to start")
|
||||
}
|
||||
|
||||
// SendMessage sends a message to all.
|
||||
func SendMessage(message models.ChatEvent) {
|
||||
if _server == nil {
|
||||
return
|
||||
}
|
||||
|
||||
_server.SendToAll(message)
|
||||
}
|
||||
|
||||
// GetMessages gets all of the messages.
|
||||
func GetMessages() []models.ChatEvent {
|
||||
if _server == nil {
|
||||
return []models.ChatEvent{}
|
||||
}
|
||||
|
||||
return getChatHistory()
|
||||
}
|
||||
|
||||
func GetModerationChatMessages() []models.ChatEvent {
|
||||
return getChatModerationHistory()
|
||||
}
|
||||
|
||||
func GetClient(clientID string) *Client {
|
||||
l.RLock()
|
||||
defer l.RUnlock()
|
||||
|
||||
for _, client := range _server.Clients {
|
||||
if client.ClientID == clientID {
|
||||
return client
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// GetClientsForUser will return chat connections that are owned by a specific user.
|
||||
func GetClientsForUser(userID string) ([]*ChatClient, error) {
|
||||
clients := map[string][]*ChatClient{}
|
||||
|
||||
for _, client := range _server.clients {
|
||||
clients[client.User.Id] = append(clients[client.User.Id], client)
|
||||
}
|
||||
|
||||
if _, exists := clients[userID]; !exists {
|
||||
return nil, errors.New("no connections for user found")
|
||||
}
|
||||
|
||||
return clients[userID], nil
|
||||
}
|
||||
|
||||
func GetClients() []*ChatClient {
|
||||
clients := []*ChatClient{}
|
||||
|
||||
// Convert the keyed map to a slice.
|
||||
for _, client := range _server.clients {
|
||||
clients = append(clients, client)
|
||||
}
|
||||
|
||||
sort.Slice(clients, func(i, j int) bool {
|
||||
return clients[i].ConnectedAt.Before(clients[j].ConnectedAt)
|
||||
})
|
||||
|
||||
return clients
|
||||
}
|
||||
|
||||
func SendSystemMessage(text string, ephemeral bool) error {
|
||||
message := events.SystemMessageEvent{
|
||||
MessageEvent: events.MessageEvent{
|
||||
Body: text,
|
||||
},
|
||||
}
|
||||
message.SetDefaults()
|
||||
message.RenderBody()
|
||||
|
||||
if err := Broadcast(&message); err != nil {
|
||||
log.Errorln("error sending system message", err)
|
||||
}
|
||||
|
||||
if !ephemeral {
|
||||
saveEvent(message.Id, "system", message.Body, message.GetMessageType(), nil, message.Timestamp)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func SendSystemAction(text string, ephemeral bool) error {
|
||||
message := events.ActionEvent{
|
||||
MessageEvent: events.MessageEvent{
|
||||
Body: text,
|
||||
},
|
||||
}
|
||||
|
||||
message.SetDefaults()
|
||||
message.RenderBody()
|
||||
|
||||
if err := Broadcast(&message); err != nil {
|
||||
log.Errorln("error sending system chat action")
|
||||
}
|
||||
|
||||
if !ephemeral {
|
||||
saveEvent(message.Id, "action", message.Body, message.GetMessageType(), nil, message.Timestamp)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func SendAllWelcomeMessage() {
|
||||
_server.sendAllWelcomeMessage()
|
||||
}
|
||||
|
||||
func Broadcast(event events.OutboundEvent) error {
|
||||
return _server.Broadcast(event.GetBroadcastPayload())
|
||||
}
|
||||
|
||||
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)
|
||||
}
|
||||
|
||||
190
core/chat/chatclient.go
Normal file
190
core/chat/chatclient.go
Normal file
@@ -0,0 +1,190 @@
|
||||
package chat
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"time"
|
||||
|
||||
log "github.com/sirupsen/logrus"
|
||||
"golang.org/x/time/rate"
|
||||
|
||||
"github.com/gorilla/websocket"
|
||||
"github.com/owncast/owncast/config"
|
||||
"github.com/owncast/owncast/core/chat/events"
|
||||
"github.com/owncast/owncast/core/user"
|
||||
"github.com/owncast/owncast/geoip"
|
||||
)
|
||||
|
||||
type ChatClient struct {
|
||||
id uint
|
||||
accessToken string
|
||||
conn *websocket.Conn
|
||||
User *user.User `json:"user"`
|
||||
server *ChatServer
|
||||
ipAddress string `json:"-"`
|
||||
// Buffered channel of outbound messages.
|
||||
send chan []byte
|
||||
rateLimiter *rate.Limiter
|
||||
Geo *geoip.GeoDetails `json:"geo"`
|
||||
MessageCount int `json:"messageCount"`
|
||||
UserAgent string `json:"userAgent"`
|
||||
ConnectedAt time.Time `json:"connectedAt"`
|
||||
}
|
||||
|
||||
type chatClientEvent struct {
|
||||
data []byte
|
||||
client *ChatClient
|
||||
}
|
||||
|
||||
const (
|
||||
// Time allowed to write a message to the peer.
|
||||
writeWait = 10 * time.Second
|
||||
|
||||
// Time allowed to read the next pong message from the peer.
|
||||
pongWait = 60 * time.Second
|
||||
|
||||
// Send pings to peer with this period. Must be less than pongWait.
|
||||
pingPeriod = (pongWait * 9) / 10
|
||||
|
||||
// Maximum message size allowed from peer.
|
||||
// Larger messages get thrown away.
|
||||
// Messages > *2 the socket gets closed.
|
||||
maxMessageSize = config.MaxSocketPayloadSize
|
||||
)
|
||||
|
||||
var upgrader = websocket.Upgrader{
|
||||
ReadBufferSize: 1024,
|
||||
WriteBufferSize: 1024,
|
||||
}
|
||||
|
||||
var (
|
||||
newline = []byte{'\n'}
|
||||
space = []byte{' '}
|
||||
)
|
||||
|
||||
func (c *ChatClient) sendConnectedClientInfo() {
|
||||
payload := events.EventPayload{
|
||||
"type": events.ConnectedUserInfo,
|
||||
"user": c.User,
|
||||
}
|
||||
|
||||
c.sendPayload(payload)
|
||||
}
|
||||
|
||||
func (c *ChatClient) readPump() {
|
||||
c.rateLimiter = rate.NewLimiter(0.6, 5)
|
||||
|
||||
defer func() {
|
||||
c.close()
|
||||
}()
|
||||
|
||||
// If somebody is sending 2x the max message size they're likely a bad actor
|
||||
// and should be disconnected. Below we throw away messages > max size.
|
||||
c.conn.SetReadLimit(maxMessageSize * 2)
|
||||
|
||||
_ = c.conn.SetReadDeadline(time.Now().Add(pongWait))
|
||||
c.conn.SetPongHandler(func(string) error { _ = c.conn.SetReadDeadline(time.Now().Add(pongWait)); return nil })
|
||||
for {
|
||||
_, message, err := c.conn.ReadMessage()
|
||||
|
||||
if err != nil {
|
||||
if websocket.IsUnexpectedCloseError(err, websocket.CloseGoingAway, websocket.CloseAbnormalClosure) {
|
||||
c.close()
|
||||
}
|
||||
break
|
||||
}
|
||||
|
||||
// Throw away messages greater than max message size.
|
||||
if len(message) > maxMessageSize {
|
||||
c.sendAction("Sorry, that message exceeded the maximum size and can't be delivered.")
|
||||
continue
|
||||
}
|
||||
|
||||
// Guard against floods.
|
||||
if !c.passesRateLimit() {
|
||||
continue
|
||||
}
|
||||
|
||||
message = bytes.TrimSpace(bytes.Replace(message, newline, space, -1))
|
||||
c.handleEvent(message)
|
||||
}
|
||||
}
|
||||
|
||||
func (c *ChatClient) writePump() {
|
||||
ticker := time.NewTicker(pingPeriod)
|
||||
defer func() {
|
||||
ticker.Stop()
|
||||
c.conn.Close()
|
||||
}()
|
||||
|
||||
for {
|
||||
select {
|
||||
case message, ok := <-c.send:
|
||||
_ = c.conn.SetWriteDeadline(time.Now().Add(writeWait))
|
||||
if !ok {
|
||||
// The server closed the channel.
|
||||
_ = c.conn.WriteMessage(websocket.CloseMessage, []byte{})
|
||||
return
|
||||
}
|
||||
|
||||
w, err := c.conn.NextWriter(websocket.TextMessage)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
if _, err := w.Write(message); err != nil {
|
||||
log.Debugln(err)
|
||||
}
|
||||
|
||||
if err := w.Close(); err != nil {
|
||||
return
|
||||
}
|
||||
case <-ticker.C:
|
||||
_ = c.conn.SetWriteDeadline(time.Now().Add(writeWait))
|
||||
if err := c.conn.WriteMessage(websocket.PingMessage, nil); err != nil {
|
||||
return
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func (c *ChatClient) handleEvent(data []byte) {
|
||||
c.server.inbound <- chatClientEvent{data: data, client: c}
|
||||
}
|
||||
|
||||
func (c *ChatClient) close() {
|
||||
log.Traceln("client closed:", c.User.DisplayName, c.id, c.ipAddress)
|
||||
|
||||
c.conn.Close()
|
||||
c.server.unregister <- c
|
||||
}
|
||||
|
||||
func (c *ChatClient) passesRateLimit() bool {
|
||||
if !c.rateLimiter.Allow() {
|
||||
log.Debugln("Client", c.id, c.User.DisplayName, "has exceeded the messaging rate limiting thresholds.")
|
||||
return false
|
||||
}
|
||||
|
||||
return true
|
||||
}
|
||||
|
||||
func (c *ChatClient) sendPayload(payload events.EventPayload) {
|
||||
var data []byte
|
||||
data, err := json.Marshal(payload)
|
||||
if err != nil {
|
||||
log.Errorln(err)
|
||||
return
|
||||
}
|
||||
|
||||
c.send <- data
|
||||
}
|
||||
|
||||
func (c *ChatClient) sendAction(message string) {
|
||||
clientMessage := events.ActionEvent{
|
||||
MessageEvent: events.MessageEvent{
|
||||
Body: message,
|
||||
},
|
||||
}
|
||||
clientMessage.SetDefaults()
|
||||
clientMessage.RenderBody()
|
||||
c.sendPayload(clientMessage.GetBroadcastPayload())
|
||||
}
|
||||
@@ -1,241 +0,0 @@
|
||||
package chat
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io"
|
||||
"time"
|
||||
|
||||
log "github.com/sirupsen/logrus"
|
||||
"golang.org/x/net/websocket"
|
||||
|
||||
"github.com/owncast/owncast/geoip"
|
||||
"github.com/owncast/owncast/models"
|
||||
"github.com/owncast/owncast/utils"
|
||||
|
||||
"github.com/teris-io/shortid"
|
||||
"golang.org/x/time/rate"
|
||||
)
|
||||
|
||||
const channelBufSize = 100
|
||||
|
||||
//Client represents a chat client.
|
||||
type Client struct {
|
||||
ConnectedAt time.Time
|
||||
MessageCount int
|
||||
UserAgent string
|
||||
IPAddress string
|
||||
Username *string
|
||||
ClientID string // How we identify unique viewers when counting viewer counts.
|
||||
Geo *geoip.GeoDetails `json:"geo"`
|
||||
Ignore bool // If set to true this will not be treated as a viewer
|
||||
|
||||
socketID string // How we identify a single websocket client.
|
||||
ws *websocket.Conn
|
||||
ch chan models.ChatEvent
|
||||
pingch chan models.PingMessage
|
||||
usernameChangeChannel chan models.NameChangeEvent
|
||||
userJoinedChannel chan models.UserJoinedEvent
|
||||
|
||||
doneCh chan bool
|
||||
|
||||
rateLimiter *rate.Limiter
|
||||
}
|
||||
|
||||
// NewClient creates a new chat client.
|
||||
func NewClient(ws *websocket.Conn) *Client {
|
||||
if ws == nil {
|
||||
log.Panicln("ws cannot be nil")
|
||||
}
|
||||
|
||||
var ignoreClient = false
|
||||
for _, extraData := range ws.Config().Protocol {
|
||||
if extraData == "IGNORE_CLIENT" {
|
||||
ignoreClient = true
|
||||
}
|
||||
}
|
||||
|
||||
ch := make(chan models.ChatEvent, channelBufSize)
|
||||
doneCh := make(chan bool)
|
||||
pingch := make(chan models.PingMessage)
|
||||
usernameChangeChannel := make(chan models.NameChangeEvent)
|
||||
userJoinedChannel := make(chan models.UserJoinedEvent)
|
||||
|
||||
ipAddress := utils.GetIPAddressFromRequest(ws.Request())
|
||||
userAgent := ws.Request().UserAgent()
|
||||
socketID, _ := shortid.Generate()
|
||||
clientID := socketID
|
||||
|
||||
rateLimiter := rate.NewLimiter(0.6, 5)
|
||||
|
||||
return &Client{time.Now(), 0, userAgent, ipAddress, nil, clientID, nil, ignoreClient, socketID, ws, ch, pingch, usernameChangeChannel, userJoinedChannel, doneCh, rateLimiter}
|
||||
}
|
||||
|
||||
func (c *Client) write(msg models.ChatEvent) {
|
||||
select {
|
||||
case c.ch <- msg:
|
||||
default:
|
||||
_server.removeClient(c)
|
||||
_server.err(fmt.Errorf("client %s is disconnected", c.ClientID))
|
||||
}
|
||||
}
|
||||
|
||||
// Listen Write and Read request via channel.
|
||||
func (c *Client) listen() {
|
||||
go c.listenWrite()
|
||||
c.listenRead()
|
||||
}
|
||||
|
||||
// Listen write request via channel.
|
||||
func (c *Client) listenWrite() {
|
||||
for {
|
||||
select {
|
||||
// Send a PING keepalive
|
||||
case msg := <-c.pingch:
|
||||
if err := websocket.JSON.Send(c.ws, msg); err != nil {
|
||||
c.handleClientSocketError(err)
|
||||
}
|
||||
// send message to the client
|
||||
case msg := <-c.ch:
|
||||
if err := websocket.JSON.Send(c.ws, msg); err != nil {
|
||||
c.handleClientSocketError(err)
|
||||
}
|
||||
case msg := <-c.usernameChangeChannel:
|
||||
if err := websocket.JSON.Send(c.ws, msg); err != nil {
|
||||
c.handleClientSocketError(err)
|
||||
}
|
||||
case msg := <-c.userJoinedChannel:
|
||||
if err := websocket.JSON.Send(c.ws, msg); err != nil {
|
||||
c.handleClientSocketError(err)
|
||||
}
|
||||
|
||||
// receive done request
|
||||
case <-c.doneCh:
|
||||
_server.removeClient(c)
|
||||
c.doneCh <- true // for listenRead method
|
||||
return
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func (c *Client) handleClientSocketError(err error) {
|
||||
_server.removeClient(c)
|
||||
}
|
||||
|
||||
func (c *Client) passesRateLimit() bool {
|
||||
if !c.rateLimiter.Allow() {
|
||||
log.Debugln("Client", c.ClientID, "has exceeded the messaging rate limiting thresholds.")
|
||||
return false
|
||||
}
|
||||
|
||||
return true
|
||||
}
|
||||
|
||||
// Listen read request via channel.
|
||||
func (c *Client) listenRead() {
|
||||
for {
|
||||
select {
|
||||
// receive done request
|
||||
case <-c.doneCh:
|
||||
_server.remove(c)
|
||||
c.doneCh <- true // for listenWrite method
|
||||
return
|
||||
|
||||
// read data from websocket connection
|
||||
default:
|
||||
var data []byte
|
||||
if err := websocket.Message.Receive(c.ws, &data); err != nil {
|
||||
if err == io.EOF {
|
||||
c.doneCh <- true
|
||||
return
|
||||
}
|
||||
c.handleClientSocketError(err)
|
||||
}
|
||||
|
||||
if !c.passesRateLimit() {
|
||||
continue
|
||||
}
|
||||
|
||||
var messageTypeCheck map[string]interface{}
|
||||
|
||||
// Bad messages should be thrown away
|
||||
if err := json.Unmarshal(data, &messageTypeCheck); err != nil {
|
||||
log.Debugln("Badly formatted message received from", c.Username, c.ws.Request().RemoteAddr)
|
||||
continue
|
||||
}
|
||||
|
||||
// If we can't tell the type of message, also throw it away.
|
||||
if messageTypeCheck == nil {
|
||||
log.Debugln("Untyped message received from", c.Username, c.ws.Request().RemoteAddr)
|
||||
continue
|
||||
}
|
||||
|
||||
messageType := messageTypeCheck["type"].(string)
|
||||
|
||||
if messageType == models.MessageSent {
|
||||
c.chatMessageReceived(data)
|
||||
} else if messageType == models.UserNameChanged {
|
||||
c.userChangedName(data)
|
||||
} else if messageType == models.UserJoined {
|
||||
c.userJoined(data)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func (c *Client) userJoined(data []byte) {
|
||||
var msg models.UserJoinedEvent
|
||||
if err := json.Unmarshal(data, &msg); err != nil {
|
||||
log.Errorln(err)
|
||||
return
|
||||
}
|
||||
|
||||
msg.ID = shortid.MustGenerate()
|
||||
msg.Type = models.UserJoined
|
||||
msg.Timestamp = time.Now()
|
||||
|
||||
c.Username = &msg.Username
|
||||
|
||||
_server.userJoined(msg)
|
||||
}
|
||||
|
||||
func (c *Client) userChangedName(data []byte) {
|
||||
var msg models.NameChangeEvent
|
||||
if err := json.Unmarshal(data, &msg); err != nil {
|
||||
log.Errorln(err)
|
||||
}
|
||||
msg.Type = models.UserNameChanged
|
||||
msg.ID = shortid.MustGenerate()
|
||||
_server.usernameChanged(msg)
|
||||
c.Username = &msg.NewName
|
||||
}
|
||||
|
||||
func (c *Client) chatMessageReceived(data []byte) {
|
||||
var msg models.ChatEvent
|
||||
if err := json.Unmarshal(data, &msg); err != nil {
|
||||
log.Errorln(err)
|
||||
}
|
||||
|
||||
msg.SetDefaults()
|
||||
|
||||
c.MessageCount++
|
||||
c.Username = &msg.Author
|
||||
|
||||
msg.ClientID = c.ClientID
|
||||
msg.RenderAndSanitizeMessageBody()
|
||||
|
||||
_server.SendToAll(msg)
|
||||
}
|
||||
|
||||
// GetViewerClientFromChatClient returns a general models.Client from a chat websocket client.
|
||||
func (c *Client) GetViewerClientFromChatClient() models.Client {
|
||||
return models.Client{
|
||||
ConnectedAt: c.ConnectedAt,
|
||||
MessageCount: c.MessageCount,
|
||||
UserAgent: c.UserAgent,
|
||||
IPAddress: c.IPAddress,
|
||||
Username: c.Username,
|
||||
ClientID: c.ClientID,
|
||||
Geo: geoip.GetGeoFromIP(c.IPAddress),
|
||||
}
|
||||
}
|
||||
102
core/chat/events.go
Normal file
102
core/chat/events.go
Normal file
@@ -0,0 +1,102 @@
|
||||
package chat
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"strings"
|
||||
|
||||
"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"
|
||||
log "github.com/sirupsen/logrus"
|
||||
)
|
||||
|
||||
func (s *ChatServer) userNameChanged(eventData chatClientEvent) {
|
||||
var receivedEvent events.NameChangeEvent
|
||||
if err := json.Unmarshal(eventData.data, &receivedEvent); err != nil {
|
||||
log.Errorln("error unmarshalling to NameChangeEvent", err)
|
||||
return
|
||||
}
|
||||
|
||||
proposedUsername := receivedEvent.NewName
|
||||
blocklist := data.GetForbiddenUsernameList()
|
||||
|
||||
for _, blockedName := range blocklist {
|
||||
normalizedName := strings.TrimSpace(blockedName)
|
||||
normalizedName = strings.ToLower(normalizedName)
|
||||
if strings.Contains(normalizedName, proposedUsername) {
|
||||
// Denied.
|
||||
log.Debugln(eventData.client.User.DisplayName, "blocked from changing name to", proposedUsername, "due to blocked name", normalizedName)
|
||||
message := fmt.Sprintf("You cannot change your name to **%s**.", proposedUsername)
|
||||
s.sendActionToClient(eventData.client, message)
|
||||
|
||||
// Resend the client's user so their username is in sync.
|
||||
eventData.client.sendConnectedClientInfo()
|
||||
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
savedUser := user.GetUserByToken(eventData.client.accessToken)
|
||||
oldName := savedUser.DisplayName
|
||||
|
||||
// Save the new name
|
||||
user.ChangeUsername(eventData.client.User.Id, receivedEvent.NewName)
|
||||
|
||||
// Update the connected clients associated user with the new name
|
||||
eventData.client.User = savedUser
|
||||
|
||||
// Send chat event letting everyone about about the name change
|
||||
savedUser.DisplayName = receivedEvent.NewName
|
||||
|
||||
broadcastEvent := events.NameChangeBroadcast{
|
||||
Oldname: oldName,
|
||||
}
|
||||
broadcastEvent.User = savedUser
|
||||
broadcastEvent.SetDefaults()
|
||||
payload := broadcastEvent.GetBroadcastPayload()
|
||||
if err := s.Broadcast(payload); err != nil {
|
||||
log.Errorln("error broadcasting NameChangeEvent", err)
|
||||
return
|
||||
}
|
||||
|
||||
// Send chat user name changed webhook
|
||||
receivedEvent.User = savedUser
|
||||
webhooks.SendChatEventUsernameChanged(receivedEvent)
|
||||
}
|
||||
|
||||
func (s *ChatServer) userMessageSent(eventData chatClientEvent) {
|
||||
var event events.UserMessageEvent
|
||||
if err := json.Unmarshal(eventData.data, &event); err != nil {
|
||||
log.Errorln("error unmarshalling to UserMessageEvent", err)
|
||||
return
|
||||
}
|
||||
|
||||
event.SetDefaults()
|
||||
|
||||
// Ignore empty messages
|
||||
if event.Empty() {
|
||||
return
|
||||
}
|
||||
|
||||
event.User = user.GetUserByToken(eventData.client.accessToken)
|
||||
|
||||
// Guard against nil users
|
||||
if event.User == nil {
|
||||
return
|
||||
}
|
||||
|
||||
payload := event.GetBroadcastPayload()
|
||||
if err := s.Broadcast(payload); err != nil {
|
||||
log.Errorln("error broadcasting UserMessageEvent payload", err)
|
||||
return
|
||||
}
|
||||
|
||||
// Send chat message sent webhook
|
||||
webhooks.SendChatEvent(&event)
|
||||
|
||||
SaveUserMessage(event)
|
||||
|
||||
eventData.client.MessageCount = eventData.client.MessageCount + 1
|
||||
}
|
||||
20
core/chat/events/actionEvent.go
Normal file
20
core/chat/events/actionEvent.go
Normal file
@@ -0,0 +1,20 @@
|
||||
package events
|
||||
|
||||
type ActionEvent struct {
|
||||
Event
|
||||
MessageEvent
|
||||
}
|
||||
|
||||
// ActionEvent will return the object to send to all chat users.
|
||||
func (e *ActionEvent) GetBroadcastPayload() EventPayload {
|
||||
return EventPayload{
|
||||
"id": e.Id,
|
||||
"timestamp": e.Timestamp,
|
||||
"body": e.Body,
|
||||
"type": e.GetMessageType(),
|
||||
}
|
||||
}
|
||||
|
||||
func (e *ActionEvent) GetMessageType() EventType {
|
||||
return ChatActionSent
|
||||
}
|
||||
156
core/chat/events/events.go
Normal file
156
core/chat/events/events.go
Normal file
@@ -0,0 +1,156 @@
|
||||
package events
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"regexp"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/microcosm-cc/bluemonday"
|
||||
"github.com/teris-io/shortid"
|
||||
"github.com/yuin/goldmark"
|
||||
"github.com/yuin/goldmark/extension"
|
||||
"github.com/yuin/goldmark/renderer/html"
|
||||
"mvdan.cc/xurls"
|
||||
|
||||
"github.com/owncast/owncast/core/user"
|
||||
log "github.com/sirupsen/logrus"
|
||||
)
|
||||
|
||||
// EventPayload is a generic key/value map for sending out to chat clients.
|
||||
type EventPayload map[string]interface{}
|
||||
|
||||
type OutboundEvent interface {
|
||||
GetBroadcastPayload() EventPayload
|
||||
GetMessageType() EventType
|
||||
}
|
||||
|
||||
// Event is any kind of event. A type is required to be specified.
|
||||
type Event struct {
|
||||
Type EventType `json:"type"`
|
||||
Id string `json:"id"`
|
||||
Timestamp time.Time `json:"timestamp"`
|
||||
}
|
||||
|
||||
type UserEvent struct {
|
||||
User *user.User `json:"user"`
|
||||
HiddenAt *time.Time `json:"hiddenAt,omitempty"`
|
||||
}
|
||||
|
||||
// MessageEvent is an event that has a message body.
|
||||
type MessageEvent struct {
|
||||
OutboundEvent `json:"-"`
|
||||
Body string `json:"body"`
|
||||
RawBody string `json:"-"`
|
||||
}
|
||||
|
||||
type SystemActionEvent struct {
|
||||
Event
|
||||
MessageEvent
|
||||
}
|
||||
|
||||
// SetDefaults will set default properties of all inbound events.
|
||||
func (e *Event) SetDefaults() {
|
||||
e.Id = shortid.MustGenerate()
|
||||
e.Timestamp = time.Now()
|
||||
}
|
||||
|
||||
// SetDefaults will set default properties of all inbound events.
|
||||
func (e *UserMessageEvent) SetDefaults() {
|
||||
e.Id = shortid.MustGenerate()
|
||||
e.Timestamp = time.Now()
|
||||
e.RenderAndSanitizeMessageBody()
|
||||
}
|
||||
|
||||
// RenderAndSanitizeMessageBody will turn markdown into HTML, sanitize raw user-supplied HTML and standardize
|
||||
// the message into something safe and renderable for clients.
|
||||
func (m *MessageEvent) RenderAndSanitizeMessageBody() {
|
||||
m.RawBody = m.Body
|
||||
|
||||
// Set the new, sanitized and rendered message body
|
||||
m.Body = RenderAndSanitize(m.RawBody)
|
||||
}
|
||||
|
||||
// Empty will return if this message's contents is empty.
|
||||
func (m *MessageEvent) Empty() bool {
|
||||
return m.Body == ""
|
||||
}
|
||||
|
||||
// RenderBody will render markdown to html without any sanitization.
|
||||
func (m *MessageEvent) RenderBody() {
|
||||
m.RawBody = m.Body
|
||||
m.Body = RenderMarkdown(m.RawBody)
|
||||
}
|
||||
|
||||
// RenderAndSanitize will turn markdown into HTML, sanitize raw user-supplied HTML and standardize
|
||||
// the message into something safe and renderable for clients.
|
||||
func RenderAndSanitize(raw string) string {
|
||||
rendered := RenderMarkdown(raw)
|
||||
safe := sanitize(rendered)
|
||||
|
||||
// Set the new, sanitized and rendered message body
|
||||
return strings.TrimSpace(safe)
|
||||
}
|
||||
|
||||
func RenderMarkdown(raw string) string {
|
||||
markdown := goldmark.New(
|
||||
goldmark.WithRendererOptions(
|
||||
html.WithUnsafe(),
|
||||
),
|
||||
goldmark.WithExtensions(
|
||||
extension.NewLinkify(
|
||||
extension.WithLinkifyAllowedProtocols([][]byte{
|
||||
[]byte("http:"),
|
||||
[]byte("https:"),
|
||||
}),
|
||||
extension.WithLinkifyURLRegexp(
|
||||
xurls.Strict,
|
||||
),
|
||||
),
|
||||
),
|
||||
)
|
||||
|
||||
trimmed := strings.TrimSpace(raw)
|
||||
var buf bytes.Buffer
|
||||
if err := markdown.Convert([]byte(trimmed), &buf); err != nil {
|
||||
log.Debugln(err)
|
||||
}
|
||||
|
||||
return buf.String()
|
||||
}
|
||||
|
||||
func sanitize(raw string) string {
|
||||
p := bluemonday.StrictPolicy()
|
||||
|
||||
// Require URLs to be parseable by net/url.Parse
|
||||
p.AllowStandardURLs()
|
||||
p.RequireParseableURLs(true)
|
||||
|
||||
// Allow links
|
||||
p.AllowAttrs("href").OnElements("a")
|
||||
|
||||
// Force all URLs to have "noreferrer" in their rel attribute.
|
||||
p.RequireNoReferrerOnLinks(true)
|
||||
|
||||
// Links will get target="_blank" added to them.
|
||||
p.AddTargetBlankToFullyQualifiedLinks(true)
|
||||
|
||||
// Allow breaks
|
||||
p.AllowElements("br", "p")
|
||||
|
||||
// Allow img tags from the the local emoji directory only
|
||||
p.AllowAttrs("src", "alt", "class", "title").Matching(regexp.MustCompile(`(?i)/img/emoji`)).OnElements("img")
|
||||
p.AllowAttrs("class").OnElements("img")
|
||||
|
||||
// Allow bold
|
||||
p.AllowElements("strong")
|
||||
|
||||
// Allow emphasis
|
||||
p.AllowElements("em")
|
||||
|
||||
// Allow code blocks
|
||||
p.AllowElements("code")
|
||||
p.AllowElements("pre")
|
||||
|
||||
return p.Sanitize(raw)
|
||||
}
|
||||
34
core/chat/events/eventtype.go
Normal file
34
core/chat/events/eventtype.go
Normal file
@@ -0,0 +1,34 @@
|
||||
package events
|
||||
|
||||
// EventType is the type of a websocket event.
|
||||
type EventType = string
|
||||
|
||||
const (
|
||||
// MessageSent is the event sent when a chat event takes place.
|
||||
MessageSent EventType = "CHAT"
|
||||
// UserJoined is the event sent when a chat user join action takes place.
|
||||
UserJoined EventType = "USER_JOINED"
|
||||
// UserNameChanged is the event sent when a chat username change takes place.
|
||||
UserNameChanged EventType = "NAME_CHANGE"
|
||||
// VisibiltyToggled is the event sent when a chat message's visibility changes.
|
||||
VisibiltyToggled EventType = "VISIBILITY-UPDATE"
|
||||
// PING is a ping message.
|
||||
PING EventType = "PING"
|
||||
// PONG is a pong message.
|
||||
PONG EventType = "PONG"
|
||||
// StreamStarted represents a stream started event.
|
||||
StreamStarted EventType = "STREAM_STARTED"
|
||||
// StreamStopped represents a stream stopped event.
|
||||
StreamStopped EventType = "STREAM_STOPPED"
|
||||
// SystemMessageSent is the event sent when a system message is sent.
|
||||
SystemMessageSent EventType = "SYSTEM"
|
||||
// ChatDisabled is when a user is explicitly disabled and blocked from using chat.
|
||||
ChatDisabled EventType = "CHAT_DISABLED"
|
||||
// ConnectedUserInfo is a private event to a user letting them know their user details.
|
||||
ConnectedUserInfo EventType = "CONNECTED_USER_INFO"
|
||||
// ChatActionSent is a generic chat action that can be used for anything that doesn't need specific handling or formatting.
|
||||
ChatActionSent EventType = "CHAT_ACTION"
|
||||
ErrorNeedsRegistration EventType = "ERROR_NEEDS_REGISTRATION"
|
||||
ErrorMaxConnectionsExceeded EventType = "ERROR_MAX_CONNECTIONS_EXCEEDED"
|
||||
ErrorUserDisabled EventType = "ERROR_USER_DISABLED"
|
||||
)
|
||||
26
core/chat/events/nameChangeEvent.go
Normal file
26
core/chat/events/nameChangeEvent.go
Normal file
@@ -0,0 +1,26 @@
|
||||
package events
|
||||
|
||||
// NameChangeEvent is received when a user changes their chat display name.
|
||||
type NameChangeEvent struct {
|
||||
Event
|
||||
UserEvent
|
||||
NewName string `json:"newName"`
|
||||
}
|
||||
|
||||
// NameChangeEventBroadcast is fired when a user changes their chat display name.
|
||||
type NameChangeBroadcast struct {
|
||||
Event
|
||||
UserEvent
|
||||
Oldname string `json:"oldName"`
|
||||
}
|
||||
|
||||
// GetBroadcastPayload will return the object to send to all chat users.
|
||||
func (e *NameChangeBroadcast) GetBroadcastPayload() EventPayload {
|
||||
return EventPayload{
|
||||
"id": e.Id,
|
||||
"timestamp": e.Timestamp,
|
||||
"user": e.User,
|
||||
"oldName": e.Oldname,
|
||||
"type": UserNameChanged,
|
||||
}
|
||||
}
|
||||
26
core/chat/events/systemMessageEvent.go
Normal file
26
core/chat/events/systemMessageEvent.go
Normal file
@@ -0,0 +1,26 @@
|
||||
package events
|
||||
|
||||
import "github.com/owncast/owncast/core/data"
|
||||
|
||||
// SystemMessageEvent is a message displayed in chat on behalf of the server.
|
||||
type SystemMessageEvent struct {
|
||||
Event
|
||||
MessageEvent
|
||||
}
|
||||
|
||||
// SystemMessageEvent will return the object to send to all chat users.
|
||||
func (e *SystemMessageEvent) GetBroadcastPayload() EventPayload {
|
||||
return EventPayload{
|
||||
"id": e.Id,
|
||||
"timestamp": e.Timestamp,
|
||||
"body": e.Body,
|
||||
"type": SystemMessageSent,
|
||||
"user": EventPayload{
|
||||
"displayName": data.GetServerName(),
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
func (e *SystemMessageEvent) GetMessageType() EventType {
|
||||
return SystemMessageSent
|
||||
}
|
||||
17
core/chat/events/userDisabledEvent.go
Normal file
17
core/chat/events/userDisabledEvent.go
Normal file
@@ -0,0 +1,17 @@
|
||||
package events
|
||||
|
||||
// UserDisabledEvent is the event fired when a user is banned/blocked and disconnected from chat.
|
||||
type UserDisabledEvent struct {
|
||||
Event
|
||||
UserEvent
|
||||
}
|
||||
|
||||
// GetBroadcastPayload will return the object to send to all chat users.
|
||||
func (e *UserDisabledEvent) GetBroadcastPayload() EventPayload {
|
||||
return EventPayload{
|
||||
"type": ErrorUserDisabled,
|
||||
"id": e.Id,
|
||||
"timestamp": e.Timestamp,
|
||||
"user": e.User,
|
||||
}
|
||||
}
|
||||
17
core/chat/events/userJoinedEvent.go
Normal file
17
core/chat/events/userJoinedEvent.go
Normal file
@@ -0,0 +1,17 @@
|
||||
package events
|
||||
|
||||
// UserJoinedEvent is the event fired when a user joins chat.
|
||||
type UserJoinedEvent struct {
|
||||
Event
|
||||
UserEvent
|
||||
}
|
||||
|
||||
// GetBroadcastPayload will return the object to send to all chat users.
|
||||
func (e *UserJoinedEvent) GetBroadcastPayload() EventPayload {
|
||||
return EventPayload{
|
||||
"type": UserJoined,
|
||||
"id": e.Id,
|
||||
"timestamp": e.Timestamp,
|
||||
"user": e.User,
|
||||
}
|
||||
}
|
||||
24
core/chat/events/userMessageEvent.go
Normal file
24
core/chat/events/userMessageEvent.go
Normal file
@@ -0,0 +1,24 @@
|
||||
package events
|
||||
|
||||
// UserMessageEvent is an inbound message from a user.
|
||||
type UserMessageEvent struct {
|
||||
Event
|
||||
UserEvent
|
||||
MessageEvent
|
||||
}
|
||||
|
||||
// GetBroadcastPayload will return the object to send to all chat users.
|
||||
func (e *UserMessageEvent) GetBroadcastPayload() EventPayload {
|
||||
return EventPayload{
|
||||
"id": e.Id,
|
||||
"timestamp": e.Timestamp,
|
||||
"body": e.Body,
|
||||
"user": e.User,
|
||||
"type": MessageSent,
|
||||
"visible": e.HiddenAt == nil,
|
||||
}
|
||||
}
|
||||
|
||||
func (e *UserMessageEvent) GetMessageType() EventType {
|
||||
return MessageSent
|
||||
}
|
||||
@@ -3,7 +3,7 @@ package chat
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/owncast/owncast/models"
|
||||
"github.com/owncast/owncast/core/chat/events"
|
||||
)
|
||||
|
||||
// Test a bunch of arbitrary markup and markdown to make sure we get sanitized
|
||||
@@ -25,7 +25,7 @@ blah blah blah
|
||||
<p><a href="http://owncast.online" rel="nofollow noreferrer noopener" target="_blank">test link</a>
|
||||
<img class="emoji" src="/img/emoji/bananadance.gif"></p>`
|
||||
|
||||
result := models.RenderAndSanitize(messageContent)
|
||||
result := events.RenderAndSanitize(messageContent)
|
||||
if result != expected {
|
||||
t.Errorf("message rendering/sanitation does not match expected. Got\n%s, \n\n want:\n%s", result, expected)
|
||||
}
|
||||
@@ -35,7 +35,7 @@ blah blah blah
|
||||
func TestBlockRemoteImages(t *testing.T) {
|
||||
messageContent := `<img src="https://via.placeholder.com/350x150"> test `
|
||||
expected := `<p> test </p>`
|
||||
result := models.RenderAndSanitize(messageContent)
|
||||
result := events.RenderAndSanitize(messageContent)
|
||||
|
||||
if result != expected {
|
||||
t.Errorf("message rendering/sanitation does not match expected. Got\n%s, \n\n want:\n%s", result, expected)
|
||||
@@ -46,7 +46,7 @@ func TestBlockRemoteImages(t *testing.T) {
|
||||
func TestAllowEmojiImages(t *testing.T) {
|
||||
messageContent := `<img src="/img/emoji/beerparrot.gif"> test `
|
||||
expected := `<p><img src="/img/emoji/beerparrot.gif"> test <img src="/img/emoji/beerparrot.gif"></p>`
|
||||
result := models.RenderAndSanitize(messageContent)
|
||||
result := events.RenderAndSanitize(messageContent)
|
||||
|
||||
if result != expected {
|
||||
t.Errorf("message rendering/sanitation does not match expected. Got\n%s, \n\n want:\n%s", result, expected)
|
||||
@@ -57,7 +57,7 @@ func TestAllowEmojiImages(t *testing.T) {
|
||||
func TestAllowHTML(t *testing.T) {
|
||||
messageContent := `<img src="/img/emoji/beerparrot.gif"><ul><li>**test thing**</li></ul>`
|
||||
expected := "<p><img src=\"/img/emoji/beerparrot.gif\"><ul><li><strong>test thing</strong></li></ul></p>\n"
|
||||
result := models.RenderMarkdown(messageContent)
|
||||
result := events.RenderMarkdown(messageContent)
|
||||
|
||||
if result != expected {
|
||||
t.Errorf("message rendering does not match expected. Got\n%s, \n\n want:\n%s", result, expected)
|
||||
|
||||
@@ -1,8 +1,8 @@
|
||||
package chat
|
||||
|
||||
import (
|
||||
"github.com/owncast/owncast/core/chat/events"
|
||||
"github.com/owncast/owncast/core/webhooks"
|
||||
"github.com/owncast/owncast/models"
|
||||
log "github.com/sirupsen/logrus"
|
||||
)
|
||||
|
||||
@@ -22,8 +22,11 @@ func SetMessagesVisibility(messageIDs []string, visibility bool) error {
|
||||
log.Errorln(err)
|
||||
continue
|
||||
}
|
||||
message.MessageType = models.VisibiltyToggled
|
||||
_server.sendAll(message)
|
||||
payload := message.GetBroadcastPayload()
|
||||
payload["type"] = events.VisibiltyToggled
|
||||
if err := _server.Broadcast(payload); err != nil {
|
||||
log.Debugln(err)
|
||||
}
|
||||
|
||||
go webhooks.SendChatEvent(message)
|
||||
}
|
||||
|
||||
@@ -1,171 +1,309 @@
|
||||
package chat
|
||||
|
||||
import (
|
||||
"database/sql"
|
||||
"fmt"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
_ "github.com/mattn/go-sqlite3"
|
||||
"github.com/owncast/owncast/core/chat/events"
|
||||
"github.com/owncast/owncast/core/data"
|
||||
"github.com/owncast/owncast/core/user"
|
||||
"github.com/owncast/owncast/models"
|
||||
log "github.com/sirupsen/logrus"
|
||||
)
|
||||
|
||||
var _db *sql.DB
|
||||
var _datastore *data.Datastore
|
||||
|
||||
const (
|
||||
maxBacklogHours = 5 // Keep backlog max hours worth of messages
|
||||
maxBacklogNumber = 50 // Return max number of messages in history request
|
||||
)
|
||||
|
||||
func setupPersistence() {
|
||||
_db = data.GetDatabase()
|
||||
createTable()
|
||||
_datastore = data.GetDatastore()
|
||||
createMessagesTable()
|
||||
|
||||
chatDataPruner := time.NewTicker(5 * time.Minute)
|
||||
go func() {
|
||||
runPruner()
|
||||
for range chatDataPruner.C {
|
||||
runPruner()
|
||||
}
|
||||
}()
|
||||
}
|
||||
|
||||
func createTable() {
|
||||
func createMessagesTable() {
|
||||
createTableSQL := `CREATE TABLE IF NOT EXISTS messages (
|
||||
"id" string NOT NULL PRIMARY KEY,
|
||||
"author" TEXT,
|
||||
"user_id" INTEGER,
|
||||
"body" TEXT,
|
||||
"messageType" TEXT,
|
||||
"visible" INTEGER,
|
||||
"timestamp" DATE
|
||||
"eventType" TEXT,
|
||||
"hidden_at" DATETIME,
|
||||
"timestamp" DATETIME
|
||||
);`
|
||||
|
||||
stmt, err := _db.Prepare(createTableSQL)
|
||||
stmt, err := _datastore.DB.Prepare(createTableSQL)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
log.Fatal("error creating chat messages table", err)
|
||||
}
|
||||
defer stmt.Close()
|
||||
if _, err := stmt.Exec(); err != nil {
|
||||
log.Warnln(err)
|
||||
log.Fatal("error creating chat messages table", err)
|
||||
}
|
||||
}
|
||||
|
||||
func addMessage(message models.ChatEvent) {
|
||||
tx, err := _db.Begin()
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
stmt, err := tx.Prepare("INSERT INTO messages(id, author, body, messageType, visible, timestamp) values(?, ?, ?, ?, ?, ?)")
|
||||
func SaveUserMessage(event events.UserMessageEvent) {
|
||||
saveEvent(event.Id, event.User.Id, event.Body, event.Type, event.HiddenAt, event.Timestamp)
|
||||
}
|
||||
|
||||
func saveEvent(id string, userId string, body string, eventType string, hidden *time.Time, timestamp time.Time) {
|
||||
_datastore.DbLock.Lock()
|
||||
defer _datastore.DbLock.Unlock()
|
||||
|
||||
tx, err := _datastore.DB.Begin()
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
log.Errorln("error saving", eventType, err)
|
||||
return
|
||||
}
|
||||
|
||||
defer tx.Rollback() // nolint
|
||||
|
||||
stmt, err := tx.Prepare("INSERT INTO messages(id, user_id, body, eventType, hidden_at, timestamp) values(?, ?, ?, ?, ?, ?)")
|
||||
if err != nil {
|
||||
log.Errorln("error saving", eventType, err)
|
||||
return
|
||||
}
|
||||
|
||||
defer stmt.Close()
|
||||
|
||||
if _, err := stmt.Exec(message.ID, message.Author, message.Body, message.MessageType, 1, message.Timestamp); err != nil {
|
||||
log.Fatal(err)
|
||||
if _, err = stmt.Exec(id, userId, body, eventType, hidden, timestamp); err != nil {
|
||||
log.Errorln("error saving", eventType, err)
|
||||
return
|
||||
}
|
||||
if err := tx.Commit(); err != nil {
|
||||
log.Fatal(err)
|
||||
if err = tx.Commit(); err != nil {
|
||||
log.Errorln("error saving", eventType, err)
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
func getChat(query string) []models.ChatEvent {
|
||||
history := make([]models.ChatEvent, 0)
|
||||
rows, err := _db.Query(query)
|
||||
func getChat(query string) []events.UserMessageEvent {
|
||||
history := make([]events.UserMessageEvent, 0)
|
||||
rows, err := _datastore.DB.Query(query)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
log.Errorln("error fetching chat history", err)
|
||||
return history
|
||||
}
|
||||
defer rows.Close()
|
||||
|
||||
for rows.Next() {
|
||||
var id string
|
||||
var author string
|
||||
var userId string
|
||||
var body string
|
||||
var messageType models.EventType
|
||||
var visible int
|
||||
var hiddenAt *time.Time
|
||||
var timestamp time.Time
|
||||
|
||||
err = rows.Scan(&id, &author, &body, &messageType, &visible, ×tamp)
|
||||
var userDisplayName *string
|
||||
var userDisplayColor *int
|
||||
var userCreatedAt *time.Time
|
||||
var userDisabledAt *time.Time
|
||||
var previousUsernames *string
|
||||
var userNameChangedAt *time.Time
|
||||
|
||||
// Convert a database row into a chat event
|
||||
err = rows.Scan(&id, &userId, &body, &messageType, &hiddenAt, ×tamp, &userDisplayName, &userDisplayColor, &userCreatedAt, &userDisabledAt, &previousUsernames, &userNameChangedAt)
|
||||
if err != nil {
|
||||
log.Debugln(err)
|
||||
log.Error("There is a problem with the chat database. Restore a backup of owncast.db or remove it and start over.")
|
||||
log.Errorln("There is a problem converting query to chat objects. Please report this:", query)
|
||||
break
|
||||
}
|
||||
|
||||
message := models.ChatEvent{}
|
||||
message.ID = id
|
||||
message.Author = author
|
||||
message.Body = body
|
||||
message.MessageType = messageType
|
||||
message.Visible = visible == 1
|
||||
message.Timestamp = timestamp
|
||||
// System messages and chat actions are special and are not from real users
|
||||
if messageType == events.SystemMessageSent || messageType == events.ChatActionSent {
|
||||
name := "Owncast"
|
||||
userDisplayName = &name
|
||||
color := 200
|
||||
userDisplayColor = &color
|
||||
}
|
||||
|
||||
if previousUsernames == nil {
|
||||
previousUsernames = userDisplayName
|
||||
}
|
||||
|
||||
if userCreatedAt == nil {
|
||||
now := time.Now()
|
||||
userCreatedAt = &now
|
||||
}
|
||||
|
||||
user := user.User{
|
||||
Id: userId,
|
||||
AccessToken: "",
|
||||
DisplayName: *userDisplayName,
|
||||
DisplayColor: *userDisplayColor,
|
||||
CreatedAt: *userCreatedAt,
|
||||
DisabledAt: userDisabledAt,
|
||||
NameChangedAt: userNameChangedAt,
|
||||
PreviousNames: strings.Split(*previousUsernames, ","),
|
||||
}
|
||||
|
||||
message := events.UserMessageEvent{
|
||||
Event: events.Event{
|
||||
Type: messageType,
|
||||
Id: id,
|
||||
Timestamp: timestamp,
|
||||
},
|
||||
UserEvent: events.UserEvent{
|
||||
User: &user,
|
||||
HiddenAt: hiddenAt,
|
||||
},
|
||||
MessageEvent: events.MessageEvent{
|
||||
Body: body,
|
||||
RawBody: body,
|
||||
},
|
||||
}
|
||||
|
||||
history = append(history, message)
|
||||
}
|
||||
|
||||
if err := rows.Err(); err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
||||
return history
|
||||
}
|
||||
|
||||
func getChatModerationHistory() []models.ChatEvent {
|
||||
var query = "SELECT * FROM messages WHERE messageType == 'CHAT' AND datetime(timestamp) >=datetime('now', '-5 Hour')"
|
||||
func GetChatModerationHistory() []events.UserMessageEvent {
|
||||
// Get all messages regardless of visibility
|
||||
var query = "SELECT messages.id, user_id, body, eventType, hidden_at, timestamp, display_name, display_color, created_at, disabled_at, previous_names, namechanged_at FROM messages INNER JOIN users ON messages.user_id = users.id ORDER BY timestamp DESC"
|
||||
return getChat(query)
|
||||
}
|
||||
|
||||
func getChatHistory() []models.ChatEvent {
|
||||
// Get all messages sent within the past 5hrs, max 50
|
||||
var query = "SELECT * FROM (SELECT * FROM messages WHERE datetime(timestamp) >=datetime('now', '-5 Hour') AND visible = 1 ORDER BY timestamp DESC LIMIT 50) ORDER BY timestamp asc"
|
||||
func GetChatHistory() []events.UserMessageEvent {
|
||||
// Get all visible messages
|
||||
var query = fmt.Sprintf("SELECT id, user_id, body, eventType, hidden_at, timestamp, display_name, display_color, created_at, disabled_at, previous_names, namechanged_at FROM (SELECT * FROM messages LEFT OUTER JOIN users ON messages.user_id = users.id WHERE hidden_at IS NULL ORDER BY timestamp DESC LIMIT %d) ORDER BY timestamp asc", maxBacklogNumber)
|
||||
return getChat(query)
|
||||
}
|
||||
|
||||
// SetMessageVisibilityForUserId will bulk change the visibility of messages for a user
|
||||
// and then send out visibility changed events to chat clients.
|
||||
func SetMessageVisibilityForUserId(userID string, visible bool) error {
|
||||
// Get a list of IDs from this user within the 5hr window to send to the connected clients to hide
|
||||
ids := make([]string, 0)
|
||||
query := fmt.Sprintf("SELECT messages.id, user_id, body, eventType, hidden_at, timestamp, display_name, display_color, created_at, disabled_at, previous_names, namechanged_at FROM messages INNER JOIN users ON messages.user_id = users.id WHERE user_id IS '%s'", userID)
|
||||
messages := getChat(query)
|
||||
|
||||
if len(messages) == 0 {
|
||||
return nil
|
||||
}
|
||||
|
||||
for _, message := range messages {
|
||||
ids = append(ids, message.Id)
|
||||
}
|
||||
|
||||
// Tell the clients to hide/show these messages.
|
||||
return SetMessagesVisibility(ids, visible)
|
||||
}
|
||||
|
||||
func saveMessageVisibility(messageIDs []string, visible bool) error {
|
||||
tx, err := _db.Begin()
|
||||
_datastore.DbLock.Lock()
|
||||
defer _datastore.DbLock.Unlock()
|
||||
|
||||
tx, err := _datastore.DB.Begin()
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
return err
|
||||
}
|
||||
|
||||
stmt, err := tx.Prepare("UPDATE messages SET visible=? WHERE id IN (?" + strings.Repeat(",?", len(messageIDs)-1) + ")")
|
||||
stmt, err := tx.Prepare("UPDATE messages SET hidden_at=? WHERE id IN (?" + strings.Repeat(",?", len(messageIDs)-1) + ")")
|
||||
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
return err
|
||||
}
|
||||
defer stmt.Close()
|
||||
|
||||
var hiddenAt *time.Time
|
||||
if !visible {
|
||||
now := time.Now()
|
||||
hiddenAt = &now
|
||||
} else {
|
||||
hiddenAt = nil
|
||||
}
|
||||
|
||||
args := make([]interface{}, len(messageIDs)+1)
|
||||
args[0] = visible
|
||||
args[0] = hiddenAt
|
||||
for i, id := range messageIDs {
|
||||
args[i+1] = id
|
||||
}
|
||||
|
||||
if _, err := stmt.Exec(args...); err != nil {
|
||||
log.Fatal(err)
|
||||
if _, err = stmt.Exec(args...); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if err := tx.Commit(); err != nil {
|
||||
log.Fatal(err)
|
||||
if err = tx.Commit(); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func getMessageById(messageID string) (models.ChatEvent, error) {
|
||||
func getMessageById(messageID string) (*events.UserMessageEvent, error) {
|
||||
var query = "SELECT * FROM messages WHERE id = ?"
|
||||
row := _db.QueryRow(query, messageID)
|
||||
row := _datastore.DB.QueryRow(query, messageID)
|
||||
|
||||
var id string
|
||||
var author string
|
||||
var userId string
|
||||
var body string
|
||||
var messageType models.EventType
|
||||
var visible int
|
||||
var eventType models.EventType
|
||||
var hiddenAt *time.Time
|
||||
var timestamp time.Time
|
||||
|
||||
err := row.Scan(&id, &author, &body, &messageType, &visible, ×tamp)
|
||||
err := row.Scan(&id, &userId, &body, &eventType, &hiddenAt, ×tamp)
|
||||
if err != nil {
|
||||
log.Errorln(err)
|
||||
return models.ChatEvent{}, err
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return models.ChatEvent{
|
||||
ID: id,
|
||||
Author: author,
|
||||
Body: body,
|
||||
MessageType: messageType,
|
||||
Visible: visible == 1,
|
||||
Timestamp: timestamp,
|
||||
user := user.GetUserById(userId)
|
||||
|
||||
return &events.UserMessageEvent{
|
||||
events.Event{
|
||||
Type: eventType,
|
||||
Id: id,
|
||||
Timestamp: timestamp,
|
||||
},
|
||||
events.UserEvent{
|
||||
User: user,
|
||||
HiddenAt: hiddenAt,
|
||||
},
|
||||
events.MessageEvent{
|
||||
Body: body,
|
||||
},
|
||||
}, nil
|
||||
}
|
||||
|
||||
// Only keep recent messages so we don't keep more chat data than needed
|
||||
// for privacy and efficiency reasons.
|
||||
func runPruner() {
|
||||
_datastore.DbLock.Lock()
|
||||
defer _datastore.DbLock.Unlock()
|
||||
|
||||
log.Traceln("Removing chat messages older than", maxBacklogHours, "hours")
|
||||
|
||||
deleteStatement := `DELETE FROM messages WHERE timestamp <= datetime('now', 'localtime', ?)`
|
||||
tx, err := _datastore.DB.Begin()
|
||||
if err != nil {
|
||||
log.Debugln(err)
|
||||
return
|
||||
}
|
||||
|
||||
stmt, err := tx.Prepare(deleteStatement)
|
||||
if err != nil {
|
||||
log.Debugln(err)
|
||||
return
|
||||
}
|
||||
defer stmt.Close()
|
||||
|
||||
if _, err = stmt.Exec(fmt.Sprintf("-%d hours", maxBacklogHours)); err != nil {
|
||||
log.Debugln(err)
|
||||
return
|
||||
}
|
||||
if err = tx.Commit(); err != nil {
|
||||
log.Debugln(err)
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,191 +1,317 @@
|
||||
package chat
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"encoding/json"
|
||||
"net/http"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
log "github.com/sirupsen/logrus"
|
||||
"golang.org/x/net/websocket"
|
||||
|
||||
"github.com/gorilla/websocket"
|
||||
|
||||
"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/models"
|
||||
"github.com/owncast/owncast/utils"
|
||||
)
|
||||
|
||||
var (
|
||||
_server *server
|
||||
)
|
||||
var _server *ChatServer
|
||||
|
||||
var l = &sync.RWMutex{}
|
||||
type ChatServer struct {
|
||||
mu sync.RWMutex
|
||||
seq uint
|
||||
clients map[uint]*ChatClient
|
||||
maxClientCount uint
|
||||
|
||||
// Server represents the server which handles the chat.
|
||||
type server struct {
|
||||
Clients map[string]*Client
|
||||
// send outbound message payload to all clients
|
||||
outbound chan []byte
|
||||
|
||||
pattern string
|
||||
listener models.ChatListener
|
||||
// receive inbound message payload from all clients
|
||||
inbound chan chatClientEvent
|
||||
|
||||
addCh chan *Client
|
||||
delCh chan *Client
|
||||
sendAllCh chan models.ChatEvent
|
||||
pingCh chan models.PingMessage
|
||||
doneCh chan bool
|
||||
errCh chan error
|
||||
// unregister requests from clients.
|
||||
unregister chan *ChatClient
|
||||
}
|
||||
|
||||
// Add adds a client to the server.
|
||||
func (s *server) add(c *Client) {
|
||||
s.addCh <- c
|
||||
}
|
||||
|
||||
// Remove removes a client from the server.
|
||||
func (s *server) remove(c *Client) {
|
||||
s.delCh <- c
|
||||
}
|
||||
|
||||
// SendToAll sends a message to all of the connected clients.
|
||||
func (s *server) SendToAll(msg models.ChatEvent) {
|
||||
s.sendAllCh <- msg
|
||||
}
|
||||
|
||||
// Err handles an error.
|
||||
func (s *server) err(err error) {
|
||||
s.errCh <- err
|
||||
}
|
||||
|
||||
func (s *server) sendAll(msg models.ChatEvent) {
|
||||
l.RLock()
|
||||
for _, c := range s.Clients {
|
||||
c.write(msg)
|
||||
func NewChat() *ChatServer {
|
||||
server := &ChatServer{
|
||||
clients: map[uint]*ChatClient{},
|
||||
outbound: make(chan []byte),
|
||||
inbound: make(chan chatClientEvent),
|
||||
unregister: make(chan *ChatClient),
|
||||
maxClientCount: handleMaxConnectionCount(),
|
||||
}
|
||||
l.RUnlock()
|
||||
|
||||
return server
|
||||
}
|
||||
|
||||
func (s *server) ping() {
|
||||
ping := models.PingMessage{MessageType: models.PING}
|
||||
|
||||
l.RLock()
|
||||
for _, c := range s.Clients {
|
||||
c.pingch <- ping
|
||||
}
|
||||
l.RUnlock()
|
||||
}
|
||||
|
||||
func (s *server) usernameChanged(msg models.NameChangeEvent) {
|
||||
l.RLock()
|
||||
for _, c := range s.Clients {
|
||||
c.usernameChangeChannel <- msg
|
||||
}
|
||||
l.RUnlock()
|
||||
|
||||
go webhooks.SendChatEventUsernameChanged(msg)
|
||||
}
|
||||
|
||||
func (s *server) userJoined(msg models.UserJoinedEvent) {
|
||||
l.RLock()
|
||||
if s.listener.IsStreamConnected() {
|
||||
for _, c := range s.Clients {
|
||||
c.userJoinedChannel <- msg
|
||||
}
|
||||
}
|
||||
l.RUnlock()
|
||||
|
||||
go webhooks.SendChatEventUserJoined(msg)
|
||||
}
|
||||
|
||||
func (s *server) onConnection(ws *websocket.Conn) {
|
||||
client := NewClient(ws)
|
||||
|
||||
defer func() {
|
||||
s.removeClient(client)
|
||||
|
||||
if err := ws.Close(); err != nil {
|
||||
log.Debugln(err)
|
||||
//s.errCh <- err
|
||||
}
|
||||
}()
|
||||
|
||||
s.add(client)
|
||||
client.listen()
|
||||
}
|
||||
|
||||
// Listen and serve.
|
||||
// It serves client connection and broadcast request.
|
||||
func (s *server) Listen() {
|
||||
http.Handle(s.pattern, websocket.Handler(s.onConnection))
|
||||
|
||||
log.Tracef("Starting the websocket listener on: %s", s.pattern)
|
||||
|
||||
func (s *ChatServer) Run() {
|
||||
for {
|
||||
select {
|
||||
// add new a client
|
||||
case c := <-s.addCh:
|
||||
l.Lock()
|
||||
s.Clients[c.socketID] = c
|
||||
|
||||
if !c.Ignore {
|
||||
s.listener.ClientAdded(c.GetViewerClientFromChatClient())
|
||||
s.sendWelcomeMessageToClient(c)
|
||||
}
|
||||
l.Unlock()
|
||||
|
||||
// remove a client
|
||||
case c := <-s.delCh:
|
||||
s.removeClient(c)
|
||||
case msg := <-s.sendAllCh:
|
||||
if data.GetChatDisabled() {
|
||||
break
|
||||
case client := <-s.unregister:
|
||||
if _, ok := s.clients[client.id]; ok {
|
||||
s.mu.Lock()
|
||||
delete(s.clients, client.id)
|
||||
close(client.send)
|
||||
s.mu.Unlock()
|
||||
}
|
||||
|
||||
if !msg.Empty() {
|
||||
// set defaults before sending msg to anywhere
|
||||
msg.SetDefaults()
|
||||
|
||||
s.listener.MessageSent(msg)
|
||||
s.sendAll(msg)
|
||||
|
||||
// Store in the message history
|
||||
if !msg.Ephemeral {
|
||||
addMessage(msg)
|
||||
}
|
||||
|
||||
// Send webhooks
|
||||
go webhooks.SendChatEvent(msg)
|
||||
}
|
||||
case ping := <-s.pingCh:
|
||||
fmt.Println("PING?", ping)
|
||||
|
||||
case err := <-s.errCh:
|
||||
log.Trace("Error: ", err.Error())
|
||||
|
||||
case <-s.doneCh:
|
||||
return
|
||||
case message := <-s.inbound:
|
||||
s.eventReceived(message)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func (s *server) removeClient(c *Client) {
|
||||
l.Lock()
|
||||
if _, ok := s.Clients[c.socketID]; ok {
|
||||
delete(s.Clients, c.socketID)
|
||||
|
||||
s.listener.ClientRemoved(c.socketID)
|
||||
log.Tracef("The client was connected for %s and sent %d messages (%s)", time.Since(c.ConnectedAt), c.MessageCount, c.ClientID)
|
||||
// Addclient registers new connection as a User.
|
||||
func (s *ChatServer) Addclient(conn *websocket.Conn, user *user.User, accessToken string, userAgent string) *ChatClient {
|
||||
client := &ChatClient{
|
||||
server: s,
|
||||
conn: conn,
|
||||
User: user,
|
||||
ipAddress: conn.RemoteAddr().String(),
|
||||
accessToken: accessToken,
|
||||
send: make(chan []byte, 256),
|
||||
UserAgent: userAgent,
|
||||
ConnectedAt: time.Now(),
|
||||
}
|
||||
l.Unlock()
|
||||
|
||||
s.mu.Lock()
|
||||
{
|
||||
client.id = s.seq
|
||||
s.clients[client.id] = client
|
||||
s.seq++
|
||||
}
|
||||
s.mu.Unlock()
|
||||
|
||||
log.Traceln("Adding client", client.id, "total count:", len(s.clients))
|
||||
|
||||
go client.writePump()
|
||||
go client.readPump()
|
||||
|
||||
client.sendConnectedClientInfo()
|
||||
|
||||
if getStatus().Online {
|
||||
s.sendUserJoinedMessage(client)
|
||||
s.sendWelcomeMessageToClient(client)
|
||||
}
|
||||
|
||||
return client
|
||||
}
|
||||
|
||||
func (s *server) sendWelcomeMessageToClient(c *Client) {
|
||||
go func() {
|
||||
// Add an artificial delay so people notice this message come in.
|
||||
time.Sleep(7 * time.Second)
|
||||
func (s *ChatServer) sendUserJoinedMessage(c *ChatClient) {
|
||||
userJoinedEvent := events.UserJoinedEvent{}
|
||||
userJoinedEvent.SetDefaults()
|
||||
userJoinedEvent.User = c.User
|
||||
|
||||
welcomeMessage := data.GetServerWelcomeMessage()
|
||||
if welcomeMessage != "" {
|
||||
initialMessage := models.ChatEvent{ClientID: "owncast-server", Author: data.GetServerName(), Body: welcomeMessage, ID: "initial-message-1", MessageType: "SYSTEM", Visible: true, Timestamp: time.Now()}
|
||||
c.write(initialMessage)
|
||||
if err := s.Broadcast(userJoinedEvent.GetBroadcastPayload()); err != nil {
|
||||
log.Errorln("error adding client to chat server", err)
|
||||
}
|
||||
|
||||
// Send chat user joined webhook
|
||||
webhooks.SendChatEventUserJoined(userJoinedEvent)
|
||||
}
|
||||
|
||||
func (s *ChatServer) ClientClosed(c *ChatClient) {
|
||||
s.mu.Lock()
|
||||
defer s.mu.Unlock()
|
||||
c.close()
|
||||
|
||||
if _, ok := s.clients[c.id]; ok {
|
||||
log.Debugln("Deleting", c.id)
|
||||
delete(s.clients, c.id)
|
||||
}
|
||||
}
|
||||
|
||||
func (s *ChatServer) HandleClientConnection(w http.ResponseWriter, r *http.Request) {
|
||||
if data.GetChatDisabled() {
|
||||
_, _ = w.Write([]byte(events.ChatDisabled))
|
||||
return
|
||||
}
|
||||
|
||||
// Limit concurrent chat connections
|
||||
if uint(len(s.clients)) >= s.maxClientCount {
|
||||
log.Warnln("rejecting incoming client connection as it exceeds the max client count of", s.maxClientCount)
|
||||
_, _ = w.Write([]byte(events.ErrorMaxConnectionsExceeded))
|
||||
return
|
||||
}
|
||||
|
||||
conn, err := upgrader.Upgrade(w, r, nil)
|
||||
if err != nil {
|
||||
log.Debugln(err)
|
||||
return
|
||||
}
|
||||
|
||||
accessToken := r.URL.Query().Get("accessToken")
|
||||
if accessToken == "" {
|
||||
log.Errorln("Access token is required")
|
||||
// Return HTTP status code
|
||||
conn.Close()
|
||||
return
|
||||
}
|
||||
|
||||
// A user is required to use the websocket
|
||||
user := user.GetUserByToken(accessToken)
|
||||
if user == nil {
|
||||
_ = conn.WriteJSON(events.EventPayload{
|
||||
"type": events.ErrorNeedsRegistration,
|
||||
})
|
||||
// Send error that registration is required
|
||||
conn.Close()
|
||||
return
|
||||
}
|
||||
|
||||
// User is disabled therefore we should disconnect.
|
||||
if user.DisabledAt != nil {
|
||||
log.Traceln("Disabled user", user.Id, user.DisplayName, "rejected")
|
||||
_ = conn.WriteJSON(events.EventPayload{
|
||||
"type": events.ErrorUserDisabled,
|
||||
})
|
||||
conn.Close()
|
||||
return
|
||||
}
|
||||
|
||||
userAgent := r.UserAgent()
|
||||
|
||||
s.Addclient(conn, user, accessToken, userAgent)
|
||||
}
|
||||
|
||||
// Broadcast sends message to all connected clients.
|
||||
func (s *ChatServer) Broadcast(payload events.EventPayload) error {
|
||||
data, err := json.Marshal(payload)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
s.mu.Lock()
|
||||
defer s.mu.Unlock()
|
||||
|
||||
for _, client := range s.clients {
|
||||
if client == nil {
|
||||
continue
|
||||
}
|
||||
}()
|
||||
|
||||
select {
|
||||
case client.send <- data:
|
||||
default:
|
||||
close(client.send)
|
||||
delete(s.clients, client.id)
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s *ChatServer) Send(payload events.EventPayload, client *ChatClient) {
|
||||
data, err := json.Marshal(payload)
|
||||
if err != nil {
|
||||
log.Errorln(err)
|
||||
return
|
||||
}
|
||||
|
||||
client.send <- data
|
||||
}
|
||||
|
||||
// DisconnectUser will forcefully disconnect all clients belonging to a user by ID.
|
||||
func (s *ChatServer) DisconnectUser(userID string) {
|
||||
s.mu.Lock()
|
||||
clients, err := GetClientsForUser(userID)
|
||||
s.mu.Unlock()
|
||||
|
||||
if err != nil || clients == nil || len(clients) == 0 {
|
||||
log.Debugln("Requested to disconnect user", userID, err)
|
||||
return
|
||||
}
|
||||
|
||||
for _, client := range clients {
|
||||
log.Traceln("Disconnecting client", client.User.Id, "owned by", client.User.DisplayName)
|
||||
|
||||
go func(client *ChatClient) {
|
||||
event := events.UserDisabledEvent{}
|
||||
event.SetDefaults()
|
||||
|
||||
// Send this disabled event specifically to this single connected client
|
||||
// to let them know they've been banned.
|
||||
_server.Send(event.GetBroadcastPayload(), client)
|
||||
|
||||
// Give the socket time to send out the above message.
|
||||
// Unfortunately I don't know of any way to get a real callback to know when
|
||||
// the message was successfully sent, so give it a couple seconds.
|
||||
time.Sleep(2 * time.Second)
|
||||
|
||||
// Forcefully disconnect if still valid.
|
||||
if client != nil {
|
||||
client.close()
|
||||
}
|
||||
}(client)
|
||||
}
|
||||
}
|
||||
|
||||
func (s *ChatServer) eventReceived(event chatClientEvent) {
|
||||
var typecheck map[string]interface{}
|
||||
if err := json.Unmarshal(event.data, &typecheck); err != nil {
|
||||
log.Debugln(err)
|
||||
}
|
||||
|
||||
eventType := typecheck["type"]
|
||||
|
||||
switch eventType {
|
||||
case events.MessageSent:
|
||||
s.userMessageSent(event)
|
||||
|
||||
case events.UserNameChanged:
|
||||
s.userNameChanged(event)
|
||||
|
||||
default:
|
||||
log.Debugln(eventType, "event not found:", typecheck)
|
||||
}
|
||||
}
|
||||
|
||||
func (s *ChatServer) sendWelcomeMessageToClient(c *ChatClient) {
|
||||
// Add an artificial delay so people notice this message come in.
|
||||
time.Sleep(7 * time.Second)
|
||||
|
||||
welcomeMessage := utils.RenderSimpleMarkdown(data.GetServerWelcomeMessage())
|
||||
|
||||
if welcomeMessage != "" {
|
||||
s.sendSystemMessageToClient(c, welcomeMessage)
|
||||
}
|
||||
}
|
||||
|
||||
func (s *ChatServer) sendAllWelcomeMessage() {
|
||||
welcomeMessage := utils.RenderSimpleMarkdown(data.GetServerWelcomeMessage())
|
||||
|
||||
if welcomeMessage != "" {
|
||||
clientMessage := events.SystemMessageEvent{
|
||||
Event: events.Event{},
|
||||
MessageEvent: events.MessageEvent{
|
||||
Body: welcomeMessage,
|
||||
},
|
||||
}
|
||||
clientMessage.SetDefaults()
|
||||
_ = s.Broadcast(clientMessage.GetBroadcastPayload())
|
||||
}
|
||||
}
|
||||
|
||||
func (s *ChatServer) sendSystemMessageToClient(c *ChatClient, message string) {
|
||||
clientMessage := events.SystemMessageEvent{
|
||||
Event: events.Event{},
|
||||
MessageEvent: events.MessageEvent{
|
||||
Body: message,
|
||||
},
|
||||
}
|
||||
clientMessage.SetDefaults()
|
||||
s.Send(clientMessage.GetBroadcastPayload(), c)
|
||||
}
|
||||
|
||||
func (s *ChatServer) sendActionToClient(c *ChatClient, message string) {
|
||||
clientMessage := events.ActionEvent{
|
||||
MessageEvent: events.MessageEvent{
|
||||
Body: message,
|
||||
},
|
||||
}
|
||||
clientMessage.SetDefaults()
|
||||
clientMessage.RenderBody()
|
||||
s.Send(clientMessage.GetBroadcastPayload(), c)
|
||||
}
|
||||
|
||||
29
core/chat/utils.go
Normal file
29
core/chat/utils.go
Normal file
@@ -0,0 +1,29 @@
|
||||
package chat
|
||||
|
||||
import (
|
||||
"syscall"
|
||||
|
||||
log "github.com/sirupsen/logrus"
|
||||
)
|
||||
|
||||
// Set the soft file handler limit as 70% of
|
||||
// the max as the client connection limit.
|
||||
func handleMaxConnectionCount() uint {
|
||||
var rLimit syscall.Rlimit
|
||||
if err := syscall.Getrlimit(syscall.RLIMIT_NOFILE, &rLimit); err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
originalLimit := rLimit.Cur
|
||||
// Set the limit to 70% of max so the machine doesn't die even if it's maxed out for some reason.
|
||||
proposedLimit := int(float32(rLimit.Max) * 0.7)
|
||||
|
||||
rLimit.Cur = uint64(proposedLimit)
|
||||
if err := syscall.Setrlimit(syscall.RLIMIT_NOFILE, &rLimit); err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
log.Traceln("Max process connection count increased from", originalLimit, "to", proposedLimit)
|
||||
|
||||
return uint(float32(rLimit.Cur))
|
||||
}
|
||||
Reference in New Issue
Block a user