add a couple post endpoints + /timelines/home

This commit is contained in:
sam 2023-09-06 16:32:33 +02:00
parent dd72a1f4c1
commit 9f052dc9ef
Signed by: sam
GPG key ID: B4EF20DDE721CAA1
24 changed files with 462 additions and 32 deletions

View 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
}

View 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,
}
}