indiescrobble/controllers/scrobble.go

190 lines
4.7 KiB
Go
Raw Normal View History

package controllers
import (
"net/http"
2022-02-19 15:47:25 +00:00
"time"
"git.jamesravey.me/ravenscroftj/indiescrobble/config"
"git.jamesravey.me/ravenscroftj/indiescrobble/models"
"git.jamesravey.me/ravenscroftj/indiescrobble/services/micropub"
"git.jamesravey.me/ravenscroftj/indiescrobble/services/scrobble"
"github.com/gin-gonic/gin"
2022-02-19 15:47:25 +00:00
"gorm.io/gorm"
)
2022-02-19 15:47:25 +00:00
type ScrobbleController struct{
db *gorm.DB
scrobbler *scrobble.Scrobbler
}
func NewScrobbleController(db *gorm.DB) *ScrobbleController{
return &ScrobbleController{db, scrobble.NewScrobbler(db)}
}
/*Do the actual post to the user's site*/
func (s *ScrobbleController) DoScrobble(c *gin.Context){
err := c.Request.ParseForm()
// this is an authed endpoint so 'user' must be set and if not panicking is fair
currentUser := c.MustGet("user").(*models.BaseUser)
if err != nil{
c.HTML(http.StatusBadRequest, "error.tmpl", gin.H{
"message": err,
})
2022-02-19 15:47:25 +00:00
return
}
post, err := s.scrobbler.Scrobble(&c.Request.Form, currentUser)
if err != nil{
c.HTML(http.StatusBadRequest, "error.tmpl", gin.H{
"message": err,
})
return
}
c.HTML(http.StatusOK, "scrobble/done.tmpl", gin.H{
"user": currentUser,
"scrobbleTypeName": scrobble.ScrobbleTypeNames[post.PostType],
"post": post,
})
2022-02-19 15:47:25 +00:00
}
2022-02-19 15:47:25 +00:00
/*Display the scrobble form and allow user to search for and add media*/
func (s *ScrobbleController) ScrobbleForm(c *gin.Context){
err := c.Request.ParseForm()
// this is an authed endpoint so 'user' must be set and if not panicking is fair
currentUser := c.MustGet("user").(*models.BaseUser)
if err != nil{
c.HTML(http.StatusBadRequest, "error.tmpl", gin.H{
"message": err,
})
return
}
scrobbleType := c.Request.Form.Get("type")
2022-02-19 15:47:25 +00:00
if c.Request.Form.Get("item") != "" {
item, err := s.scrobbler.GetItemByID(&c.Request.Form)
2022-02-19 15:47:25 +00:00
if err != nil {
c.HTML(http.StatusBadRequest, "error.tmpl", gin.H{
"message": err,
})
return
2022-02-19 15:47:25 +00:00
}else{
c.HTML(http.StatusOK, "scrobble/compose.tmpl", gin.H{
2022-02-19 15:47:25 +00:00
"user": currentUser,
"scrobbleType": scrobbleType,
"scrobblePlaceholder": scrobble.ScrobblePlaceholders[scrobbleType],
"scrobbleTypeName": scrobble.ScrobbleTypeNames[scrobbleType],
"item": item,
"now": time.Now().Format(config.BROWSER_TIME_FORMAT),
2022-02-19 15:47:25 +00:00
})
return
}
2022-02-19 15:47:25 +00:00
}else if query := c.Request.Form.Get("q"); query != "" {
searchResults, err := s.scrobbler.Search(&c.Request.Form)
if err != nil{
c.HTML(http.StatusBadRequest, "error.tmpl", gin.H{
"message": err,
})
return
}
c.HTML(http.StatusOK, "scrobble/search.tmpl", gin.H{
2022-02-19 15:47:25 +00:00
"user": currentUser,
"scrobbleType": scrobbleType,
"scrobblePlaceholder": scrobble.ScrobblePlaceholders[scrobbleType],
"scrobbleTypeName": scrobble.ScrobbleTypeNames[scrobbleType],
"searchEngine": s.scrobbler.GetSearchEngineNameForType(scrobbleType),
"searchResults": searchResults,
"now": time.Now().Format("2006-01-02T15:04"),
})
}else if scrobbleType := c.Request.Form.Get("type"); scrobbleType != "" {
c.HTML(http.StatusOK, "scrobble/search.tmpl", gin.H{
2022-02-19 15:47:25 +00:00
"user": currentUser,
"scrobbleType": scrobbleType,
"scrobblePlaceholder": scrobble.ScrobblePlaceholders[scrobbleType],
"scrobbleTypeName": scrobble.ScrobbleTypeNames[scrobbleType],
"now": time.Now().Format("2006-01-02T15:04"),
})
}else{
c.HTML(http.StatusOK, "scrobble/begin.tmpl", gin.H{
"user": currentUser,
"scrobbleTypes": scrobble.ScrobbleTypeNames,
"now": time.Now().Format("2006-01-02T15:04"),
})
2022-02-19 15:47:25 +00:00
}
}
/*Preview the content of a scrobble to be submitted to \*/
2022-02-19 15:47:25 +00:00
func (s *ScrobbleController) PreviewScrobble(c *gin.Context){
err := c.Request.ParseForm()
// this is an authed endpoint so 'user' must be set and if not panicking is fair
currentUser := c.MustGet("user").(*models.BaseUser)
if err != nil{
c.HTML(http.StatusBadRequest, "error.tmpl", gin.H{
"message": err,
})
}
post, err := s.scrobbler.Preview(&c.Request.Form)
if err != nil{
c.HTML(http.StatusBadRequest, "error.tmpl", gin.H{
"message": err,
})
}
scrobbleType := c.Request.Form.Get("type")
discovery := micropub.MicropubDiscoveryService{}
2022-02-13 15:49:22 +00:00
config, err := discovery.Discover(currentUser.Me, currentUser.Token )
if err != nil{
c.HTML(http.StatusBadRequest, "error.tmpl", gin.H{
"message": err,
})
return
}
postBody, err := s.scrobbler.BuildMicroPubPayload(post)
if err != nil{
c.HTML(http.StatusBadRequest, "error.tmpl", gin.H{
"message": err,
})
return
}
c.HTML(http.StatusOK, "scrobble/preview.tmpl", gin.H{
"user": currentUser,
"scrobbleType": scrobbleType,
"scrobbleTypeName": scrobble.ScrobbleTypeNames[scrobbleType],
"post": post,
2022-02-13 15:49:22 +00:00
"config": config,
"summary": s.scrobbler.GenerateSummary(post),
"postBody": string(postBody),
})
}