justlog/api/admin.go

122 lines
2.7 KiB
Go
Raw Normal View History

2020-09-26 01:45:18 +02:00
package api
import (
"encoding/json"
"fmt"
"net/http"
)
func (s *Server) authenticateAdmin(w http.ResponseWriter, r *http.Request) bool {
apiKey := r.Header.Get("X-Api-Key")
2020-09-27 12:30:39 +02:00
if apiKey == "" || apiKey != s.cfg.AdminAPIKey {
2020-09-26 01:45:18 +02:00
http.Error(w, "No I don't think so.", http.StatusForbidden)
return false
}
2020-09-27 12:30:39 +02:00
return true
2020-09-26 01:45:18 +02:00
}
2020-10-08 21:52:58 +02:00
type channelsDeleteRequest struct {
// list of userIds
2020-10-08 21:52:58 +02:00
Channels []string `json:"channels"`
}
type channelConfigsJoinRequest struct {
// list of userIds
2020-10-08 21:52:58 +02:00
Channels []string `json:"channels"`
}
2020-12-05 17:52:12 +01:00
// swagger:route POST /admin/channels admin addChannels
//
// Will add the channels to log, only works with userIds
2020-12-05 17:52:12 +01:00
//
// Consumes:
// - application/json
//
// Produces:
// - application/json
// - text/plain
//
// Security:
// - api_key:
//
// Schemes: https
//
// Responses:
// 200:
// 400:
// 405:
// 500:
// swagger:route DELETE /admin/channels admin deleteChannels
//
// Will remove the channels to log, only works with userIds
2020-12-05 17:52:12 +01:00
//
// Consumes:
// - application/json
//
// Produces:
// - application/json
// - text/plain
//
// Security:
// - api_key:
//
// Schemes: https
//
// Responses:
// 200:
// 400:
// 405:
// 500:
2020-10-08 21:52:58 +02:00
func (s *Server) writeChannels(w http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodPost && r.Method != http.MethodDelete {
http.Error(w, "We'll see, we'll see. The winner gets tea.", http.StatusMethodNotAllowed)
return
}
if r.Method == http.MethodDelete {
var request channelsDeleteRequest
2020-09-26 09:13:47 +02:00
err := json.NewDecoder(r.Body).Decode(&request)
if err != nil {
http.Error(w, "ANYWAYS: "+err.Error(), http.StatusBadRequest)
return
}
2020-10-08 21:52:58 +02:00
s.cfg.RemoveChannels(request.Channels...)
data, err := s.helixClient.GetUsersByUserIds(request.Channels)
if err != nil {
http.Error(w, "Failed to get channel names to leave, config might be already updated", http.StatusInternalServerError)
return
}
for _, userData := range data {
s.bot.Part(userData.Login)
2020-10-08 21:52:58 +02:00
}
2020-09-26 09:13:47 +02:00
2020-10-08 21:52:58 +02:00
writeJSON(fmt.Sprintf("Doubters? Removed channels %v", request.Channels), http.StatusOK, w, r)
2020-09-26 01:45:18 +02:00
return
}
2020-10-08 21:52:58 +02:00
var request channelConfigsJoinRequest
err := json.NewDecoder(r.Body).Decode(&request)
if err != nil {
http.Error(w, "ANYWAYS: "+err.Error(), http.StatusBadRequest)
return
}
s.cfg.AddChannels(request.Channels...)
data, err := s.helixClient.GetUsersByUserIds(request.Channels)
if err != nil {
http.Error(w, "Failed to get channel names to join, config might be already updated", http.StatusInternalServerError)
return
}
for _, userData := range data {
s.bot.Join(userData.Login)
}
writeJSON(fmt.Sprintf("Doubters? Joined channels or already in: %v", request.Channels), http.StatusOK, w, r)
2020-09-26 01:45:18 +02:00
}