~erock/pico

3583e4a2040dccdd3cfc23d9b2de83404d2b5124 — Antonio Mika a month ago 17c7257
Fixed slug being filename when file contents are the same and allow cms post management for future posts
5 files changed, 47 insertions(+), 5 deletions(-)

M .env.example
M db/db.go
M db/postgres/storage.go
M filehandlers/post_handler.go
M wish/cms/ui/posts/posts.go
M .env.example => .env.example +3 -3
@@ 14,7 14,7 @@ LISTS_HOST=
LISTS_SSH_PORT=2222
LISTS_WEB_PORT=3000
LISTS_PROM_PORT=9222
LISTS_DOMAIN=lists.pico.sh:3000
LISTS_DOMAIN=lists.dev.pico.sh:3000
LISTS_EMAIL=hello@pico.sh
LISTS_SUBDOMAINS=1
LISTS_CUSTOMDOMAINS=1


@@ 32,7 32,7 @@ PASTES_HOST=
PASTES_SSH_PORT=2222
PASTES_WEB_PORT=3000
PASTES_PROM_PORT=9222
PASTES_DOMAIN=pastes.pico.sh:3001
PASTES_DOMAIN=pastes.dev.pico.sh:3001
PASTES_EMAIL=hello@pico.sh
PASTES_SUBDOMAINS=1
PASTES_CUSTOMDOMAINS=1


@@ 50,7 50,7 @@ PROSE_HOST=
PROSE_SSH_PORT=2222
PROSE_WEB_PORT=3000
PROSE_PROM_PORT=9222
PROSE_DOMAIN=prose.pico.sh:3002
PROSE_DOMAIN=prose.dev.pico.sh:3002
PROSE_EMAIL=hello@pico.sh
PROSE_SUBDOMAINS=1
PROSE_CUSTOMDOMAINS=1

M db/db.go => db/db.go +1 -0
@@ 108,6 108,7 @@ type DB interface {
	FindPosts() ([]*Post, error)
	FindPost(postID string) (*Post, error)
	FindPostsForUser(userID string, space string) ([]*Post, error)
	FindAllPostsForUser(userID string, space string) ([]*Post, error)
	FindPostsBeforeDate(date *time.Time, space string) ([]*Post, error)
	FindUpdatedPostsForUser(userID string, space string) ([]*Post, error)
	FindPostWithFilename(filename string, userID string, space string) (*Post, error)

M db/postgres/storage.go => db/postgres/storage.go +41 -0
@@ 50,6 50,15 @@ const (
		publish_at::date <= CURRENT_DATE AND
		cur_space = $2
	ORDER BY publish_at DESC`
	sqlSelectAllPostsForUser = `
	SELECT posts.id, user_id, filename, slug, title, text, description, publish_at,
		app_users.name as username, posts.updated_at
	FROM posts
	LEFT OUTER JOIN app_users ON app_users.id = posts.user_id
	WHERE
		user_id = $1 AND
		cur_space = $2
	ORDER BY publish_at DESC`
	sqlSelectPostsByTag = `
	SELECT posts.id, user_id, filename, slug, title, text, description, publish_at,
		app_users.name as username, posts.updated_at


@@ 569,6 578,38 @@ func (me *PsqlDB) FindPostsForUser(userID string, space string) ([]*db.Post, err
	return posts, nil
}

func (me *PsqlDB) FindAllPostsForUser(userID string, space string) ([]*db.Post, error) {
	var posts []*db.Post
	rs, err := me.Db.Query(sqlSelectAllPostsForUser, userID, space)
	if err != nil {
		return posts, err
	}
	for rs.Next() {
		post := &db.Post{}
		err := rs.Scan(
			&post.ID,
			&post.UserID,
			&post.Filename,
			&post.Slug,
			&post.Title,
			&post.Text,
			&post.Description,
			&post.PublishAt,
			&post.Username,
			&post.UpdatedAt,
		)
		if err != nil {
			return posts, err
		}

		posts = append(posts, post)
	}
	if rs.Err() != nil {
		return posts, rs.Err()
	}
	return posts, nil
}

func (me *PsqlDB) FindPosts() ([]*db.Post, error) {
	var posts []*db.Post
	rs, err := me.Db.Query(sqlSelectPosts)

M filehandlers/post_handler.go => filehandlers/post_handler.go +1 -1
@@ 154,7 154,7 @@ func (h *ScpUploadHandler) Write(s ssh.Session, entry *utils.FileEntry) (string,
	} else {
		if text == post.Text {
			logger.Infof("(%s) found, but text is identical, skipping", filename)
			return h.Cfg.FullPostURL(user.Name, filename, h.Cfg.IsSubdomains(), true), nil
			return h.Cfg.FullPostURL(user.Name, metadata.Slug, h.Cfg.IsSubdomains(), true), nil
		}

		logger.Infof("(%s) found, updating record", filename)

M wish/cms/ui/posts/posts.go => wish/cms/ui/posts/posts.go +1 -1
@@ 318,7 318,7 @@ func LoadPosts(m Model) tea.Cmd {

func (m Model) fetchPosts(userID string) tea.Cmd {
	return func() tea.Msg {
		posts, _ := m.dbpool.FindPostsForUser(userID, m.cfg.Space)
		posts, _ := m.dbpool.FindAllPostsForUser(userID, m.cfg.Space)
		loader := PostLoader{
			Posts: posts,
		}