9
"github.com/go-chi/chi/v5"
10
"xelbot.com/reprogl/container"
11
"xelbot.com/reprogl/models"
12
"xelbot.com/reprogl/models/repositories"
13
"xelbot.com/reprogl/session"
14
"xelbot.com/reprogl/views"
17
func PageAction(app *container.Application) http.HandlerFunc {
18
return func(w http.ResponseWriter, r *http.Request) {
19
slug := chi.URLParam(r, "slug")
22
if user, ok := session.GetIdentity(r.Context()); ok {
23
isAdmin = user.IsAdmin()
26
repo := repositories.ArticleRepository{DB: app.DB}
27
article, err := repo.GetBySlug(slug, isAdmin)
29
if errors.Is(err, models.RecordNotFound) {
32
app.ServerError(w, err)
38
tagRepo := repositories.TagRepository{DB: app.DB}
39
article.Tags, err = tagRepo.GetCollectionByArticle(article)
41
app.ServerError(w, err)
46
commentRepo := repositories.CommentRepository{DB: app.DB}
47
lastUpdate, err := commentRepo.GetLastUpdate(article.ID)
49
app.ServerError(w, err)
54
cache := app.GetIntCache()
58
if recentID, found = cache.Get("last_recent_id"); !found {
59
recentID, err = repo.GetLastRecentPostsID()
61
app.ServerError(w, err)
65
cache.Set("last_recent_id", recentID, 24*time.Hour)
67
if article.ID >= recentID {
68
article.RecentPostsID = strconv.Itoa(article.ID)
70
article.RecentPostsID = "0"
73
templateData := views.NewArticlePageData(
76
r.Header.Get("Accept"),
78
cfg := container.GetConfig()
79
templateData.AppendTitle(article.Title)
80
templateData.SetCanonical(container.GenerateAbsoluteURL("article", "slug", slug))
81
templateData.SetOpenGraphProperty("og:type", "article")
82
templateData.SetOpenGraphProperty("article:published_time", article.CreatedAt.Format(time.RFC3339))
83
templateData.SetOpenGraphProperty("article:modified_time", article.UpdatedAt.Format(time.RFC3339))
84
templateData.SetOpenGraphProperty("article:author", cfg.Author.FullName)
85
if article.HasImage() {
86
image := article.SrcImageForOpenGraph()
88
templateData.SetOpenGraphProperty("og:image", cfg.CDNBaseURL+"/uploads/"+image.Path)
89
templateData.SetOpenGraphProperty("og:image:width", strconv.Itoa(image.Width))
90
templateData.SetOpenGraphProperty("og:image:height", strconv.Itoa(image.Height))
92
if article.Alt.Valid && len(article.Alt.String) > 0 {
93
templateData.SetOpenGraphProperty("og:image:alt", article.Alt.String)
97
err = views.WriteTemplateWithContext(r.Context(), w, "article.gohtml", templateData)
99
app.ServerError(w, err)