James Tombleson
a1324ee1c1
* basic output looks to be working * cron was updated to add to the queue and post messages * new route to make discord webhook subscriptions * updated swag tags * swag * Updated delete subscription call * removed the time value as it throws off the msg template * updated logging * updated swagger * updated new subscription route * Updated logging and remove items from the queue if they dont have a subscription * updated getArticles to return the 50 newest for the portal * added endpoint to see if an item exists already * formatting * updated listArticles * added colors and updated the image * Updated to use the pointer in twitch * added the twitch login command to cron... it works now * found a better way to disable http2 for reddit. Test worked right away too * updated the cron tasks to run collected once and hour or longer depending on the service
116 lines
2.8 KiB
Go
116 lines
2.8 KiB
Go
package routes
|
|
|
|
import (
|
|
"encoding/json"
|
|
"log"
|
|
"net/http"
|
|
"strings"
|
|
|
|
"github.com/google/uuid"
|
|
"github.com/jtom38/newsbot/collector/database"
|
|
)
|
|
|
|
// GetDiscordWebHooks
|
|
// @Summary Returns the top 100 entries from the queue to be processed.
|
|
// @Produce application/json
|
|
// @Tags Config, Discord, Webhook
|
|
// @Router /discord/webhooks [get]
|
|
func (s *Server) GetDiscordWebHooks(w http.ResponseWriter, r *http.Request) {
|
|
w.Header().Set("Content-Type", "application/json")
|
|
|
|
res, err := s.Db.ListDiscordWebhooks(*s.ctx, 100)
|
|
if err != nil {
|
|
w.Write([]byte(err.Error()))
|
|
panic(err)
|
|
}
|
|
|
|
bres, err := json.Marshal(res)
|
|
if err != nil {
|
|
w.Write([]byte(err.Error()))
|
|
panic(err)
|
|
}
|
|
|
|
w.Write(bres)
|
|
}
|
|
|
|
// GetDiscorWebHooksById
|
|
// @Summary Returns the top 100 entries from the queue to be processed.
|
|
// @Produce application/json
|
|
// @Param id query string true "id"
|
|
// @Tags Config, Discord, Webhook
|
|
// @Router /discord/webhooks/byId [get]
|
|
func (s *Server) GetDiscordWebHooksById(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, "id is missing", http.StatusBadRequest)
|
|
return
|
|
}
|
|
|
|
uuid, err := uuid.Parse(_id)
|
|
if err != nil {
|
|
http.Error(w, "unable to parse id value", http.StatusBadRequest)
|
|
return
|
|
}
|
|
|
|
res, err := s.Db.GetDiscordWebHooksByID(*s.ctx, uuid)
|
|
if err != nil {
|
|
http.Error(w, "no record found", http.StatusBadRequest)
|
|
return
|
|
}
|
|
|
|
bres, err := json.Marshal(res)
|
|
if err != nil {
|
|
http.Error(w, "unable to convert to json", http.StatusBadRequest)
|
|
panic(err)
|
|
}
|
|
|
|
w.Write(bres)
|
|
}
|
|
|
|
// 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 Config, Discord, Webhook
|
|
// @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
|
|
}
|
|
if _server == ""{
|
|
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,
|
|
}
|
|
s.Db.CreateDiscordWebHook(*s.ctx, params)
|
|
|
|
bJson, err := json.Marshal(¶ms)
|
|
if err != nil {
|
|
log.Panicln(err)
|
|
}
|
|
w.Header().Set("Content-Type", "application/json")
|
|
w.Write(bJson)
|
|
}
|