2022-09-05 18:34:24 +00:00
|
|
|
package web
|
2022-08-03 17:41:13 +00:00
|
|
|
|
|
|
|
import (
|
2022-09-08 18:52:04 +00:00
|
|
|
"fmt"
|
2022-08-03 17:41:13 +00:00
|
|
|
"h4kor/owl-blogs"
|
|
|
|
"net/http"
|
2022-09-08 18:52:04 +00:00
|
|
|
"net/url"
|
2022-08-03 17:43:00 +00:00
|
|
|
"os"
|
|
|
|
"path"
|
2022-08-23 15:59:17 +00:00
|
|
|
"strings"
|
2022-11-19 14:54:26 +00:00
|
|
|
"time"
|
2022-08-03 17:41:13 +00:00
|
|
|
|
|
|
|
"github.com/julienschmidt/httprouter"
|
|
|
|
)
|
|
|
|
|
2022-08-06 18:22:09 +00:00
|
|
|
func getUserFromRepo(repo *owl.Repository, ps httprouter.Params) (owl.User, error) {
|
2022-09-05 18:50:46 +00:00
|
|
|
if config, _ := repo.Config(); config.SingleUser != "" {
|
|
|
|
return repo.GetUser(config.SingleUser)
|
2022-08-03 18:48:47 +00:00
|
|
|
}
|
|
|
|
userName := ps.ByName("user")
|
|
|
|
user, err := repo.GetUser(userName)
|
|
|
|
if err != nil {
|
|
|
|
return owl.User{}, err
|
|
|
|
}
|
|
|
|
return user, nil
|
|
|
|
}
|
|
|
|
|
2022-08-06 18:22:09 +00:00
|
|
|
func repoIndexHandler(repo *owl.Repository) func(http.ResponseWriter, *http.Request, httprouter.Params) {
|
2022-08-03 17:41:13 +00:00
|
|
|
return func(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
|
2022-08-06 18:22:09 +00:00
|
|
|
html, err := owl.RenderUserList(*repo)
|
2022-08-03 17:41:13 +00:00
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
println("Error rendering index: ", err.Error())
|
|
|
|
w.WriteHeader(http.StatusInternalServerError)
|
|
|
|
w.Write([]byte("Internal server error"))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
println("Rendering index")
|
|
|
|
w.Write([]byte(html))
|
|
|
|
}
|
|
|
|
}
|
2022-08-03 17:43:00 +00:00
|
|
|
|
2022-08-06 18:22:09 +00:00
|
|
|
func userIndexHandler(repo *owl.Repository) func(http.ResponseWriter, *http.Request, httprouter.Params) {
|
2022-08-03 17:43:00 +00:00
|
|
|
return func(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
|
2022-08-03 18:48:47 +00:00
|
|
|
user, err := getUserFromRepo(repo, ps)
|
2022-08-03 17:43:00 +00:00
|
|
|
if err != nil {
|
|
|
|
println("Error getting user: ", err.Error())
|
2022-08-13 09:26:17 +00:00
|
|
|
notFoundHandler(repo)(w, r)
|
2022-08-03 17:43:00 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
html, err := owl.RenderIndexPage(user)
|
|
|
|
if err != nil {
|
|
|
|
println("Error rendering index page: ", err.Error())
|
|
|
|
w.WriteHeader(http.StatusInternalServerError)
|
2022-11-08 18:57:20 +00:00
|
|
|
html, _ := owl.RenderUserError(user, owl.ErrorMessage{
|
|
|
|
Error: "Internal server error",
|
|
|
|
Message: "Internal server error",
|
|
|
|
})
|
|
|
|
w.Write([]byte(html))
|
2022-08-03 17:43:00 +00:00
|
|
|
return
|
|
|
|
}
|
2022-08-13 13:32:26 +00:00
|
|
|
println("Rendering index page for user", user.Name())
|
|
|
|
w.Write([]byte(html))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-12-01 18:10:08 +00:00
|
|
|
func postListHandler(repo *owl.Repository) func(http.ResponseWriter, *http.Request, httprouter.Params) {
|
|
|
|
return func(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
|
|
|
|
listId := ps.ByName("list")
|
|
|
|
user, err := getUserFromRepo(repo, ps)
|
|
|
|
if err != nil {
|
|
|
|
println("Error getting user: ", err.Error())
|
|
|
|
notFoundHandler(repo)(w, r)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
list, err := user.GetPostList(listId)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
println("Error getting post list: ", err.Error())
|
|
|
|
notFoundUserHandler(repo, user)(w, r)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
html, err := owl.RenderPostList(user, list)
|
|
|
|
if err != nil {
|
|
|
|
println("Error rendering index page: ", err.Error())
|
|
|
|
w.WriteHeader(http.StatusInternalServerError)
|
|
|
|
html, _ := owl.RenderUserError(user, owl.ErrorMessage{
|
|
|
|
Error: "Internal server error",
|
|
|
|
Message: "Internal server error",
|
|
|
|
})
|
|
|
|
w.Write([]byte(html))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
println("Rendering index page for user", user.Name())
|
|
|
|
w.Write([]byte(html))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-08-23 15:59:17 +00:00
|
|
|
func userWebmentionHandler(repo *owl.Repository) func(http.ResponseWriter, *http.Request, httprouter.Params) {
|
|
|
|
return func(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
|
|
|
|
user, err := getUserFromRepo(repo, ps)
|
|
|
|
if err != nil {
|
2022-08-23 19:08:14 +00:00
|
|
|
w.WriteHeader(http.StatusBadRequest)
|
2022-08-23 15:59:17 +00:00
|
|
|
w.Write([]byte("User not found"))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
err = r.ParseForm()
|
|
|
|
if err != nil {
|
|
|
|
w.WriteHeader(http.StatusBadRequest)
|
|
|
|
w.Write([]byte("Unable to parse form data"))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
params := r.PostForm
|
|
|
|
target := params["target"]
|
|
|
|
source := params["source"]
|
|
|
|
if len(target) == 0 {
|
|
|
|
w.WriteHeader(http.StatusBadRequest)
|
|
|
|
w.Write([]byte("No target provided"))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if len(source) == 0 {
|
|
|
|
w.WriteHeader(http.StatusBadRequest)
|
|
|
|
w.Write([]byte("No source provided"))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-08-23 19:06:29 +00:00
|
|
|
if len(target[0]) < 7 || (target[0][:7] != "http://" && target[0][:8] != "https://") {
|
|
|
|
w.WriteHeader(http.StatusBadRequest)
|
|
|
|
w.Write([]byte("Not a valid target"))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(source[0]) < 7 || (source[0][:7] != "http://" && source[0][:8] != "https://") {
|
|
|
|
w.WriteHeader(http.StatusBadRequest)
|
|
|
|
w.Write([]byte("Not a valid source"))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if source[0] == target[0] {
|
|
|
|
w.WriteHeader(http.StatusBadRequest)
|
|
|
|
w.Write([]byte("target and source are equal"))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-12-05 20:09:23 +00:00
|
|
|
tryAlias := func(target string) owl.Post {
|
2022-09-08 18:52:04 +00:00
|
|
|
parsedTarget, _ := url.Parse(target)
|
|
|
|
aliases, _ := repo.PostAliases()
|
|
|
|
fmt.Printf("aliases %v", aliases)
|
|
|
|
fmt.Printf("parsedTarget %v", parsedTarget)
|
|
|
|
if _, ok := aliases[parsedTarget.Path]; ok {
|
|
|
|
return aliases[parsedTarget.Path]
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-12-05 20:09:23 +00:00
|
|
|
var aliasPost owl.Post
|
2022-08-23 15:59:17 +00:00
|
|
|
parts := strings.Split(target[0], "/")
|
|
|
|
if len(parts) < 2 {
|
2022-09-08 18:52:04 +00:00
|
|
|
aliasPost = tryAlias(target[0])
|
|
|
|
if aliasPost == nil {
|
|
|
|
w.WriteHeader(http.StatusBadRequest)
|
|
|
|
w.Write([]byte("Not found"))
|
|
|
|
return
|
|
|
|
}
|
2022-08-23 15:59:17 +00:00
|
|
|
}
|
|
|
|
postId := parts[len(parts)-2]
|
2022-09-08 18:52:04 +00:00
|
|
|
foundPost, err := user.GetPost(postId)
|
|
|
|
if err != nil && aliasPost == nil {
|
|
|
|
aliasPost = tryAlias(target[0])
|
|
|
|
if aliasPost == nil {
|
|
|
|
w.WriteHeader(http.StatusBadRequest)
|
|
|
|
w.Write([]byte("Post not found"))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if aliasPost != nil {
|
2022-10-13 19:03:16 +00:00
|
|
|
foundPost = aliasPost
|
2022-08-23 15:59:17 +00:00
|
|
|
}
|
2022-09-08 18:52:04 +00:00
|
|
|
err = foundPost.AddIncomingWebmention(source[0])
|
2022-08-23 15:59:17 +00:00
|
|
|
if err != nil {
|
|
|
|
w.WriteHeader(http.StatusInternalServerError)
|
|
|
|
w.Write([]byte("Unable to process webmention"))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
w.WriteHeader(http.StatusAccepted)
|
|
|
|
w.Write([]byte(""))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-08-13 13:32:26 +00:00
|
|
|
func userRSSHandler(repo *owl.Repository) func(http.ResponseWriter, *http.Request, httprouter.Params) {
|
|
|
|
return func(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
|
|
|
|
user, err := getUserFromRepo(repo, ps)
|
|
|
|
if err != nil {
|
|
|
|
println("Error getting user: ", err.Error())
|
|
|
|
notFoundHandler(repo)(w, r)
|
|
|
|
return
|
|
|
|
}
|
2022-08-16 18:36:05 +00:00
|
|
|
xml, err := owl.RenderRSSFeed(user)
|
2022-08-13 13:32:26 +00:00
|
|
|
if err != nil {
|
|
|
|
println("Error rendering index page: ", err.Error())
|
|
|
|
w.WriteHeader(http.StatusInternalServerError)
|
2022-11-08 18:57:20 +00:00
|
|
|
html, _ := owl.RenderUserError(user, owl.ErrorMessage{
|
|
|
|
Error: "Internal server error",
|
|
|
|
Message: "Internal server error",
|
|
|
|
})
|
|
|
|
w.Write([]byte(html))
|
2022-08-13 13:32:26 +00:00
|
|
|
return
|
|
|
|
}
|
2022-08-03 18:48:47 +00:00
|
|
|
println("Rendering index page for user", user.Name())
|
2022-08-16 18:36:05 +00:00
|
|
|
w.Header().Set("Content-Type", "application/rss+xml")
|
|
|
|
w.Write([]byte(xml))
|
2022-08-03 17:43:00 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-08-06 18:22:09 +00:00
|
|
|
func postHandler(repo *owl.Repository) func(http.ResponseWriter, *http.Request, httprouter.Params) {
|
2022-08-03 17:43:00 +00:00
|
|
|
return func(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
|
|
|
|
postId := ps.ByName("post")
|
2022-08-03 18:48:47 +00:00
|
|
|
|
|
|
|
user, err := getUserFromRepo(repo, ps)
|
2022-08-03 17:43:00 +00:00
|
|
|
if err != nil {
|
|
|
|
println("Error getting user: ", err.Error())
|
2022-08-13 09:26:17 +00:00
|
|
|
notFoundHandler(repo)(w, r)
|
2022-08-03 17:43:00 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
post, err := user.GetPost(postId)
|
2022-08-20 20:46:52 +00:00
|
|
|
|
2022-08-03 17:43:00 +00:00
|
|
|
if err != nil {
|
|
|
|
println("Error getting post: ", err.Error())
|
2022-11-08 18:57:20 +00:00
|
|
|
notFoundUserHandler(repo, user)(w, r)
|
2022-08-03 17:43:00 +00:00
|
|
|
return
|
|
|
|
}
|
2022-08-20 20:46:52 +00:00
|
|
|
|
2022-08-22 19:15:36 +00:00
|
|
|
meta := post.Meta()
|
2022-08-20 20:46:52 +00:00
|
|
|
if meta.Draft {
|
|
|
|
println("Post is a draft")
|
2022-11-08 18:57:20 +00:00
|
|
|
notFoundUserHandler(repo, user)(w, r)
|
2022-08-20 20:46:52 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-10-13 19:03:16 +00:00
|
|
|
html, err := owl.RenderPost(post)
|
2022-08-03 17:43:00 +00:00
|
|
|
if err != nil {
|
|
|
|
println("Error rendering post: ", err.Error())
|
|
|
|
w.WriteHeader(http.StatusInternalServerError)
|
2022-11-08 18:57:20 +00:00
|
|
|
html, _ := owl.RenderUserError(user, owl.ErrorMessage{
|
|
|
|
Error: "Internal server error",
|
|
|
|
Message: "Internal server error",
|
|
|
|
})
|
|
|
|
w.Write([]byte(html))
|
2022-08-03 17:43:00 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
println("Rendering post", postId)
|
|
|
|
w.Write([]byte(html))
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-08-06 18:22:09 +00:00
|
|
|
func postMediaHandler(repo *owl.Repository) func(http.ResponseWriter, *http.Request, httprouter.Params) {
|
2022-08-03 17:43:00 +00:00
|
|
|
return func(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
|
|
|
|
postId := ps.ByName("post")
|
|
|
|
filepath := ps.ByName("filepath")
|
|
|
|
|
2022-08-03 18:48:47 +00:00
|
|
|
user, err := getUserFromRepo(repo, ps)
|
2022-08-03 17:43:00 +00:00
|
|
|
if err != nil {
|
|
|
|
println("Error getting user: ", err.Error())
|
2022-08-13 09:26:17 +00:00
|
|
|
notFoundHandler(repo)(w, r)
|
2022-08-03 17:43:00 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
post, err := user.GetPost(postId)
|
|
|
|
if err != nil {
|
|
|
|
println("Error getting post: ", err.Error())
|
2022-11-08 18:57:20 +00:00
|
|
|
notFoundUserHandler(repo, user)(w, r)
|
2022-08-03 17:43:00 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
filepath = path.Join(post.MediaDir(), filepath)
|
|
|
|
if _, err := os.Stat(filepath); err != nil {
|
|
|
|
println("Error getting file: ", err.Error())
|
2022-11-08 18:57:20 +00:00
|
|
|
notFoundUserHandler(repo, user)(w, r)
|
2022-08-03 17:43:00 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
http.ServeFile(w, r, filepath)
|
|
|
|
}
|
|
|
|
}
|
2022-08-06 18:17:39 +00:00
|
|
|
|
2022-11-08 20:22:02 +00:00
|
|
|
func userMicropubHandler(repo *owl.Repository) func(http.ResponseWriter, *http.Request, httprouter.Params) {
|
|
|
|
return func(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
|
|
|
|
user, err := getUserFromRepo(repo, ps)
|
|
|
|
if err != nil {
|
|
|
|
println("Error getting user: ", err.Error())
|
|
|
|
notFoundHandler(repo)(w, r)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-11-19 12:43:10 +00:00
|
|
|
// parse request form
|
|
|
|
err = r.ParseForm()
|
|
|
|
if err != nil {
|
|
|
|
w.WriteHeader(http.StatusBadRequest)
|
|
|
|
w.Write([]byte("Bad request"))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-11-08 20:22:02 +00:00
|
|
|
// verify access token
|
2022-11-19 13:08:16 +00:00
|
|
|
token := r.Header.Get("Authorization")
|
|
|
|
if token == "" {
|
|
|
|
token = r.Form.Get("access_token")
|
2022-11-19 12:43:10 +00:00
|
|
|
} else {
|
2022-11-19 13:08:16 +00:00
|
|
|
token = strings.TrimPrefix(token, "Bearer ")
|
2022-11-19 12:43:10 +00:00
|
|
|
}
|
|
|
|
|
2022-11-08 20:22:02 +00:00
|
|
|
valid, _ := user.ValidateAccessToken(token)
|
|
|
|
if !valid {
|
|
|
|
w.WriteHeader(http.StatusUnauthorized)
|
|
|
|
w.Write([]byte("Unauthorized"))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
h := r.Form.Get("h")
|
|
|
|
content := r.Form.Get("content")
|
|
|
|
name := r.Form.Get("name")
|
|
|
|
inReplyTo := r.Form.Get("in-reply-to")
|
|
|
|
|
|
|
|
if h != "entry" {
|
|
|
|
w.WriteHeader(http.StatusBadRequest)
|
2022-11-19 14:55:38 +00:00
|
|
|
w.Write([]byte("Bad request. h must be entry. Got " + h))
|
2022-11-08 20:22:02 +00:00
|
|
|
return
|
|
|
|
}
|
2022-11-19 12:43:10 +00:00
|
|
|
if content == "" {
|
2022-11-08 20:22:02 +00:00
|
|
|
w.WriteHeader(http.StatusBadRequest)
|
2022-11-19 12:43:10 +00:00
|
|
|
w.Write([]byte("Bad request. content is required"))
|
2022-11-08 20:22:02 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// create post
|
2022-12-05 19:47:52 +00:00
|
|
|
post, err := user.CreateNewPost(
|
2022-11-08 20:22:02 +00:00
|
|
|
owl.PostMeta{
|
|
|
|
Title: name,
|
2022-12-05 19:31:48 +00:00
|
|
|
Reply: owl.ReplyData{
|
2022-11-08 20:22:02 +00:00
|
|
|
Url: inReplyTo,
|
|
|
|
},
|
2022-11-19 14:54:26 +00:00
|
|
|
Date: time.Now(),
|
2022-11-08 20:22:02 +00:00
|
|
|
},
|
|
|
|
content,
|
|
|
|
)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
w.WriteHeader(http.StatusInternalServerError)
|
|
|
|
w.Write([]byte("Internal server error"))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-11-19 12:43:10 +00:00
|
|
|
w.Header().Add("Location", post.FullUrl())
|
2022-11-08 20:22:02 +00:00
|
|
|
w.WriteHeader(http.StatusCreated)
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-09-10 13:22:18 +00:00
|
|
|
func userMediaHandler(repo *owl.Repository) func(http.ResponseWriter, *http.Request, httprouter.Params) {
|
|
|
|
return func(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
|
|
|
|
filepath := ps.ByName("filepath")
|
|
|
|
|
|
|
|
user, err := getUserFromRepo(repo, ps)
|
|
|
|
if err != nil {
|
|
|
|
println("Error getting user: ", err.Error())
|
|
|
|
notFoundHandler(repo)(w, r)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
filepath = path.Join(user.MediaDir(), filepath)
|
|
|
|
if _, err := os.Stat(filepath); err != nil {
|
|
|
|
println("Error getting file: ", err.Error())
|
2022-11-08 18:57:20 +00:00
|
|
|
notFoundUserHandler(repo, user)(w, r)
|
2022-09-10 13:22:18 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
http.ServeFile(w, r, filepath)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-08-06 18:22:09 +00:00
|
|
|
func notFoundHandler(repo *owl.Repository) func(http.ResponseWriter, *http.Request) {
|
2022-08-06 18:17:39 +00:00
|
|
|
return func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
path := r.URL.Path
|
|
|
|
aliases, _ := repo.PostAliases()
|
|
|
|
if _, ok := aliases[path]; ok {
|
|
|
|
http.Redirect(w, r, aliases[path].UrlPath(), http.StatusMovedPermanently)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
w.WriteHeader(http.StatusNotFound)
|
|
|
|
w.Write([]byte("Not found"))
|
|
|
|
}
|
|
|
|
}
|
2022-11-08 18:57:20 +00:00
|
|
|
|
|
|
|
func notFoundUserHandler(repo *owl.Repository, user owl.User) func(http.ResponseWriter, *http.Request) {
|
|
|
|
return func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
path := r.URL.Path
|
|
|
|
aliases, _ := repo.PostAliases()
|
|
|
|
if _, ok := aliases[path]; ok {
|
|
|
|
http.Redirect(w, r, aliases[path].UrlPath(), http.StatusMovedPermanently)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
w.WriteHeader(http.StatusNotFound)
|
|
|
|
html, _ := owl.RenderUserError(user, owl.ErrorMessage{
|
|
|
|
Error: "Not found",
|
|
|
|
Message: "The page you requested could not be found",
|
|
|
|
})
|
|
|
|
w.Write([]byte(html))
|
|
|
|
}
|
|
|
|
}
|