2022-07-21 05:42:23 +02:00
|
|
|
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 {
|
2022-12-14 04:17:32 +01:00
|
|
|
Id uint `json:"id"`
|
2022-07-21 05:42:23 +02:00
|
|
|
MessageCount int `json:"messageCount"`
|
|
|
|
UserAgent string `json:"userAgent"`
|
|
|
|
ConnectedAt time.Time `json:"connectedAt"`
|
|
|
|
Geo string `json:"geo,omitempty"`
|
|
|
|
}
|
|
|
|
|
|
|
|
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{
|
2022-12-14 04:17:32 +01:00
|
|
|
Id: c.Id,
|
2022-07-21 05:42:23 +02:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
}
|