Update chat message visibility for moderation (#524)
* update message viz in db * create admin endpoint to update message visibility * convert UpdateMessageVisibility api to take in an array of IDs to change visibility on instead * Support requesting filtered or unfiltered chat messages * Handle UPDATE chat events on front and backend for toggling messages * Return entire message with UPDATE events * Remove the UPDATE message type * Revert "Remove the UPDATE message type" This reverts commit 3a83df3d492f7ecf2bab65e845aa2b0365d3a7f6. * update -> visibility update * completely remove messages when they turn hidden on VISIBILITY-UPDATEs, and insert them if they turn visible * Explicitly set visibility * Fix multi-id sql updates * increate scroll buffer a bit so chat scrolls when new large messages come in * Add automated test around chat moderation * Add new chat admin APIs to api spec * Commit updated API documentation Co-authored-by: Gabe Kangas <gabek@real-ity.com> Co-authored-by: Owncast <owncast@owncast.online>
This commit is contained in:
co-authored by
Gabe Kangas
Owncast
parent
0452c4c5fc
commit
8a74af202d
@@ -2,6 +2,7 @@ package chat
|
||||
|
||||
import (
|
||||
"database/sql"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
_ "github.com/mattn/go-sqlite3"
|
||||
@@ -38,7 +39,7 @@ func createTable() {
|
||||
}
|
||||
}
|
||||
|
||||
func addMessage(message models.ChatMessage) {
|
||||
func addMessage(message models.ChatEvent) {
|
||||
tx, err := _db.Begin()
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
@@ -60,11 +61,16 @@ func addMessage(message models.ChatMessage) {
|
||||
}
|
||||
}
|
||||
|
||||
func getChatHistory() []models.ChatMessage {
|
||||
history := make([]models.ChatMessage, 0)
|
||||
func getChatHistory(filtered bool) []models.ChatEvent {
|
||||
history := make([]models.ChatEvent, 0)
|
||||
|
||||
// Get all messages sent within the past day
|
||||
rows, err := _db.Query("SELECT * FROM messages WHERE visible = 1 AND messageType != 'SYSTEM' AND datetime(timestamp) >=datetime('now', '-1 Day')")
|
||||
var query = "SELECT * FROM messages WHERE messageType != 'SYSTEM' AND datetime(timestamp) >=datetime('now', '-1 Day')"
|
||||
if filtered {
|
||||
query = query + " AND visible = 1"
|
||||
}
|
||||
|
||||
rows, err := _db.Query(query)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
@@ -85,7 +91,7 @@ func getChatHistory() []models.ChatMessage {
|
||||
break
|
||||
}
|
||||
|
||||
message := models.ChatMessage{}
|
||||
message := models.ChatEvent{}
|
||||
message.ID = id
|
||||
message.Author = author
|
||||
message.Body = body
|
||||
@@ -102,3 +108,64 @@ func getChatHistory() []models.ChatMessage {
|
||||
|
||||
return history
|
||||
}
|
||||
|
||||
func saveMessageVisibility(messageIDs []string, visible bool) error {
|
||||
tx, err := _db.Begin()
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
||||
stmt, err := tx.Prepare("UPDATE messages SET visible=? WHERE id IN (?" + strings.Repeat(",?", len(messageIDs)-1) + ")")
|
||||
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
return err
|
||||
}
|
||||
defer stmt.Close()
|
||||
|
||||
args := make([]interface{}, len(messageIDs)+1)
|
||||
args[0] = visible
|
||||
for i, id := range messageIDs {
|
||||
args[i+1] = id
|
||||
}
|
||||
|
||||
_, err = stmt.Exec(args...)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
return err
|
||||
}
|
||||
|
||||
if err = tx.Commit(); err != nil {
|
||||
log.Fatal(err)
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func getMessageById(messageID string) (models.ChatEvent, error) {
|
||||
var query = "SELECT * FROM messages WHERE id = ?"
|
||||
row := _db.QueryRow(query, messageID)
|
||||
|
||||
var id string
|
||||
var author string
|
||||
var body string
|
||||
var messageType string
|
||||
var visible int
|
||||
var timestamp time.Time
|
||||
|
||||
err := row.Scan(&id, &author, &body, &messageType, &visible, ×tamp)
|
||||
if err != nil {
|
||||
log.Errorln(err)
|
||||
return models.ChatEvent{}, err
|
||||
}
|
||||
|
||||
return models.ChatEvent{
|
||||
ID: id,
|
||||
Author: author,
|
||||
Body: body,
|
||||
MessageType: messageType,
|
||||
Visible: visible == 1,
|
||||
Timestamp: timestamp,
|
||||
}, nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user