2024-04-23 07:15:38 -07:00
|
|
|
package v1
|
2022-06-19 22:02:44 -07:00
|
|
|
|
|
|
|
import (
|
2024-04-23 22:18:07 -07:00
|
|
|
"fmt"
|
2022-06-19 22:02:44 -07:00
|
|
|
"net/http"
|
|
|
|
"strings"
|
|
|
|
|
2024-04-23 07:15:38 -07:00
|
|
|
"git.jamestombleson.com/jtom38/newsbot-api/internal/database"
|
2024-04-23 22:18:07 -07:00
|
|
|
"git.jamestombleson.com/jtom38/newsbot-api/internal/domain"
|
2024-04-23 07:15:38 -07:00
|
|
|
"git.jamestombleson.com/jtom38/newsbot-api/internal/domain/models"
|
2022-06-19 22:02:44 -07:00
|
|
|
"github.com/google/uuid"
|
2024-04-23 22:18:07 -07:00
|
|
|
"github.com/labstack/echo/v4"
|
2022-06-19 22:02:44 -07:00
|
|
|
)
|
|
|
|
|
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
|
|
|
// 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]
|
2024-04-23 22:18:07 -07:00
|
|
|
func (s *Handler) ListDiscordWebHooks(c echo.Context) error {
|
2023-01-22 10:12:55 -08:00
|
|
|
p := ListDiscordWebhooks{
|
|
|
|
ApiStatusModel: ApiStatusModel{
|
|
|
|
Message: "OK",
|
|
|
|
StatusCode: http.StatusOK,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2024-04-23 22:18:07 -07:00
|
|
|
res, err := s.dto.ListDiscordWebHooks(c.Request().Context(), 50)
|
2022-06-19 22:02:44 -07:00
|
|
|
if err != nil {
|
2024-04-23 22:18:07 -07:00
|
|
|
return c.JSON(http.StatusInternalServerError, err)
|
2022-06-19 22:02:44 -07:00
|
|
|
}
|
2023-01-22 10:12:55 -08:00
|
|
|
p.Payload = res
|
2024-04-23 22:18:07 -07:00
|
|
|
return c.JSON(http.StatusOK, 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"
|
2024-04-23 22:18:07 -07:00
|
|
|
func (s *Handler) GetDiscordWebHooksById(c echo.Context) error {
|
2023-01-22 10:12:55 -08:00
|
|
|
p := GetDiscordWebhook{
|
|
|
|
ApiStatusModel: ApiStatusModel{
|
|
|
|
Message: "OK",
|
|
|
|
StatusCode: http.StatusOK,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2024-04-23 22:18:07 -07:00
|
|
|
_id := c.Param("ID")
|
2022-06-19 22:02:44 -07:00
|
|
|
if _id == "" {
|
2024-04-23 22:18:07 -07:00
|
|
|
return c.JSON(http.StatusBadRequest, domain.ErrorResponse{
|
|
|
|
Message: ErrIdValueMissing,
|
|
|
|
})
|
2022-06-19 22:02:44 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
uuid, err := uuid.Parse(_id)
|
|
|
|
if err != nil {
|
2024-04-23 22:18:07 -07:00
|
|
|
return c.JSON(http.StatusBadRequest, domain.ErrorResponse{
|
|
|
|
Message: ErrUnableToParseId,
|
|
|
|
})
|
2022-06-19 22:02:44 -07:00
|
|
|
}
|
2022-12-04 08:49:17 -08:00
|
|
|
|
2024-04-23 22:18:07 -07:00
|
|
|
res, err := s.dto.GetDiscordWebhook(c.Request().Context(), uuid)
|
2022-06-19 22:02:44 -07:00
|
|
|
if err != nil {
|
2024-04-23 22:18:07 -07:00
|
|
|
return c.JSON(http.StatusBadRequest, domain.ErrorResponse{
|
|
|
|
Message: ErrNoRecordFound,
|
|
|
|
})
|
2022-06-19 22:02:44 -07:00
|
|
|
}
|
2023-01-22 10:12:55 -08:00
|
|
|
p.Payload = res
|
2024-04-23 22:18:07 -07:00
|
|
|
return c.JSON(http.StatusOK, 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"
|
2024-04-23 22:18:07 -07:00
|
|
|
func (s *Handler) GetDiscordWebHooksByServerAndChannel(c echo.Context) error {
|
2023-01-22 10:12:55 -08:00
|
|
|
p := ListDiscordWebhooks{
|
|
|
|
ApiStatusModel: ApiStatusModel{
|
|
|
|
Message: "OK",
|
|
|
|
StatusCode: http.StatusOK,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2024-04-23 22:18:07 -07:00
|
|
|
_server := c.QueryParam("server")
|
2022-11-30 21:43:53 -08:00
|
|
|
if _server == "" {
|
2024-04-23 22:18:07 -07:00
|
|
|
return c.JSON(http.StatusBadRequest, domain.ErrorResponse{
|
|
|
|
Message: ErrIdValueMissing,
|
|
|
|
})
|
2022-11-30 21:43:53 -08:00
|
|
|
}
|
|
|
|
|
2024-04-23 22:18:07 -07:00
|
|
|
_channel := c.QueryParam("channel")
|
2022-11-30 21:43:53 -08:00
|
|
|
if _channel == "" {
|
2024-04-23 22:18:07 -07:00
|
|
|
return c.JSON(http.StatusBadRequest, domain.ErrorResponse{
|
|
|
|
Message: fmt.Sprintf("%s channel", ErrParameterMissing),
|
|
|
|
})
|
2022-11-30 21:43:53 -08:00
|
|
|
}
|
2023-01-22 10:12:55 -08:00
|
|
|
|
2024-04-23 22:18:07 -07:00
|
|
|
res, err := s.dto.GetDiscordWebHookByServerAndChannel(c.Request().Context(), _server, _channel)
|
2022-11-30 21:43:53 -08:00
|
|
|
if err != nil {
|
2024-04-23 22:18:07 -07:00
|
|
|
return c.JSON(http.StatusInternalServerError, domain.ErrorResponse{
|
|
|
|
Message: err.Error(),
|
|
|
|
})
|
2022-11-30 21:43:53 -08:00
|
|
|
}
|
|
|
|
|
2023-01-22 10:12:55 -08:00
|
|
|
p.Payload = res
|
2024-04-23 22:18:07 -07:00
|
|
|
return c.JSON(http.StatusOK, 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]
|
2024-04-23 22:18:07 -07:00
|
|
|
func (s *Handler) NewDiscordWebHook(c echo.Context) error {
|
|
|
|
_url := c.QueryParam("url")
|
|
|
|
_server := c.QueryParam("server")
|
|
|
|
_channel := c.QueryParam("channel")
|
2022-06-19 22:02:44 -07:00
|
|
|
|
|
|
|
if _url == "" {
|
2024-04-23 22:18:07 -07:00
|
|
|
return c.JSON(http.StatusBadRequest, domain.ErrorResponse{
|
|
|
|
Message: "url is missing a value",
|
|
|
|
})
|
2022-06-19 22:02:44 -07:00
|
|
|
}
|
|
|
|
if !strings.Contains(_url, "discord.com/api/webhooks") {
|
2024-04-23 22:18:07 -07:00
|
|
|
return c.JSON(http.StatusBadRequest, domain.ErrorResponse{
|
|
|
|
Message: "invalid url",
|
|
|
|
})
|
2022-06-19 22:02:44 -07:00
|
|
|
}
|
2022-12-04 08:49:17 -08:00
|
|
|
if _server == "" {
|
2024-04-23 22:18:07 -07:00
|
|
|
return c.JSON(http.StatusBadRequest, domain.ErrorResponse{
|
|
|
|
Message: "server is missing",
|
|
|
|
})
|
2022-06-19 22:02:44 -07:00
|
|
|
}
|
|
|
|
if _channel == "" {
|
2024-04-23 22:18:07 -07:00
|
|
|
return c.JSON(http.StatusBadRequest, domain.ErrorResponse{
|
|
|
|
Message: "channel is missing",
|
|
|
|
})
|
2022-06-19 22:02:44 -07:00
|
|
|
}
|
|
|
|
params := database.CreateDiscordWebHookParams{
|
|
|
|
ID: uuid.New(),
|
|
|
|
Url: _url,
|
|
|
|
Server: _server,
|
|
|
|
Channel: _channel,
|
|
|
|
Enabled: true,
|
|
|
|
}
|
2024-04-23 22:18:07 -07:00
|
|
|
s.Db.CreateDiscordWebHook(c.Request().Context(), params)
|
2022-06-19 22:02:44 -07:00
|
|
|
|
2024-04-23 22:18:07 -07:00
|
|
|
return c.JSON(http.StatusOK, params)
|
2022-06-19 22:02:44 -07:00
|
|
|
}
|
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]
|
2024-04-23 22:18:07 -07:00
|
|
|
func (s *Handler) disableDiscordWebHook(c echo.Context) error {
|
|
|
|
id := c.Param("ID")
|
2022-08-21 20:02:45 -07:00
|
|
|
uuid, err := uuid.Parse(id)
|
|
|
|
if err != nil {
|
2024-04-23 22:18:07 -07:00
|
|
|
return c.JSON(http.StatusBadRequest, domain.ErrorResponse{
|
|
|
|
Message: err.Error(),
|
|
|
|
})
|
2022-08-21 20:02:45 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// Check to make sure we can find the record
|
2024-04-23 22:18:07 -07:00
|
|
|
_, err = s.Db.GetDiscordWebHooksByID(c.Request().Context(), uuid)
|
2022-08-21 20:02:45 -07:00
|
|
|
if err != nil {
|
2024-04-23 22:18:07 -07:00
|
|
|
return c.JSON(http.StatusInternalServerError, err.Error())
|
2022-08-21 20:02:45 -07:00
|
|
|
}
|
|
|
|
|
2024-04-23 22:18:07 -07:00
|
|
|
err = s.Db.DisableDiscordWebHook(c.Request().Context(), uuid)
|
2022-08-21 20:02:45 -07:00
|
|
|
if err != nil {
|
2024-04-23 22:18:07 -07:00
|
|
|
return c.JSON(http.StatusInternalServerError, domain.ErrorResponse{
|
|
|
|
Message: err.Error(),
|
|
|
|
})
|
2022-08-21 20:02:45 -07:00
|
|
|
}
|
2024-04-23 22:18:07 -07:00
|
|
|
return nil
|
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]
|
2024-04-23 22:18:07 -07:00
|
|
|
func (s *Handler) enableDiscordWebHook(c echo.Context) error {
|
|
|
|
id := c.Param("ID")
|
2022-08-21 20:02:45 -07:00
|
|
|
uuid, err := uuid.Parse(id)
|
|
|
|
if err != nil {
|
2024-04-23 22:18:07 -07:00
|
|
|
return c.JSON(http.StatusBadRequest, domain.ErrorResponse{
|
|
|
|
Message: err.Error(),
|
|
|
|
})
|
2022-08-21 20:02:45 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// Check to make sure we can find the record
|
2024-04-23 22:18:07 -07:00
|
|
|
_, err = s.Db.GetDiscordWebHooksByID(c.Request().Context(), uuid)
|
2022-08-21 20:02:45 -07:00
|
|
|
if err != nil {
|
2024-04-23 22:18:07 -07:00
|
|
|
return c.JSON(http.StatusInternalServerError, err.Error())
|
2022-08-21 20:02:45 -07:00
|
|
|
}
|
|
|
|
|
2024-04-23 22:18:07 -07:00
|
|
|
err = s.Db.EnableDiscordWebHook(c.Request().Context(), uuid)
|
2022-08-21 20:02:45 -07:00
|
|
|
if err != nil {
|
2024-04-23 22:18:07 -07:00
|
|
|
return c.JSON(http.StatusInternalServerError, err.Error())
|
2022-08-21 20:02:45 -07:00
|
|
|
}
|
2024-04-23 22:18:07 -07:00
|
|
|
return nil
|
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]
|
2024-04-23 22:18:07 -07:00
|
|
|
func (s *Handler) deleteDiscordWebHook(c echo.Context) error {
|
2022-08-21 20:02:45 -07:00
|
|
|
//var item model.Sources = model.Sources{}
|
|
|
|
|
2024-04-23 22:18:07 -07:00
|
|
|
id := c.Param("ID")
|
2022-08-21 20:02:45 -07:00
|
|
|
uuid, err := uuid.Parse(id)
|
|
|
|
if err != nil {
|
2024-04-23 22:18:07 -07:00
|
|
|
return c.JSON(http.StatusBadRequest, err.Error())
|
2022-08-21 20:02:45 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// Check to make sure we can find the record
|
2024-04-23 22:18:07 -07:00
|
|
|
_, err = s.Db.GetDiscordQueueByID(c.Request().Context(), uuid)
|
2022-08-21 20:02:45 -07:00
|
|
|
if err != nil {
|
2024-04-23 22:18:07 -07:00
|
|
|
return c.JSON(http.StatusInternalServerError, err.Error())
|
2022-08-21 20:02:45 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// Delete the record
|
2024-04-23 22:18:07 -07:00
|
|
|
err = s.Db.DeleteDiscordWebHooks(c.Request().Context(), uuid)
|
2022-08-21 20:02:45 -07:00
|
|
|
if err != nil {
|
2024-04-23 22:18:07 -07:00
|
|
|
return c.JSON(http.StatusInternalServerError, err.Error())
|
2022-08-21 20:02:45 -07:00
|
|
|
}
|
2024-04-23 22:18:07 -07:00
|
|
|
|
|
|
|
return nil
|
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]
|
2024-04-23 22:18:07 -07:00
|
|
|
func (s *Handler) UpdateDiscordWebHook(c echo.Context) error {
|
|
|
|
id := c.Param("ID")
|
2022-11-06 17:17:46 -08:00
|
|
|
|
|
|
|
uuid, err := uuid.Parse(id)
|
|
|
|
if err != nil {
|
2024-04-23 22:18:07 -07:00
|
|
|
return c.JSON(http.StatusInternalServerError, err.Error())
|
2022-11-06 17:17:46 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Check to make sure we can find the record
|
2024-04-23 22:18:07 -07:00
|
|
|
_, err = s.Db.GetDiscordQueueByID(c.Request().Context(), uuid)
|
2022-11-06 17:17:46 -08:00
|
|
|
if err != nil {
|
2024-04-23 22:18:07 -07:00
|
|
|
return c.JSON(http.StatusInternalServerError, err.Error())
|
2022-11-06 17:17:46 -08:00
|
|
|
}
|
2024-04-23 22:18:07 -07:00
|
|
|
|
|
|
|
return nil
|
2022-11-06 17:17:46 -08:00
|
|
|
}
|