1
0
Fork 0

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: ... .

This commit is contained in:
Alexander Andreev 2023-09-25 00:23:06 +04:00
parent 906f73c8a6
commit 85a3a9692e
Signed by: Arav
GPG Key ID: D22A817D95815393
12 changed files with 44 additions and 10 deletions

View File

@ -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

View File

@ -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
}

View File

@ -2,5 +2,6 @@ UPDATE OR REPLACE `post`
SET
`category_id` = ?,
`title` = ?,
`url` = ?,
`body` = ?
WHERE `post_id` = ?;

View File

@ -4,6 +4,7 @@ SELECT
`category`.`name` AS `category`,
`post`.`date`,
`post`.`title`,
`post`.`url`,
`post`.`body`
FROM `post`
LEFT JOIN `category`

View File

@ -1,4 +1,4 @@
INSERT INTO `post`
(`category_id`, `date`, `title`, `body`)
(`category_id`, `date`, `title`, `url`, `body`)
VALUES
(?, ?, ?, ?);
(?, ?, ?, ?, ?);

View File

@ -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`)

View File

@ -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

View File

@ -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; }

View File

@ -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))

View File

@ -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

View File

@ -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

View File

@ -14,7 +14,7 @@ rss(version='2.0')
category= post.Category.Name
guid!= `${post.PostID()}`
pubDate= util.ToClientTimezone(post.Date, r).Format(time.RFC1123Z)
| <link>#{host}/mindflow##{post.PostID()}</link>
| <link>#{post.PostURL(r.Host, true)}</link>
author!= author
description
| <![CDATA[