Merge pull request #57 from stevedylandev/feat/jotts-public-notes
51947875
Jotts Public Notes
11 file(s) · +132 −28
Jotts Public Notes
| 25 | 25 | ||
| 26 | 26 | type Note = store.Note |
|
| 27 | 27 | type NoteInput = store.NoteInput |
|
| 28 | + | type UpdateNoteRequest = store.UpdateNoteRequest |
|
| 28 | 29 | ||
| 29 | 30 | type indexPageData struct { |
|
| 30 | 31 | Notes []Note |
| 18 | 18 | ||
| 19 | 19 | func listNotes(db *sql.DB) ([]Note, error) { return store.List(db) } |
|
| 20 | 20 | ||
| 21 | - | func updateNoteByShortID(db *sql.DB, shortID, title, content string) (*Note, error) { |
|
| 22 | - | return store.UpdateByShortID(db, shortID, title, content) |
|
| 21 | + | func updateNoteByShortID(db *sql.DB, shortID, title, content string, public *bool) (*Note, error) { |
|
| 22 | + | return store.UpdateByShortID(db, shortID, title, content, public) |
|
| 23 | 23 | } |
|
| 24 | 24 | ||
| 25 | 25 | func deleteNoteByShortID(db *sql.DB, shortID string) (bool, error) { |
| 45 | 45 | t.Fatalf("not newest first: %#v", all) |
|
| 46 | 46 | } |
|
| 47 | 47 | ||
| 48 | - | updated, err := updateNoteByShortID(db, first.ShortID, "updated", "changed") |
|
| 48 | + | updated, err := updateNoteByShortID(db, first.ShortID, "updated", "changed", nil) |
|
| 49 | 49 | if err != nil { |
|
| 50 | 50 | t.Fatal(err) |
|
| 51 | 51 | } |
|
| 52 | 52 | if updated == nil || updated.Title != "updated" || updated.Content != "changed" { |
|
| 53 | 53 | t.Fatalf("unexpected update: %#v", updated) |
|
| 54 | 54 | } |
|
| 55 | - | updated, err = updateNoteByShortID(db, "missing", "x", "y") |
|
| 55 | + | updated, err = updateNoteByShortID(db, "missing", "x", "y", nil) |
|
| 56 | 56 | if err != nil { |
|
| 57 | 57 | t.Fatal(err) |
|
| 58 | 58 | } |
| 53 | 53 | ||
| 54 | 54 | func (a *App) apiUpdateNote(w http.ResponseWriter, r *http.Request) { |
|
| 55 | 55 | shortID := r.PathValue("short_id") |
|
| 56 | - | var body NoteInput |
|
| 56 | + | var body UpdateNoteRequest |
|
| 57 | 57 | if !web.DecodeJSON(w, r, &body) { |
|
| 58 | 58 | return |
|
| 59 | 59 | } |
|
| 62 | 62 | http.Error(w, "title required", http.StatusBadRequest) |
|
| 63 | 63 | return |
|
| 64 | 64 | } |
|
| 65 | - | note, err := updateNoteByShortID(a.DB, shortID, title, body.Content) |
|
| 65 | + | note, err := updateNoteByShortID(a.DB, shortID, title, body.Content, body.Public) |
|
| 66 | 66 | if err != nil { |
|
| 67 | 67 | web.WriteError(w, http.StatusInternalServerError, err.Error()) |
|
| 68 | 68 | return |
|
| 3 | 3 | import ( |
|
| 4 | 4 | "html/template" |
|
| 5 | 5 | "net/http" |
|
| 6 | + | "strconv" |
|
| 6 | 7 | "strings" |
|
| 7 | 8 | ||
| 8 | 9 | "github.com/stevedylandev/andromeda/pkg/auth" |
|
| 95 | 96 | web.Render(a.Templates, w, "view.html", viewPageData{Note: *note, Rendered: template.HTML(rendered)}, a.Log) |
|
| 96 | 97 | } |
|
| 97 | 98 | ||
| 99 | + | func (a *App) viewPublicNoteHandler(w http.ResponseWriter, r *http.Request) { |
|
| 100 | + | shortID := r.PathValue("short_id") |
|
| 101 | + | note, err := getNoteByShortID(a.DB, shortID) |
|
| 102 | + | if err != nil { |
|
| 103 | + | a.Log.Error("get note failed", "err", err) |
|
| 104 | + | http.Error(w, "internal server error", http.StatusInternalServerError) |
|
| 105 | + | return |
|
| 106 | + | } |
|
| 107 | + | if note == nil { |
|
| 108 | + | http.Error(w, "Note not found", http.StatusNotFound) |
|
| 109 | + | return |
|
| 110 | + | } |
|
| 111 | + | if !note.Public { |
|
| 112 | + | http.Error(w, "Note not public", http.StatusUnauthorized) |
|
| 113 | + | return |
|
| 114 | + | } |
|
| 115 | + | rendered, err := renderMarkdown(note.Content) |
|
| 116 | + | if err != nil { |
|
| 117 | + | a.Log.Error("render markdown failed", "err", err) |
|
| 118 | + | http.Error(w, "internal server error", http.StatusInternalServerError) |
|
| 119 | + | return |
|
| 120 | + | } |
|
| 121 | + | web.Render(a.Templates, w, "view-public.html", viewPageData{Note: *note, Rendered: template.HTML(rendered)}, a.Log) |
|
| 122 | + | } |
|
| 123 | + | ||
| 98 | 124 | func (a *App) editNoteGetHandler(w http.ResponseWriter, r *http.Request) { |
|
| 99 | 125 | shortID := r.PathValue("short_id") |
|
| 100 | 126 | note, err := getNoteByShortID(a.DB, shortID) |
|
| 122 | 148 | web.RedirectWithError(w, r, "/notes/"+shortID+"/edit", "Title is required") |
|
| 123 | 149 | return |
|
| 124 | 150 | } |
|
| 125 | - | note, err := updateNoteByShortID(a.DB, shortID, title, content) |
|
| 151 | + | var public *bool |
|
| 152 | + | if _, ok := r.Form["public"]; ok { |
|
| 153 | + | v, err := strconv.ParseBool(r.FormValue("public")) |
|
| 154 | + | if err != nil { |
|
| 155 | + | web.RedirectWithError(w, r, "/notes/"+shortID+"/edit", "Not boolean value") |
|
| 156 | + | return |
|
| 157 | + | } |
|
| 158 | + | public = &v |
|
| 159 | + | } |
|
| 160 | + | note, err := updateNoteByShortID(a.DB, shortID, title, content, public) |
|
| 126 | 161 | if err != nil { |
|
| 127 | 162 | a.Log.Error("update note failed", "err", err) |
|
| 128 | 163 | web.RedirectWithError(w, r, "/notes/"+shortID+"/edit", "Failed to update note") |
|
| 15 | 15 | Content string `json:"content"` |
|
| 16 | 16 | CreatedAt string `json:"created_at"` |
|
| 17 | 17 | UpdatedAt string `json:"updated_at"` |
|
| 18 | + | Public bool `json:"public"` |
|
| 18 | 19 | } |
|
| 19 | 20 | ||
| 20 | 21 | type NoteInput struct { |
|
| 22 | 23 | Content string `json:"content"` |
|
| 23 | 24 | } |
|
| 24 | 25 | ||
| 25 | - | const noteColumns = `id, short_id, title, content, created_at, updated_at` |
|
| 26 | + | type UpdateNoteRequest struct { |
|
| 27 | + | Title string `json:"title"` |
|
| 28 | + | Content string `json:"content"` |
|
| 29 | + | Public *bool `json:"public,omitempty"` |
|
| 30 | + | } |
|
| 31 | + | ||
| 32 | + | const noteColumns = `id, short_id, title, content, created_at, updated_at, public` |
|
| 26 | 33 | ||
| 27 | 34 | const schema = ` |
|
| 28 | 35 | CREATE TABLE IF NOT EXISTS notes ( |
|
| 31 | 38 | title TEXT NOT NULL, |
|
| 32 | 39 | content TEXT NOT NULL, |
|
| 33 | 40 | created_at TEXT NOT NULL DEFAULT (datetime('now')), |
|
| 34 | - | updated_at TEXT NOT NULL DEFAULT (datetime('now')) |
|
| 41 | + | updated_at TEXT NOT NULL DEFAULT (datetime('now')), |
|
| 42 | + | public BOOL DEFAULT (false) |
|
| 35 | 43 | ); |
|
| 36 | 44 | ` |
|
| 37 | 45 | ||
| 38 | 46 | func Open(path string) (*sql.DB, error) { |
|
| 39 | - | return sqlite.Open(path, schema) |
|
| 47 | + | db, err := sqlite.Open(path, schema) |
|
| 48 | + | if err != nil { |
|
| 49 | + | return nil, err |
|
| 50 | + | } |
|
| 51 | + | db.Exec(`ALTER TABLE notes ADD COLUMN public BOOL DEFAULT (false)`) |
|
| 52 | + | return db, nil |
|
| 40 | 53 | } |
|
| 41 | 54 | ||
| 42 | 55 | func scanNote(scanner interface{ Scan(dest ...any) error }) (*Note, error) { |
|
| 43 | 56 | var n Note |
|
| 44 | - | err := scanner.Scan(&n.ID, &n.ShortID, &n.Title, &n.Content, &n.CreatedAt, &n.UpdatedAt) |
|
| 57 | + | err := scanner.Scan(&n.ID, &n.ShortID, &n.Title, &n.Content, &n.CreatedAt, &n.UpdatedAt, &n.Public) |
|
| 45 | 58 | if errors.Is(err, sql.ErrNoRows) { |
|
| 46 | 59 | return nil, nil |
|
| 47 | 60 | } |
|
| 80 | 93 | var out []Note |
|
| 81 | 94 | for rows.Next() { |
|
| 82 | 95 | var n Note |
|
| 83 | - | if err := rows.Scan(&n.ID, &n.ShortID, &n.Title, &n.Content, &n.CreatedAt, &n.UpdatedAt); err != nil { |
|
| 96 | + | if err := rows.Scan(&n.ID, &n.ShortID, &n.Title, &n.Content, &n.CreatedAt, &n.UpdatedAt, &n.Public); err != nil { |
|
| 84 | 97 | return nil, err |
|
| 85 | 98 | } |
|
| 86 | 99 | out = append(out, n) |
|
| 88 | 101 | return out, rows.Err() |
|
| 89 | 102 | } |
|
| 90 | 103 | ||
| 91 | - | func UpdateByShortID(db *sql.DB, shortID, title, content string) (*Note, error) { |
|
| 92 | - | res, err := db.Exec(`UPDATE notes SET title = ?, content = ?, updated_at = datetime('now') WHERE short_id = ?`, title, content, shortID) |
|
| 93 | - | if err != nil { |
|
| 94 | - | return nil, err |
|
| 95 | - | } |
|
| 96 | - | n, _ := res.RowsAffected() |
|
| 97 | - | if n == 0 { |
|
| 98 | - | return nil, nil |
|
| 99 | - | } |
|
| 100 | - | return GetByShortID(db, shortID) |
|
| 104 | + | ||
| 105 | + | func UpdateByShortID(db *sql.DB, shortID, title, content string, public *bool) (*Note, error) { |
|
| 106 | + | res, err := db.Exec( |
|
| 107 | + | `UPDATE notes |
|
| 108 | + | SET title = ?, content = ?, public = COALESCE(?, public), updated_at = datetime('now') |
|
| 109 | + | WHERE short_id = ?`, |
|
| 110 | + | title, content, public, shortID, |
|
| 111 | + | ) |
|
| 112 | + | if err != nil { |
|
| 113 | + | return nil, err |
|
| 114 | + | } |
|
| 115 | + | n, _ := res.RowsAffected() |
|
| 116 | + | if n == 0 { |
|
| 117 | + | return nil, nil |
|
| 118 | + | } |
|
| 119 | + | return GetByShortID(db, shortID) |
|
| 101 | 120 | } |
|
| 102 | 121 | ||
| 103 | 122 | func DeleteByShortID(db *sql.DB, shortID string) (bool, error) { |
|
| 29 | 29 | mux.HandleFunc("GET /notes/new", requireSession(a.newNoteGetHandler)) |
|
| 30 | 30 | mux.HandleFunc("POST /notes", requireSession(a.createNoteHandler)) |
|
| 31 | 31 | mux.HandleFunc("GET /notes/{short_id}", requireSession(a.viewNoteHandler)) |
|
| 32 | + | mux.HandleFunc("GET /n/{short_id}", a.viewPublicNoteHandler) |
|
| 32 | 33 | mux.HandleFunc("GET /notes/{short_id}/edit", requireSession(a.editNoteGetHandler)) |
|
| 33 | 34 | mux.HandleFunc("POST /notes/{short_id}", requireSession(a.updateNoteHandler)) |
|
| 34 | 35 | mux.HandleFunc("POST /notes/{short_id}/delete", requireSession(a.deleteNoteHandler)) |
| 25 | 25 | <input type="text" id="title" name="title" value="{{.Note.Title}}" required> |
|
| 26 | 26 | <label for="content">content</label> |
|
| 27 | 27 | <textarea id="content" name="content">{{.Note.Content}}</textarea> |
|
| 28 | + | <div style="display: flex; align-items: center; padding: 1rem 0;"> |
|
| 29 | + | <label style="padding-right: 1rem;" for="public">Public</label> |
|
| 30 | + | <input type="checkbox" id="public" name="public" value="true" {{if .Note.Public}}checked{{end}}> |
|
| 31 | + | <input type="hidden" name="public" value="false"> |
|
| 32 | + | </div> |
|
| 28 | 33 | <button type="submit">save</button> |
|
| 29 | 34 | </form> |
|
| 30 | 35 | </main> |
| 1 | + | <!doctype html> |
|
| 2 | + | <html lang="en"> |
|
| 3 | + | <head> |
|
| 4 | + | <meta charset="UTF-8" /> |
|
| 5 | + | <meta name="viewport" content="width=device-width, initial-scale=1.0" /> |
|
| 6 | + | <title>Jotts — {{.Note.Title}}</title> |
|
| 7 | + | <link rel="apple-touch-icon" sizes="180x180" href="/static/apple-touch-icon.png"> |
|
| 8 | + | <link rel="icon" type="image/png" sizes="32x32" href="/static/favicon-32x32.png"> |
|
| 9 | + | <link rel="icon" type="image/png" sizes="16x16" href="/static/favicon-16x16.png"> |
|
| 10 | + | <link rel="manifest" href="/static/site.webmanifest"> |
|
| 11 | + | <link rel="icon" href="/static/favicon.ico"> |
|
| 12 | + | <meta property="og:title" content="Jotts"> |
|
| 13 | + | <meta property="og:image" content="/static/og.png"> |
|
| 14 | + | <meta property="og:type" content="website"> |
|
| 15 | + | <meta name="theme-color" content="#121113" /> |
|
| 16 | + | <link rel="stylesheet" href="/assets/darkmatter.css"> |
|
| 17 | + | <link rel="stylesheet" href="/static/styles.css"> |
|
| 18 | + | </head> |
|
| 19 | + | <body> |
|
| 20 | + | <main> |
|
| 21 | + | <div class="note-header" style="margin-top:2rem;"> |
|
| 22 | + | <h1>{{.Note.Title}}</h1> |
|
| 23 | + | </div> |
|
| 24 | + | <template id="raw-md">{{.Note.Content}}</template> |
|
| 25 | + | <article class="markdown-body">{{.Rendered}}</article> |
|
| 26 | + | </main> |
|
| 27 | + | <script> |
|
| 28 | + | function copyMarkdown() { |
|
| 29 | + | const md = document.getElementById("raw-md").content.textContent; |
|
| 30 | + | const btn = document.getElementById("copy-md-btn"); |
|
| 31 | + | navigator.clipboard.writeText(md).then(() => { |
|
| 32 | + | btn.textContent = "copied!"; |
|
| 33 | + | setTimeout(() => { btn.textContent = "copy"; }, 1500); |
|
| 34 | + | }); |
|
| 35 | + | } |
|
| 36 | + | document.querySelectorAll("time.note-date").forEach(el => { |
|
| 37 | + | const d = new Date(el.getAttribute("datetime")); |
|
| 38 | + | if (!isNaN(d)) { el.textContent = d.toLocaleString(); } |
|
| 39 | + | }); |
|
| 40 | + | </script> |
|
| 41 | + | </body> |
|
| 42 | + | </html> |
| 21 | 21 | List() ([]Note, error) |
|
| 22 | 22 | Get(shortID string) (*Note, error) |
|
| 23 | 23 | Create(title, content string) (*Note, error) |
|
| 24 | - | Update(shortID, title, content string) (*Note, error) |
|
| 24 | + | Update(shortID, title, content string, public *bool) (*Note, error) |
|
| 25 | 25 | Delete(shortID string) (bool, error) |
|
| 26 | 26 | RemoteURL() string |
|
| 27 | 27 | Close() error |
|
| 34 | 34 | func (b *LocalBackend) List() ([]Note, error) { return store.List(b.DB) } |
|
| 35 | 35 | func (b *LocalBackend) Get(s string) (*Note, error) { return store.GetByShortID(b.DB, s) } |
|
| 36 | 36 | func (b *LocalBackend) Create(t, c string) (*Note, error) { return store.Create(b.DB, t, c) } |
|
| 37 | - | func (b *LocalBackend) Update(s, t, c string) (*Note, error) { |
|
| 38 | - | return store.UpdateByShortID(b.DB, s, t, c) |
|
| 37 | + | func (b *LocalBackend) Update(s, t, c string, public *bool) (*Note, error) { |
|
| 38 | + | return store.UpdateByShortID(b.DB, s, t, c, public) |
|
| 39 | 39 | } |
|
| 40 | 40 | func (b *LocalBackend) Delete(s string) (bool, error) { return store.DeleteByShortID(b.DB, s) } |
|
| 41 | 41 | func (b *LocalBackend) RemoteURL() string { return "" } |
|
| 116 | 116 | return &n, nil |
|
| 117 | 117 | } |
|
| 118 | 118 | ||
| 119 | - | func (r *RemoteBackend) Update(shortID, title, content string) (*Note, error) { |
|
| 119 | + | func (r *RemoteBackend) Update(shortID, title, content string, public *bool) (*Note, error) { |
|
| 120 | 120 | var n Note |
|
| 121 | - | if err := r.do("PUT", "/api/notes/"+shortID, store.NoteInput{Title: title, Content: content}, &n); err != nil { |
|
| 121 | + | if err := r.do("PUT", "/api/notes/"+shortID, store.UpdateNoteRequest{Title: title, Content: content, Public: public}, &n); err != nil { |
|
| 122 | 122 | if err == errNotFound { |
|
| 123 | 123 | return nil, nil |
|
| 124 | 124 | } |
|
| 22 | 22 | if shortID == "" { |
|
| 23 | 23 | note, err = b.Create(title, content) |
|
| 24 | 24 | } else { |
|
| 25 | - | note, err = b.Update(shortID, title, content) |
|
| 25 | + | public := false |
|
| 26 | + | note, err = b.Update(shortID, title, content, &public) |
|
| 26 | 27 | } |
|
| 27 | 28 | return noteSavedMsg{Note: note, Err: err} |
|
| 28 | 29 | } |