2024-04-23 07:15:38 -07:00
|
|
|
package v1
|
2022-06-19 22:02:44 -07:00
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"log"
|
|
|
|
"net/http"
|
|
|
|
"strings"
|
|
|
|
|
2024-04-23 07:15:38 -07:00
|
|
|
"git.jamestombleson.com/jtom38/newsbot-api/internal/database"
|
|
|
|
"git.jamestombleson.com/jtom38/newsbot-api/internal/domain/models"
|
2022-08-21 20:02:45 -07:00
|
|
|
"github.com/go-chi/chi/v5"
|
2022-06-19 22:02:44 -07:00
|
|
|
"github.com/google/uuid"
|
|
|
|
)
|
|
|
|
|
2023-01-31 08:19:23 -08:00
|
|
|
type ListDiscordWebhooks struct {
|
|
|
|
ApiStatusModel
|
|
|
|
Payload []models.DiscordWebHooksDto `json:"payload"`
|
|
|
|
}
|
|
|
|
|
|
|
|
type GetDiscordWebhook struct {
|
|
|
|
ApiStatusModel
|
|
|
|
Payload models.DiscordWebHooksDto `json:"payload"`
|
|
|
|
}
|
|
|
|
|
2023-01-22 10:12:55 -08:00
|
|
|
func (s Server) DiscordWebHookRouter() http.Handler {
|
|
|
|
r := chi.NewRouter()
|
|
|
|
|
|
|
|
r.Get("/", s.ListDiscordWebHooks)
|
|
|
|
r.Post("/new", s.NewDiscordWebHook)
|
|
|
|
r.Get("/by/serverAndChannel", s.GetDiscordWebHooksByServerAndChannel)
|
|
|
|
r.Route("/{ID}", func(r chi.Router) {
|
|
|
|
r.Get("/", s.GetDiscordWebHooksById)
|
|
|
|
r.Delete("/", s.deleteDiscordWebHook)
|
|
|
|
r.Post("/disable", s.disableDiscordWebHook)
|
|
|
|
r.Post("/enable", s.enableDiscordWebHook)
|
|
|
|
})
|
|
|
|
|
|
|
|
return r
|
|
|
|
}
|
|
|
|
|
|
|
|
// ListDiscordWebhooks
|
2022-06-19 22:02:44 -07:00
|
|
|
// @Summary Returns the top 100 entries from the queue to be processed.
|
|
|
|
// @Produce application/json
|
2023-01-22 10:12:55 -08:00
|
|
|
// @Tags Discord, Webhook
|
2022-06-19 22:02:44 -07:00
|
|
|
// @Router /discord/webhooks [get]
|
2023-01-22 10:12:55 -08:00
|
|
|
func (s *Server) ListDiscordWebHooks(w http.ResponseWriter, r *http.Request) {
|
|
|
|
p := ListDiscordWebhooks{
|
|
|
|
ApiStatusModel: ApiStatusModel{
|
|
|
|
Message: "OK",
|
|
|
|
StatusCode: http.StatusOK,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
res, err := s.dto.ListDiscordWebHooks(r.Context(), 50)
|
2022-06-19 22:02:44 -07:00
|
|
|
if err != nil {
|
2023-01-22 10:12:55 -08:00
|
|
|
s.WriteError(w, err.Error(), http.StatusInternalServerError)
|
|
|
|
return
|
2022-06-19 22:02:44 -07:00
|
|
|
}
|
2023-01-22 10:12:55 -08:00
|
|
|
p.Payload = res
|
2023-01-31 08:19:23 -08:00
|
|
|
s.WriteJson(w, p)
|
2023-01-22 10:12:55 -08:00
|
|
|
}
|
|
|
|
|
2022-08-21 20:02:45 -07:00
|
|
|
// GetDiscordWebHook
|
2022-06-19 22:02:44 -07:00
|
|
|
// @Summary Returns the top 100 entries from the queue to be processed.
|
|
|
|
// @Produce application/json
|
2022-08-21 20:02:45 -07:00
|
|
|
// @Param id path string true "id"
|
2023-01-22 10:12:55 -08:00
|
|
|
// @Tags Discord, Webhook
|
2022-08-21 20:02:45 -07:00
|
|
|
// @Router /discord/webhooks/{id} [get]
|
2023-01-22 10:12:55 -08:00
|
|
|
// @Success 200 {object} GetDiscordWebhook "OK"
|
2022-06-19 22:02:44 -07:00
|
|
|
func (s *Server) GetDiscordWebHooksById(w http.ResponseWriter, r *http.Request) {
|
2023-01-22 10:12:55 -08:00
|
|
|
p := GetDiscordWebhook{
|
|
|
|
ApiStatusModel: ApiStatusModel{
|
|
|
|
Message: "OK",
|
|
|
|
StatusCode: http.StatusOK,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2022-12-04 08:49:17 -08:00
|
|
|
_id := chi.URLParam(r, "ID")
|
2022-06-19 22:02:44 -07:00
|
|
|
if _id == "" {
|
2023-01-22 10:12:55 -08:00
|
|
|
s.WriteError(w, "id is missing", http.StatusBadRequest)
|
2022-06-19 22:02:44 -07:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
uuid, err := uuid.Parse(_id)
|
|
|
|
if err != nil {
|
2023-01-22 10:12:55 -08:00
|
|
|
s.WriteError(w, "unable to parse id value", http.StatusBadRequest)
|
2022-06-19 22:02:44 -07:00
|
|
|
return
|
|
|
|
}
|
2022-12-04 08:49:17 -08:00
|
|
|
|
2023-01-22 10:12:55 -08:00
|
|
|
res, err := s.dto.GetDiscordWebhook(r.Context(), uuid)
|
2022-06-19 22:02:44 -07:00
|
|
|
if err != nil {
|
2023-01-22 10:12:55 -08:00
|
|
|
s.WriteError(w, "no record found", http.StatusBadRequest)
|
2022-06-19 22:02:44 -07:00
|
|
|
return
|
|
|
|
}
|
2023-01-22 10:12:55 -08:00
|
|
|
p.Payload = res
|
2023-01-31 08:19:23 -08:00
|
|
|
s.WriteJson(w, p)
|
2022-06-19 22:02:44 -07:00
|
|
|
}
|
|
|
|
|
2022-11-30 21:43:53 -08:00
|
|
|
// GetDiscordWebHookByServerAndChannel
|
|
|
|
// @Summary Returns all the known web hooks based on the Server and Channel given.
|
|
|
|
// @Produce application/json
|
|
|
|
// @Param server query string true "Fancy Server"
|
|
|
|
// @Param channel query string true "memes"
|
2023-01-22 10:12:55 -08:00
|
|
|
// @Tags Discord, Webhook
|
2022-11-30 21:43:53 -08:00
|
|
|
// @Router /discord/webhooks/by/serverAndChannel [get]
|
2023-01-22 10:12:55 -08:00
|
|
|
// @Success 200 {object} ListDiscordWebhooks "OK"
|
2022-11-30 21:43:53 -08:00
|
|
|
func (s *Server) GetDiscordWebHooksByServerAndChannel(w http.ResponseWriter, r *http.Request) {
|
2023-01-22 10:12:55 -08:00
|
|
|
p := ListDiscordWebhooks{
|
|
|
|
ApiStatusModel: ApiStatusModel{
|
|
|
|
Message: "OK",
|
|
|
|
StatusCode: http.StatusOK,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2022-11-30 21:43:53 -08:00
|
|
|
query := r.URL.Query()
|
|
|
|
_server := query["server"][0]
|
|
|
|
if _server == "" {
|
2023-01-22 10:12:55 -08:00
|
|
|
s.WriteError(w, "ID is missing", http.StatusInternalServerError)
|
2022-11-30 21:43:53 -08:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
_channel := query["channel"][0]
|
|
|
|
if _channel == "" {
|
2023-01-22 10:12:55 -08:00
|
|
|
s.WriteError(w, "Channel is missing", http.StatusInternalServerError)
|
2022-11-30 21:43:53 -08:00
|
|
|
return
|
|
|
|
}
|
2023-01-22 10:12:55 -08:00
|
|
|
|
|
|
|
res, err := s.dto.GetDiscordWebHookByServerAndChannel(r.Context(), _server, _channel)
|
2022-11-30 21:43:53 -08:00
|
|
|
if err != nil {
|
2023-01-22 10:12:55 -08:00
|
|
|
s.WriteError(w, err.Error(), http.StatusInternalServerError)
|
2022-11-30 21:43:53 -08:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-01-22 10:12:55 -08:00
|
|
|
p.Payload = res
|
2023-01-31 08:19:23 -08:00
|
|
|
s.WriteJson(w, p)
|
2022-11-30 21:43:53 -08:00
|
|
|
}
|
|
|
|
|
2022-06-19 22:02:44 -07:00
|
|
|
// NewDiscordWebHook
|
|
|
|
// @Summary Creates a new record for a discord web hook to post data to.
|
|
|
|
// @Param url query string true "url"
|
|
|
|
// @Param server query string true "Server name"
|
2022-07-12 15:28:31 -07:00
|
|
|
// @Param channel query string true "Channel name"
|
2023-01-22 10:12:55 -08:00
|
|
|
// @Tags Discord, Webhook
|
2022-06-19 22:02:44 -07:00
|
|
|
// @Router /discord/webhooks/new [post]
|
|
|
|
func (s *Server) NewDiscordWebHook(w http.ResponseWriter, r *http.Request) {
|
|
|
|
query := r.URL.Query()
|
|
|
|
_url := query["url"][0]
|
|
|
|
_server := query["server"][0]
|
|
|
|
_channel := query["channel"][0]
|
|
|
|
|
|
|
|
if _url == "" {
|
|
|
|
http.Error(w, "url is missing a value", http.StatusBadRequest)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if !strings.Contains(_url, "discord.com/api/webhooks") {
|
|
|
|
http.Error(w, "invalid url", http.StatusBadRequest)
|
|
|
|
return
|
|
|
|
}
|
2022-12-04 08:49:17 -08:00
|
|
|
if _server == "" {
|
2022-06-19 22:02:44 -07:00
|
|
|
http.Error(w, "server is missing", http.StatusBadRequest)
|
|
|
|
}
|
|
|
|
if _channel == "" {
|
|
|
|
http.Error(w, "channel is missing", http.StatusBadRequest)
|
|
|
|
}
|
|
|
|
params := database.CreateDiscordWebHookParams{
|
|
|
|
ID: uuid.New(),
|
|
|
|
Url: _url,
|
|
|
|
Server: _server,
|
|
|
|
Channel: _channel,
|
|
|
|
Enabled: true,
|
|
|
|
}
|
2023-01-31 12:39:54 -08:00
|
|
|
s.Db.CreateDiscordWebHook(r.Context(), params)
|
2022-06-19 22:02:44 -07:00
|
|
|
|
|
|
|
bJson, err := json.Marshal(¶ms)
|
|
|
|
if err != nil {
|
|
|
|
log.Panicln(err)
|
|
|
|
}
|
|
|
|
w.Header().Set("Content-Type", "application/json")
|
|
|
|
w.Write(bJson)
|
|
|
|
}
|
2022-08-21 20:02:45 -07:00
|
|
|
|
|
|
|
// DisableDiscordWebHooks
|
|
|
|
// @Summary Disables a Webhook from being used.
|
|
|
|
// @Param id path string true "id"
|
2023-01-22 10:12:55 -08:00
|
|
|
// @Tags Discord, Webhook
|
|
|
|
// @Router /discord/webhooks/{ID}/disable [post]
|
2022-08-21 20:02:45 -07:00
|
|
|
func (s *Server) disableDiscordWebHook(w http.ResponseWriter, r *http.Request) {
|
|
|
|
id := chi.URLParam(r, "ID")
|
|
|
|
uuid, err := uuid.Parse(id)
|
|
|
|
if err != nil {
|
2023-01-22 10:12:55 -08:00
|
|
|
s.WriteError(w, err.Error(), http.StatusBadRequest)
|
|
|
|
return
|
2022-08-21 20:02:45 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// Check to make sure we can find the record
|
2023-01-31 12:39:54 -08:00
|
|
|
_, err = s.Db.GetDiscordWebHooksByID(r.Context(), uuid)
|
2022-08-21 20:02:45 -07:00
|
|
|
if err != nil {
|
2023-01-22 10:12:55 -08:00
|
|
|
s.WriteError(w, err.Error(), http.StatusBadRequest)
|
|
|
|
return
|
2022-08-21 20:02:45 -07:00
|
|
|
}
|
|
|
|
|
2023-01-31 12:39:54 -08:00
|
|
|
err = s.Db.DisableDiscordWebHook(r.Context(), uuid)
|
2022-08-21 20:02:45 -07:00
|
|
|
if err != nil {
|
2023-01-22 10:12:55 -08:00
|
|
|
s.WriteError(w, err.Error(), http.StatusInternalServerError)
|
2022-08-21 20:02:45 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// EnableDiscordWebHook
|
|
|
|
// @Summary Enables a source to continue processing.
|
|
|
|
// @Param id path string true "id"
|
2023-01-22 10:12:55 -08:00
|
|
|
// @Tags Discord, Webhook
|
|
|
|
// @Router /discord/webhooks/{ID}/enable [post]
|
2022-08-21 20:02:45 -07:00
|
|
|
func (s *Server) enableDiscordWebHook(w http.ResponseWriter, r *http.Request) {
|
|
|
|
id := chi.URLParam(r, "ID")
|
|
|
|
uuid, err := uuid.Parse(id)
|
|
|
|
if err != nil {
|
2023-01-22 10:12:55 -08:00
|
|
|
s.WriteError(w, err.Error(), http.StatusBadRequest)
|
2022-08-21 20:02:45 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// Check to make sure we can find the record
|
2023-01-31 12:39:54 -08:00
|
|
|
_, err = s.Db.GetDiscordWebHooksByID(r.Context(), uuid)
|
2022-08-21 20:02:45 -07:00
|
|
|
if err != nil {
|
2023-01-22 10:12:55 -08:00
|
|
|
s.WriteError(w, err.Error(), http.StatusBadRequest)
|
2022-08-21 20:02:45 -07:00
|
|
|
}
|
|
|
|
|
2023-01-31 12:39:54 -08:00
|
|
|
err = s.Db.EnableDiscordWebHook(r.Context(), uuid)
|
2022-08-21 20:02:45 -07:00
|
|
|
if err != nil {
|
2023-01-22 10:12:55 -08:00
|
|
|
s.WriteError(w, err.Error(), http.StatusInternalServerError)
|
2022-08-21 20:02:45 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// DeleteDiscordWebHook
|
|
|
|
// @Summary Deletes a record by ID.
|
|
|
|
// @Param id path string true "id"
|
2023-01-22 10:12:55 -08:00
|
|
|
// @Tags Discord, Webhook
|
|
|
|
// @Router /discord/webhooks/{ID} [delete]
|
2022-08-21 20:02:45 -07:00
|
|
|
func (s *Server) deleteDiscordWebHook(w http.ResponseWriter, r *http.Request) {
|
|
|
|
//var item model.Sources = model.Sources{}
|
|
|
|
|
|
|
|
id := chi.URLParam(r, "ID")
|
|
|
|
uuid, err := uuid.Parse(id)
|
|
|
|
if err != nil {
|
2023-01-22 10:12:55 -08:00
|
|
|
s.WriteError(w, err.Error(), http.StatusBadRequest)
|
2022-08-21 20:02:45 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// Check to make sure we can find the record
|
2023-01-31 12:39:54 -08:00
|
|
|
_, err = s.Db.GetDiscordQueueByID(r.Context(), uuid)
|
2022-08-21 20:02:45 -07:00
|
|
|
if err != nil {
|
2023-01-22 10:12:55 -08:00
|
|
|
s.WriteError(w, err.Error(), http.StatusBadRequest)
|
2022-08-21 20:02:45 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// Delete the record
|
2023-01-31 12:39:54 -08:00
|
|
|
err = s.Db.DeleteDiscordWebHooks(r.Context(), uuid)
|
2022-08-21 20:02:45 -07:00
|
|
|
if err != nil {
|
2023-01-22 10:12:55 -08:00
|
|
|
s.WriteError(w, err.Error(), http.StatusInternalServerError)
|
2022-08-21 20:02:45 -07:00
|
|
|
}
|
|
|
|
}
|
2022-11-06 17:17:46 -08:00
|
|
|
|
|
|
|
// UpdateDiscordWebHook
|
|
|
|
// @Summary Updates a valid discord webhook ID based on the body given.
|
|
|
|
// @Param id path string true "id"
|
2023-01-22 10:12:55 -08:00
|
|
|
// @Tags Discord, Webhook
|
2022-11-30 21:43:53 -08:00
|
|
|
// @Router /discord/webhooks/{id} [patch]
|
2022-11-06 17:17:46 -08:00
|
|
|
func (s *Server) UpdateDiscordWebHook(w http.ResponseWriter, r *http.Request) {
|
|
|
|
id := chi.URLParam(r, "ID")
|
|
|
|
|
|
|
|
uuid, err := uuid.Parse(id)
|
|
|
|
if err != nil {
|
|
|
|
log.Panicln(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check to make sure we can find the record
|
2023-01-31 12:39:54 -08:00
|
|
|
_, err = s.Db.GetDiscordQueueByID(r.Context(), uuid)
|
2022-11-06 17:17:46 -08:00
|
|
|
if err != nil {
|
|
|
|
http.Error(w, err.Error(), http.StatusBadRequest)
|
|
|
|
}
|
|
|
|
}
|