mirror of
https://github.com/owncast/owncast.git
synced 2024-12-24 18:18:24 +03:00
85e7af3d5f
* chore(go): update go version to 1.20. Closes #2185 * chore(go): run better align against project To optimize struct field order. Closes #2870 * chore(go): update CI jobs to use Go 1.20 * fix(go): linter warnings for Go 1.20 update
73 lines
1.7 KiB
Go
73 lines
1.7 KiB
Go
package moderation
|
|
|
|
import (
|
|
"encoding/json"
|
|
"net/http"
|
|
"strings"
|
|
"time"
|
|
|
|
"github.com/owncast/owncast/controllers"
|
|
"github.com/owncast/owncast/core/chat"
|
|
"github.com/owncast/owncast/core/chat/events"
|
|
"github.com/owncast/owncast/core/user"
|
|
log "github.com/sirupsen/logrus"
|
|
)
|
|
|
|
// GetUserDetails returns the details of a chat user for moderators.
|
|
func GetUserDetails(w http.ResponseWriter, r *http.Request) {
|
|
type connectedClient struct {
|
|
ConnectedAt time.Time `json:"connectedAt"`
|
|
UserAgent string `json:"userAgent"`
|
|
Geo string `json:"geo,omitempty"`
|
|
Id uint `json:"id"`
|
|
MessageCount int `json:"messageCount"`
|
|
}
|
|
|
|
type response struct {
|
|
User *user.User `json:"user"`
|
|
ConnectedClients []connectedClient `json:"connectedClients"`
|
|
Messages []events.UserMessageEvent `json:"messages"`
|
|
}
|
|
|
|
pathComponents := strings.Split(r.URL.Path, "/")
|
|
uid := pathComponents[len(pathComponents)-1]
|
|
|
|
u := user.GetUserByID(uid)
|
|
|
|
if u == nil {
|
|
w.WriteHeader(http.StatusNotFound)
|
|
return
|
|
}
|
|
|
|
c, _ := chat.GetClientsForUser(uid)
|
|
clients := make([]connectedClient, len(c))
|
|
for i, c := range c {
|
|
client := connectedClient{
|
|
Id: c.Id,
|
|
MessageCount: c.MessageCount,
|
|
UserAgent: c.UserAgent,
|
|
ConnectedAt: c.ConnectedAt,
|
|
}
|
|
if c.Geo != nil {
|
|
client.Geo = c.Geo.CountryCode
|
|
}
|
|
|
|
clients[i] = client
|
|
}
|
|
|
|
messages, err := chat.GetMessagesFromUser(uid)
|
|
if err != nil {
|
|
log.Errorln(err)
|
|
}
|
|
|
|
res := response{
|
|
User: u,
|
|
ConnectedClients: clients,
|
|
Messages: messages,
|
|
}
|
|
|
|
w.Header().Set("Content-Type", "application/json")
|
|
if err := json.NewEncoder(w).Encode(res); err != nil {
|
|
controllers.InternalErrorHandler(w, err)
|
|
}
|
|
}
|