Skip to content

Commit

Permalink
POST endpoint to subscribe to messages
Browse files Browse the repository at this point in the history
  • Loading branch information
cfindlayisme committed May 23, 2024
1 parent 47c40b9 commit b62a0f9
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 7 deletions.
19 changes: 19 additions & 0 deletions bruno/POST Privmsg Subscription.bru
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
meta {
name: POST Privmsg Subscription
type: http
seq: 3
}

post {
url: http://localhost:8080/subscription
body: json
auth: none
}

body:json {
{
"Target": "Test",
"URL": "http://localhost/404",
"Password": "{{WMB_PASSWORD}}"
}
}
1 change: 1 addition & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ func main() {
router.POST("/message", requesthandlers.PostMessage)
router.GET("/message", requesthandlers.QueryMessage)
router.POST("/directedMessage", requesthandlers.PostDirectedMessage)
router.POST("/subscription", requesthandlers.PostSubscribePrivmsg)

err := router.Run(listenAddress)

Expand Down
10 changes: 8 additions & 2 deletions model/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,12 @@ type DirectedIncomingMessage struct {
}

type DirectedOutgoingMessage struct {
Target string `json:"target"`
Message string `json:"message"`
Target string
Message string
}

type PrivmsgSubscription struct {
Target string
URL string
Password string
}
44 changes: 39 additions & 5 deletions requesthandlers/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"github.com/cfindlayisme/wmb/env"
"github.com/cfindlayisme/wmb/ircclient"
"github.com/cfindlayisme/wmb/model"
"github.com/cfindlayisme/wmb/webhook"
"github.com/gin-gonic/gin"
)

Expand All @@ -15,13 +16,20 @@ func validateMessage(msg model.IncomingMessage, c *gin.Context) bool {
c.String(http.StatusBadRequest, "Message cannot contain newline characters")
return false
}
if env.GetWebhookPassword() != msg.Password {
c.String(http.StatusUnauthorized, "Invalid password")
return false
}
validatePassword(msg.Password, c)

return true
}

func validatePassword(password string, c *gin.Context) bool {
if env.GetWebhookPassword() == password {
return true
}
c.String(http.StatusUnauthorized, "Invalid password")

return false
}

func PostMessage(c *gin.Context) {
var msg model.IncomingMessage

Expand Down Expand Up @@ -71,7 +79,6 @@ func QueryMessage(c *gin.Context) {
var msg model.IncomingMessage

if err := c.ShouldBindQuery(&msg); err != nil {

c.String(http.StatusBadRequest, "Invalid query parameters")
return
}
Expand All @@ -88,3 +95,30 @@ func QueryMessage(c *gin.Context) {
c.String(http.StatusOK, "Message sent")

}

func PostSubscribePrivmsg(c *gin.Context) {
var subscription model.PrivmsgSubscription

if err := c.BindJSON(&subscription); err != nil {
c.String(http.StatusBadRequest, "Invalid query parameters")
return
}

if !validatePassword(subscription.Password, c) {
return
}

success := webhook.SubscribePrivmsg(subscription.Target, subscription.URL)

if success {
c.JSON(http.StatusOK, gin.H{
"status": "success",
"message": "Subscription successful",
})
} else {
c.JSON(http.StatusBadRequest, gin.H{
"status": "failure",
"message": "Subscription failed",
})
}
}

0 comments on commit b62a0f9

Please sign in to comment.