Browse Source
* 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>pull/544/head
18 changed files with 375 additions and 64 deletions
@ -0,0 +1,60 @@ |
|||||||
|
package admin |
||||||
|
|
||||||
|
// this is endpoint logic
|
||||||
|
|
||||||
|
import ( |
||||||
|
"encoding/json" |
||||||
|
"net/http" |
||||||
|
|
||||||
|
"github.com/owncast/owncast/controllers" |
||||||
|
"github.com/owncast/owncast/core" |
||||||
|
"github.com/owncast/owncast/core/chat" |
||||||
|
log "github.com/sirupsen/logrus" |
||||||
|
) |
||||||
|
|
||||||
|
// UpdateMessageVisibility updates an array of message IDs to have the same visiblity.
|
||||||
|
func UpdateMessageVisibility(w http.ResponseWriter, r *http.Request) { |
||||||
|
if r.Method != "POST" { |
||||||
|
controllers.WriteSimpleResponse(w, false, r.Method+" not supported") |
||||||
|
return |
||||||
|
} |
||||||
|
|
||||||
|
decoder := json.NewDecoder(r.Body) |
||||||
|
var request messageVisibilityUpdateRequest // creates an empty struc
|
||||||
|
|
||||||
|
err := decoder.Decode(&request) // decode the json into `request`
|
||||||
|
if err != nil { |
||||||
|
log.Errorln(err) |
||||||
|
controllers.WriteSimpleResponse(w, false, "") |
||||||
|
return |
||||||
|
} |
||||||
|
|
||||||
|
// // make sql update call here.
|
||||||
|
// // := means create a new var
|
||||||
|
// _db := data.GetDatabase()
|
||||||
|
// updateMessageVisibility(_db, request)
|
||||||
|
|
||||||
|
if err := chat.SetMessagesVisibility(request.IDArray, request.Visible); err != nil { |
||||||
|
controllers.WriteSimpleResponse(w, false, err.Error()) |
||||||
|
return |
||||||
|
} |
||||||
|
|
||||||
|
controllers.WriteSimpleResponse(w, true, "changed") |
||||||
|
} |
||||||
|
|
||||||
|
type messageVisibilityUpdateRequest struct { |
||||||
|
IDArray []string `json:"idArray"` |
||||||
|
Visible bool `json:"visible"` |
||||||
|
} |
||||||
|
|
||||||
|
// GetChatMessages returns all of the chat messages, unfiltered.
|
||||||
|
func GetChatMessages(w http.ResponseWriter, r *http.Request) { |
||||||
|
// middleware.EnableCors(&w)
|
||||||
|
w.Header().Set("Content-Type", "application/json") |
||||||
|
|
||||||
|
messages := core.GetAllChatMessages(false) |
||||||
|
|
||||||
|
if err := json.NewEncoder(w).Encode(messages); err != nil { |
||||||
|
log.Errorln(err) |
||||||
|
} |
||||||
|
} |
@ -0,0 +1,28 @@ |
|||||||
|
package chat |
||||||
|
|
||||||
|
import ( |
||||||
|
log "github.com/sirupsen/logrus" |
||||||
|
) |
||||||
|
|
||||||
|
func SetMessagesVisibility(messageIDs []string, visibility bool) error { |
||||||
|
// Save new message visibility
|
||||||
|
if err := saveMessageVisibility(messageIDs, visibility); err != nil { |
||||||
|
log.Errorln(err) |
||||||
|
return err |
||||||
|
} |
||||||
|
|
||||||
|
// Send an update event to all clients for each message.
|
||||||
|
// Note: Our client expects a single message at a time, so we can't just
|
||||||
|
// send an array of messages in a single update.
|
||||||
|
for _, id := range messageIDs { |
||||||
|
message, err := getMessageById(id) |
||||||
|
if err != nil { |
||||||
|
log.Errorln(err) |
||||||
|
continue |
||||||
|
} |
||||||
|
message.MessageType = VISIBILITYUPDATE |
||||||
|
_server.sendAll(message) |
||||||
|
} |
||||||
|
|
||||||
|
return nil |
||||||
|
} |
File diff suppressed because one or more lines are too long
@ -0,0 +1,54 @@ |
|||||||
|
const { test } = require('@jest/globals'); |
||||||
|
var request = require('supertest'); |
||||||
|
request = request('http://127.0.0.1:8080'); |
||||||
|
|
||||||
|
const WebSocket = require('ws'); |
||||||
|
var ws; |
||||||
|
|
||||||
|
const testVisibilityMessage = { |
||||||
|
author: "username", |
||||||
|
body: "message " + Math.floor(Math.random() * 100), |
||||||
|
type: 'CHAT', |
||||||
|
visible: true, |
||||||
|
timestamp: new Date().toISOString() |
||||||
|
}; |
||||||
|
|
||||||
|
test('can send a chat message', (done) => { |
||||||
|
ws = new WebSocket('ws://127.0.0.1:8080/entry', { |
||||||
|
origin: 'http://localhost', |
||||||
|
}); |
||||||
|
|
||||||
|
function onOpen() { |
||||||
|
ws.send(JSON.stringify(testVisibilityMessage), function () { |
||||||
|
ws.close(); |
||||||
|
done(); |
||||||
|
}); |
||||||
|
} |
||||||
|
|
||||||
|
ws.on('open', onOpen); |
||||||
|
}); |
||||||
|
|
||||||
|
var messageId; |
||||||
|
|
||||||
|
test('verify we can make API call to mark message as hidden', async (done) => { |
||||||
|
const res = await request.get('/api/chat').expect(200); |
||||||
|
const message = res.body[0]; |
||||||
|
messageId = message.id; |
||||||
|
await request.post('/api/admin/chat/updatemessagevisibility') |
||||||
|
.auth('admin', 'abc123') |
||||||
|
.send({ "idArray": [messageId], "visible": false }).expect(200); |
||||||
|
done(); |
||||||
|
}); |
||||||
|
|
||||||
|
test('verify message has become hidden', async (done) => { |
||||||
|
const res = await request.get('/api/admin/chat/messages') |
||||||
|
.expect(200) |
||||||
|
.auth('admin', 'abc123') |
||||||
|
|
||||||
|
const message = res.body.filter(obj => { |
||||||
|
return obj.id === messageId; |
||||||
|
}); |
||||||
|
expect(message.length).toBe(1); |
||||||
|
expect(message[0].visible).toBe(false); |
||||||
|
done(); |
||||||
|
}); |
Loading…
Reference in new issue