newsbot-api/internal/handler/v1/discordwebhooks.go

389 lines
12 KiB
Go
Raw Normal View History

2024-04-23 07:15:38 -07:00
package v1
import (
"net/http"
"strconv"
"strings"
2024-04-23 22:18:07 -07:00
"git.jamestombleson.com/jtom38/newsbot-api/internal/domain"
"git.jamestombleson.com/jtom38/newsbot-api/internal/services"
2024-04-23 22:18:07 -07:00
"github.com/labstack/echo/v4"
)
// ListDiscordWebhooks
// @Summary Returns the top 100
// @Produce application/json
// @Tags DiscordWebhook
// @Router /v1/discord/webhooks [get]
// @Success 200 {object} domain.DiscordWebhookResponse
// @Failure 400 {object} domain.BaseResponse
// @Failure 500 {object} domain.BaseResponse
// @Security Bearer
2024-04-23 22:18:07 -07:00
func (s *Handler) ListDiscordWebHooks(c echo.Context) error {
_, err := s.ValidateJwtToken(c, domain.ScopeDiscordWebhookRead)
if err != nil {
return s.WriteError(c, err, http.StatusBadRequest)
}
p := domain.DiscordWebhookResponse{
BaseResponse: domain.BaseResponse{
Message: ResponseMessageSuccess,
},
}
res, err := s.repo.DiscordWebHooks.ListByServerName(c.Request().Context(), "")
if err != nil {
2024-04-23 22:18:07 -07:00
return c.JSON(http.StatusInternalServerError, err)
}
p.Payload = services.DiscordWebhooksToDto(res)
2024-04-23 22:18:07 -07:00
return c.JSON(http.StatusOK, p)
}
// GetDiscordWebHook
// @Summary Returns the top 100 entries from the queue to be processed.
// @Produce application/json
// @Param id path int true "id"
// @Tags DiscordWebhook
// @Router /v1/discord/webhooks/{id} [get]
// @Success 200 {object} domain.DiscordWebhookResponse "OK"
// @Failure 400 {object} domain.BaseResponse
// @Failure 500 {object} domain.BaseResponse
// @Security Bearer
2024-04-23 22:18:07 -07:00
func (s *Handler) GetDiscordWebHooksById(c echo.Context) error {
_, err := s.ValidateJwtToken(c, domain.ScopeDiscordWebhookRead)
if err != nil {
return s.WriteError(c, err, http.StatusBadRequest)
}
p := domain.DiscordWebhookResponse{
BaseResponse: domain.BaseResponse{
Message: ResponseMessageSuccess,
},
}
id, err := strconv.Atoi(c.Param("ID"))
if err != nil {
return s.WriteError(c, err, http.StatusBadRequest)
}
res, err := s.repo.DiscordWebHooks.GetById(c.Request().Context(), int64(id))
if err != nil {
return s.WriteError(c, err, http.StatusInternalServerError)
}
var dtos []domain.DiscordWebHookDto
dtos = append(dtos, services.DiscordWebhookToDto(res))
p.Payload = dtos
2024-04-23 22:18:07 -07:00
return c.JSON(http.StatusOK, p)
}
// 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"
// @Tags DiscordWebhook
// @Router /v1/discord/webhooks/by/serverAndChannel [get]
// @Success 200 {object} domain.DiscordWebhookResponse "OK"
// @Failure 400 {object} domain.BaseResponse
// @Failure 500 {object} domain.BaseResponse
// @Security Bearer
2024-04-23 22:18:07 -07:00
func (s *Handler) GetDiscordWebHooksByServerAndChannel(c echo.Context) error {
_, err := s.ValidateJwtToken(c, domain.ScopeDiscordWebhookRead)
if err != nil {
return s.WriteError(c, err, http.StatusBadRequest)
}
p := domain.DiscordWebhookResponse{
BaseResponse: domain.BaseResponse{
Message: ResponseMessageSuccess,
},
}
2024-04-23 22:18:07 -07:00
_server := c.QueryParam("server")
if _server == "" {
return s.WriteMessage(c, "server was not defined", http.StatusBadRequest)
}
2024-04-23 22:18:07 -07:00
_channel := c.QueryParam("channel")
if _channel == "" {
return s.WriteMessage(c, "channel was not defined", http.StatusBadRequest)
}
res, err := s.repo.DiscordWebHooks.ListByServerAndChannel(c.Request().Context(), _server, _channel)
if err != nil {
return s.WriteError(c, err, http.StatusInternalServerError)
}
p.Payload = services.DiscordWebhooksToDto(res)
2024-04-23 22:18:07 -07:00
return c.JSON(http.StatusOK, p)
}
// 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"
// @Param channel query string true "Channel name"
// @Tags DiscordWebhook
// @Router /v1/discord/webhooks/new [post]
// @Success 200 {object} domain.DiscordWebhookResponse "OK"
// @Failure 400 {object} domain.BaseResponse
// @Failure 500 {object} domain.BaseResponse
// @Security Bearer
2024-04-23 22:18:07 -07:00
func (s *Handler) NewDiscordWebHook(c echo.Context) error {
token, err := s.ValidateJwtToken(c, domain.ScopeDiscordWebHookCreate)
if err != nil {
return s.WriteError(c, err, http.StatusBadRequest)
}
2024-04-23 22:18:07 -07:00
_url := c.QueryParam("url")
_server := c.QueryParam("server")
_channel := c.QueryParam("channel")
if _url == "" {
return c.JSON(http.StatusBadRequest, domain.BaseResponse{
2024-04-23 22:18:07 -07:00
Message: "url is missing a value",
})
}
if !strings.Contains(_url, "discord.com/api/webhooks") {
return c.JSON(http.StatusBadRequest, domain.BaseResponse{
2024-04-23 22:18:07 -07:00
Message: "invalid url",
})
}
if _server == "" {
return c.JSON(http.StatusBadRequest, domain.BaseResponse{
2024-04-23 22:18:07 -07:00
Message: "server is missing",
})
}
if _channel == "" {
return c.JSON(http.StatusBadRequest, domain.BaseResponse{
2024-04-23 22:18:07 -07:00
Message: "channel is missing",
})
}
user, err := s.repo.Users.GetUser(c.Request().Context(), token.UserName)
if err != nil {
return s.WriteMessage(c, ErrUserUnknown, http.StatusBadRequest)
}
rows, err := s.repo.DiscordWebHooks.Create(c.Request().Context(), user.ID, _url, _server, _channel, true)
if err != nil {
return s.WriteError(c, err, http.StatusInternalServerError)
}
if rows != 1 {
return s.WriteMessage(c, "data was not written to database", http.StatusInternalServerError)
}
item, err := s.repo.DiscordWebHooks.GetByUrl(c.Request().Context(), _url)
if err != nil {
return s.WriteError(c, err, http.StatusInternalServerError)
}
var dtos []domain.DiscordWebHookDto
dtos = append(dtos, services.DiscordWebhookToDto(item))
return c.JSON(http.StatusOK, domain.DiscordWebhookResponse{
BaseResponse: domain.BaseResponse{
Message: ResponseMessageSuccess,
},
Payload: dtos,
})
}
// DisableDiscordWebHooks
// @Summary Disables a Webhook from being used.
// @Param id path int true "id"
// @Tags DiscordWebhook
// @Router /v1/discord/webhooks/{ID}/disable [post]
// @Success 200 {object} domain.DiscordWebhookResponse "OK"
// @Failure 400 {object} domain.BaseResponse
// @Failure 500 {object} domain.BaseResponse
// @Security Bearer
2024-04-23 22:18:07 -07:00
func (s *Handler) disableDiscordWebHook(c echo.Context) error {
_, err := s.ValidateJwtToken(c, domain.ScopeDiscordWebHookCreate)
if err != nil {
return s.WriteError(c, err, http.StatusBadRequest)
}
id, err := strconv.Atoi(c.Param("ID"))
if err != nil {
return c.JSON(http.StatusBadRequest, domain.BaseResponse{
2024-04-23 22:18:07 -07:00
Message: err.Error(),
})
}
// Check to make sure we can find the record
record, err := s.repo.DiscordWebHooks.GetById(c.Request().Context(), int64(id))
if err != nil {
return s.WriteError(c, err, http.StatusInternalServerError)
}
if record.UserID != s.GetUserIdFromJwtToken(c) {
return s.WriteMessage(c, ErrYouDontOwnTheRecord, http.StatusBadRequest)
}
// flip the it
updated, err := s.repo.DiscordWebHooks.Disable(c.Request().Context(), int64(id))
if err != nil {
return s.WriteError(c, err, http.StatusInternalServerError)
}
// make sure we got a row updated
if updated != 1 {
return s.WriteMessage(c, "unexpected number of updates found", http.StatusInternalServerError)
}
item, err := s.repo.DiscordWebHooks.GetById(c.Request().Context(), int64(id))
if err != nil {
return s.WriteError(c, err, http.StatusInternalServerError)
}
var dtos []domain.DiscordWebHookDto
dtos = append(dtos, services.DiscordWebhookToDto(item))
return c.JSON(http.StatusOK, domain.DiscordWebhookResponse{
BaseResponse: domain.BaseResponse{
Message: ResponseMessageSuccess,
},
Payload: dtos,
})
}
// EnableDiscordWebHook
// @Summary Enables a source to continue processing.
// @Param id path int true "id"
// @Tags DiscordWebhook
// @Router /v1/discord/webhooks/{ID}/enable [post]
// @Security Bearer
2024-04-23 22:18:07 -07:00
func (s *Handler) enableDiscordWebHook(c echo.Context) error {
_, err := s.ValidateJwtToken(c, domain.ScopeDiscordWebHookCreate)
if err != nil {
return s.WriteError(c, err, http.StatusBadRequest)
}
id, err := strconv.Atoi(c.Param("ID"))
if err != nil {
return s.WriteError(c, err, http.StatusBadRequest)
}
// Check to make sure we can find the record
record, err := s.repo.DiscordWebHooks.GetById(c.Request().Context(), int64(id))
if err != nil {
return s.WriteError(c, err, http.StatusBadRequest)
}
if record.UserID != s.GetUserIdFromJwtToken(c) {
return s.WriteMessage(c, ErrYouDontOwnTheRecord, http.StatusBadRequest)
}
updated, err := s.repo.DiscordWebHooks.Enable(c.Request().Context(), int64(id))
if err != nil {
return s.WriteError(c, err, http.StatusInternalServerError)
}
if updated != 1 {
return s.WriteMessage(c, ErrFailedToUpdateRecord, http.StatusInternalServerError)
}
item, err := s.repo.DiscordWebHooks.GetById(c.Request().Context(), int64(id))
if err != nil {
return s.WriteError(c, err, http.StatusInternalServerError)
}
var dtos []domain.DiscordWebHookDto
dtos = append(dtos, services.DiscordWebhookToDto(item))
return c.JSON(http.StatusOK, domain.DiscordWebhookResponse{
BaseResponse: domain.BaseResponse{
Message: ResponseMessageSuccess,
},
Payload: dtos,
})
}
// DeleteDiscordWebHook
// @Summary Deletes a record by ID.
// @Param id path string true "id"
// @Tags DiscordWebhook
// @Router /v1/discord/webhooks/{ID} [delete]
// @Success 200 {object} domain.DiscordWebhookResponse "OK"
// @Failure 400 {object} domain.BaseResponse
// @Failure 500 {object} domain.BaseResponse
2024-04-23 22:18:07 -07:00
func (s *Handler) deleteDiscordWebHook(c echo.Context) error {
_, err := s.ValidateJwtToken(c, domain.ScopeDiscordWebHookCreate)
if err != nil {
return s.WriteError(c, err, http.StatusBadRequest)
}
id, err := strconv.Atoi(c.Param("ID"))
if err != nil {
2024-04-23 22:18:07 -07:00
return c.JSON(http.StatusBadRequest, err.Error())
}
// Check to make sure we can find the record
record, err := s.repo.DiscordWebHooks.GetById(c.Request().Context(), int64(id))
if err != nil {
2024-04-23 22:18:07 -07:00
return c.JSON(http.StatusInternalServerError, err.Error())
}
if record.UserID != s.GetUserIdFromJwtToken(c) {
return s.WriteMessage(c, ErrYouDontOwnTheRecord, http.StatusBadRequest)
}
// Soft delete the record
updated, err := s.repo.DiscordWebHooks.SoftDelete(c.Request().Context(), int64(id))
if err != nil {
2024-04-23 22:18:07 -07:00
return c.JSON(http.StatusInternalServerError, err.Error())
}
2024-04-23 22:18:07 -07:00
if updated != 1 {
return s.WriteMessage(c, ErrFailedToUpdateRecord, http.StatusInternalServerError)
}
item, err := s.repo.DiscordWebHooks.GetById(c.Request().Context(), int64(id))
if err != nil {
return s.WriteError(c, err, http.StatusInternalServerError)
}
var dtos []domain.DiscordWebHookDto
dtos = append(dtos, services.DiscordWebhookToDto(item))
return c.JSON(http.StatusOK, domain.DiscordWebhookResponse{
BaseResponse: domain.BaseResponse{
Message: ResponseMessageSuccess,
},
Payload: dtos,
})
}
// UpdateDiscordWebHook
// @Summary Updates a valid discord webhook ID based on the body given.
// @Param id path string true "id"
// @Tags DiscordWebhook
// @Router /v1/discord/webhooks/{id} [patch]
// @Success 200 {object} domain.DiscordWebhookResponse "OK"
// @Failure 400 {object} domain.BaseResponse
// @Failure 500 {object} domain.BaseResponse
//func (s *Handler) UpdateDiscordWebHook(c echo.Context) error {
// id, err := strconv.Atoi(c.Param("ID"))
// if err != nil {
// return c.JSON(http.StatusInternalServerError, err.Error())
// }
//
// // Check to make sure we can find the record
// _, err = s.repo.DiscordWebHooks.GetById(c.Request().Context(), int64(id))
// if err != nil {
// return c.JSON(http.StatusInternalServerError, err.Error())
// }
//
// // Soft delete the record
// updated, err := s.repo.DiscordWebHooks(c.Request().Context(), int64(id))
// if err != nil {
// return c.JSON(http.StatusInternalServerError, err.Error())
// }
//
// _, err = s.Db.GetDiscordQueueByID(c.Request().Context(), uuid)
// if err != nil {
// return c.JSON(http.StatusInternalServerError, err.Error())
// }
//
// return nil
//}