Skip to content

Commit

Permalink
fix embedr go:embed
Browse files Browse the repository at this point in the history
  • Loading branch information
bnewbold committed Apr 12, 2024
1 parent 2861ec7 commit cfaf5cc
Show file tree
Hide file tree
Showing 17 changed files with 69 additions and 11 deletions.
1 change: 1 addition & 0 deletions bskyweb/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ test-coverage.out

# Don't check in the binary.
/bskyweb
/embedr

# Don't accidentally commit JS-generated code
static/js/*.js
Expand Down
5 changes: 5 additions & 0 deletions bskyweb/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ help: ## Print info about all commands
.PHONY: build
build: ## Build all executables
go build ./cmd/bskyweb
go build ./cmd/embedr

.PHONY: test
test: ## Run all tests
Expand Down Expand Up @@ -43,3 +44,7 @@ check: ## Compile everything, checking syntax (does not output binaries)
.PHONY: run-dev-bskyweb
run-dev-bskyweb: .env ## Runs 'bskyweb' for local dev
GOLOG_LOG_LEVEL=info go run ./cmd/bskyweb serve

.PHONY: run-dev-embedr
run-dev-embedr: .env ## Runs 'embedr' for local dev
GOLOG_LOG_LEVEL=info go run ./cmd/embedr serve
52 changes: 52 additions & 0 deletions bskyweb/README.embed.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@

## oEmbed

<https://oembed.com/>

* URL scheme: `https://bsky.app/profile/*/post/*`
* API endpoint: `https://embed.bsky.app/oembed`

Request params:

- `url` (required): support both AT-URI and bsky.app URL
- `maxwidth` (optional): [220..550], 325 is default
- `maxheight` (not supported!)
- `format` (optional): only `json` supported

Response format:

- `type` (required): "rich"
- `version` (required): "1.0"
- `author_name` (optional): display name
- `author_url` (optional): profile URL
- `provider_name` (optional): "Bluesky Social"
- `provider_url` (optional): "https://bsky.app"
- `cache_age` (optional, integer seconds): 86400 (24 hours) (?)
- `width` (required): ?
- `height` (required): ?

Not used:

- title (optional): A text title, describing the resource.
- thumbnail_url (optional): A URL to a thumbnail image representing the resource. The thumbnail must respect any maxwidth and maxheight parameters. If this parameter is present, thumbnail_width and thumbnail_height must also be present.
- thumbnail_width (optional): The width of the optional thumbnail. If this parameter is present, thumbnail_url and thumbnail_height must also be present.
- thumbnail_height (optional): The height of the optional thumbnail. If this parameter is present, thumbnail_url and thumbnail_width must also be present.

Only `json` is supported; `xml` is a 501.

```
<link rel="alternate" type="application/json+oembed" href="https://embed.bsky.app/oembed?format=json&url=https://bsky.app/profile/bnewbold.net/post/abc123" />
```


## iframe URL

`https://embed.bsky.app/embed/<did>/app.bsky.feed.post/<rkey>`
`https://embed.bsky.app/static/embed.js`

```
<blockquote class="bluesky-post" data-lang="en" data-align="center">
<p lang="en" dir="ltr">{{ post-text }}</p>
&mdash; US Department of the Interior (@Interior) <a href="https://twitter.com/Interior/status/463440424141459456?ref_src=twsrc%5Etfw">May 5, 2014</a>
</blockquote>
```
19 changes: 8 additions & 11 deletions bskyweb/cmd/embedr/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ func serve(cctx *cli.Context) error {
e.HideBanner = true

tmpl := &Template{
templates: template.Must(template.ParseGlob("embed-templates/*.html")),
templates: template.Must(template.ParseFS(bskyweb.EmbedrTemplateFS, "embedr-templates/*.html")),
}
e.Renderer = tmpl
e.HTTPErrorHandler = server.errorHandler
Expand All @@ -104,7 +104,7 @@ func serve(cctx *cli.Context) error {
e.Use(middleware.LoggerWithConfig(middleware.LoggerConfig{
// Don't log requests for static content.
Skipper: func(c echo.Context) bool {
return strings.HasPrefix(c.Request().URL.Path, "/embed-static")
return strings.HasPrefix(c.Request().URL.Path, "/embedr-static")
},
}))
e.Use(middleware.RateLimiterWithConfig(middleware.RateLimiterConfig{
Expand Down Expand Up @@ -174,7 +174,7 @@ func serve(cctx *cli.Context) error {
e.GET("/", server.WebHome)
e.GET("/embed.js", echo.WrapHandler(staticHandler))
e.GET("/oembed", server.WebOEmbed)
e.GET("/embed/did/app.bsky.feed.post/rkey", server.WebPostEmbed)
e.GET("/embed/:did/app.bsky.feed.post/:rkey", server.WebPostEmbed)

// Start the server.
log.Infof("starting server address=%s", httpAddress)
Expand Down Expand Up @@ -253,18 +253,16 @@ func (srv *Server) WebPostEmbed(c echo.Context) error {
if err != nil {
return c.Render(http.StatusOK, "postEmbed.html", data)
}
handleOrDIDParam := c.Param("handleOrDID")
handleOrDID, err := syntax.ParseAtIdentifier(handleOrDIDParam)
didParam := c.Param("did")
did, err := syntax.ParseDID(didParam)
if err != nil {
return c.Render(http.StatusOK, "postEmbed.html", data)
}

identifier := handleOrDID.Normalize().String()

// requires two fetches: first fetch profile (!)
pv, err := appbsky.ActorGetProfile(ctx, srv.xrpcc, identifier)
pv, err := appbsky.ActorGetProfile(ctx, srv.xrpcc, did.String())
if err != nil {
log.Warnf("failed to fetch profile for: %s\t%v", identifier, err)
log.Warnf("failed to fetch profile for: %s\t%v", did, err)
return c.Render(http.StatusOK, "postEmbed.html", data)
}
unauthedViewingOkay := true
Expand All @@ -277,8 +275,7 @@ func (srv *Server) WebPostEmbed(c echo.Context) error {
if !unauthedViewingOkay {
return c.Render(http.StatusOK, "postEmbed.html", data)
}
did := pv.Did
data["did"] = did
data["did"] = did.String()

// then fetch the post thread (with extra context)
uri := fmt.Sprintf("at://%s/app.bsky.feed.post/%s", did, rkey)
Expand Down
File renamed without changes.
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
3 changes: 3 additions & 0 deletions bskyweb/templates.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,6 @@ import "embed"

//go:embed templates/*
var TemplateFS embed.FS

//go:embed embedr-templates/*
var EmbedrTemplateFS embed.FS

0 comments on commit cfaf5cc

Please sign in to comment.