2022-08-03 14:55:48 +00:00
|
|
|
package owl
|
2022-07-20 17:35:31 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2022-07-21 17:44:07 +00:00
|
|
|
"io/ioutil"
|
2022-08-13 16:47:27 +00:00
|
|
|
"net/url"
|
2022-07-20 17:35:31 +00:00
|
|
|
"os"
|
|
|
|
"path"
|
2022-08-17 19:57:55 +00:00
|
|
|
"sort"
|
2022-07-20 17:35:31 +00:00
|
|
|
"time"
|
2022-07-27 19:53:56 +00:00
|
|
|
|
|
|
|
"gopkg.in/yaml.v2"
|
2022-07-20 17:35:31 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type User struct {
|
2022-08-05 20:04:03 +00:00
|
|
|
repo *Repository
|
2022-07-20 17:35:31 +00:00
|
|
|
name string
|
|
|
|
}
|
|
|
|
|
2022-07-27 19:53:56 +00:00
|
|
|
type UserConfig struct {
|
2022-09-06 19:32:31 +00:00
|
|
|
Title string `yaml:"title"`
|
|
|
|
SubTitle string `yaml:"subtitle"`
|
|
|
|
HeaderColor string `yaml:"header_color"`
|
|
|
|
TwitterHandle string `yaml:"twitter_handle"`
|
2022-07-27 19:53:56 +00:00
|
|
|
}
|
|
|
|
|
2022-07-20 17:35:31 +00:00
|
|
|
func (user User) Dir() string {
|
2022-07-24 14:19:21 +00:00
|
|
|
return path.Join(user.repo.UsersDir(), user.name)
|
2022-07-20 17:35:31 +00:00
|
|
|
}
|
|
|
|
|
2022-08-03 17:41:13 +00:00
|
|
|
func (user User) UrlPath() string {
|
2022-08-03 18:34:42 +00:00
|
|
|
return user.repo.UserUrlPath(user)
|
2022-07-23 15:19:47 +00:00
|
|
|
}
|
|
|
|
|
2022-08-13 16:47:27 +00:00
|
|
|
func (user User) FullUrl() string {
|
|
|
|
url, _ := url.JoinPath(user.repo.FullUrl(), user.UrlPath())
|
|
|
|
return url
|
|
|
|
}
|
|
|
|
|
2022-08-31 18:20:16 +00:00
|
|
|
func (user User) WebmentionUrl() string {
|
|
|
|
url, _ := url.JoinPath(user.FullUrl(), "webmention/")
|
|
|
|
return url
|
|
|
|
}
|
|
|
|
|
2022-07-23 15:19:47 +00:00
|
|
|
func (user User) PostDir() string {
|
|
|
|
return path.Join(user.Dir(), "public")
|
|
|
|
}
|
|
|
|
|
2022-07-27 19:53:56 +00:00
|
|
|
func (user User) MetaDir() string {
|
|
|
|
return path.Join(user.Dir(), "meta")
|
|
|
|
}
|
|
|
|
|
|
|
|
func (user User) ConfigFile() string {
|
|
|
|
return path.Join(user.MetaDir(), "config.yml")
|
|
|
|
}
|
|
|
|
|
2022-07-20 17:35:31 +00:00
|
|
|
func (user User) Name() string {
|
|
|
|
return user.name
|
|
|
|
}
|
|
|
|
|
2022-08-21 10:27:28 +00:00
|
|
|
func (user User) Posts() ([]*Post, error) {
|
2022-08-03 16:03:10 +00:00
|
|
|
postFiles := listDir(path.Join(user.Dir(), "public"))
|
2022-08-21 10:27:28 +00:00
|
|
|
posts := make([]*Post, 0)
|
2022-07-23 15:19:47 +00:00
|
|
|
for _, id := range postFiles {
|
2022-08-03 16:03:10 +00:00
|
|
|
// if is a directory and has index.md, add to posts
|
|
|
|
if dirExists(path.Join(user.Dir(), "public", id)) {
|
|
|
|
if fileExists(path.Join(user.Dir(), "public", id, "index.md")) {
|
2022-08-17 19:57:55 +00:00
|
|
|
post, _ := user.GetPost(id)
|
2022-08-21 10:27:28 +00:00
|
|
|
posts = append(posts, &post)
|
2022-08-03 16:03:10 +00:00
|
|
|
}
|
2022-07-23 15:19:47 +00:00
|
|
|
}
|
|
|
|
}
|
2022-08-17 19:57:55 +00:00
|
|
|
|
2022-08-20 20:35:51 +00:00
|
|
|
// remove drafts
|
2022-08-22 06:06:46 +00:00
|
|
|
n := 0
|
|
|
|
for _, post := range posts {
|
2022-08-22 19:15:36 +00:00
|
|
|
meta := post.Meta()
|
2022-08-22 06:06:46 +00:00
|
|
|
if !meta.Draft {
|
|
|
|
posts[n] = post
|
|
|
|
n++
|
2022-08-20 20:35:51 +00:00
|
|
|
}
|
|
|
|
}
|
2022-08-22 06:06:46 +00:00
|
|
|
posts = posts[:n]
|
2022-08-20 20:35:51 +00:00
|
|
|
|
2022-08-17 20:21:44 +00:00
|
|
|
type PostWithDate struct {
|
2022-08-21 10:27:28 +00:00
|
|
|
post *Post
|
2022-08-17 20:21:44 +00:00
|
|
|
date time.Time
|
|
|
|
}
|
2022-08-17 19:57:55 +00:00
|
|
|
|
2022-08-17 20:21:44 +00:00
|
|
|
postDates := make([]PostWithDate, len(posts))
|
|
|
|
for i, post := range posts {
|
2022-08-22 19:15:36 +00:00
|
|
|
meta := post.Meta()
|
2022-08-17 20:21:44 +00:00
|
|
|
date, err := time.Parse(time.RFC1123Z, meta.Date)
|
2022-08-17 19:57:55 +00:00
|
|
|
if err != nil {
|
|
|
|
// invalid date -> use 1970-01-01
|
2022-08-17 20:21:44 +00:00
|
|
|
date = time.Time{}
|
2022-08-17 19:57:55 +00:00
|
|
|
}
|
2022-08-17 20:21:44 +00:00
|
|
|
postDates[i] = PostWithDate{post: post, date: date}
|
|
|
|
}
|
|
|
|
|
|
|
|
// sort posts by date
|
|
|
|
sort.Slice(postDates, func(i, j int) bool {
|
|
|
|
return postDates[i].date.After(postDates[j].date)
|
2022-08-17 19:57:55 +00:00
|
|
|
})
|
|
|
|
|
2022-08-17 20:21:44 +00:00
|
|
|
for i, post := range postDates {
|
|
|
|
posts[i] = post.post
|
|
|
|
}
|
|
|
|
|
2022-07-23 15:19:47 +00:00
|
|
|
return posts, nil
|
2022-07-21 17:44:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (user User) GetPost(id string) (Post, error) {
|
2022-08-13 09:26:17 +00:00
|
|
|
// check if posts index.md exists
|
|
|
|
if !fileExists(path.Join(user.Dir(), "public", id, "index.md")) {
|
|
|
|
return Post{}, fmt.Errorf("post %s does not exist", id)
|
|
|
|
}
|
|
|
|
|
2022-08-05 20:04:03 +00:00
|
|
|
post := Post{user: &user, id: id}
|
2022-08-22 19:15:36 +00:00
|
|
|
// post.loadMeta()
|
|
|
|
meta := post.Meta()
|
|
|
|
title := meta.Title
|
2022-07-21 17:44:07 +00:00
|
|
|
post.title = fmt.Sprint(title)
|
|
|
|
|
|
|
|
return post, nil
|
2022-07-20 19:12:18 +00:00
|
|
|
}
|
|
|
|
|
2022-07-21 17:02:37 +00:00
|
|
|
func (user User) CreateNewPost(title string) (Post, error) {
|
2022-07-20 17:35:31 +00:00
|
|
|
timestamp := time.Now().UTC().Unix()
|
|
|
|
folder_name := fmt.Sprintf("%d-%s", timestamp, title)
|
|
|
|
post_dir := path.Join(user.Dir(), "public", folder_name)
|
|
|
|
|
|
|
|
// if post already exists, add -n to the end of the name
|
|
|
|
i := 0
|
|
|
|
for {
|
|
|
|
if dirExists(post_dir) {
|
|
|
|
i++
|
|
|
|
folder_name = fmt.Sprintf("%d-%s-%d", timestamp, title, i)
|
|
|
|
post_dir = path.Join(user.Dir(), "public", folder_name)
|
|
|
|
} else {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
2022-08-05 20:04:03 +00:00
|
|
|
post := Post{user: &user, id: folder_name, title: title}
|
2022-07-21 17:44:07 +00:00
|
|
|
|
|
|
|
initial_content := ""
|
|
|
|
initial_content += "---\n"
|
|
|
|
initial_content += "title: " + title + "\n"
|
2022-08-16 19:16:14 +00:00
|
|
|
initial_content += "date: " + time.Now().UTC().Format(time.RFC1123Z) + "\n"
|
2022-07-21 17:44:07 +00:00
|
|
|
initial_content += "---\n"
|
|
|
|
initial_content += "\n"
|
|
|
|
initial_content += "Write your post here.\n"
|
2022-07-20 17:35:31 +00:00
|
|
|
|
|
|
|
// create post file
|
|
|
|
os.Mkdir(post_dir, 0755)
|
2022-07-21 17:02:37 +00:00
|
|
|
os.WriteFile(post.ContentFile(), []byte(initial_content), 0644)
|
2022-08-01 17:50:29 +00:00
|
|
|
// create media dir
|
|
|
|
os.Mkdir(post.MediaDir(), 0755)
|
2022-07-21 17:02:37 +00:00
|
|
|
return post, nil
|
2022-07-20 17:35:31 +00:00
|
|
|
}
|
2022-07-21 17:44:07 +00:00
|
|
|
|
|
|
|
func (user User) Template() (string, error) {
|
|
|
|
// load base.html
|
|
|
|
path := path.Join(user.Dir(), "meta", "base.html")
|
|
|
|
base_html, err := ioutil.ReadFile(path)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
return string(base_html), nil
|
|
|
|
}
|
2022-07-27 19:53:56 +00:00
|
|
|
|
|
|
|
func (user User) Config() (UserConfig, error) {
|
|
|
|
config_path := user.ConfigFile()
|
|
|
|
config_data, err := ioutil.ReadFile(config_path)
|
|
|
|
if err != nil {
|
|
|
|
return UserConfig{}, err
|
|
|
|
}
|
|
|
|
var meta UserConfig
|
|
|
|
err = yaml.Unmarshal(config_data, &meta)
|
|
|
|
if err != nil {
|
|
|
|
return UserConfig{}, err
|
|
|
|
}
|
|
|
|
return meta, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (user User) SetConfig(new_config UserConfig) error {
|
|
|
|
config_path := user.ConfigFile()
|
|
|
|
config_data, err := yaml.Marshal(new_config)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
err = ioutil.WriteFile(config_path, config_data, 0644)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
2022-08-06 17:38:13 +00:00
|
|
|
|
|
|
|
func (user User) PostAliases() (map[string]*Post, error) {
|
|
|
|
post_aliases := make(map[string]*Post)
|
|
|
|
posts, err := user.Posts()
|
|
|
|
if err != nil {
|
|
|
|
return post_aliases, err
|
|
|
|
}
|
2022-08-17 19:57:55 +00:00
|
|
|
for _, post := range posts {
|
2022-08-06 17:38:13 +00:00
|
|
|
if err != nil {
|
|
|
|
return post_aliases, err
|
|
|
|
}
|
|
|
|
for _, alias := range post.Aliases() {
|
2022-08-21 10:27:28 +00:00
|
|
|
post_aliases[alias] = post
|
2022-08-06 17:38:13 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return post_aliases, nil
|
|
|
|
}
|