Skip to content

Commit

Permalink
Add same filtering to embed JS
Browse files Browse the repository at this point in the history
  • Loading branch information
le717 committed Jun 8, 2024
1 parent 01cfb9a commit f106cd2
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 5 deletions.
1 change: 1 addition & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ _Released TDB_
- **Breaking change**: `FLASK_ENV=production` is a required environment variable due to Flask changes
- This is automatically handled for you if using the included `docker-compose` file
- Add new `/webring-embed.js` route to automatically generate and embed a simple webring listing
- Add ability to control showing rotted and same-origin results for root entrypoint and simple embed
- Update minimum Python version to 3.11
- Update to Flask v3
- ~~Update SQLAlchemy to v2~~ _update pending_
Expand Down
9 changes: 8 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,18 @@
- Optional linkrot event logging to [Discord](https://discord.com/) channel
- Text error log fallback if disabled

### Auto-embed feature
### Filtering webring items

Starting with version 1.3.0, TODO: write me!

### Auto-embed webring

Starting with version 1.3.0, a JavaScript file is provided to generate and embed a simple rendering
of the webring into your site. It includes the entire ring in the script, preventing any additional
requests to fetch ring entries.

TODO: noscript/no js backup


TODO: write me!

Expand All @@ -25,6 +31,7 @@ also not minified, which could also increase your page load time. If you want or
control over loading and displaying the ring, it is suggested to manually call the ring's root URL
to fetch the entries and display them as you desire.


## Required Secrets

- Flask secret key (`SECRET_KEY`)
Expand Down
20 changes: 16 additions & 4 deletions src/views/embed.py
Original file line number Diff line number Diff line change
@@ -1,19 +1,31 @@
from flask import Response, make_response, render_template, request
from webargs.flaskparser import use_kwargs

from src.core.database import weblink as db
from src.core.models.WebLink import WebLink
from src.core.models.WebLink import WebLink, WebLinkGet

from ..blueprints import route_embed

__all__ = ["embed"]


@route_embed.get("")
def embed() -> Response:
"""Get a small JavaScript file that automatically embeds the webring on your site."""
@use_kwargs(WebLinkGet().fields, location="query")
def embed(**kwargs) -> Response:
"""Get a small JavaScript file that automatically embeds the webring on your site.
Provide the appropriate query string arguments to filter the result set as desired.
"""
# Remove the site making the request from the result set if told to
query_args = {}
if kwargs["exclude_origin"]:
query_args["http_origin"] = request.headers.get("ORIGIN")

# Get all current links in the webring, including dead links, excluding the current site,
# and convert them to plain dictionaries for including in the JavaScript file directly,
# which removes the need for a fetch request on the client
all_links = WebLink(only=["id", "url", "title", "description"]).dump(
db.get_all(include_rotted=True, http_origin=request.headers.get("ORIGIN")),
db.get_all(include_rotted=kwargs["include_rotted"], **query_args),
many=True,
)

Expand Down

0 comments on commit f106cd2

Please sign in to comment.