add a couple post endpoints + /timelines/home
This commit is contained in:
parent
dd72a1f4c1
commit
9f052dc9ef
24 changed files with 462 additions and 32 deletions
47
web/api/timelines/home_timeline.go
Normal file
47
web/api/timelines/home_timeline.go
Normal file
|
@ -0,0 +1,47 @@
|
|||
package timelines
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
"strconv"
|
||||
|
||||
"git.sleepycat.moe/sam/mercury/web/api"
|
||||
"github.com/oklog/ulid/v2"
|
||||
"github.com/rs/zerolog/log"
|
||||
)
|
||||
|
||||
type timelineResponse struct {
|
||||
Posts []api.Post `json:"posts"`
|
||||
}
|
||||
|
||||
func (app *App) Home(w http.ResponseWriter, r *http.Request) (timelineResponse, error) {
|
||||
ctx := r.Context()
|
||||
token, _ := app.TokenFromContext(ctx)
|
||||
|
||||
var before, after *ulid.ULID
|
||||
if id, err := ulid.Parse(r.FormValue("before")); err == nil {
|
||||
before = &id
|
||||
}
|
||||
if id, err := ulid.Parse(r.FormValue("after")); err == nil {
|
||||
after = &id
|
||||
}
|
||||
|
||||
if before != nil && after != nil {
|
||||
return timelineResponse{}, api.Error{Code: api.ErrBadRequest, Details: "`before` and `after` are mutually exclusive"}
|
||||
}
|
||||
limit, err := strconv.Atoi(r.FormValue("limit"))
|
||||
if err != nil || limit <= 0 || limit > 100 {
|
||||
limit = 100
|
||||
}
|
||||
|
||||
posts, err := app.Timeline().Home(ctx, token.UserID, limit, before, after)
|
||||
if err != nil {
|
||||
log.Err(err).Msg("getting posts from database")
|
||||
}
|
||||
|
||||
resp := timelineResponse{Posts: make([]api.Post, len(posts))}
|
||||
for i := range posts {
|
||||
resp.Posts[i] = api.DBPostToPost(posts[i].Post, posts[i].Blog)
|
||||
}
|
||||
|
||||
return resp, nil
|
||||
}
|
13
web/api/timelines/module.go
Normal file
13
web/api/timelines/module.go
Normal file
|
@ -0,0 +1,13 @@
|
|||
package timelines
|
||||
|
||||
import "git.sleepycat.moe/sam/mercury/web/app"
|
||||
|
||||
type App struct {
|
||||
*app.App
|
||||
}
|
||||
|
||||
func New(app *app.App) *App {
|
||||
return &App{
|
||||
App: app,
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue