feat(help-center): expose public article show
This commit is contained in:
@@ -116,6 +116,60 @@ func (h *ArticleHandler) PublicSearch(c *gin.Context) {
|
||||
})
|
||||
}
|
||||
|
||||
// PublicShow returns a public help-center article by slug.
|
||||
// GET /hc/:slug/articles/:article_slug
|
||||
func (h *ArticleHandler) PublicShow(c *gin.Context) {
|
||||
portal, article, ok := h.resolvePublicArticle(c)
|
||||
if !ok {
|
||||
return
|
||||
}
|
||||
article.Portal = *portal
|
||||
c.JSON(http.StatusOK, publicArticlePayload(article, portal.Slug))
|
||||
}
|
||||
|
||||
// PublicArticle dispatches Chatwoot article, markdown, and tracking-pixel suffix paths.
|
||||
func (h *ArticleHandler) PublicArticle(c *gin.Context) {
|
||||
slug := publicArticleSlugRaw(c)
|
||||
switch {
|
||||
case strings.HasSuffix(slug, ".md"):
|
||||
h.PublicMarkdown(c)
|
||||
case strings.HasSuffix(slug, ".png"):
|
||||
h.PublicTrackingPixel(c)
|
||||
default:
|
||||
h.PublicShow(c)
|
||||
}
|
||||
}
|
||||
|
||||
// PublicMarkdown returns the raw markdown for a published public article.
|
||||
// GET /hc/:slug/articles/:article_slug.md
|
||||
func (h *ArticleHandler) PublicMarkdown(c *gin.Context) {
|
||||
_, article, ok := h.resolvePublicArticle(c)
|
||||
if !ok {
|
||||
return
|
||||
}
|
||||
if article.Status != string(model.ArticleStatusPublished) {
|
||||
response.AbortWithStatusError(c, http.StatusNotFound, response.ErrNotFound, "article not found")
|
||||
return
|
||||
}
|
||||
c.Data(http.StatusOK, "text/markdown; charset=utf-8", []byte(article.Content))
|
||||
}
|
||||
|
||||
// PublicTrackingPixel increments views for published articles and serves a 1x1 PNG.
|
||||
// GET /hc/:slug/articles/:article_slug.png
|
||||
func (h *ArticleHandler) PublicTrackingPixel(c *gin.Context) {
|
||||
_, article, ok := h.resolvePublicArticle(c)
|
||||
if !ok {
|
||||
return
|
||||
}
|
||||
if article.Status == string(model.ArticleStatusPublished) {
|
||||
if err := h.svc.IncrementViews(c.Request.Context(), article.ID); err != nil {
|
||||
applogger.L().Warnf("Increment public article pixel views %d: %v", article.ID, err)
|
||||
}
|
||||
}
|
||||
c.Header("Cache-Control", "private, max-age=86400")
|
||||
c.Data(http.StatusOK, "image/png", publicArticleTrackingPixelPNG)
|
||||
}
|
||||
|
||||
// NewArticleHandler creates a new ArticleHandler.
|
||||
func NewArticleHandler(svc *service.ArticleService, portalSvc ...*service.PortalService) *ArticleHandler {
|
||||
h := &ArticleHandler{svc: svc}
|
||||
@@ -563,6 +617,46 @@ func (h *ArticleHandler) resolvePublicPortal(c *gin.Context) (*model.Portal, boo
|
||||
return portal, true
|
||||
}
|
||||
|
||||
func (h *ArticleHandler) resolvePublicArticle(c *gin.Context) (*model.Portal, *model.Article, bool) {
|
||||
portal, ok := h.resolvePublicPortal(c)
|
||||
if !ok {
|
||||
return nil, nil, false
|
||||
}
|
||||
slug := publicArticleSlugParam(c)
|
||||
article, err := h.svc.GetByPortalAndSlug(c.Request.Context(), portal.ID, slug)
|
||||
if err != nil {
|
||||
response.AbortWithStatusError(c, http.StatusNotFound, response.ErrNotFound, "article not found")
|
||||
return nil, nil, false
|
||||
}
|
||||
return portal, article, true
|
||||
}
|
||||
|
||||
func publicArticleSlugParam(c *gin.Context) string {
|
||||
value := publicArticleSlugRaw(c)
|
||||
return strings.TrimSuffix(strings.TrimSuffix(value, ".md"), ".png")
|
||||
}
|
||||
|
||||
func publicArticleSlugRaw(c *gin.Context) string {
|
||||
for _, key := range []string{"article_slug", "article_slug.md", "article_slug.png"} {
|
||||
if value := c.Param(key); value != "" {
|
||||
return value
|
||||
}
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
var publicArticleTrackingPixelPNG = []byte{
|
||||
0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a,
|
||||
0x00, 0x00, 0x00, 0x0d, 0x49, 0x48, 0x44, 0x52,
|
||||
0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x01,
|
||||
0x08, 0x06, 0x00, 0x00, 0x00, 0x1f, 0x15, 0xc4,
|
||||
0x89, 0x00, 0x00, 0x00, 0x0a, 0x49, 0x44, 0x41,
|
||||
0x54, 0x78, 0x9c, 0x63, 0x00, 0x01, 0x00, 0x00,
|
||||
0x05, 0x00, 0x01, 0x0d, 0x0a, 0x2d, 0xb4, 0x00,
|
||||
0x00, 0x00, 0x00, 0x49, 0x45, 0x4e, 0x44, 0xae,
|
||||
0x42, 0x60, 0x82,
|
||||
}
|
||||
|
||||
func publicArticlePagination(c *gin.Context) (int, int, int, bool) {
|
||||
page := 1
|
||||
if raw := c.Query("page"); raw != "" {
|
||||
|
||||
@@ -245,6 +245,75 @@ func (s *ArticleHandlerTestSuite) TestPublicSearch_NotFoundForArchivedPortal() {
|
||||
assert.Equal(s.T(), http.StatusNotFound, w.Code)
|
||||
}
|
||||
|
||||
func (s *ArticleHandlerTestSuite) TestPublicShow_ReturnsArticleBySlug() {
|
||||
portal := &model.Portal{AccountID: s.account.ID, Name: "Show Portal", Slug: "show-portal"}
|
||||
s.Require().NoError(s.db.Create(portal).Error)
|
||||
category := &model.Category{AccountID: s.account.ID, PortalID: portal.ID, Name: "Guides", Slug: "guides", Locale: "en"}
|
||||
s.Require().NoError(s.db.Create(category).Error)
|
||||
article := &model.Article{AccountID: s.account.ID, PortalID: portal.ID, CategoryID: &category.ID, Title: "Install", Slug: "install", Content: "Install content", Status: "published", Locale: "en"}
|
||||
s.Require().NoError(s.db.Create(article).Error)
|
||||
|
||||
r := gin.New()
|
||||
r.GET("/hc/:slug/articles/:article_slug", s.handler.PublicArticle)
|
||||
|
||||
w := httptest.NewRecorder()
|
||||
req, _ := http.NewRequest("GET", "/hc/show-portal/articles/install", nil)
|
||||
r.ServeHTTP(w, req)
|
||||
|
||||
assert.Equal(s.T(), http.StatusOK, w.Code)
|
||||
var payload map[string]any
|
||||
s.Require().NoError(json.Unmarshal(w.Body.Bytes(), &payload))
|
||||
assert.Equal(s.T(), "install", payload["slug"])
|
||||
assert.Equal(s.T(), "Install content", payload["content"])
|
||||
assert.Equal(s.T(), "hc/show-portal/articles/install", payload["link"])
|
||||
assert.Equal(s.T(), "guides", payload["category"].(map[string]any)["slug"])
|
||||
}
|
||||
|
||||
func (s *ArticleHandlerTestSuite) TestPublicMarkdown_ReturnsOnlyPublishedMarkdown() {
|
||||
portal := &model.Portal{AccountID: s.account.ID, Name: "Markdown Portal", Slug: "markdown-portal"}
|
||||
s.Require().NoError(s.db.Create(portal).Error)
|
||||
s.Require().NoError(s.db.Create(&model.Article{AccountID: s.account.ID, PortalID: portal.ID, Title: "Markdown", Slug: "markdown", Content: "# Raw markdown", Status: "published", Locale: "en"}).Error)
|
||||
s.Require().NoError(s.db.Create(&model.Article{AccountID: s.account.ID, PortalID: portal.ID, Title: "Draft", Slug: "markdown-draft", Content: "draft", Status: "draft", Locale: "en"}).Error)
|
||||
|
||||
r := gin.New()
|
||||
r.GET("/hc/:slug/articles/:article_slug", s.handler.PublicArticle)
|
||||
|
||||
w := httptest.NewRecorder()
|
||||
req, _ := http.NewRequest("GET", "/hc/markdown-portal/articles/markdown.md", nil)
|
||||
r.ServeHTTP(w, req)
|
||||
|
||||
assert.Equal(s.T(), http.StatusOK, w.Code)
|
||||
assert.Equal(s.T(), "text/markdown; charset=utf-8", w.Header().Get("Content-Type"))
|
||||
assert.Equal(s.T(), "# Raw markdown", w.Body.String())
|
||||
|
||||
w = httptest.NewRecorder()
|
||||
req, _ = http.NewRequest("GET", "/hc/markdown-portal/articles/markdown-draft.md", nil)
|
||||
r.ServeHTTP(w, req)
|
||||
|
||||
assert.Equal(s.T(), http.StatusNotFound, w.Code)
|
||||
}
|
||||
|
||||
func (s *ArticleHandlerTestSuite) TestPublicTrackingPixel_IncrementsPublishedArticleViews() {
|
||||
portal := &model.Portal{AccountID: s.account.ID, Name: "Pixel Portal", Slug: "pixel-portal"}
|
||||
s.Require().NoError(s.db.Create(portal).Error)
|
||||
article := &model.Article{AccountID: s.account.ID, PortalID: portal.ID, Title: "Pixel", Slug: "pixel", Content: "pixel", Status: "published", Locale: "en", Views: 3}
|
||||
s.Require().NoError(s.db.Create(article).Error)
|
||||
|
||||
r := gin.New()
|
||||
r.GET("/hc/:slug/articles/:article_slug", s.handler.PublicArticle)
|
||||
|
||||
w := httptest.NewRecorder()
|
||||
req, _ := http.NewRequest("GET", "/hc/pixel-portal/articles/pixel.png", nil)
|
||||
r.ServeHTTP(w, req)
|
||||
|
||||
assert.Equal(s.T(), http.StatusOK, w.Code)
|
||||
assert.Equal(s.T(), "image/png", w.Header().Get("Content-Type"))
|
||||
assert.NotEmpty(s.T(), w.Body.Bytes())
|
||||
var updated model.Article
|
||||
s.Require().NoError(s.db.First(&updated, article.ID).Error)
|
||||
assert.Equal(s.T(), 4, updated.Views)
|
||||
}
|
||||
|
||||
func (s *ArticleHandlerTestSuite) TestStatusCounts_BadRequest_InvalidPortalID() {
|
||||
r := gin.New()
|
||||
r.GET("/api/v1/accounts/:account_id/portals/:portal_id/articles/status_counts", s.handler.StatusCounts)
|
||||
|
||||
Reference in New Issue
Block a user