simplify embedr API fetches
This commit is contained in:
committed by
Dan Abramov
parent
cf8970832a
commit
6dab394200
@@ -20,20 +20,7 @@ var ErrPostNotPublic = errors.New("post is not publicly accessible")
|
|||||||
|
|
||||||
func (srv *Server) getBlueskyPost(ctx context.Context, did syntax.DID, rkey syntax.RecordKey) (*appbsky.FeedDefs_PostView, error) {
|
func (srv *Server) getBlueskyPost(ctx context.Context, did syntax.DID, rkey syntax.RecordKey) (*appbsky.FeedDefs_PostView, error) {
|
||||||
|
|
||||||
// requires two fetches: first fetch profile
|
// fetch the post post (with extra context)
|
||||||
pv, err := appbsky.ActorGetProfile(ctx, srv.xrpcc, did.String())
|
|
||||||
if err != nil {
|
|
||||||
log.Warnf("failed to fetch profile for: %s\t%v", did, err)
|
|
||||||
// TODO: detect 404, specifically?
|
|
||||||
return nil, ErrPostNotFound
|
|
||||||
}
|
|
||||||
for _, label := range pv.Labels {
|
|
||||||
if label.Src == pv.Did && label.Val == "!no-unauthenticated" {
|
|
||||||
return nil, ErrPostNotPublic
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// then fetch the post thread (with extra context)
|
|
||||||
uri := fmt.Sprintf("at://%s/app.bsky.feed.post/%s", did, rkey)
|
uri := fmt.Sprintf("at://%s/app.bsky.feed.post/%s", did, rkey)
|
||||||
tpv, err := appbsky.FeedGetPostThread(ctx, srv.xrpcc, 1, 0, uri)
|
tpv, err := appbsky.FeedGetPostThread(ctx, srv.xrpcc, 1, 0, uri)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -47,7 +34,14 @@ func (srv *Server) getBlueskyPost(ctx context.Context, did syntax.DID, rkey synt
|
|||||||
} else if tpv.Thread.FeedDefs_ThreadViewPost.Post == nil {
|
} else if tpv.Thread.FeedDefs_ThreadViewPost.Post == nil {
|
||||||
return nil, ErrPostNotFound
|
return nil, ErrPostNotFound
|
||||||
}
|
}
|
||||||
return tpv.Thread.FeedDefs_ThreadViewPost.Post, nil
|
|
||||||
|
postView := tpv.Thread.FeedDefs_ThreadViewPost.Post
|
||||||
|
for _, label := range postView.Author.Labels {
|
||||||
|
if label.Src == postView.Author.Did && label.Val == "!no-unauthenticated" {
|
||||||
|
return nil, ErrPostNotPublic
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return postView, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (srv *Server) WebHome(c echo.Context) error {
|
func (srv *Server) WebHome(c echo.Context) error {
|
||||||
@@ -127,7 +121,7 @@ func (srv *Server) WebOEmbed(c echo.Context) error {
|
|||||||
return c.String(http.StatusNotImplemented, "Unsupported oEmbed format: "+formatParam)
|
return c.String(http.StatusNotImplemented, "Unsupported oEmbed format: "+formatParam)
|
||||||
}
|
}
|
||||||
|
|
||||||
// XXX: do we actually do something with width?
|
// TODO: do we actually do something with width?
|
||||||
width := 550
|
width := 550
|
||||||
maxWidthParam := c.QueryParam("maxwidth")
|
maxWidthParam := c.QueryParam("maxwidth")
|
||||||
if maxWidthParam != "" {
|
if maxWidthParam != "" {
|
||||||
@@ -182,7 +176,6 @@ func (srv *Server) WebOEmbed(c echo.Context) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (srv *Server) WebPostEmbed(c echo.Context) error {
|
func (srv *Server) WebPostEmbed(c echo.Context) error {
|
||||||
ctx := c.Request().Context()
|
|
||||||
|
|
||||||
// sanity check arguments. don't 4xx, just let app handle if not expected format
|
// sanity check arguments. don't 4xx, just let app handle if not expected format
|
||||||
rkeyParam := c.Param("rkey")
|
rkeyParam := c.Param("rkey")
|
||||||
@@ -195,18 +188,20 @@ func (srv *Server) WebPostEmbed(c echo.Context) error {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return c.String(http.StatusBadRequest, fmt.Sprintf("Invalid DID: %v", err))
|
return c.String(http.StatusBadRequest, fmt.Sprintf("Invalid DID: %v", err))
|
||||||
}
|
}
|
||||||
|
_ = rkey
|
||||||
|
_ = did
|
||||||
|
|
||||||
postView, err := srv.getBlueskyPost(ctx, did, rkey)
|
// NOTE: this request was't really necessary; the JS will do the same fetch
|
||||||
if err == ErrPostNotFound {
|
/*
|
||||||
return c.String(http.StatusNotFound, fmt.Sprintf("%v", err))
|
postView, err := srv.getBlueskyPost(ctx, did, rkey)
|
||||||
} else if err == ErrPostNotPublic {
|
if err == ErrPostNotFound {
|
||||||
return c.String(http.StatusForbidden, fmt.Sprintf("%v", err))
|
return c.String(http.StatusNotFound, fmt.Sprintf("%v", err))
|
||||||
} else if err != nil {
|
} else if err == ErrPostNotPublic {
|
||||||
return c.String(http.StatusInternalServerError, fmt.Sprintf("%v", err))
|
return c.String(http.StatusForbidden, fmt.Sprintf("%v", err))
|
||||||
}
|
} else if err != nil {
|
||||||
|
return c.String(http.StatusInternalServerError, fmt.Sprintf("%v", err))
|
||||||
|
}
|
||||||
|
*/
|
||||||
|
|
||||||
data := map[string]interface{}{
|
return c.Render(http.StatusOK, "postEmbed.html", nil)
|
||||||
"post": postView,
|
|
||||||
}
|
|
||||||
return c.Render(http.StatusOK, "postEmbed.html", data)
|
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user