diff --git a/account.go b/account.go index aead6c5..1557820 100644 --- a/account.go +++ b/account.go @@ -16,6 +16,7 @@ import ( "html/template" "net/http" "regexp" + "strconv" "strings" "sync" "time" @@ -691,6 +692,22 @@ func viewMyPostsAPI(app *App, u *User, w http.ResponseWriter, r *http.Request) e return ErrBadRequestedType } + isAnonPosts := r.FormValue("anonymous") == "1" + if isAnonPosts { + pageStr := r.FormValue("page") + pg, err := strconv.Atoi(pageStr) + if err != nil { + log.Error("Error parsing page parameter '%s': %s", pageStr, err) + pg = 1 + } + + p, err := app.db.GetAnonymousPosts(u, pg) + if err != nil { + return err + } + return impart.WriteSuccess(w, p, http.StatusOK) + } + var err error p := GetPostsCache(u.ID) if p == nil { diff --git a/handle.go b/handle.go index 1b5470f..1424c59 100644 --- a/handle.go +++ b/handle.go @@ -287,6 +287,26 @@ func (h *Handler) UserAPI(f userHandlerFunc) http.HandlerFunc { return h.UserAll(false, f, apiAuth) } +// UserWebAPI handles endpoints that accept a user authorized either via the web (cookies) or an Authorization header. +func (h *Handler) UserWebAPI(f userHandlerFunc) http.HandlerFunc { + return h.UserAll(false, f, func(app *App, r *http.Request) (*User, error) { + // Authorize user via cookies + u := getUserSession(app, r) + if u != nil { + return u, nil + } + + // Fall back to access token, since user isn't logged in via web + var err error + u, err = apiAuth(app, r) + if err != nil { + return nil, err + } + + return u, nil + }) +} + func (h *Handler) UserAll(web bool, f userHandlerFunc, a authFunc) http.HandlerFunc { return func(w http.ResponseWriter, r *http.Request) { handleFunc := func() error { diff --git a/routes.go b/routes.go index b34bd3d..bebe383 100644 --- a/routes.go +++ b/routes.go @@ -109,7 +109,7 @@ func InitRoutes(apper Apper, r *mux.Router) *mux.Router { write.HandleFunc("/api/me", handler.All(viewMeAPI)).Methods("GET") apiMe := write.PathPrefix("/api/me/").Subrouter() apiMe.HandleFunc("/", handler.All(viewMeAPI)).Methods("GET") - apiMe.HandleFunc("/posts", handler.UserAPI(viewMyPostsAPI)).Methods("GET") + apiMe.HandleFunc("/posts", handler.UserWebAPI(viewMyPostsAPI)).Methods("GET") apiMe.HandleFunc("/collections", handler.UserAPI(viewMyCollectionsAPI)).Methods("GET") apiMe.HandleFunc("/password", handler.All(updatePassphrase)).Methods("POST") apiMe.HandleFunc("/self", handler.All(updateSettings)).Methods("POST") diff --git a/static/js/posts.js b/static/js/posts.js index 58b55a2..ef3143d 100644 --- a/static/js/posts.js +++ b/static/js/posts.js @@ -181,7 +181,7 @@ var localPosts = function() { undoDelete: UndoDelete, }; }(); -var createPostEl = function(post) { +var createPostEl = function(post, owned) { var $post = document.createElement('div'); var title = (post.title || post.id); title = title.replace(/ edit' + (hasDraft ? 'ed' : '') + ' delete'; + $post.innerHTML += '

' + posted + ' edit' + (hasDraft ? 'ed' : '') + ' delete

'; if (post.error) { $post.innerHTML += '

Sync error: ' + post.error + '

'; } if (post.summary) { $post.innerHTML += '

' + post.summary.replace(/'; + } else if (post.body) { + var preview; + if (post.body.length > 140) { + preview = post.body.substr(0, 140) + '...'; + } else { + preview = post.body; + } + $post.innerHTML += '

' + preview.replace(/'; } return $post; }; diff --git a/templates/user/articles.tmpl b/templates/user/articles.tmpl index e96d51e..0eccc9e 100644 --- a/templates/user/articles.tmpl +++ b/templates/user/articles.tmpl @@ -1,5 +1,11 @@ {{define "articles"}} {{template "header" .}} +

@@ -15,7 +21,7 @@ {{ if .AnonymousPosts }}

These are your draft posts. You can share them individually (without a blog) or move them to your blog when you're ready.

-
+
{{ range $el := .AnonymousPosts }}

@@ -39,9 +45,12 @@

{{if .Summary}}

{{.SummaryHTML}}

{{end}}
{{end}} -
{{ else }}
+
+{{if eq (len .AnonymousPosts) 10}}

Load more...

{{end}} +{{ else }}

Your anonymous and draft posts will show up here once you've published some. You'll be able to share them individually (without a blog) or move them to a blog when you're ready.

{{if not .SingleUser}}

Alternatively, see your blogs and their posts on your Blogs page.

{{end}} +

Start writing

{{ end }}
@@ -145,6 +154,50 @@ function postsLoaded(n) { syncing = true; }); } + +var $loadMore = H.getEl("load-more-p"); +var curPage = 1; +var isLoadingMore = false; +function loadMorePosts() { + if (isLoadingMore === true) { + return; + } + var $link = this; + isLoadingMore = true; + + $link.className = 'loading'; + $link.textContent = 'Loading posts...'; + + var $posts = H.getEl("anon-posts"); + + curPage++; + + var http = new XMLHttpRequest(); + var url = "/api/me/posts?anonymous=1&page=" + curPage; + http.open("GET", url, true); + http.setRequestHeader("Content-type", "application/json"); + http.onreadystatechange = function() { + if (http.readyState == 4) { + if (http.status == 200) { + var data = JSON.parse(http.responseText); + for (var i=0; i