fix some endpoints
This commit is contained in:
parent
7aee99ac42
commit
6f1b94c040
10 changed files with 47 additions and 41 deletions
|
@ -12,13 +12,7 @@ type Blog struct {
|
|||
Domain *string `json:"domain"`
|
||||
Bio string `json:"bio"`
|
||||
|
||||
Account blogPartialAccount `json:"account"`
|
||||
}
|
||||
|
||||
type blogPartialAccount struct {
|
||||
ID ulid.ULID `json:"id"`
|
||||
Username string `json:"username"`
|
||||
Domain *string `json:"domain"`
|
||||
Account Account `json:"account"`
|
||||
}
|
||||
|
||||
func DBBlogToBlog(b database.Blog, a database.Account) Blog {
|
||||
|
@ -27,10 +21,6 @@ func DBBlogToBlog(b database.Blog, a database.Account) Blog {
|
|||
Name: b.Name,
|
||||
Domain: b.Domain,
|
||||
Bio: b.Bio,
|
||||
Account: blogPartialAccount{
|
||||
ID: a.ID,
|
||||
Username: a.Username,
|
||||
Domain: a.Domain,
|
||||
},
|
||||
Account: DBAccountToAccount(a),
|
||||
}
|
||||
}
|
||||
|
|
|
@ -11,25 +11,15 @@ type Post struct {
|
|||
Source *string `json:"source"`
|
||||
Visibility database.PostVisibility `json:"visibility"`
|
||||
|
||||
Blog postPartialBlog `json:"blog"`
|
||||
Blog Blog `json:"blog"`
|
||||
}
|
||||
|
||||
type postPartialBlog struct {
|
||||
ID ulid.ULID `json:"id"`
|
||||
Name string `json:"name"`
|
||||
Domain *string `json:"domain"`
|
||||
}
|
||||
|
||||
func DBPostToPost(p database.Post, b database.Blog) Post {
|
||||
func DBPostToPost(p database.Post, b database.Blog, a database.Account) Post {
|
||||
return Post{
|
||||
ID: p.ID,
|
||||
Content: p.Content,
|
||||
Source: p.Source,
|
||||
Visibility: p.Visibility,
|
||||
Blog: postPartialBlog{
|
||||
ID: p.BlogID,
|
||||
Name: b.Name,
|
||||
Domain: b.Domain,
|
||||
},
|
||||
Blog: DBBlogToBlog(b, a),
|
||||
}
|
||||
}
|
||||
|
|
|
@ -75,6 +75,12 @@ func (app *App) Create(w http.ResponseWriter, r *http.Request) (api.Post, error)
|
|||
return api.Post{}, err
|
||||
}
|
||||
|
||||
acct, err := app.Account(conn).ByID(ctx, blog.AccountID)
|
||||
if err != nil {
|
||||
log.Err(err).Msg("fetching account")
|
||||
return api.Post{}, err
|
||||
}
|
||||
|
||||
if blog.AccountID != token.UserID {
|
||||
return api.Post{}, api.Error{Code: api.ErrNotYourObject}
|
||||
}
|
||||
|
@ -87,5 +93,5 @@ func (app *App) Create(w http.ResponseWriter, r *http.Request) (api.Post, error)
|
|||
}
|
||||
|
||||
// TODO: federate post + push to websockets
|
||||
return api.DBPostToPost(post, blog), nil
|
||||
return api.DBPostToPost(post, blog, acct), nil
|
||||
}
|
||||
|
|
|
@ -40,5 +40,11 @@ func (app *App) GetID(w http.ResponseWriter, r *http.Request) (api.Post, error)
|
|||
return api.Post{}, err
|
||||
}
|
||||
|
||||
return api.DBPostToPost(post, blog), nil
|
||||
acct, err := app.Account(conn).ByID(ctx, blog.AccountID)
|
||||
if err != nil {
|
||||
log.Err(err).Str("id", blog.AccountID.String()).Msg("fetching account from database")
|
||||
return api.Post{}, err
|
||||
}
|
||||
|
||||
return api.DBPostToPost(post, blog, acct), nil
|
||||
}
|
||||
|
|
|
@ -36,11 +36,12 @@ func (app *App) Home(w http.ResponseWriter, r *http.Request) (timelineResponse,
|
|||
posts, err := app.Timeline().Home(ctx, token.UserID, limit, before, after)
|
||||
if err != nil {
|
||||
log.Err(err).Msg("getting posts from database")
|
||||
return timelineResponse{}, err
|
||||
}
|
||||
|
||||
resp := timelineResponse{Posts: make([]api.Post, len(posts))}
|
||||
for i := range posts {
|
||||
resp.Posts[i] = api.DBPostToPost(posts[i].Post, posts[i].Blog)
|
||||
resp.Posts[i] = api.DBPostToPost(posts[i].Post, posts[i].Blog, posts[i].Account)
|
||||
}
|
||||
|
||||
return resp, nil
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue