Improve custom emoji support

This commit is contained in:
Thomas Sileo 2022-07-16 07:48:24 +02:00
parent 883f87b6e4
commit 1197f132ce
4 changed files with 29 additions and 19 deletions

View file

@ -76,7 +76,6 @@ _RESIZED_CACHE: MutableMapping[tuple[str, int], tuple[bytes, str, Any]] = LFUCac
# #
# Next: # Next:
# - fix stream (only content from follows + mention, and dedup shares) # - fix stream (only content from follows + mention, and dedup shares)
# - custom emoji in data/
# - allow to undo follow requests # - allow to undo follow requests
# - indieauth tweaks # - indieauth tweaks
# - API for posting notes # - API for posting notes
@ -171,6 +170,11 @@ class CustomMiddleware:
app = FastAPI(docs_url=None, redoc_url=None) app = FastAPI(docs_url=None, redoc_url=None)
app.mount(
"/static/custom_emoji",
StaticFiles(directory="data/custom_emoji"),
name="static_custom_emoji",
)
app.mount("/static", StaticFiles(directory="app/static"), name="static") app.mount("/static", StaticFiles(directory="app/static"), name="static")
app.include_router(admin.router, prefix="/admin") app.include_router(admin.router, prefix="/admin")
app.include_router(admin.unauthenticated_router, prefix="/admin") app.include_router(admin.unauthenticated_router, prefix="/admin")

View file

@ -157,7 +157,7 @@
<div class="actor-box h-card p-author"> <div class="actor-box h-card p-author">
<div class="icon-box"> <div class="icon-box">
<img src="{{ actor.resized_icon_url }}" class="actor-icon u-photo"> <img src="{{ actor.resized_icon_url }}" alt="{{ actor.display_name }}'s avatar" class="actor-icon u-photo">
</div> </div>
<a href="{{ actor.url }}" class="u-url" style=""> <a href="{{ actor.url }}" class="u-url" style="">
<div><strong>{{ actor.display_name | clean_html(actor) | safe }}</strong></div> <div><strong>{{ actor.display_name | clean_html(actor) | safe }}</strong></div>

View file

@ -15,23 +15,27 @@ EMOJIS_BY_NAME: dict[str, "RawObject"] = {}
def _load_emojis(root_dir: Path, base_url: str) -> None: def _load_emojis(root_dir: Path, base_url: str) -> None:
if EMOJIS: if EMOJIS:
return return
for emoji in (root_dir / "app" / "static" / "emoji").iterdir(): for dir_name, path in (
mt = mimetypes.guess_type(emoji.name)[0] (root_dir / "app" / "static" / "emoji", "emoji"),
if mt and mt.startswith("image/"): (root_dir / "data" / "custom_emoji", "custom_emoji"),
name = emoji.name.split(".")[0] ):
ap_emoji: "RawObject" = { for emoji in dir_name.iterdir():
"type": "Emoji", mt = mimetypes.guess_type(emoji.name)[0]
"name": f":{name}:", if mt and mt.startswith("image/"):
"updated": "1970-01-01T00:00:00Z", # XXX: we don't track date name = emoji.name.split(".")[0]
"id": f"{base_url}/e/{name}", ap_emoji: "RawObject" = {
"icon": { "type": "Emoji",
"mediaType": mt, "name": f":{name}:",
"type": "Image", "updated": "1970-01-01T00:00:00Z", # XXX: we don't track date
"url": f"{base_url}/static/emoji/{emoji.name}", "id": f"{base_url}/e/{name}",
}, "icon": {
} "mediaType": mt,
EMOJIS[emoji.name] = ap_emoji "type": "Image",
EMOJIS_BY_NAME[ap_emoji["name"]] = ap_emoji "url": f"{base_url}/static/{path}/{emoji.name}",
},
}
EMOJIS[emoji.name] = ap_emoji
EMOJIS_BY_NAME[ap_emoji["name"]] = ap_emoji
def tags(content: str) -> list["RawObject"]: def tags(content: str) -> list["RawObject"]:

2
data/custom_emoji/.gitignore vendored Normal file
View file

@ -0,0 +1,2 @@
*
!.gitignore