47 lines
1.1 KiB
Go
47 lines
1.1 KiB
Go
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
|
|
}
|