From 5f9fc2a8667a9438b6336d75026f9a455fc9c4ce Mon Sep 17 00:00:00 2001 From: grm Date: Mon, 14 Sep 2026 22:58:08 +0300 Subject: Let the blogger set a post's date, when writing it and afterwards The post date is posts.created_at: it already drives ordering, the archive, the feed and the displayed dates, so making it editable is a form field rather than a new column. The datetime-local input degrades to a text box on old browsers, so the server also accepts the value typed by hand; blank keeps the current date. Co-Authored-By: Claude Opus 5 Claude-Session: https://claude.ai/code/session_01Sd8UPWrvyYCLj97JexNw3A --- AGENTS.md | 7 +++++++ internal/i18n/el.go | 1 + internal/store/posts.go | 13 +++++++++---- internal/web/handlers_posts.go | 25 +++++++++++++++++++++++-- internal/web/static/dashboard.css | 2 +- internal/web/templates/dashboard/post_form.html | 2 ++ internal/web/web_test.go | 18 ++++++++++++++++++ 7 files changed, 61 insertions(+), 7 deletions(-) diff --git a/AGENTS.md b/AGENTS.md index 97798dc..09d9b7d 100644 --- a/AGENTS.md +++ b/AGENTS.md @@ -218,6 +218,13 @@ internal/web/ server.go (host router, middleware, render helpers) Adding a language: an entry in `i18n.languages`, a catalog file, a month table in `monthTables` (nominative, in-date and short forms), and the test gets a copy per language if its rules differ. +- **Post date** is `posts.created_at` — there is no separate "published + at": the blogger sets it on the post form (`posted_at`, a + `datetime-local` input that degrades to a text box; `parsePostDate` + accepts `YYYY-MM-DD[THH:MM]`), for new and existing posts alike, and it + drives ordering, the archive, the feed and the displayed dates. Blank keeps + the current value; a new post starts at now. Times are in the server's + zone, which is also the one dates are rendered in. - **Slugs**: auto-generated from the title; on collision generated slugs get `-2`, `-3`…, user-typed slugs return a 409 with a message. - **Errors**: `store.ErrNotFound` / `store.ErrConflict` are the sentinels diff --git a/internal/i18n/el.go b/internal/i18n/el.go index 55b6cf6..eaaab5f 100644 --- a/internal/i18n/el.go +++ b/internal/i18n/el.go @@ -92,6 +92,7 @@ var el = map[string]string{ "Slug may only contain lowercase letters, digits and dashes.": "Η διεύθυνση επιτρέπει μόνο πεζά λατινικά γράμματα, ψηφία και παύλες.", "Pick a page for this post.": "Επιλέξτε σελίδα για τη δημοσίευση.", "Post is too long (200 KB max).": "Η δημοσίευση είναι πολύ μεγάλη (έως 200 KB).", + "Enter the date as YYYY-MM-DD HH:MM.": "Γράψτε την ημερομηνία ως ΕΕΕΕ-ΜΜ-ΗΗ ΩΩ:ΛΛ.", "A post with that slug already exists on this page; choose another slug.": "Υπάρχει ήδη δημοσίευση με αυτή τη διεύθυνση στη σελίδα· διαλέξτε άλλη.", "Saved. Refresh your blog to see it.": "Αποθηκεύτηκε. Ανανεώστε το ιστολόγιό σας για να το δείτε.", "Image not added:": "Η εικόνα δεν προστέθηκε:", diff --git a/internal/store/posts.go b/internal/store/posts.go index 94bc856..569640b 100644 --- a/internal/store/posts.go +++ b/internal/store/posts.go @@ -108,10 +108,15 @@ func (bs *BlogStore) PublishedPostBySlug(ctx context.Context, pageID int64, slug WHERE p.page_id=$1 AND p.slug=$2 AND p.published`, pageID, slug)) } +// CreatePost inserts the post; a zero CreatedAt means now. The post date is +// the blogger's to choose, so it is a plain column rather than a fixed stamp. func (bs *BlogStore) CreatePost(ctx context.Context, p *Post) (*Post, error) { + if p.CreatedAt.IsZero() { + p.CreatedAt = time.Now() + } var id int64 - err := bs.db.QueryRow(ctx, `INSERT INTO posts (page_id, slug, title, body_md, body_html, published) VALUES ($1,$2,$3,$4,$5,$6) RETURNING id`, - p.PageID, p.Slug, p.Title, p.BodyMD, p.BodyHTML, p.Published).Scan(&id) + err := bs.db.QueryRow(ctx, `INSERT INTO posts (page_id, slug, title, body_md, body_html, published, created_at) VALUES ($1,$2,$3,$4,$5,$6,$7) RETURNING id`, + p.PageID, p.Slug, p.Title, p.BodyMD, p.BodyHTML, p.Published, p.CreatedAt).Scan(&id) if err != nil { return nil, wrap(err) } @@ -119,8 +124,8 @@ func (bs *BlogStore) CreatePost(ctx context.Context, p *Post) (*Post, error) { } func (bs *BlogStore) UpdatePost(ctx context.Context, p *Post) error { - _, err := bs.db.Exec(ctx, `UPDATE posts SET page_id=$2, slug=$3, title=$4, body_md=$5, body_html=$6, published=$7, updated_at=now() WHERE id=$1`, - p.ID, p.PageID, p.Slug, p.Title, p.BodyMD, p.BodyHTML, p.Published) + _, err := bs.db.Exec(ctx, `UPDATE posts SET page_id=$2, slug=$3, title=$4, body_md=$5, body_html=$6, published=$7, created_at=$8, updated_at=now() WHERE id=$1`, + p.ID, p.PageID, p.Slug, p.Title, p.BodyMD, p.BodyHTML, p.Published, p.CreatedAt) return wrap(err) } diff --git a/internal/web/handlers_posts.go b/internal/web/handlers_posts.go index 88d5c20..8327912 100644 --- a/internal/web/handlers_posts.go +++ b/internal/web/handlers_posts.go @@ -5,6 +5,7 @@ import ( "net/http" "strconv" "strings" + "time" "github.com/gramanas/blogspace/internal/markdown" "github.com/gramanas/blogspace/internal/slug" @@ -46,7 +47,7 @@ func (s *Server) handlePostForm(w http.ResponseWriter, r *http.Request) { s.serverError(w, err) return } - p := &store.Post{Published: true} + p := &store.Post{Published: true, CreatedAt: time.Now()} if r.PathValue("id") != "" { if p = s.loadPost(w, r); p == nil { return @@ -66,7 +67,7 @@ func (s *Server) handlePostSave(w http.ResponseWriter, r *http.Request) { s.serverError(w, err) return } - p := &store.Post{} + p := &store.Post{CreatedAt: time.Now()} if r.PathValue("id") != "" { if p = s.loadPost(w, r); p == nil { return @@ -84,6 +85,13 @@ func (s *Server) handlePostSave(w http.ResponseWriter, r *http.Request) { fail := func(status int, msg string) { s.renderStatus(w, r, status, "dashboard/post_form.html", map[string]any{"post": p, "pages": pages, "error": msg}) } + if at := strings.TrimSpace(r.FormValue("posted_at")); at != "" { // blank keeps the current date + var ok bool + if p.CreatedAt, ok = parsePostDate(at); !ok { + fail(http.StatusBadRequest, s.tr(r, "Enter the date as YYYY-MM-DD HH:MM.")) + return + } + } img, err := s.readUpload(r, "inline_image") if err != nil { fail(http.StatusBadRequest, s.tr(r, "Image not added:")+" "+err.Error()) @@ -137,6 +145,19 @@ func (s *Server) handlePostSave(w http.ResponseWriter, r *http.Request) { redirectOK(w, r, "/b/"+blog.Subdomain+"/posts/"+strconv.FormatInt(p.ID, 10)+"/edit", s.tr(r, "Saved. Refresh your blog to see it.")) } +// parsePostDate reads the post form's date: what a datetime-local input sends, +// or the same typed by hand where the browser shows a plain text box. The time +// of day is optional. Times are taken in the server's zone, which is also the +// zone the blog displays them in. +func parsePostDate(v string) (time.Time, bool) { + for _, layout := range []string{"2006-01-02T15:04", "2006-01-02 15:04", "2006-01-02T15:04:05", "2006-01-02"} { + if t, err := time.ParseInLocation(layout, v, time.Local); err == nil { + return t, true + } + } + return time.Time{}, false +} + func (s *Server) handlePostDeleteConfirm(w http.ResponseWriter, r *http.Request) { p := s.loadPost(w, r) if p == nil { diff --git a/internal/web/static/dashboard.css b/internal/web/static/dashboard.css index 7a66ba5..5c766e7 100644 --- a/internal/web/static/dashboard.css +++ b/internal/web/static/dashboard.css @@ -59,7 +59,7 @@ p { margin: 0.6em 0; } label { display: block; margin: 0.8em 0; font-family: "IBM Plex Mono", "SF Mono", Menlo, Consolas, "Liberation Mono", "Courier New", monospace; font-size: 0.8em; text-transform: uppercase; letter-spacing: 0.05em; } label .muted { text-transform: none; letter-spacing: 0; } label code { text-transform: none; letter-spacing: 0; } -input[type=text], input[type=password], input[type=number], input:not([type]), input[type=file], select, textarea { +input[type=text], input[type=password], input[type=number], input[type=datetime-local], input:not([type]), input[type=file], select, textarea { width: 100%; max-width: 100%; padding: 0.5em 0.6em; margin-top: 0.25em; border: 2px solid #1d1a17; font: inherit; font-family: -apple-system, "Segoe UI", "Helvetica Neue", Helvetica, Arial, sans-serif; font-size: 16px; text-transform: none; letter-spacing: 0; background: #fffdf8; color: #1d1a17; } input:focus, select:focus, textarea:focus, button:focus, .btn:focus { outline: 2px solid #c8541a; outline-offset: 1px; } input[type=color] { width: 4em; height: 2.3em; padding: 0.1em; margin-top: 0.25em; border: 2px solid #1d1a17; background: #fffdf8; } diff --git a/internal/web/templates/dashboard/post_form.html b/internal/web/templates/dashboard/post_form.html index af48215..8069a9a 100644 --- a/internal/web/templates/dashboard/post_form.html +++ b/internal/web/templates/dashboard/post_form.html @@ -11,6 +11,8 @@ + {{template "editor" (dict "name" "body" "value" .Data.post.BodyMD "rows" 24 "tall" true "upload" (print "/b/" .Blog.Subdomain "/images/upload") "csrf" .CSRF)}} diff --git a/internal/web/web_test.go b/internal/web/web_test.go index ae26029..d7de43a 100644 --- a/internal/web/web_test.go +++ b/internal/web/web_test.go @@ -220,6 +220,24 @@ func TestValidLinkURL(t *testing.T) { } } +func TestParsePostDate(t *testing.T) { + for in, want := range map[string]time.Time{ + "2026-09-14T08:30": time.Date(2026, 9, 14, 8, 30, 0, 0, time.Local), + "2026-09-14 08:30": time.Date(2026, 9, 14, 8, 30, 0, 0, time.Local), + "2026-09-14T08:30:15": time.Date(2026, 9, 14, 8, 30, 15, 0, time.Local), + "2026-09-14": time.Date(2026, 9, 14, 0, 0, 0, 0, time.Local), + } { + if got, ok := parsePostDate(in); !ok || !got.Equal(want) { + t.Errorf("%q: got %v %v, want %v", in, got, ok, want) + } + } + for _, bad := range []string{"", "14/09/2026", "2026-13-01", "yesterday", "2026-09-14T25:00"} { + if _, ok := parsePostDate(bad); ok { + t.Errorf("%q should be rejected", bad) + } + } +} + func TestGroupArchive(t *testing.T) { d := func(y int, m time.Month, day int) time.Time { return time.Date(y, m, day, 12, 0, 0, 0, time.UTC) } posts := []store.PostRef{{Title: "d", CreatedAt: d(2026, 9, 3)}, {Title: "c", CreatedAt: d(2026, 9, 1)}, {Title: "b", CreatedAt: d(2026, 2, 1)}, {Title: "a", CreatedAt: d(2025, 12, 31)}} -- cgit v1.2.3