Skip to content

Commit

Permalink
Rewrite Micropub endpoint using indielib
Browse files Browse the repository at this point in the history
  • Loading branch information
jlelse committed Dec 19, 2023
1 parent 50bbf41 commit 869d0c8
Show file tree
Hide file tree
Showing 11 changed files with 779 additions and 1,195 deletions.
2 changes: 2 additions & 0 deletions app.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,8 @@ type goBlog struct {
// Microformats
mfInit sync.Once
mfCache *ristretto.Cache
// Micropub
mpImpl *micropubImplementation
// Minify
min minify.Minifier
// Plugins
Expand Down
28 changes: 17 additions & 11 deletions editor.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
"go.goblog.app/app/pkgs/bufferpool"
"go.goblog.app/app/pkgs/contenttype"
"go.goblog.app/app/pkgs/htmlbuilder"
"go.hacdias.com/indielib/micropub"
"gopkg.in/yaml.v3"
ws "nhooyr.io/websocket"
)
Expand Down Expand Up @@ -97,26 +98,30 @@ func (a *goBlog) serveEditorPost(w http.ResponseWriter, r *http.Request) {
Data: &editorRenderData{
presetParams: parsePresetPostParamsFromQuery(r),
updatePostUrl: a.fullPostURL(post),
updatePostContent: a.postToMfItem(post).Properties.Content[0],
updatePostContent: post.contentWithParams(),
},
})
case "createpost", "updatepost":
reqBody := map[string]any{}
if action == "updatepost" {
reqBody["action"] = actionUpdate
reqBody["action"] = micropub.ActionUpdate
reqBody["url"] = r.FormValue("url")
reqBody["replace"] = map[string][]string{"content": {r.FormValue("content")}}
} else {
blog, _ := a.getBlog(r)
reqBody["type"] = []string{"h-entry"}
reqBody["properties"] = map[string][]string{"content": {r.FormValue("content")}}
reqBody["properties"] = map[string][]string{"content": {r.FormValue("content")}, "blog": {blog}}
}
req, _ := requests.URL("").BodyJSON(reqBody).Request(r.Context())
a.editorMicropubPost(w, req, false)
a.editorMicropubPost(w, req, false, "")
case "upload":
a.editorMicropubPost(w, r, true)
a.editorMicropubPost(w, r, true, "")
case "delete", "undelete":
req, _ := requests.URL("").Method(http.MethodPost).ContentType(contenttype.WWWForm).Param("action", action).Param("url", r.FormValue("url")).Request(r.Context())
a.editorMicropubPost(w, req, false)
req, _ := requests.URL("").
Method(http.MethodPost).
BodyForm(url.Values{"action": {action}, "url": {r.FormValue("url")}}).
Request(r.Context())
a.editorMicropubPost(w, req, false, r.FormValue("url"))
case "tts":
parsedURL, err := url.Parse(r.FormValue("url"))
if err != nil {
Expand Down Expand Up @@ -153,20 +158,21 @@ func (a *goBlog) serveEditorPost(w http.ResponseWriter, r *http.Request) {
}
}

func (a *goBlog) editorMicropubPost(w http.ResponseWriter, r *http.Request, media bool) {
func (a *goBlog) editorMicropubPost(w http.ResponseWriter, r *http.Request, media bool, redirectSuccess string) {
recorder := httptest.NewRecorder()
if media {
addAllScopes(http.HandlerFunc(a.serveMicropubMedia)).ServeHTTP(recorder, r)
addAllScopes(a.getMicropubImplementation().getMediaHandler()).ServeHTTP(recorder, r)
} else {
addAllScopes(http.HandlerFunc(a.serveMicropubPost)).ServeHTTP(recorder, r)
addAllScopes(a.getMicropubImplementation().getHandler()).ServeHTTP(recorder, r)
}
result := recorder.Result()
if location := result.Header.Get("Location"); location != "" {
http.Redirect(w, r, location, http.StatusFound)
return
}
if result.StatusCode >= 200 && result.StatusCode < 400 {
http.Redirect(w, r, editorPath, http.StatusFound)
redirectPath := defaultIfEmpty(redirectSuccess, editorPath)
http.Redirect(w, r, redirectPath, http.StatusFound)
return
}
w.WriteHeader(result.StatusCode)
Expand Down
48 changes: 24 additions & 24 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ require (
git.jlel.se/jlelse/goldmark-mark v0.0.0-20210522162520-9788c89266a4
git.jlel.se/jlelse/template-strings v0.0.0-20220211095702-c012e3b5045b
github.com/PuerkitoBio/goquery v1.8.1
github.com/alecthomas/chroma/v2 v2.10.0
github.com/alecthomas/chroma/v2 v2.12.0
github.com/araddon/dateparse v0.0.0-20210429162001-6b43995a97de
github.com/c2h5oh/datasize v0.0.0-20220606134207-859f65c6625b
github.com/c2h5oh/datasize v0.0.0-20231215233829-aa82cc1e6500
github.com/carlmjohnson/requests v0.23.5
// master
github.com/cretz/bine v0.2.1-0.20221201125941-b9d31d9c7866
Expand All @@ -20,52 +20,52 @@ require (
github.com/dmulholl/mp3lib v1.0.0
github.com/elnormous/contenttype v1.0.4
github.com/emersion/go-smtp v0.19.0
github.com/go-ap/activitypub v0.0.0-20231105151936-af32623a589b
github.com/go-ap/client v0.0.0-20231105152939-03833203c71e
github.com/go-ap/activitypub v0.0.0-20231114162308-e219254dc5c9
github.com/go-ap/client v0.0.0-20231114162455-f09cf9766e95
github.com/go-ap/jsonld v0.0.0-20221030091449-f2a191312c73
github.com/go-chi/chi/v5 v5.0.10
github.com/go-fed/httpsig v1.1.0
github.com/go-telegram-bot-api/telegram-bot-api/v5 v5.5.1
github.com/google/uuid v1.4.0
github.com/google/uuid v1.5.0
github.com/gorilla/handlers v1.5.2
github.com/gorilla/sessions v1.2.2
github.com/gorilla/websocket v1.5.1
github.com/jlaffaye/ftp v0.2.0
github.com/jlelse/feeds v1.3.0
github.com/justinas/alice v1.2.0
github.com/kaorimatz/go-opml v0.0.0-20210201121027-bc8e2852d7f9
github.com/klauspost/compress v1.17.2
github.com/klauspost/compress v1.17.4
github.com/lestrrat-go/file-rotatelogs v2.4.0+incompatible
github.com/lopezator/migrator v0.3.1
github.com/mattn/go-sqlite3 v1.14.18
github.com/mattn/go-sqlite3 v1.14.19
github.com/mergestat/timediff v0.0.3
github.com/microcosm-cc/bluemonday v1.0.26
github.com/mmcdole/gofeed v1.2.1
github.com/paulmach/go.geojson v1.5.0
github.com/posener/wstest v1.2.0
github.com/pquerna/otp v1.4.0
github.com/samber/lo v1.38.1
github.com/samber/lo v1.39.0
github.com/schollz/sqlite3dump v1.3.1
github.com/snabb/sitemap v1.0.4
github.com/sourcegraph/conc v0.3.0
github.com/spf13/cast v1.5.1
github.com/spf13/viper v1.17.0
github.com/spf13/cast v1.6.0
github.com/spf13/viper v1.18.2
github.com/stretchr/testify v1.8.4
github.com/tdewolff/minify/v2 v2.20.6
github.com/tdewolff/minify/v2 v2.20.10
github.com/tkrajina/gpxgo v1.3.1
github.com/tomnomnom/linkheader v0.0.0-20180905144013-02ca5825eb80
github.com/traefik/yaegi v0.15.1
github.com/vcraescu/go-paginator/v2 v2.0.0
github.com/xhit/go-simple-mail/v2 v2.16.0
github.com/yuin/goldmark v1.6.0
github.com/yuin/goldmark-emoji v1.0.2
go.hacdias.com/indielib v0.1.0
golang.org/x/crypto v0.15.0
golang.org/x/net v0.18.0
go.hacdias.com/indielib v0.2.2
golang.org/x/crypto v0.17.0
golang.org/x/net v0.19.0
golang.org/x/sync v0.5.0
golang.org/x/text v0.14.0
gopkg.in/yaml.v3 v3.0.1
maunium.net/go/mautrix v0.16.1
maunium.net/go/mautrix v0.16.2
nhooyr.io/websocket v1.8.10
willnorris.com/go/microformats v1.2.0
)
Expand All @@ -84,7 +84,7 @@ require (
github.com/felixge/httpsnoop v1.0.4 // indirect
github.com/fsnotify/fsnotify v1.7.0 // indirect
github.com/go-ap/errors v0.0.0-20231003111023-183eef4b31b7 // indirect
github.com/golang/glog v1.1.2 // indirect
github.com/golang/glog v1.2.0 // indirect
github.com/golang/protobuf v1.5.3 // indirect
github.com/gorilla/css v1.0.1 // indirect
github.com/gorilla/securecookie v1.1.2 // indirect
Expand All @@ -101,30 +101,30 @@ require (
github.com/mmcdole/goxpp v1.1.0 // indirect
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
github.com/modern-go/reflect2 v1.0.2 // indirect
github.com/pelletier/go-toml/v2 v2.1.0 // indirect
github.com/pelletier/go-toml/v2 v2.1.1 // indirect
github.com/pkg/errors v0.9.1 // indirect
github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2 // indirect
github.com/rs/xid v1.5.0 // indirect
github.com/rs/zerolog v1.31.0 // indirect
github.com/sagikazarmark/locafero v0.3.0 // indirect
github.com/sagikazarmark/locafero v0.4.0 // indirect
github.com/sagikazarmark/slog-shim v0.1.0 // indirect
github.com/snabb/diagio v1.0.4 // indirect
github.com/spf13/afero v1.10.0 // indirect
github.com/spf13/afero v1.11.0 // indirect
github.com/spf13/pflag v1.0.5 // indirect
github.com/subosito/gotenv v1.6.0 // indirect
github.com/tdewolff/parse/v2 v2.7.4 // indirect
github.com/tdewolff/parse/v2 v2.7.7 // indirect
github.com/tidwall/gjson v1.17.0 // indirect
github.com/tidwall/match v1.1.1 // indirect
github.com/tidwall/pretty v1.2.1 // indirect
github.com/tidwall/sjson v1.2.5 // indirect
github.com/toorop/go-dkim v0.0.0-20201103131630-e1cd1a0a5208 // indirect
github.com/valyala/fastjson v1.6.4 // indirect
go.mau.fi/util v0.2.0 // indirect
go.mau.fi/util v0.2.1 // indirect
go.uber.org/multierr v1.11.0 // indirect
golang.org/x/exp v0.0.0-20231110203233-9a3e6036ecaa // indirect
golang.org/x/exp v0.0.0-20231214170342-aacd6d4b4611 // indirect
golang.org/x/image v0.14.0 // indirect
golang.org/x/oauth2 v0.14.0 // indirect
golang.org/x/sys v0.14.0 // indirect
golang.org/x/oauth2 v0.15.0 // indirect
golang.org/x/sys v0.15.0 // indirect
google.golang.org/appengine v1.6.8 // indirect
google.golang.org/protobuf v1.31.0 // indirect
gopkg.in/ini.v1 v1.67.0 // indirect
Expand Down
Loading

0 comments on commit 869d0c8

Please sign in to comment.