From 85a3a9692e9e511089b2f04685305fdb3a967908 Mon Sep 17 00:00:00 2001 From: "Alexander \"Arav\" Andreev" Date: Mon, 25 Sep 2023 00:23:06 +0400 Subject: [PATCH] A big revamp for mindflow and RSS. First of all, now RSS is 100% valid and has actual links instead of just a link to a post in a mindflow. Also, on mindflow page each post that has a URL field set has it displayed as Link: ... . --- internal/http/api_mindflow_handlers.go | 5 +++-- pkg/mindflow/database/sqlite/db.go | 9 +++++--- .../database/sqlite/queries/postEdit.sql | 1 + .../database/sqlite/queries/postGetAll.sql | 1 + .../database/sqlite/queries/postNew.sql | 4 ++-- .../database/sqlite/queries/schema.sql | 1 + pkg/mindflow/mindflow.go | 22 ++++++++++++++++++- web/assets/css/mindflow_admin.css | 3 ++- web/assets/js/mindflow_admin.js | 1 + web/templates/mindflow.pug | 3 +++ web/templates/mindflow_admin.pug | 2 ++ web/templates/rss.pug | 2 +- 12 files changed, 44 insertions(+), 10 deletions(-) diff --git a/internal/http/api_mindflow_handlers.go b/internal/http/api_mindflow_handlers.go index 23342f8..23b9c91 100644 --- a/internal/http/api_mindflow_handlers.go +++ b/internal/http/api_mindflow_handlers.go @@ -42,7 +42,7 @@ func (h *MindflowApiHandlers) NewPost(w http.ResponseWriter, r *http.Request) { category.ID, _ = strconv.ParseInt(r.FormValue("category"), 10, 64) } - post, err = mindflow.NewPost(*category, r.FormValue("title"), r.FormValue("body")) + post, err = mindflow.NewPost(*category, r.FormValue("title"), r.FormValue("url"), r.FormValue("body")) if err != nil { http.Error(w, err.Error(), http.StatusInternalServerError) return @@ -67,6 +67,7 @@ func (h *MindflowApiHandlers) EditPost(w http.ResponseWriter, r *http.Request) { r.ParseForm() body := cleanupNewlines(r.FormValue("body")) + url := r.FormValue("url") category.ID, err = strconv.ParseInt(r.FormValue("category"), 10, 64) if err != nil { @@ -74,7 +75,7 @@ func (h *MindflowApiHandlers) EditPost(w http.ResponseWriter, r *http.Request) { return } - post, err = mindflow.NewPost(category, r.FormValue("title"), body) + post, err = mindflow.NewPost(category, r.FormValue("title"), url, body) if err != nil { http.Error(w, err.Error(), http.StatusInternalServerError) return diff --git a/pkg/mindflow/database/sqlite/db.go b/pkg/mindflow/database/sqlite/db.go index 5427668..25e8d88 100644 --- a/pkg/mindflow/database/sqlite/db.go +++ b/pkg/mindflow/database/sqlite/db.go @@ -122,7 +122,8 @@ func (s *SQLiteMindflow) NewPost(post *mindflow.Post) error { } defer tx.Rollback() - r, err := tx.Stmt(stmtPostNew).Exec(post.Category.ID, post.Date.UTC().Unix(), post.Title, post.Body) + r, err := tx.Stmt(stmtPostNew).Exec(post.Category.ID, post.Date.UTC().Unix(), + post.Title, post.URL, post.Body) if err != nil { return err } @@ -144,7 +145,8 @@ func (s *SQLiteMindflow) EditPost(post *mindflow.Post) error { } defer tx.Rollback() - _, err = tx.Stmt(stmtPostEdit).Exec(post.Category.ID, post.Title, post.Body, post.ID) + _, err = tx.Stmt(stmtPostEdit).Exec(post.Category.ID, post.Title, post.URL, + post.Body, post.ID) if err != nil { return err } @@ -187,7 +189,8 @@ func (s *SQLiteMindflow) Posts() (posts []mindflow.Post, err error) { var post mindflow.Post var date_unix int64 - if err = rows.Scan(&post.ID, &post.Category.ID, &post.Category.Name, &date_unix, &post.Title, &post.Body); err != nil { + if err = rows.Scan(&post.ID, &post.Category.ID, &post.Category.Name, + &date_unix, &post.Title, &post.URL, &post.Body); err != nil { return nil, err } diff --git a/pkg/mindflow/database/sqlite/queries/postEdit.sql b/pkg/mindflow/database/sqlite/queries/postEdit.sql index 0406706..ef974d8 100644 --- a/pkg/mindflow/database/sqlite/queries/postEdit.sql +++ b/pkg/mindflow/database/sqlite/queries/postEdit.sql @@ -2,5 +2,6 @@ UPDATE OR REPLACE `post` SET `category_id` = ?, `title` = ?, + `url` = ?, `body` = ? WHERE `post_id` = ?; \ No newline at end of file diff --git a/pkg/mindflow/database/sqlite/queries/postGetAll.sql b/pkg/mindflow/database/sqlite/queries/postGetAll.sql index 0b7551f..4a88c1a 100644 --- a/pkg/mindflow/database/sqlite/queries/postGetAll.sql +++ b/pkg/mindflow/database/sqlite/queries/postGetAll.sql @@ -4,6 +4,7 @@ SELECT `category`.`name` AS `category`, `post`.`date`, `post`.`title`, + `post`.`url`, `post`.`body` FROM `post` LEFT JOIN `category` diff --git a/pkg/mindflow/database/sqlite/queries/postNew.sql b/pkg/mindflow/database/sqlite/queries/postNew.sql index 65eb413..b3a6812 100644 --- a/pkg/mindflow/database/sqlite/queries/postNew.sql +++ b/pkg/mindflow/database/sqlite/queries/postNew.sql @@ -1,4 +1,4 @@ INSERT INTO `post` - (`category_id`, `date`, `title`, `body`) + (`category_id`, `date`, `title`, `url`, `body`) VALUES - (?, ?, ?, ?); \ No newline at end of file + (?, ?, ?, ?, ?); \ No newline at end of file diff --git a/pkg/mindflow/database/sqlite/queries/schema.sql b/pkg/mindflow/database/sqlite/queries/schema.sql index 2f4a550..c84e66f 100644 --- a/pkg/mindflow/database/sqlite/queries/schema.sql +++ b/pkg/mindflow/database/sqlite/queries/schema.sql @@ -15,6 +15,7 @@ CREATE TABLE IF NOT EXISTS `post` ( `category_id` INTEGER NOT NULL, `date` INTEGER NOT NULL, `title` TEXT NOT NULL, + `url` TEXT NOT NULL DEFAULT "", `body` TEXT NOT NULL, PRIMARY KEY (`post_id`), FOREIGN KEY (`category_id`) diff --git a/pkg/mindflow/mindflow.go b/pkg/mindflow/mindflow.go index ea9218a..2e97839 100644 --- a/pkg/mindflow/mindflow.go +++ b/pkg/mindflow/mindflow.go @@ -5,6 +5,8 @@ import ( "fmt" "strings" "time" + + "git.arav.su/Arav/dwelling-home/pkg/util" ) type Category struct { @@ -24,10 +26,11 @@ type Post struct { Category Category Date time.Time Title string + URL string Body string } -func NewPost(category Category, title, body string) (*Post, error) { +func NewPost(category Category, title, url, body string) (*Post, error) { if title == "" || body == "" { return nil, errors.New("empty title or body is not allowed") } @@ -35,6 +38,7 @@ func NewPost(category Category, title, body string) (*Post, error) { Category: category, Date: time.Now().UTC(), Title: title, + URL: url, Body: body}, nil } @@ -44,6 +48,22 @@ func (p *Post) PostID() string { return fmt.Sprint(name, "-", p.Date.UTC().Format("20060102-150405")) } +func (p *Post) PostURL(host string, rss bool) string { + if p.URL != "" { + if p.URL[0] == ':' { + lastColon := strings.IndexByte(p.URL[1:], ':') + service := p.URL[1 : lastColon+1] + return strings.Replace(p.URL, p.URL[:lastColon+2], + util.GetServiceByHost(host, service), 1) + } else if rss && p.URL[0] == '/' { + return util.GetServiceByHost(host, "") + p.URL + } + } else if p.URL == "" && rss { + return util.GetServiceByHost(host, "") + "/mindflow#" + p.PostID() + } + return p.URL +} + type Mindflow interface { NewPost(post *Post) error EditPost(post *Post) error diff --git a/web/assets/css/mindflow_admin.css b/web/assets/css/mindflow_admin.css index d8b3cbc..788db10 100644 --- a/web/assets/css/mindflow_admin.css +++ b/web/assets/css/mindflow_admin.css @@ -18,7 +18,7 @@ form#add { gap: .5rem; grid-template-areas: "c c" - "t t" + "t u" "b b" "a a"; grid-template-columns: 1fr 1fr; @@ -28,6 +28,7 @@ form#add { form#add select { grid-area: c; } form#add input[name="title"] { grid-area: t; } +form#add input[name="url"] { grid-area: u; } form#add textarea[name="body"] { grid-area: b; } form#add button[type="submit"] { grid-area: a; } diff --git a/web/assets/js/mindflow_admin.js b/web/assets/js/mindflow_admin.js index bb42cea..290a8d8 100644 --- a/web/assets/js/mindflow_admin.js +++ b/web/assets/js/mindflow_admin.js @@ -7,6 +7,7 @@ function edit_post(e) { let data = new URLSearchParams(); data.append("category", get_field(e.target, "category")) data.append("title", get_field(e.target, "title")) + data.append("url", get_field(e.target, "url")) data.append("body", get_field(e.target, "body")) fetch(`/api/mindflow/${get_field(e.target, "post-id")}`, {method: "PATCH", body: data}) .catch(e => console.log(e)) diff --git a/web/templates/mindflow.pug b/web/templates/mindflow.pug index 6ebd554..ccb3bc9 100755 --- a/web/templates/mindflow.pug +++ b/web/templates/mindflow.pug @@ -31,6 +31,9 @@ block content header a(href=`#${post.PostID()}`) h3= post.Category.Name + ": " + post.Title + if (post.URL != "") + p Link: + a(href=post.PostURL(r.Host, false)) #{post.PostURL(r.Host, false)} each line in strings.Split(post.Body, "\n") p!= line footer diff --git a/web/templates/mindflow_admin.pug b/web/templates/mindflow_admin.pug index e68f959..e32bebf 100755 --- a/web/templates/mindflow_admin.pug +++ b/web/templates/mindflow_admin.pug @@ -37,6 +37,7 @@ block content each category in categories option(value=category.ID) #{category.Name} input(type='text', placeholder='Title' name='title' required='') + input(type='text', placeholder='URL' name='url') textarea(placeholder='Body post' name='body' required='') button(type="submit") Add section @@ -55,6 +56,7 @@ block content option(value=category.ID) #{category.Name} input(type='hidden', name='post-id' value=post.ID) input(type='text', placeholder='Title' name='title' value=post.Title required='') + input(type='text', placeholder='URL' name='url' value=post.URL) textarea(placeholder='Body post' name='body' required='')!= post.Body button(name='edit-post') Edit button(name='delete-post') Delete diff --git a/web/templates/rss.pug b/web/templates/rss.pug index 5b268c2..18da5bc 100755 --- a/web/templates/rss.pug +++ b/web/templates/rss.pug @@ -14,7 +14,7 @@ rss(version='2.0') category= post.Category.Name guid!= `${post.PostID()}` pubDate= util.ToClientTimezone(post.Date, r).Format(time.RFC1123Z) - | #{host}/mindflow##{post.PostID()} + | #{post.PostURL(r.Host, true)} author!= author description |