Compare commits
1
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
e09b7a7040 |
+2
-5
@@ -17,7 +17,6 @@ import (
|
|||||||
|
|
||||||
"plumber"
|
"plumber"
|
||||||
"plumber/internal/blob"
|
"plumber/internal/blob"
|
||||||
"plumber/internal/events"
|
|
||||||
"plumber/internal/mail"
|
"plumber/internal/mail"
|
||||||
"plumber/internal/store"
|
"plumber/internal/store"
|
||||||
"plumber/internal/web"
|
"plumber/internal/web"
|
||||||
@@ -35,7 +34,7 @@ func main() {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatalf("mail: %v", err)
|
log.Fatalf("mail: %v", err)
|
||||||
}
|
}
|
||||||
handler := newHandler(db, sessions, uploader, notifier, events.New())
|
handler := newHandler(db, sessions, uploader, notifier)
|
||||||
run(&http.Server{
|
run(&http.Server{
|
||||||
Addr: listenAddr(),
|
Addr: listenAddr(),
|
||||||
Handler: handler,
|
Handler: handler,
|
||||||
@@ -59,15 +58,13 @@ func openDB() (*sql.DB, *store.SessionStore) {
|
|||||||
return db, sessions
|
return db, sessions
|
||||||
}
|
}
|
||||||
|
|
||||||
func newHandler(db *sql.DB, sessions *store.SessionStore, uploader blob.Uploader, notifier mail.Notifier, bus events.Publisher) http.Handler {
|
func newHandler(db *sql.DB, sessions *store.SessionStore, uploader blob.Uploader, notifier mail.Notifier) http.Handler {
|
||||||
srv, err := web.New(store.NewPostgres(db), sessions.Store(), plumber.TemplateFS, plumber.StaticFS, web.Config{
|
srv, err := web.New(store.NewPostgres(db), sessions.Store(), plumber.TemplateFS, plumber.StaticFS, web.Config{
|
||||||
AdminSetupSecret: strings.TrimSpace(os.Getenv("ADMIN_SETUP_SECRET")),
|
AdminSetupSecret: strings.TrimSpace(os.Getenv("ADMIN_SETUP_SECRET")),
|
||||||
SecureCookie: secureCookieFromEnv(),
|
SecureCookie: secureCookieFromEnv(),
|
||||||
TrustedProxies: parseTrustedProxies(os.Getenv("TRUSTED_PROXY_CIDRS")),
|
TrustedProxies: parseTrustedProxies(os.Getenv("TRUSTED_PROXY_CIDRS")),
|
||||||
Blob: uploader,
|
Blob: uploader,
|
||||||
Mail: notifier,
|
Mail: notifier,
|
||||||
Events: bus,
|
|
||||||
BaseURL: strings.TrimRight(strings.TrimSpace(os.Getenv("APP_BASE_URL")), "/"),
|
|
||||||
})
|
})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatalf("server: %v", err)
|
log.Fatalf("server: %v", err)
|
||||||
|
|||||||
@@ -61,52 +61,6 @@ SET
|
|||||||
updated_at = sqlc.arg(updated_at)
|
updated_at = sqlc.arg(updated_at)
|
||||||
WHERE id = sqlc.arg(id);
|
WHERE id = sqlc.arg(id);
|
||||||
|
|
||||||
-- name: CreatePostImage :exec
|
|
||||||
INSERT INTO post_images (
|
|
||||||
id, post_id, object_key, public_url, description, position, width, height, created_at
|
|
||||||
)
|
|
||||||
VALUES (
|
|
||||||
sqlc.arg(id),
|
|
||||||
sqlc.arg(post_id),
|
|
||||||
sqlc.arg(object_key),
|
|
||||||
sqlc.arg(public_url),
|
|
||||||
sqlc.arg(description),
|
|
||||||
sqlc.arg(position),
|
|
||||||
sqlc.arg(width),
|
|
||||||
sqlc.arg(height),
|
|
||||||
sqlc.arg(created_at)
|
|
||||||
);
|
|
||||||
|
|
||||||
-- name: DeletePostImages :exec
|
|
||||||
DELETE FROM post_images
|
|
||||||
WHERE post_id = sqlc.arg(post_id);
|
|
||||||
|
|
||||||
-- name: ListPostImages :many
|
|
||||||
SELECT
|
|
||||||
id, post_id, object_key, public_url, description, position, width, height, created_at
|
|
||||||
FROM post_images
|
|
||||||
WHERE post_id = sqlc.arg(post_id)
|
|
||||||
ORDER BY position;
|
|
||||||
|
|
||||||
-- name: ListPostThreadImages :many
|
|
||||||
WITH RECURSIVE thread AS (
|
|
||||||
SELECT p.id
|
|
||||||
FROM posts p
|
|
||||||
WHERE p.id = sqlc.arg(root_id) AND p.parent_id IS NULL
|
|
||||||
|
|
||||||
UNION ALL
|
|
||||||
|
|
||||||
SELECT child.id
|
|
||||||
FROM posts child
|
|
||||||
JOIN thread parent ON child.parent_id = parent.id
|
|
||||||
)
|
|
||||||
SELECT
|
|
||||||
images.id, images.post_id, images.object_key, images.public_url,
|
|
||||||
images.description, images.position, images.width, images.height, images.created_at
|
|
||||||
FROM post_images images
|
|
||||||
JOIN thread ON thread.id = images.post_id
|
|
||||||
ORDER BY images.post_id, images.position;
|
|
||||||
|
|
||||||
-- name: UpdateRootPostState :execrows
|
-- name: UpdateRootPostState :execrows
|
||||||
UPDATE posts
|
UPDATE posts
|
||||||
SET
|
SET
|
||||||
|
|||||||
@@ -1,79 +0,0 @@
|
|||||||
package events
|
|
||||||
|
|
||||||
import (
|
|
||||||
"context"
|
|
||||||
"log"
|
|
||||||
"sync"
|
|
||||||
)
|
|
||||||
|
|
||||||
const defaultBuffer = 64
|
|
||||||
|
|
||||||
// Publisher is the site-facing write side of the bus.
|
|
||||||
type Publisher interface {
|
|
||||||
Publish(ctx context.Context, ev any)
|
|
||||||
}
|
|
||||||
|
|
||||||
// Bus is an in-process pub/sub with one worker and a bounded queue.
|
|
||||||
type Bus struct {
|
|
||||||
ch chan any
|
|
||||||
mu sync.Mutex
|
|
||||||
subs []func(context.Context, any)
|
|
||||||
closed sync.Once
|
|
||||||
}
|
|
||||||
|
|
||||||
// New starts a worker that delivers events to subscribers in publish order.
|
|
||||||
func New() *Bus {
|
|
||||||
return newBus(defaultBuffer, true)
|
|
||||||
}
|
|
||||||
|
|
||||||
func newBus(buffer int, start bool) *Bus {
|
|
||||||
if buffer < 1 {
|
|
||||||
buffer = 1
|
|
||||||
}
|
|
||||||
b := &Bus{ch: make(chan any, buffer)}
|
|
||||||
if start {
|
|
||||||
go b.loop()
|
|
||||||
}
|
|
||||||
return b
|
|
||||||
}
|
|
||||||
|
|
||||||
// Publish enqueues ev. It never blocks the caller; a full buffer is dropped.
|
|
||||||
func (b *Bus) Publish(_ context.Context, ev any) {
|
|
||||||
if b == nil {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
select {
|
|
||||||
case b.ch <- ev:
|
|
||||||
default:
|
|
||||||
log.Printf("events: dropped %T", ev)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Subscribe adds a handler. Handlers run serially on the worker.
|
|
||||||
func (b *Bus) Subscribe(fn func(context.Context, any)) {
|
|
||||||
if b == nil || fn == nil {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
b.mu.Lock()
|
|
||||||
b.subs = append(b.subs, fn)
|
|
||||||
b.mu.Unlock()
|
|
||||||
}
|
|
||||||
|
|
||||||
func (b *Bus) loop() {
|
|
||||||
for ev := range b.ch {
|
|
||||||
b.mu.Lock()
|
|
||||||
subs := append([]func(context.Context, any){}, b.subs...)
|
|
||||||
b.mu.Unlock()
|
|
||||||
for _, fn := range subs {
|
|
||||||
fn(context.Background(), ev)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Close stops the worker. Safe to call more than once.
|
|
||||||
func (b *Bus) Close() {
|
|
||||||
if b == nil {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
b.closed.Do(func() { close(b.ch) })
|
|
||||||
}
|
|
||||||
@@ -1,104 +0,0 @@
|
|||||||
package events
|
|
||||||
|
|
||||||
import (
|
|
||||||
"context"
|
|
||||||
"sync"
|
|
||||||
"testing"
|
|
||||||
"time"
|
|
||||||
)
|
|
||||||
|
|
||||||
func TestPermalink(t *testing.T) {
|
|
||||||
t.Parallel()
|
|
||||||
|
|
||||||
tests := []struct {
|
|
||||||
base, root, post, want string
|
|
||||||
}{
|
|
||||||
{"", "root-1", "post-2", "/questions/root-1#post-post-2"},
|
|
||||||
{"https://www.askaplumberfirst.com/", "root-1", "post-2", "https://www.askaplumberfirst.com/questions/root-1#post-post-2"},
|
|
||||||
{"https://www.askaplumberfirst.com", "a b", "c/d", "https://www.askaplumberfirst.com/questions/a%20b#post-c%2Fd"},
|
|
||||||
}
|
|
||||||
for _, tc := range tests {
|
|
||||||
if got := Permalink(tc.base, tc.root, tc.post); got != tc.want {
|
|
||||||
t.Fatalf("Permalink(%q, %q, %q) = %q, want %q", tc.base, tc.root, tc.post, got, tc.want)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestNopAndRecording(t *testing.T) {
|
|
||||||
t.Parallel()
|
|
||||||
|
|
||||||
Nop{}.Publish(context.Background(), PostCreated{})
|
|
||||||
|
|
||||||
rec := &Recording{}
|
|
||||||
rec.Publish(context.Background(), PostCreated{PostEvent: PostEvent{PostID: "a"}})
|
|
||||||
rec.Publish(context.Background(), PostUpdated{PostEvent: PostEvent{PostID: "b"}})
|
|
||||||
if rec.Len() != 2 {
|
|
||||||
t.Fatalf("len = %d", rec.Len())
|
|
||||||
}
|
|
||||||
got := rec.Snapshot()
|
|
||||||
created, ok := got[0].(PostCreated)
|
|
||||||
if !ok || created.PostID != "a" {
|
|
||||||
t.Fatalf("first = %#v", got[0])
|
|
||||||
}
|
|
||||||
updated, ok := got[1].(PostUpdated)
|
|
||||||
if !ok || updated.PostID != "b" {
|
|
||||||
t.Fatalf("second = %#v", got[1])
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestBusDeliversInOrder(t *testing.T) {
|
|
||||||
t.Parallel()
|
|
||||||
|
|
||||||
bus := New()
|
|
||||||
defer bus.Close()
|
|
||||||
|
|
||||||
var mu sync.Mutex
|
|
||||||
var got []string
|
|
||||||
done := make(chan struct{})
|
|
||||||
bus.Subscribe(func(_ context.Context, ev any) {
|
|
||||||
mu.Lock()
|
|
||||||
got = append(got, ev.(string))
|
|
||||||
if len(got) == 3 {
|
|
||||||
close(done)
|
|
||||||
}
|
|
||||||
mu.Unlock()
|
|
||||||
})
|
|
||||||
|
|
||||||
ctx := context.Background()
|
|
||||||
bus.Publish(ctx, "one")
|
|
||||||
bus.Publish(ctx, "two")
|
|
||||||
bus.Publish(ctx, "three")
|
|
||||||
|
|
||||||
select {
|
|
||||||
case <-done:
|
|
||||||
case <-time.After(time.Second):
|
|
||||||
t.Fatal("timed out waiting for events")
|
|
||||||
}
|
|
||||||
mu.Lock()
|
|
||||||
defer mu.Unlock()
|
|
||||||
if len(got) != 3 || got[0] != "one" || got[1] != "two" || got[2] != "three" {
|
|
||||||
t.Fatalf("got %v", got)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestBusDropsWhenFull(t *testing.T) {
|
|
||||||
t.Parallel()
|
|
||||||
|
|
||||||
bus := newBus(1, false)
|
|
||||||
bus.Publish(context.Background(), "kept")
|
|
||||||
bus.Publish(context.Background(), "dropped")
|
|
||||||
|
|
||||||
select {
|
|
||||||
case ev := <-bus.ch:
|
|
||||||
if ev != "kept" {
|
|
||||||
t.Fatalf("got %v", ev)
|
|
||||||
}
|
|
||||||
default:
|
|
||||||
t.Fatal("expected buffered event")
|
|
||||||
}
|
|
||||||
select {
|
|
||||||
case ev := <-bus.ch:
|
|
||||||
t.Fatalf("unexpected extra event %v", ev)
|
|
||||||
default:
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,47 +0,0 @@
|
|||||||
package events
|
|
||||||
|
|
||||||
import (
|
|
||||||
"net/url"
|
|
||||||
"strings"
|
|
||||||
)
|
|
||||||
|
|
||||||
// Image is a public photo already attached to a site post.
|
|
||||||
type Image struct {
|
|
||||||
URL string
|
|
||||||
Description string
|
|
||||||
}
|
|
||||||
|
|
||||||
// PostEvent is a Discord-free snapshot of a site post after a successful write.
|
|
||||||
type PostEvent struct {
|
|
||||||
PostID string
|
|
||||||
RootID string
|
|
||||||
ParentID string
|
|
||||||
Title string
|
|
||||||
Body string
|
|
||||||
City string
|
|
||||||
AuthorID string
|
|
||||||
AuthorName string
|
|
||||||
AuthorRole string
|
|
||||||
Images []Image
|
|
||||||
Permalink string
|
|
||||||
}
|
|
||||||
|
|
||||||
// PostCreated is emitted after a successful site create.
|
|
||||||
type PostCreated struct {
|
|
||||||
PostEvent
|
|
||||||
}
|
|
||||||
|
|
||||||
// PostUpdated is emitted after a successful site edit.
|
|
||||||
type PostUpdated struct {
|
|
||||||
PostEvent
|
|
||||||
}
|
|
||||||
|
|
||||||
// Permalink builds /questions/{root}#post-{id}, prefixed by baseURL when set.
|
|
||||||
func Permalink(baseURL, rootID, postID string) string {
|
|
||||||
path := "/questions/" + url.PathEscape(rootID) + "#post-" + url.PathEscape(postID)
|
|
||||||
base := strings.TrimRight(strings.TrimSpace(baseURL), "/")
|
|
||||||
if base == "" {
|
|
||||||
return path
|
|
||||||
}
|
|
||||||
return base + path
|
|
||||||
}
|
|
||||||
@@ -1,9 +0,0 @@
|
|||||||
package events
|
|
||||||
|
|
||||||
import "context"
|
|
||||||
|
|
||||||
// Nop is a Publisher used when nothing is subscribed.
|
|
||||||
type Nop struct{}
|
|
||||||
|
|
||||||
// Publish discards ev.
|
|
||||||
func (Nop) Publish(context.Context, any) {}
|
|
||||||
@@ -1,44 +0,0 @@
|
|||||||
package events
|
|
||||||
|
|
||||||
import (
|
|
||||||
"context"
|
|
||||||
"sync"
|
|
||||||
)
|
|
||||||
|
|
||||||
// Recording is a test Publisher that records events synchronously.
|
|
||||||
type Recording struct {
|
|
||||||
mu sync.Mutex
|
|
||||||
evs []any
|
|
||||||
}
|
|
||||||
|
|
||||||
// Publish appends ev.
|
|
||||||
func (r *Recording) Publish(_ context.Context, ev any) {
|
|
||||||
if r == nil {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
r.mu.Lock()
|
|
||||||
defer r.mu.Unlock()
|
|
||||||
r.evs = append(r.evs, ev)
|
|
||||||
}
|
|
||||||
|
|
||||||
// Len returns the number of recorded events.
|
|
||||||
func (r *Recording) Len() int {
|
|
||||||
if r == nil {
|
|
||||||
return 0
|
|
||||||
}
|
|
||||||
r.mu.Lock()
|
|
||||||
defer r.mu.Unlock()
|
|
||||||
return len(r.evs)
|
|
||||||
}
|
|
||||||
|
|
||||||
// Snapshot returns a copy of recorded events.
|
|
||||||
func (r *Recording) Snapshot() []any {
|
|
||||||
if r == nil {
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
r.mu.Lock()
|
|
||||||
defer r.mu.Unlock()
|
|
||||||
out := make([]any, len(r.evs))
|
|
||||||
copy(out, r.evs)
|
|
||||||
return out
|
|
||||||
}
|
|
||||||
@@ -209,9 +209,6 @@ func (m *Memory) CreatePost(_ context.Context, post *Post) error {
|
|||||||
if err := preparePost(post); err != nil {
|
if err := preparePost(post); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
if err := preparePostImages(post); err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
m.mu.Lock()
|
m.mu.Lock()
|
||||||
defer m.mu.Unlock()
|
defer m.mu.Unlock()
|
||||||
if _, ok := m.users[post.AuthorID]; !ok {
|
if _, ok := m.users[post.AuthorID]; !ok {
|
||||||
@@ -290,10 +287,6 @@ func (m *Memory) UpdatePost(_ context.Context, post *Post) error {
|
|||||||
if body == "" {
|
if body == "" {
|
||||||
return fmt.Errorf("%w: body is required", ErrInvalidPost)
|
return fmt.Errorf("%w: body is required", ErrInvalidPost)
|
||||||
}
|
}
|
||||||
post.Body = body
|
|
||||||
if err := preparePostImages(post); err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
m.mu.Lock()
|
m.mu.Lock()
|
||||||
defer m.mu.Unlock()
|
defer m.mu.Unlock()
|
||||||
existing, ok := m.posts[post.ID]
|
existing, ok := m.posts[post.ID]
|
||||||
@@ -301,7 +294,6 @@ func (m *Memory) UpdatePost(_ context.Context, post *Post) error {
|
|||||||
return sql.ErrNoRows
|
return sql.ErrNoRows
|
||||||
}
|
}
|
||||||
existing.Body = body
|
existing.Body = body
|
||||||
existing.Images = append([]PostImage(nil), post.Images...)
|
|
||||||
existing.UpdatedAt = time.Now().UTC().Format(time.RFC3339Nano)
|
existing.UpdatedAt = time.Now().UTC().Format(time.RFC3339Nano)
|
||||||
*post = *clonePostWithAuthor(existing, m.users)
|
*post = *clonePostWithAuthor(existing, m.users)
|
||||||
return nil
|
return nil
|
||||||
@@ -459,7 +451,6 @@ func clonePost(post *Post) *Post {
|
|||||||
parentID := *post.ParentID
|
parentID := *post.ParentID
|
||||||
cp.ParentID = &parentID
|
cp.ParentID = &parentID
|
||||||
}
|
}
|
||||||
cp.Images = append([]PostImage(nil), post.Images...)
|
|
||||||
cp.Replies = nil
|
cp.Replies = nil
|
||||||
return &cp
|
return &cp
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -120,25 +120,6 @@ func migrateDropLegacyPostTables(ctx context.Context, exec execContext) error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func migratePostImages(ctx context.Context, exec execContext) error {
|
|
||||||
if _, err := exec.ExecContext(ctx, `
|
|
||||||
CREATE TABLE IF NOT EXISTS post_images (
|
|
||||||
id TEXT PRIMARY KEY,
|
|
||||||
post_id TEXT NOT NULL REFERENCES posts(id) ON DELETE CASCADE,
|
|
||||||
object_key TEXT NOT NULL UNIQUE,
|
|
||||||
public_url TEXT NOT NULL,
|
|
||||||
description TEXT NOT NULL DEFAULT '' CHECK (char_length(description) <= 500),
|
|
||||||
position SMALLINT NOT NULL CHECK (position BETWEEN 0 AND 3),
|
|
||||||
width INTEGER NOT NULL CHECK (width > 0),
|
|
||||||
height INTEGER NOT NULL CHECK (height > 0),
|
|
||||||
created_at TEXT NOT NULL,
|
|
||||||
UNIQUE (post_id, position)
|
|
||||||
)`); err != nil {
|
|
||||||
return fmt.Errorf("create post images: %w", err)
|
|
||||||
}
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func migratePostDate(ctx context.Context, exec execContext) error {
|
func migratePostDate(ctx context.Context, exec execContext) error {
|
||||||
steps := []struct {
|
steps := []struct {
|
||||||
name string
|
name string
|
||||||
@@ -330,7 +311,6 @@ CREATE TABLE IF NOT EXISTS schema_migrations (
|
|||||||
{"007_post_state", migratePostState},
|
{"007_post_state", migratePostState},
|
||||||
{"008_post_author_index", migratePostAuthorIndex},
|
{"008_post_author_index", migratePostAuthorIndex},
|
||||||
{"009_drop_legacy_post_tables", migrateDropLegacyPostTables},
|
{"009_drop_legacy_post_tables", migrateDropLegacyPostTables},
|
||||||
{"010_post_images", migratePostImages},
|
|
||||||
}
|
}
|
||||||
for _, m := range migrations {
|
for _, m := range migrations {
|
||||||
if applied[m.version] {
|
if applied[m.version] {
|
||||||
|
|||||||
@@ -75,12 +75,6 @@ CREATE TABLE users (
|
|||||||
if err := migratePostAuthorIndex(ctx, conn); err != nil {
|
if err := migratePostAuthorIndex(ctx, conn); err != nil {
|
||||||
t.Fatalf("post author index migration is not idempotent: %v", err)
|
t.Fatalf("post author index migration is not idempotent: %v", err)
|
||||||
}
|
}
|
||||||
if err := migratePostImages(ctx, conn); err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
if err := migratePostImages(ctx, conn); err != nil {
|
|
||||||
t.Fatalf("post images migration is not idempotent: %v", err)
|
|
||||||
}
|
|
||||||
if _, err := conn.ExecContext(ctx, `
|
if _, err := conn.ExecContext(ctx, `
|
||||||
INSERT INTO users (id, name, role)
|
INSERT INTO users (id, name, role)
|
||||||
VALUES ('homeowner', 'Home Owner', 'user'), ('plumber', 'The Plumber', 'admin');
|
VALUES ('homeowner', 'Home Owner', 'user'), ('plumber', 'The Plumber', 'admin');
|
||||||
@@ -110,39 +104,6 @@ VALUES ('homeowner', 'root-1', 1);`); err != nil {
|
|||||||
if postCount != 2 || voteCount != 1 {
|
if postCount != 2 || voteCount != 1 {
|
||||||
t.Fatalf("counts posts=%d votes=%d", postCount, voteCount)
|
t.Fatalf("counts posts=%d votes=%d", postCount, voteCount)
|
||||||
}
|
}
|
||||||
imageQueries := sqlc.New(conn)
|
|
||||||
for _, image := range []sqlc.CreatePostImageParams{
|
|
||||||
{ID: "root-image-1", PostID: "root-1", ObjectKey: "posts/root-1/1.jpg", PublicUrl: "https://cdn.example/root-1.jpg", Description: "Valve", Position: 0, Width: 1200, Height: 900, CreatedAt: "2026-08-26T08:00:00Z"},
|
|
||||||
{ID: "root-image-2", PostID: "root-1", ObjectKey: "posts/root-1/2.png", PublicUrl: "https://cdn.example/root-2.png", Position: 1, Width: 900, Height: 1200, CreatedAt: "2026-08-26T08:00:00Z"},
|
|
||||||
{ID: "reply-image-1", PostID: "reply-1", ObjectKey: "posts/reply-1/1.jpg", PublicUrl: "https://cdn.example/reply-1.jpg", Description: "Cartridge", Position: 0, Width: 1000, Height: 1000, CreatedAt: "2026-08-26T09:00:00Z"},
|
|
||||||
} {
|
|
||||||
if err := imageQueries.CreatePostImage(ctx, image); err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
rootImages, err := imageQueries.ListPostImages(ctx, "root-1")
|
|
||||||
if err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
if len(rootImages) != 2 ||
|
|
||||||
rootImages[0].ID != "root-image-1" ||
|
|
||||||
rootImages[1].ID != "root-image-2" {
|
|
||||||
t.Fatalf("root images = %+v", rootImages)
|
|
||||||
}
|
|
||||||
threadImages, err := imageQueries.ListPostThreadImages(ctx, "root-1")
|
|
||||||
if err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
if len(threadImages) != 3 {
|
|
||||||
t.Fatalf("thread images = %+v", threadImages)
|
|
||||||
}
|
|
||||||
if err := imageQueries.CreatePostImage(ctx, sqlc.CreatePostImageParams{
|
|
||||||
ID: "too-many", PostID: "root-1", ObjectKey: "posts/root-1/5.jpg",
|
|
||||||
PublicUrl: "https://cdn.example/root-5.jpg", Position: 4,
|
|
||||||
Width: 100, Height: 100, CreatedAt: "2026-08-26T08:00:00Z",
|
|
||||||
}); err == nil {
|
|
||||||
t.Fatal("fifth image position unexpectedly succeeded")
|
|
||||||
}
|
|
||||||
var postVoteIndexCount int
|
var postVoteIndexCount int
|
||||||
if err := conn.QueryRowContext(ctx, `
|
if err := conn.QueryRowContext(ctx, `
|
||||||
SELECT count(*)
|
SELECT count(*)
|
||||||
|
|||||||
+7
-156
@@ -5,7 +5,6 @@ import (
|
|||||||
"database/sql"
|
"database/sql"
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"math"
|
|
||||||
"sort"
|
"sort"
|
||||||
"strings"
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
@@ -28,23 +27,8 @@ const (
|
|||||||
PostStateVisible PostState = "visible"
|
PostStateVisible PostState = "visible"
|
||||||
PostStateHidden PostState = "hidden"
|
PostStateHidden PostState = "hidden"
|
||||||
PostStateLocked PostState = "locked"
|
PostStateLocked PostState = "locked"
|
||||||
MaxPostImages = 4
|
|
||||||
MaxImageDescriptionRunes = 500
|
|
||||||
)
|
)
|
||||||
|
|
||||||
// PostImage is one ordered public image attached to a post.
|
|
||||||
type PostImage struct {
|
|
||||||
ID string
|
|
||||||
PostID string
|
|
||||||
ObjectKey string
|
|
||||||
PublicURL string
|
|
||||||
Description string
|
|
||||||
Position int
|
|
||||||
Width int
|
|
||||||
Height int
|
|
||||||
CreatedAt string
|
|
||||||
}
|
|
||||||
|
|
||||||
// Post is either a root question (ParentID nil) or a reply to another post.
|
// Post is either a root question (ParentID nil) or a reply to another post.
|
||||||
type Post struct {
|
type Post struct {
|
||||||
ID string
|
ID string
|
||||||
@@ -62,7 +46,6 @@ type Post struct {
|
|||||||
Score int
|
Score int
|
||||||
Answered bool
|
Answered bool
|
||||||
UserVote int
|
UserVote int
|
||||||
Images []PostImage
|
|
||||||
Replies []*Post
|
Replies []*Post
|
||||||
db *sql.DB
|
db *sql.DB
|
||||||
}
|
}
|
||||||
@@ -80,16 +63,7 @@ func (p *Post) Create(ctx context.Context) error {
|
|||||||
if err := preparePost(p); err != nil {
|
if err := preparePost(p); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
if err := preparePostImages(p); err != nil {
|
err := sqlc.New(p.db).CreatePost(ctx, sqlc.CreatePostParams{
|
||||||
return err
|
|
||||||
}
|
|
||||||
tx, err := p.db.BeginTx(ctx, nil)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
defer tx.Rollback()
|
|
||||||
q := sqlc.New(tx)
|
|
||||||
if err := q.CreatePost(ctx, sqlc.CreatePostParams{
|
|
||||||
ID: p.ID,
|
ID: p.ID,
|
||||||
ParentID: nullableParentID(p.ParentID),
|
ParentID: nullableParentID(p.ParentID),
|
||||||
AuthorID: p.AuthorID,
|
AuthorID: p.AuthorID,
|
||||||
@@ -100,16 +74,11 @@ func (p *Post) Create(ctx context.Context) error {
|
|||||||
PostState: string(p.PostState),
|
PostState: string(p.PostState),
|
||||||
CreatedAt: p.CreatedAt,
|
CreatedAt: p.CreatedAt,
|
||||||
UpdatedAt: p.UpdatedAt,
|
UpdatedAt: p.UpdatedAt,
|
||||||
}); err != nil {
|
})
|
||||||
return mapPostCreateError(err)
|
return mapPostCreateError(err)
|
||||||
}
|
}
|
||||||
if err := createPostImages(ctx, q, p.Images); err != nil {
|
|
||||||
return mapPostCreateError(err)
|
|
||||||
}
|
|
||||||
return tx.Commit()
|
|
||||||
}
|
|
||||||
|
|
||||||
// Update changes the post body, update timestamp, and complete image set.
|
// Update changes only the post body and update timestamp.
|
||||||
func (p *Post) Update(ctx context.Context) error {
|
func (p *Post) Update(ctx context.Context) error {
|
||||||
if p == nil || p.db == nil {
|
if p == nil || p.db == nil {
|
||||||
return fmt.Errorf("post: no database")
|
return fmt.Errorf("post: no database")
|
||||||
@@ -119,16 +88,7 @@ func (p *Post) Update(ctx context.Context) error {
|
|||||||
return fmt.Errorf("%w: body is required", ErrInvalidPost)
|
return fmt.Errorf("%w: body is required", ErrInvalidPost)
|
||||||
}
|
}
|
||||||
p.UpdatedAt = time.Now().UTC().Format(time.RFC3339Nano)
|
p.UpdatedAt = time.Now().UTC().Format(time.RFC3339Nano)
|
||||||
if err := preparePostImages(p); err != nil {
|
n, err := sqlc.New(p.db).UpdatePost(ctx, sqlc.UpdatePostParams{
|
||||||
return err
|
|
||||||
}
|
|
||||||
tx, err := p.db.BeginTx(ctx, nil)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
defer tx.Rollback()
|
|
||||||
q := sqlc.New(tx)
|
|
||||||
n, err := q.UpdatePost(ctx, sqlc.UpdatePostParams{
|
|
||||||
ID: p.ID,
|
ID: p.ID,
|
||||||
Body: p.Body,
|
Body: p.Body,
|
||||||
UpdatedAt: p.UpdatedAt,
|
UpdatedAt: p.UpdatedAt,
|
||||||
@@ -139,13 +99,7 @@ func (p *Post) Update(ctx context.Context) error {
|
|||||||
if n == 0 {
|
if n == 0 {
|
||||||
return sql.ErrNoRows
|
return sql.ErrNoRows
|
||||||
}
|
}
|
||||||
if err := q.DeletePostImages(ctx, p.ID); err != nil {
|
return nil
|
||||||
return err
|
|
||||||
}
|
|
||||||
if err := createPostImages(ctx, q, p.Images); err != nil {
|
|
||||||
return mapPostCreateError(err)
|
|
||||||
}
|
|
||||||
return tx.Commit()
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func preparePost(p *Post) error {
|
func preparePost(p *Post) error {
|
||||||
@@ -199,85 +153,6 @@ func preparePost(p *Post) error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func preparePostImages(p *Post) error {
|
|
||||||
if len(p.Images) > MaxPostImages {
|
|
||||||
return fmt.Errorf("%w: at most %d images are allowed", ErrInvalidPost, MaxPostImages)
|
|
||||||
}
|
|
||||||
ids := make(map[string]bool, len(p.Images))
|
|
||||||
keys := make(map[string]bool, len(p.Images))
|
|
||||||
now := time.Now().UTC().Format(time.RFC3339Nano)
|
|
||||||
for i := range p.Images {
|
|
||||||
image := &p.Images[i]
|
|
||||||
image.ID = strings.TrimSpace(image.ID)
|
|
||||||
image.PostID = strings.TrimSpace(image.PostID)
|
|
||||||
image.ObjectKey = strings.TrimSpace(image.ObjectKey)
|
|
||||||
image.PublicURL = strings.TrimSpace(image.PublicURL)
|
|
||||||
image.Description = strings.TrimSpace(image.Description)
|
|
||||||
if image.ID == "" {
|
|
||||||
image.ID = uuid.NewString()
|
|
||||||
}
|
|
||||||
if image.PostID == "" {
|
|
||||||
image.PostID = p.ID
|
|
||||||
}
|
|
||||||
if image.PostID != p.ID {
|
|
||||||
return fmt.Errorf("%w: image belongs to another post", ErrInvalidPost)
|
|
||||||
}
|
|
||||||
if image.ObjectKey == "" || image.PublicURL == "" {
|
|
||||||
return fmt.Errorf("%w: image storage metadata is required", ErrInvalidPost)
|
|
||||||
}
|
|
||||||
if len([]rune(image.Description)) > MaxImageDescriptionRunes {
|
|
||||||
return fmt.Errorf("%w: image description is too long", ErrInvalidPost)
|
|
||||||
}
|
|
||||||
if image.Width <= 0 || image.Height <= 0 ||
|
|
||||||
image.Width > math.MaxInt32 || image.Height > math.MaxInt32 {
|
|
||||||
return fmt.Errorf("%w: invalid image dimensions", ErrInvalidPost)
|
|
||||||
}
|
|
||||||
if ids[image.ID] || keys[image.ObjectKey] {
|
|
||||||
return fmt.Errorf("%w: duplicate image", ErrInvalidPost)
|
|
||||||
}
|
|
||||||
ids[image.ID] = true
|
|
||||||
keys[image.ObjectKey] = true
|
|
||||||
image.Position = i
|
|
||||||
if image.CreatedAt == "" {
|
|
||||||
image.CreatedAt = now
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func createPostImages(ctx context.Context, q *sqlc.Queries, images []PostImage) error {
|
|
||||||
for _, image := range images {
|
|
||||||
if err := q.CreatePostImage(ctx, sqlc.CreatePostImageParams{
|
|
||||||
ID: image.ID,
|
|
||||||
PostID: image.PostID,
|
|
||||||
ObjectKey: image.ObjectKey,
|
|
||||||
PublicUrl: image.PublicURL,
|
|
||||||
Description: image.Description,
|
|
||||||
Position: int16(image.Position),
|
|
||||||
Width: int32(image.Width),
|
|
||||||
Height: int32(image.Height),
|
|
||||||
CreatedAt: image.CreatedAt,
|
|
||||||
}); err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func postImageFromSQL(image sqlc.PostImage) PostImage {
|
|
||||||
return PostImage{
|
|
||||||
ID: image.ID,
|
|
||||||
PostID: image.PostID,
|
|
||||||
ObjectKey: image.ObjectKey,
|
|
||||||
PublicURL: image.PublicUrl,
|
|
||||||
Description: image.Description,
|
|
||||||
Position: int(image.Position),
|
|
||||||
Width: int(image.Width),
|
|
||||||
Height: int(image.Height),
|
|
||||||
CreatedAt: image.CreatedAt,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func nullableParentID(parentID *string) sql.NullString {
|
func nullableParentID(parentID *string) sql.NullString {
|
||||||
if parentID == nil {
|
if parentID == nil {
|
||||||
return sql.NullString{}
|
return sql.NullString{}
|
||||||
@@ -334,8 +209,7 @@ func postFromValues(
|
|||||||
|
|
||||||
// GetPost returns one post without loading its replies.
|
// GetPost returns one post without loading its replies.
|
||||||
func GetPost(ctx context.Context, db *sql.DB, id string) (*Post, error) {
|
func GetPost(ctx context.Context, db *sql.DB, id string) (*Post, error) {
|
||||||
q := sqlc.New(db)
|
r, err := sqlc.New(db).GetPost(ctx, id)
|
||||||
r, err := q.GetPost(ctx, id)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
@@ -354,20 +228,12 @@ func GetPost(ctx context.Context, db *sql.DB, id string) (*Post, error) {
|
|||||||
r.CreatedAt,
|
r.CreatedAt,
|
||||||
r.UpdatedAt,
|
r.UpdatedAt,
|
||||||
)
|
)
|
||||||
imageRows, err := q.ListPostImages(ctx, id)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
for _, image := range imageRows {
|
|
||||||
p.Images = append(p.Images, postImageFromSQL(image))
|
|
||||||
}
|
|
||||||
return &p, nil
|
return &p, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// GetPostThread returns a root post with all descendants nested under Replies.
|
// GetPostThread returns a root post with all descendants nested under Replies.
|
||||||
func GetPostThread(ctx context.Context, db *sql.DB, rootID string) (*Post, error) {
|
func GetPostThread(ctx context.Context, db *sql.DB, rootID string) (*Post, error) {
|
||||||
q := sqlc.New(db)
|
rows, err := sqlc.New(db).ListPostThread(ctx, rootID)
|
||||||
rows, err := q.ListPostThread(ctx, rootID)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
@@ -389,21 +255,6 @@ func GetPostThread(ctx context.Context, db *sql.DB, rootID string) (*Post, error
|
|||||||
r.UpdatedAt,
|
r.UpdatedAt,
|
||||||
))
|
))
|
||||||
}
|
}
|
||||||
imageRows, err := q.ListPostThreadImages(ctx, rootID)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
postsByID := make(map[string]*Post, len(posts))
|
|
||||||
for i := range posts {
|
|
||||||
postsByID[posts[i].ID] = &posts[i]
|
|
||||||
}
|
|
||||||
for _, image := range imageRows {
|
|
||||||
post, ok := postsByID[image.PostID]
|
|
||||||
if !ok {
|
|
||||||
return nil, fmt.Errorf("image %s belongs to missing post %s", image.ID, image.PostID)
|
|
||||||
}
|
|
||||||
post.Images = append(post.Images, postImageFromSQL(image))
|
|
||||||
}
|
|
||||||
return buildPostTree(posts, rootID)
|
return buildPostTree(posts, rootID)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -267,93 +267,6 @@ func TestMemoryPostValidation(t *testing.T) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestMemoryPostImages(t *testing.T) {
|
|
||||||
t.Parallel()
|
|
||||||
|
|
||||||
ctx := context.Background()
|
|
||||||
mem := NewMemory()
|
|
||||||
homeowner := &User{Username: "images", PasswordHash: "hash", Role: RoleUser}
|
|
||||||
if err := mem.CreateUser(ctx, homeowner); err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
root := &Post{
|
|
||||||
ID: "image-root",
|
|
||||||
AuthorID: homeowner.ID,
|
|
||||||
Title: "What is leaking?",
|
|
||||||
Body: "Here are two photos.",
|
|
||||||
Images: []PostImage{
|
|
||||||
{ID: "image-a", ObjectKey: "posts/image-root/image-a.jpg", PublicURL: "https://cdn.example/image-a.jpg", Description: " Supply valve ", Position: 3, Width: 1200, Height: 900},
|
|
||||||
{ID: "image-b", ObjectKey: "posts/image-root/image-b.png", PublicURL: "https://cdn.example/image-b.png", Width: 900, Height: 1200},
|
|
||||||
},
|
|
||||||
}
|
|
||||||
if err := mem.CreatePost(ctx, root); err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
if root.Images[0].Position != 0 ||
|
|
||||||
root.Images[1].Position != 1 ||
|
|
||||||
root.Images[0].PostID != root.ID ||
|
|
||||||
root.Images[0].Description != "Supply valve" {
|
|
||||||
t.Fatalf("created images were not normalized: %+v", root.Images)
|
|
||||||
}
|
|
||||||
|
|
||||||
loaded, err := mem.GetPost(ctx, root.ID)
|
|
||||||
if err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
loaded.Images[0].Description = "mutated outside store"
|
|
||||||
reloaded, err := mem.GetPost(ctx, root.ID)
|
|
||||||
if err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
if reloaded.Images[0].Description != "Supply valve" {
|
|
||||||
t.Fatalf("stored image mutated through clone: %+v", reloaded.Images[0])
|
|
||||||
}
|
|
||||||
|
|
||||||
reloaded.Body = "Updated photos."
|
|
||||||
reloaded.Images = []PostImage{
|
|
||||||
reloaded.Images[1],
|
|
||||||
{ID: "image-c", ObjectKey: "posts/image-root/image-c.jpg", PublicURL: "https://cdn.example/image-c.jpg", Description: "Trap connection", Width: 1600, Height: 1000},
|
|
||||||
}
|
|
||||||
if err := mem.UpdatePost(ctx, reloaded); err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
updated, err := mem.GetPost(ctx, root.ID)
|
|
||||||
if err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
if len(updated.Images) != 2 ||
|
|
||||||
updated.Images[0].ID != "image-b" ||
|
|
||||||
updated.Images[0].Position != 0 ||
|
|
||||||
updated.Images[1].ID != "image-c" ||
|
|
||||||
updated.Images[1].Position != 1 {
|
|
||||||
t.Fatalf("updated images = %+v", updated.Images)
|
|
||||||
}
|
|
||||||
|
|
||||||
tooMany := &Post{
|
|
||||||
AuthorID: homeowner.ID,
|
|
||||||
Title: "Too many",
|
|
||||||
Body: "Five photos.",
|
|
||||||
Images: validPostImages(5),
|
|
||||||
}
|
|
||||||
if err := mem.CreatePost(ctx, tooMany); !errors.Is(err, ErrInvalidPost) {
|
|
||||||
t.Fatalf("five-image create error = %v, want ErrInvalidPost", err)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func validPostImages(count int) []PostImage {
|
|
||||||
images := make([]PostImage, count)
|
|
||||||
for i := range images {
|
|
||||||
images[i] = PostImage{
|
|
||||||
ID: "image-" + string(rune('a'+i)),
|
|
||||||
ObjectKey: "posts/key-" + string(rune('a'+i)) + ".jpg",
|
|
||||||
PublicURL: "https://cdn.example/" + string(rune('a'+i)) + ".jpg",
|
|
||||||
Width: 100,
|
|
||||||
Height: 100,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return images
|
|
||||||
}
|
|
||||||
|
|
||||||
func ptr(value string) *string {
|
func ptr(value string) *string {
|
||||||
return &value
|
return &value
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -22,18 +22,6 @@ type Post struct {
|
|||||||
UpdatedAt string
|
UpdatedAt string
|
||||||
}
|
}
|
||||||
|
|
||||||
type PostImage struct {
|
|
||||||
ID string
|
|
||||||
PostID string
|
|
||||||
ObjectKey string
|
|
||||||
PublicUrl string
|
|
||||||
Description string
|
|
||||||
Position int16
|
|
||||||
Width int32
|
|
||||||
Height int32
|
|
||||||
CreatedAt string
|
|
||||||
}
|
|
||||||
|
|
||||||
type PostVote struct {
|
type PostVote struct {
|
||||||
UserID string
|
UserID string
|
||||||
PostID string
|
PostID string
|
||||||
|
|||||||
@@ -57,60 +57,6 @@ func (q *Queries) CreatePost(ctx context.Context, arg CreatePostParams) error {
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
const createPostImage = `-- name: CreatePostImage :exec
|
|
||||||
INSERT INTO post_images (
|
|
||||||
id, post_id, object_key, public_url, description, position, width, height, created_at
|
|
||||||
)
|
|
||||||
VALUES (
|
|
||||||
$1,
|
|
||||||
$2,
|
|
||||||
$3,
|
|
||||||
$4,
|
|
||||||
$5,
|
|
||||||
$6,
|
|
||||||
$7,
|
|
||||||
$8,
|
|
||||||
$9
|
|
||||||
)
|
|
||||||
`
|
|
||||||
|
|
||||||
type CreatePostImageParams struct {
|
|
||||||
ID string
|
|
||||||
PostID string
|
|
||||||
ObjectKey string
|
|
||||||
PublicUrl string
|
|
||||||
Description string
|
|
||||||
Position int16
|
|
||||||
Width int32
|
|
||||||
Height int32
|
|
||||||
CreatedAt string
|
|
||||||
}
|
|
||||||
|
|
||||||
func (q *Queries) CreatePostImage(ctx context.Context, arg CreatePostImageParams) error {
|
|
||||||
_, err := q.db.ExecContext(ctx, createPostImage,
|
|
||||||
arg.ID,
|
|
||||||
arg.PostID,
|
|
||||||
arg.ObjectKey,
|
|
||||||
arg.PublicUrl,
|
|
||||||
arg.Description,
|
|
||||||
arg.Position,
|
|
||||||
arg.Width,
|
|
||||||
arg.Height,
|
|
||||||
arg.CreatedAt,
|
|
||||||
)
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
const deletePostImages = `-- name: DeletePostImages :exec
|
|
||||||
DELETE FROM post_images
|
|
||||||
WHERE post_id = $1
|
|
||||||
`
|
|
||||||
|
|
||||||
func (q *Queries) DeletePostImages(ctx context.Context, postID string) error {
|
|
||||||
_, err := q.db.ExecContext(ctx, deletePostImages, postID)
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
const deletePostVote = `-- name: DeletePostVote :exec
|
const deletePostVote = `-- name: DeletePostVote :exec
|
||||||
DELETE FROM post_votes
|
DELETE FROM post_votes
|
||||||
WHERE user_id = $1
|
WHERE user_id = $1
|
||||||
@@ -199,47 +145,6 @@ func (q *Queries) GetRootPostVoteSummary(ctx context.Context, arg GetRootPostVot
|
|||||||
return i, err
|
return i, err
|
||||||
}
|
}
|
||||||
|
|
||||||
const listPostImages = `-- name: ListPostImages :many
|
|
||||||
SELECT
|
|
||||||
id, post_id, object_key, public_url, description, position, width, height, created_at
|
|
||||||
FROM post_images
|
|
||||||
WHERE post_id = $1
|
|
||||||
ORDER BY position
|
|
||||||
`
|
|
||||||
|
|
||||||
func (q *Queries) ListPostImages(ctx context.Context, postID string) ([]PostImage, error) {
|
|
||||||
rows, err := q.db.QueryContext(ctx, listPostImages, postID)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
defer rows.Close()
|
|
||||||
items := []PostImage{}
|
|
||||||
for rows.Next() {
|
|
||||||
var i PostImage
|
|
||||||
if err := rows.Scan(
|
|
||||||
&i.ID,
|
|
||||||
&i.PostID,
|
|
||||||
&i.ObjectKey,
|
|
||||||
&i.PublicUrl,
|
|
||||||
&i.Description,
|
|
||||||
&i.Position,
|
|
||||||
&i.Width,
|
|
||||||
&i.Height,
|
|
||||||
&i.CreatedAt,
|
|
||||||
); err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
items = append(items, i)
|
|
||||||
}
|
|
||||||
if err := rows.Close(); err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
if err := rows.Err(); err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
return items, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
const listPostThread = `-- name: ListPostThread :many
|
const listPostThread = `-- name: ListPostThread :many
|
||||||
WITH RECURSIVE thread AS (
|
WITH RECURSIVE thread AS (
|
||||||
SELECT p.id, p.parent_id, p.author_id, p.title, p.body, p.city, p.post_date, p.post_state, p.created_at, p.updated_at
|
SELECT p.id, p.parent_id, p.author_id, p.title, p.body, p.city, p.post_date, p.post_state, p.created_at, p.updated_at
|
||||||
@@ -313,59 +218,6 @@ func (q *Queries) ListPostThread(ctx context.Context, rootID string) ([]ListPost
|
|||||||
return items, nil
|
return items, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
const listPostThreadImages = `-- name: ListPostThreadImages :many
|
|
||||||
WITH RECURSIVE thread AS (
|
|
||||||
SELECT p.id
|
|
||||||
FROM posts p
|
|
||||||
WHERE p.id = $1 AND p.parent_id IS NULL
|
|
||||||
|
|
||||||
UNION ALL
|
|
||||||
|
|
||||||
SELECT child.id
|
|
||||||
FROM posts child
|
|
||||||
JOIN thread parent ON child.parent_id = parent.id
|
|
||||||
)
|
|
||||||
SELECT
|
|
||||||
images.id, images.post_id, images.object_key, images.public_url,
|
|
||||||
images.description, images.position, images.width, images.height, images.created_at
|
|
||||||
FROM post_images images
|
|
||||||
JOIN thread ON thread.id = images.post_id
|
|
||||||
ORDER BY images.post_id, images.position
|
|
||||||
`
|
|
||||||
|
|
||||||
func (q *Queries) ListPostThreadImages(ctx context.Context, rootID string) ([]PostImage, error) {
|
|
||||||
rows, err := q.db.QueryContext(ctx, listPostThreadImages, rootID)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
defer rows.Close()
|
|
||||||
items := []PostImage{}
|
|
||||||
for rows.Next() {
|
|
||||||
var i PostImage
|
|
||||||
if err := rows.Scan(
|
|
||||||
&i.ID,
|
|
||||||
&i.PostID,
|
|
||||||
&i.ObjectKey,
|
|
||||||
&i.PublicUrl,
|
|
||||||
&i.Description,
|
|
||||||
&i.Position,
|
|
||||||
&i.Width,
|
|
||||||
&i.Height,
|
|
||||||
&i.CreatedAt,
|
|
||||||
); err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
items = append(items, i)
|
|
||||||
}
|
|
||||||
if err := rows.Close(); err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
if err := rows.Err(); err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
return items, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
const listRootPosts = `-- name: ListRootPosts :many
|
const listRootPosts = `-- name: ListRootPosts :many
|
||||||
WITH RECURSIVE roots AS (
|
WITH RECURSIVE roots AS (
|
||||||
SELECT p.id, p.parent_id, p.author_id, p.title, p.body, p.city, p.post_date, p.post_state, p.created_at, p.updated_at
|
SELECT p.id, p.parent_id, p.author_id, p.title, p.body, p.city, p.post_date, p.post_state, p.created_at, p.updated_at
|
||||||
|
|||||||
@@ -1,65 +0,0 @@
|
|||||||
package web
|
|
||||||
|
|
||||||
import (
|
|
||||||
"context"
|
|
||||||
|
|
||||||
"plumber/internal/events"
|
|
||||||
"plumber/internal/store"
|
|
||||||
)
|
|
||||||
|
|
||||||
func (s *Server) publishPostCreated(post, root *store.Post, author *store.User) {
|
|
||||||
s.publishPost(events.PostCreated{PostEvent: s.postEvent(post, root, author)}, root)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (s *Server) publishPostUpdated(post, root *store.Post, author *store.User) {
|
|
||||||
s.publishPost(events.PostUpdated{PostEvent: s.postEvent(post, root, author)}, root)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (s *Server) publishPost(ev any, root *store.Post) {
|
|
||||||
if root != nil && root.PostState == store.PostStateHidden {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
s.cfg.Events.Publish(context.Background(), ev)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (s *Server) postEvent(post, root *store.Post, author *store.User) events.PostEvent {
|
|
||||||
if post == nil {
|
|
||||||
return events.PostEvent{}
|
|
||||||
}
|
|
||||||
rootID := post.ID
|
|
||||||
if root != nil {
|
|
||||||
rootID = root.ID
|
|
||||||
}
|
|
||||||
ev := events.PostEvent{
|
|
||||||
PostID: post.ID,
|
|
||||||
RootID: rootID,
|
|
||||||
Title: post.Title,
|
|
||||||
Body: post.Body,
|
|
||||||
City: post.City,
|
|
||||||
AuthorID: post.AuthorID,
|
|
||||||
AuthorName: post.AuthorName,
|
|
||||||
AuthorRole: string(post.AuthorRole),
|
|
||||||
Permalink: events.Permalink(s.cfg.BaseURL, rootID, post.ID),
|
|
||||||
}
|
|
||||||
if post.ParentID != nil {
|
|
||||||
ev.ParentID = *post.ParentID
|
|
||||||
}
|
|
||||||
if author != nil {
|
|
||||||
if ev.AuthorName == "" {
|
|
||||||
ev.AuthorName = author.Name
|
|
||||||
}
|
|
||||||
if ev.AuthorRole == "" {
|
|
||||||
ev.AuthorRole = string(author.Role)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if n := len(post.Images); n > 0 {
|
|
||||||
ev.Images = make([]events.Image, 0, n)
|
|
||||||
for _, img := range post.Images {
|
|
||||||
ev.Images = append(ev.Images, events.Image{
|
|
||||||
URL: img.PublicURL,
|
|
||||||
Description: img.Description,
|
|
||||||
})
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return ev
|
|
||||||
}
|
|
||||||
@@ -1,238 +0,0 @@
|
|||||||
package web
|
|
||||||
|
|
||||||
import (
|
|
||||||
"context"
|
|
||||||
"encoding/json"
|
|
||||||
"net/http"
|
|
||||||
"net/url"
|
|
||||||
"strings"
|
|
||||||
"testing"
|
|
||||||
|
|
||||||
"plumber/internal/events"
|
|
||||||
"plumber/internal/pacific"
|
|
||||||
"plumber/internal/store"
|
|
||||||
)
|
|
||||||
|
|
||||||
func TestPostHandlersPublishEvents(t *testing.T) {
|
|
||||||
t.Parallel()
|
|
||||||
|
|
||||||
rec := &events.Recording{}
|
|
||||||
srv, mem := newTestServer(t, Config{
|
|
||||||
Events: rec,
|
|
||||||
BaseURL: "https://www.askaplumberfirst.com",
|
|
||||||
})
|
|
||||||
handler := srv.Handler()
|
|
||||||
homeowner := seedUser(t, mem, uniq("homeowner"), "hunter22", store.RoleUser)
|
|
||||||
admin := seedUser(t, mem, uniq("admin"), "hunter22", store.RoleAdmin)
|
|
||||||
homeownerCookies := loginUser(t, handler, homeowner.Username, "hunter22")
|
|
||||||
adminCookies := loginUser(t, handler, admin.Username, "hunter22")
|
|
||||||
homeownerCSRF := csrfForCookies(t, handler, homeownerCookies)
|
|
||||||
adminCSRF := csrfForCookies(t, handler, adminCookies)
|
|
||||||
|
|
||||||
submit := postForm(handler, "/submit", url.Values{
|
|
||||||
"_csrf": {homeownerCSRF},
|
|
||||||
"title": {"Leaky sink"},
|
|
||||||
"body": {"Water under the cabinet."},
|
|
||||||
"city": {"Oakland"},
|
|
||||||
}, homeownerCookies)
|
|
||||||
if submit.Code != http.StatusSeeOther {
|
|
||||||
t.Fatalf("submit status = %d: %s", submit.Code, submit.Body.String())
|
|
||||||
}
|
|
||||||
|
|
||||||
create := postForm(handler, "/posts", url.Values{
|
|
||||||
"_csrf": {homeownerCSRF},
|
|
||||||
"title": {"Second question"},
|
|
||||||
"body": {"Another leak."},
|
|
||||||
"city": {"Berkeley"},
|
|
||||||
}, homeownerCookies)
|
|
||||||
if create.Code != http.StatusSeeOther {
|
|
||||||
t.Fatalf("create status = %d: %s", create.Code, create.Body.String())
|
|
||||||
}
|
|
||||||
|
|
||||||
roots, err := mem.ListRootPosts(context.Background(), pacific.Today(), homeowner.ID)
|
|
||||||
if err != nil || len(roots) != 2 {
|
|
||||||
t.Fatalf("roots = %+v, %v", roots, err)
|
|
||||||
}
|
|
||||||
var submitRoot, createRoot store.Post
|
|
||||||
for _, root := range roots {
|
|
||||||
switch root.Title {
|
|
||||||
case "Leaky sink":
|
|
||||||
submitRoot = root
|
|
||||||
case "Second question":
|
|
||||||
createRoot = root
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if submitRoot.ID == "" || createRoot.ID == "" {
|
|
||||||
t.Fatalf("missing created roots: %+v", roots)
|
|
||||||
}
|
|
||||||
|
|
||||||
reply := postForm(handler, "/posts", url.Values{
|
|
||||||
"_csrf": {adminCSRF},
|
|
||||||
"parent_id": {createRoot.ID},
|
|
||||||
"body": {"Replace the cartridge."},
|
|
||||||
}, adminCookies)
|
|
||||||
if reply.Code != http.StatusSeeOther {
|
|
||||||
t.Fatalf("reply status = %d: %s", reply.Code, reply.Body.String())
|
|
||||||
}
|
|
||||||
thread, err := mem.GetPostThread(context.Background(), createRoot.ID)
|
|
||||||
if err != nil || len(thread.Replies) != 1 {
|
|
||||||
t.Fatalf("thread = %+v, %v", thread, err)
|
|
||||||
}
|
|
||||||
adminReply := thread.Replies[0]
|
|
||||||
|
|
||||||
edit := postForm(handler, "/posts/"+createRoot.ID+"/edit", url.Values{
|
|
||||||
"_csrf": {homeownerCSRF},
|
|
||||||
"body": {"Updated leak description."},
|
|
||||||
}, homeownerCookies)
|
|
||||||
if edit.Code != http.StatusSeeOther {
|
|
||||||
t.Fatalf("edit status = %d: %s", edit.Code, edit.Body.String())
|
|
||||||
}
|
|
||||||
|
|
||||||
empty := postForm(handler, "/posts", url.Values{
|
|
||||||
"_csrf": {homeownerCSRF},
|
|
||||||
"title": {"Missing body"},
|
|
||||||
}, homeownerCookies)
|
|
||||||
if empty.Code != http.StatusBadRequest {
|
|
||||||
t.Fatalf("empty body status = %d, want 400", empty.Code)
|
|
||||||
}
|
|
||||||
|
|
||||||
hidden := &store.Post{
|
|
||||||
AuthorID: homeowner.ID,
|
|
||||||
Title: "Hidden thread",
|
|
||||||
Body: "Not public.",
|
|
||||||
PostDate: pacific.Today(),
|
|
||||||
PostState: store.PostStateHidden,
|
|
||||||
}
|
|
||||||
if err := mem.CreatePost(context.Background(), hidden); err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
hiddenReply := postForm(handler, "/posts", url.Values{
|
|
||||||
"_csrf": {homeownerCSRF},
|
|
||||||
"parent_id": {hidden.ID},
|
|
||||||
"body": {"Should not publish."},
|
|
||||||
}, homeownerCookies)
|
|
||||||
if hiddenReply.Code != http.StatusNotFound {
|
|
||||||
t.Fatalf("hidden reply status = %d, want 404", hiddenReply.Code)
|
|
||||||
}
|
|
||||||
hiddenEdit := postForm(handler, "/posts/"+hidden.ID+"/edit", url.Values{
|
|
||||||
"_csrf": {homeownerCSRF},
|
|
||||||
"body": {"Still hidden."},
|
|
||||||
}, homeownerCookies)
|
|
||||||
if hiddenEdit.Code != http.StatusSeeOther {
|
|
||||||
t.Fatalf("hidden edit status = %d: %s", hiddenEdit.Code, hiddenEdit.Body.String())
|
|
||||||
}
|
|
||||||
|
|
||||||
got := rec.Snapshot()
|
|
||||||
if len(got) != 4 {
|
|
||||||
t.Fatalf("published %d events, want 4: %#v", len(got), got)
|
|
||||||
}
|
|
||||||
|
|
||||||
submitEv, ok := got[0].(events.PostCreated)
|
|
||||||
if !ok {
|
|
||||||
t.Fatalf("first event %T, want PostCreated", got[0])
|
|
||||||
}
|
|
||||||
assertPostEvent(t, submitEv.PostEvent, events.PostEvent{
|
|
||||||
PostID: submitRoot.ID,
|
|
||||||
RootID: submitRoot.ID,
|
|
||||||
Title: "Leaky sink",
|
|
||||||
Body: "Water under the cabinet.",
|
|
||||||
City: "Oakland",
|
|
||||||
AuthorID: homeowner.ID,
|
|
||||||
AuthorName: homeowner.Name,
|
|
||||||
AuthorRole: string(store.RoleUser),
|
|
||||||
Permalink: "https://www.askaplumberfirst.com/questions/" + submitRoot.ID + "#post-" + submitRoot.ID,
|
|
||||||
})
|
|
||||||
|
|
||||||
createEv, ok := got[1].(events.PostCreated)
|
|
||||||
if !ok {
|
|
||||||
t.Fatalf("second event %T, want PostCreated", got[1])
|
|
||||||
}
|
|
||||||
assertPostEvent(t, createEv.PostEvent, events.PostEvent{
|
|
||||||
PostID: createRoot.ID,
|
|
||||||
RootID: createRoot.ID,
|
|
||||||
Title: "Second question",
|
|
||||||
Body: "Another leak.",
|
|
||||||
City: "Berkeley",
|
|
||||||
AuthorID: homeowner.ID,
|
|
||||||
AuthorName: homeowner.Name,
|
|
||||||
AuthorRole: string(store.RoleUser),
|
|
||||||
Permalink: "https://www.askaplumberfirst.com/questions/" + createRoot.ID + "#post-" + createRoot.ID,
|
|
||||||
})
|
|
||||||
|
|
||||||
replyEv, ok := got[2].(events.PostCreated)
|
|
||||||
if !ok {
|
|
||||||
t.Fatalf("third event %T, want PostCreated", got[2])
|
|
||||||
}
|
|
||||||
assertPostEvent(t, replyEv.PostEvent, events.PostEvent{
|
|
||||||
PostID: adminReply.ID,
|
|
||||||
RootID: createRoot.ID,
|
|
||||||
ParentID: createRoot.ID,
|
|
||||||
Body: "Replace the cartridge.",
|
|
||||||
AuthorID: admin.ID,
|
|
||||||
AuthorName: admin.Name,
|
|
||||||
AuthorRole: string(store.RoleAdmin),
|
|
||||||
Permalink: "https://www.askaplumberfirst.com/questions/" + createRoot.ID + "#post-" + adminReply.ID,
|
|
||||||
})
|
|
||||||
|
|
||||||
editEv, ok := got[3].(events.PostUpdated)
|
|
||||||
if !ok {
|
|
||||||
t.Fatalf("fourth event %T, want PostUpdated", got[3])
|
|
||||||
}
|
|
||||||
assertPostEvent(t, editEv.PostEvent, events.PostEvent{
|
|
||||||
PostID: createRoot.ID,
|
|
||||||
RootID: createRoot.ID,
|
|
||||||
Title: "Second question",
|
|
||||||
Body: "Updated leak description.",
|
|
||||||
City: "Berkeley",
|
|
||||||
AuthorID: homeowner.ID,
|
|
||||||
AuthorName: homeowner.Name,
|
|
||||||
AuthorRole: string(store.RoleUser),
|
|
||||||
Permalink: "https://www.askaplumberfirst.com/questions/" + createRoot.ID + "#post-" + createRoot.ID,
|
|
||||||
})
|
|
||||||
|
|
||||||
for i, ev := range got {
|
|
||||||
raw, err := json.Marshal(ev)
|
|
||||||
if err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
if strings.Contains(strings.ToLower(string(raw)), "discord") {
|
|
||||||
t.Fatalf("event %d contains discord fields: %s", i, raw)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestStoreCreateDoesNotPublish(t *testing.T) {
|
|
||||||
t.Parallel()
|
|
||||||
|
|
||||||
rec := &events.Recording{}
|
|
||||||
_, mem := newTestServer(t, Config{Events: rec})
|
|
||||||
homeowner := seedUser(t, mem, uniq("homeowner"), "hunter22", store.RoleUser)
|
|
||||||
if err := mem.CreatePost(context.Background(), &store.Post{
|
|
||||||
AuthorID: homeowner.ID,
|
|
||||||
Title: "Direct write",
|
|
||||||
Body: "No handler.",
|
|
||||||
PostDate: pacific.Today(),
|
|
||||||
}); err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
if rec.Len() != 0 {
|
|
||||||
t.Fatalf("store.CreatePost published %d events", rec.Len())
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func assertPostEvent(t *testing.T, got, want events.PostEvent) {
|
|
||||||
t.Helper()
|
|
||||||
if got.PostID != want.PostID ||
|
|
||||||
got.RootID != want.RootID ||
|
|
||||||
got.ParentID != want.ParentID ||
|
|
||||||
got.Title != want.Title ||
|
|
||||||
got.Body != want.Body ||
|
|
||||||
got.City != want.City ||
|
|
||||||
got.AuthorID != want.AuthorID ||
|
|
||||||
got.AuthorName != want.AuthorName ||
|
|
||||||
got.AuthorRole != want.AuthorRole ||
|
|
||||||
got.Permalink != want.Permalink ||
|
|
||||||
len(got.Images) != 0 {
|
|
||||||
t.Fatalf("event = %+v, want %+v", got, want)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -85,7 +85,6 @@ func (s *Server) handleCreatePost(w http.ResponseWriter, r *http.Request) {
|
|||||||
if parent != nil {
|
if parent != nil {
|
||||||
s.notifyPostReply(parent, root, post, user)
|
s.notifyPostReply(parent, root, post, user)
|
||||||
}
|
}
|
||||||
s.publishPostCreated(post, root, user)
|
|
||||||
http.Redirect(
|
http.Redirect(
|
||||||
w,
|
w,
|
||||||
r,
|
r,
|
||||||
@@ -191,7 +190,6 @@ func (s *Server) handleEditPost(w http.ResponseWriter, r *http.Request) {
|
|||||||
http.Error(w, "could not save post", http.StatusInternalServerError)
|
http.Error(w, "could not save post", http.StatusInternalServerError)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
s.publishPostUpdated(post, root, nil)
|
|
||||||
http.Redirect(
|
http.Redirect(
|
||||||
w,
|
w,
|
||||||
r,
|
r,
|
||||||
|
|||||||
@@ -20,7 +20,6 @@ import (
|
|||||||
"github.com/go-chi/chi/v5/middleware"
|
"github.com/go-chi/chi/v5/middleware"
|
||||||
|
|
||||||
"plumber/internal/blob"
|
"plumber/internal/blob"
|
||||||
"plumber/internal/events"
|
|
||||||
"plumber/internal/geo"
|
"plumber/internal/geo"
|
||||||
"plumber/internal/mail"
|
"plumber/internal/mail"
|
||||||
"plumber/internal/pacific"
|
"plumber/internal/pacific"
|
||||||
@@ -36,8 +35,6 @@ type Config struct {
|
|||||||
TrustedProxies []*net.IPNet
|
TrustedProxies []*net.IPNet
|
||||||
Blob blob.Uploader
|
Blob blob.Uploader
|
||||||
Mail mail.Notifier
|
Mail mail.Notifier
|
||||||
Events events.Publisher
|
|
||||||
BaseURL string
|
|
||||||
}
|
}
|
||||||
|
|
||||||
type Server struct {
|
type Server struct {
|
||||||
@@ -113,9 +110,6 @@ func New(st store.Store, sessionStore scs.Store, templateFS fs.FS, staticFS fs.F
|
|||||||
if cfg.Mail == nil {
|
if cfg.Mail == nil {
|
||||||
cfg.Mail = mail.Nop{}
|
cfg.Mail = mail.Nop{}
|
||||||
}
|
}
|
||||||
if cfg.Events == nil {
|
|
||||||
cfg.Events = events.Nop{}
|
|
||||||
}
|
|
||||||
funcMap := template.FuncMap{
|
funcMap := template.FuncMap{
|
||||||
"voteCtx": func(user *store.User, csrf, view, date string, post *store.Post) voteCtx {
|
"voteCtx": func(user *store.User, csrf, view, date string, post *store.Post) voteCtx {
|
||||||
return voteCtx{User: user, CSRF: csrf, View: view, Date: date, Post: post}
|
return voteCtx{User: user, CSRF: csrf, View: view, Date: date, Post: post}
|
||||||
@@ -377,7 +371,6 @@ func (s *Server) handleSubmit(w http.ResponseWriter, r *http.Request) {
|
|||||||
http.Error(w, "could not save question", http.StatusInternalServerError)
|
http.Error(w, "could not save question", http.StatusInternalServerError)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
s.publishPostCreated(post, post, u)
|
|
||||||
http.Redirect(w, r, "/questions/"+url.PathEscape(post.ID), http.StatusSeeOther)
|
http.Redirect(w, r, "/questions/"+url.PathEscape(post.ID), http.StatusSeeOther)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
-13
@@ -42,19 +42,6 @@ CREATE INDEX IF NOT EXISTS idx_posts_root_date
|
|||||||
ON posts(post_date, post_state)
|
ON posts(post_date, post_state)
|
||||||
WHERE parent_id IS NULL;
|
WHERE parent_id IS NULL;
|
||||||
|
|
||||||
CREATE TABLE IF NOT EXISTS post_images (
|
|
||||||
id TEXT PRIMARY KEY,
|
|
||||||
post_id TEXT NOT NULL REFERENCES posts(id) ON DELETE CASCADE,
|
|
||||||
object_key TEXT NOT NULL UNIQUE,
|
|
||||||
public_url TEXT NOT NULL,
|
|
||||||
description TEXT NOT NULL DEFAULT '' CHECK (char_length(description) <= 500),
|
|
||||||
position SMALLINT NOT NULL CHECK (position BETWEEN 0 AND 3),
|
|
||||||
width INTEGER NOT NULL CHECK (width > 0),
|
|
||||||
height INTEGER NOT NULL CHECK (height > 0),
|
|
||||||
created_at TEXT NOT NULL,
|
|
||||||
UNIQUE (post_id, position)
|
|
||||||
);
|
|
||||||
|
|
||||||
CREATE TABLE IF NOT EXISTS post_votes (
|
CREATE TABLE IF NOT EXISTS post_votes (
|
||||||
user_id TEXT NOT NULL REFERENCES users(id),
|
user_id TEXT NOT NULL REFERENCES users(id),
|
||||||
post_id TEXT NOT NULL REFERENCES posts(id) ON DELETE CASCADE,
|
post_id TEXT NOT NULL REFERENCES posts(id) ON DELETE CASCADE,
|
||||||
|
|||||||
Reference in New Issue
Block a user