2022-06-19 22:02:44 -07:00
|
|
|
package routes
|
|
|
|
|
|
|
|
import (
|
2022-11-30 21:43:53 -08:00
|
|
|
"context"
|
2022-06-19 22:02:44 -07:00
|
|
|
"encoding/json"
|
|
|
|
"net/http"
|
|
|
|
|
|
|
|
"github.com/google/uuid"
|
2022-07-12 15:28:31 -07:00
|
|
|
"github.com/jtom38/newsbot/collector/database"
|
2022-06-19 22:02:44 -07:00
|
|
|
)
|
|
|
|
|
|
|
|
// GetSubscriptions
|
|
|
|
// @Summary Returns the top 100 entries from the queue to be processed.
|
|
|
|
// @Produce application/json
|
2022-12-04 08:49:17 -08:00
|
|
|
// @Tags Subscription
|
2022-06-19 22:02:44 -07:00
|
|
|
// @Router /subscriptions [get]
|
|
|
|
func (s *Server) ListSubscriptions(w http.ResponseWriter, r *http.Request) {
|
|
|
|
w.Header().Set("Content-Type", "application/json")
|
|
|
|
|
|
|
|
res, err := s.Db.ListSubscriptions(*s.ctx, 100)
|
|
|
|
if err != nil {
|
|
|
|
w.Write([]byte(err.Error()))
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
bres, err := json.Marshal(res)
|
|
|
|
if err != nil {
|
|
|
|
http.Error(w, ErrUnableToConvertToJson, http.StatusBadRequest)
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
w.Write(bres)
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetSubscriptionsByDiscordId
|
|
|
|
// @Summary Returns the top 100 entries from the queue to be processed.
|
|
|
|
// @Produce application/json
|
|
|
|
// @Param id query string true "id"
|
2022-12-04 08:49:17 -08:00
|
|
|
// @Tags Subscription
|
2022-06-19 22:02:44 -07:00
|
|
|
// @Router /subscriptions/byDiscordId [get]
|
|
|
|
func (s *Server) GetSubscriptionsByDiscordId(w http.ResponseWriter, r *http.Request) {
|
|
|
|
w.Header().Set("Content-Type", "application/json")
|
|
|
|
|
|
|
|
query := r.URL.Query()
|
|
|
|
_id := query["id"][0]
|
|
|
|
if _id == "" {
|
|
|
|
http.Error(w, ErrIdValueMissing, http.StatusBadRequest)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
uuid, err := uuid.Parse(_id)
|
|
|
|
if err != nil {
|
|
|
|
http.Error(w, ErrValueNotUuid, http.StatusBadRequest)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
res, err := s.Db.GetSubscriptionsByDiscordWebHookId(*s.ctx, uuid)
|
|
|
|
if err != nil {
|
|
|
|
http.Error(w, ErrNoRecordFound, http.StatusBadRequest)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
bres, err := json.Marshal(res)
|
|
|
|
if err != nil {
|
|
|
|
http.Error(w, ErrUnableToConvertToJson, http.StatusBadRequest)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
w.Write(bres)
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetSubscriptionsBySourceId
|
|
|
|
// @Summary Returns the top 100 entries from the queue to be processed.
|
|
|
|
// @Produce application/json
|
|
|
|
// @Param id query string true "id"
|
2022-12-04 08:49:17 -08:00
|
|
|
// @Tags Subscription
|
2022-06-19 22:02:44 -07:00
|
|
|
// @Router /subscriptions/bySourceId [get]
|
|
|
|
func (s *Server) GetSubscriptionsBySourceId(w http.ResponseWriter, r *http.Request) {
|
|
|
|
w.Header().Set("Content-Type", "application/json")
|
|
|
|
|
|
|
|
query := r.URL.Query()
|
|
|
|
_id := query["id"][0]
|
|
|
|
if _id == "" {
|
|
|
|
http.Error(w, ErrIdValueMissing, http.StatusBadRequest)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
uuid, err := uuid.Parse(_id)
|
|
|
|
if err != nil {
|
|
|
|
http.Error(w, ErrValueNotUuid, http.StatusBadRequest)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
res, err := s.Db.GetSubscriptionsByDiscordWebHookId(*s.ctx, uuid)
|
|
|
|
if err != nil {
|
|
|
|
http.Error(w, ErrNoRecordFound, http.StatusBadRequest)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
bres, err := json.Marshal(res)
|
|
|
|
if err != nil {
|
|
|
|
http.Error(w, ErrUnableToConvertToJson, http.StatusBadRequest)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
w.Write(bres)
|
2022-07-12 15:28:31 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewDiscordWebHookSubscription
|
|
|
|
// @Summary Creates a new subscription to link a post from a Source to a DiscordWebHook.
|
|
|
|
// @Param discordWebHookId query string true "discordWebHookId"
|
|
|
|
// @Param sourceId query string true "sourceId"
|
2022-12-04 08:49:17 -08:00
|
|
|
// @Tags Subscription
|
2022-07-12 15:28:31 -07:00
|
|
|
// @Router /subscriptions/new/discordwebhook [post]
|
|
|
|
func (s *Server) newDiscordWebHookSubscription(w http.ResponseWriter, r *http.Request) {
|
|
|
|
// Extract the values given
|
|
|
|
query := r.URL.Query()
|
|
|
|
discordWebHookId := query["discordWebHookId"][0]
|
|
|
|
sourceId := query["sourceId"][0]
|
|
|
|
|
|
|
|
// Check to make we didnt get a null
|
|
|
|
if discordWebHookId == "" {
|
2022-11-30 21:43:53 -08:00
|
|
|
http.Error(w, "invalid discordWebHooksId given", http.StatusBadRequest)
|
2022-07-12 15:28:31 -07:00
|
|
|
return
|
|
|
|
}
|
|
|
|
if sourceId == "" {
|
2022-11-30 21:43:53 -08:00
|
|
|
http.Error(w, "invalid sourceID given", http.StatusBadRequest)
|
2022-07-12 15:28:31 -07:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Valide they are UUID values
|
|
|
|
uHook, err := uuid.Parse(discordWebHookId)
|
|
|
|
if err != nil {
|
|
|
|
http.Error(w, "DiscordWebHooksID was not a uuid value.", http.StatusBadRequest)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
uSource, err := uuid.Parse(sourceId)
|
|
|
|
if err != nil {
|
|
|
|
http.Error(w, "SourceId was not a uuid value", http.StatusBadRequest)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check if the sub already exists
|
|
|
|
item, err := s.Db.QuerySubscriptions(*s.ctx, database.QuerySubscriptionsParams{
|
2022-11-30 21:43:53 -08:00
|
|
|
Discordwebhookid: uHook,
|
|
|
|
Sourceid: uSource,
|
2022-07-12 15:28:31 -07:00
|
|
|
})
|
|
|
|
if err == nil {
|
|
|
|
bJson, err := json.Marshal(&item)
|
|
|
|
if err != nil {
|
|
|
|
http.Error(w, err.Error(), http.StatusBadRequest)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
w.Header().Set("Content-Type", "application/json")
|
|
|
|
w.Write(bJson)
|
|
|
|
return
|
|
|
|
}
|
2022-11-30 21:43:53 -08:00
|
|
|
|
2022-07-12 15:28:31 -07:00
|
|
|
// Does not exist, so make it.
|
|
|
|
params := database.CreateSubscriptionParams{
|
2022-11-30 21:43:53 -08:00
|
|
|
ID: uuid.New(),
|
2022-07-12 15:28:31 -07:00
|
|
|
Discordwebhookid: uHook,
|
2022-11-30 21:43:53 -08:00
|
|
|
Sourceid: uSource,
|
2022-07-12 15:28:31 -07:00
|
|
|
}
|
|
|
|
s.Db.CreateSubscription(*s.ctx, params)
|
2022-11-30 21:43:53 -08:00
|
|
|
|
2022-07-12 15:28:31 -07:00
|
|
|
bJson, err := json.Marshal(¶ms)
|
|
|
|
if err != nil {
|
|
|
|
http.Error(w, err.Error(), http.StatusBadRequest)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
w.Header().Set("Content-Type", "application/json")
|
|
|
|
w.Write(bJson)
|
2022-11-30 21:43:53 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
// DeleteDiscordWebHookSubscription
|
|
|
|
// @Summary Removes a Discord WebHook Subscription based on the Subscription ID.
|
|
|
|
// @Param Id query string true "Id"
|
|
|
|
// @Tags Config, Source, Discord, Subscription
|
|
|
|
// @Router /subscriptions/discord/webhook/delete [delete]
|
|
|
|
func (s *Server) DeleteDiscordWebHookSubscription(w http.ResponseWriter, r *http.Request) {
|
|
|
|
var ErrMissingSubscriptionID string = "Request was missing a 'Id' or was a invalid UUID."
|
|
|
|
query := r.URL.Query()
|
|
|
|
|
|
|
|
uid, err := uuid.Parse(query["Id"][0])
|
|
|
|
if err != nil {
|
|
|
|
http.Error(w, ErrMissingSubscriptionID, http.StatusBadRequest)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
err = s.Db.DeleteSubscription(context.Background(), uid)
|
|
|
|
if err != nil {
|
|
|
|
http.Error(w, err.Error(), http.StatusBadRequest)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|