mirror of
https://github.com/jointakahe/takahe.git
synced 2024-11-26 01:01:00 +00:00
A better way of handling URIs between local/remote
This commit is contained in:
parent
35a45f1c55
commit
8ffe4bc145
11 changed files with 69 additions and 33 deletions
|
@ -1,9 +1,9 @@
|
||||||
from functools import partial
|
from functools import partial
|
||||||
|
|
||||||
from django.conf import settings
|
|
||||||
from django.db import models
|
from django.db import models
|
||||||
|
|
||||||
from core.uploads import upload_namer
|
from core.uploads import upload_namer
|
||||||
|
from core.uris import AutoAbsoluteUrl, RelativeAbsoluteUrl
|
||||||
from stator.models import State, StateField, StateGraph, StatorModel
|
from stator.models import State, StateField, StateGraph, StatorModel
|
||||||
|
|
||||||
|
|
||||||
|
@ -72,19 +72,19 @@ class PostAttachment(StatorModel):
|
||||||
"image/webp",
|
"image/webp",
|
||||||
]
|
]
|
||||||
|
|
||||||
def thumbnail_url(self):
|
def thumbnail_url(self) -> RelativeAbsoluteUrl:
|
||||||
if self.thumbnail:
|
if self.thumbnail:
|
||||||
return self.thumbnail.url
|
return RelativeAbsoluteUrl(self.thumbnail.url)
|
||||||
elif self.file:
|
elif self.file:
|
||||||
return self.file.url
|
return RelativeAbsoluteUrl(self.file.url)
|
||||||
else:
|
else:
|
||||||
return f"https://{settings.MAIN_DOMAIN}/proxy/post_attachment/{self.pk}/"
|
return AutoAbsoluteUrl(f"/proxy/post_attachment/{self.pk}/")
|
||||||
|
|
||||||
def full_url(self):
|
def full_url(self):
|
||||||
if self.file:
|
if self.file:
|
||||||
return self.file.url
|
return RelativeAbsoluteUrl(self.file.url)
|
||||||
else:
|
else:
|
||||||
return f"https://{settings.MAIN_DOMAIN}/proxy/post_attachment/{self.pk}/"
|
return AutoAbsoluteUrl(f"/proxy/post_attachment/{self.pk}/")
|
||||||
|
|
||||||
### ActivityPub ###
|
### ActivityPub ###
|
||||||
|
|
||||||
|
@ -105,8 +105,8 @@ class PostAttachment(StatorModel):
|
||||||
return {
|
return {
|
||||||
"id": self.pk,
|
"id": self.pk,
|
||||||
"type": "image" if self.is_image() else "unknown",
|
"type": "image" if self.is_image() else "unknown",
|
||||||
"url": self.full_url(),
|
"url": self.full_url().absolute,
|
||||||
"preview_url": self.thumbnail_url(),
|
"preview_url": self.thumbnail_url().absolute,
|
||||||
"remote_url": None,
|
"remote_url": None,
|
||||||
"meta": {
|
"meta": {
|
||||||
"original": {
|
"original": {
|
||||||
|
|
34
core/uris.py
Normal file
34
core/uris.py
Normal file
|
@ -0,0 +1,34 @@
|
||||||
|
from urllib.parse import urljoin
|
||||||
|
|
||||||
|
from django.conf import settings
|
||||||
|
|
||||||
|
|
||||||
|
class RelativeAbsoluteUrl:
|
||||||
|
"""
|
||||||
|
Represents a URL that can have both "relative" and "absolute" forms
|
||||||
|
for various use either locally or remotely.
|
||||||
|
"""
|
||||||
|
|
||||||
|
absolute: str
|
||||||
|
relative: str
|
||||||
|
|
||||||
|
def __init__(self, absolute: str, relative: str | None = None):
|
||||||
|
if "://" not in absolute:
|
||||||
|
raise ValueError(f"Absolute URL {absolute!r} is not absolute!")
|
||||||
|
self.absolute = absolute
|
||||||
|
self.relative = relative or absolute
|
||||||
|
|
||||||
|
|
||||||
|
class AutoAbsoluteUrl(RelativeAbsoluteUrl):
|
||||||
|
"""
|
||||||
|
Automatically makes the absolute variant by using either settings.MAIN_DOMAIN
|
||||||
|
or a passed identity's URI domain.
|
||||||
|
"""
|
||||||
|
|
||||||
|
def __init__(self, relative: str, identity=None):
|
||||||
|
self.relative = relative
|
||||||
|
if identity:
|
||||||
|
absolute_prefix = f"https://{identity.domain.uri_domain}/"
|
||||||
|
else:
|
||||||
|
absolute_prefix = f"https://{settings.MAIN_DOMAIN}/"
|
||||||
|
self.absolute = urljoin(absolute_prefix, self.relative)
|
|
@ -2,7 +2,7 @@
|
||||||
<div class="post user">
|
<div class="post user">
|
||||||
|
|
||||||
<a href="{{ identity.urls.view }}">
|
<a href="{{ identity.urls.view }}">
|
||||||
<img src="{{ identity.local_icon_url }}" class="icon" alt="Avatar for {{ identity.name_or_handle }}">
|
<img src="{{ identity.local_icon_url.relative }}" class="icon" alt="Avatar for {{ identity.name_or_handle }}">
|
||||||
</a>
|
</a>
|
||||||
|
|
||||||
{% if created %}
|
{% if created %}
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
<div class="uploaded-image">
|
<div class="uploaded-image">
|
||||||
<input type="hidden" name="attachment" value="{{ attachment.pk }}">
|
<input type="hidden" name="attachment" value="{{ attachment.pk }}">
|
||||||
<img src="{{ attachment.thumbnail_url }}">
|
<img src="{{ attachment.thumbnail_url.relative }}">
|
||||||
<p>
|
<p>
|
||||||
{{ attachment.name|default:"(no description)" }}
|
{{ attachment.name|default:"(no description)" }}
|
||||||
</p>
|
</p>
|
||||||
|
|
|
@ -3,7 +3,7 @@
|
||||||
<div class="post mini" data-takahe-id="{{ post.id }}" role="article">
|
<div class="post mini" data-takahe-id="{{ post.id }}" role="article">
|
||||||
|
|
||||||
<a href="{{ post.author.urls.view }}" tabindex="-1">
|
<a href="{{ post.author.urls.view }}" tabindex="-1">
|
||||||
<img src="{{ post.author.local_icon_url }}" class="icon">
|
<img src="{{ post.author.local_icon_url.relative }}" class="icon">
|
||||||
</a>
|
</a>
|
||||||
|
|
||||||
<a href="{{ post.author.urls.view }}" class="handle">
|
<a href="{{ post.author.urls.view }}" class="handle">
|
||||||
|
|
|
@ -3,7 +3,7 @@
|
||||||
<div class="post {% if reply %}reply{% endif %}" data-takahe-id="{{ post.id }}" role="article" tabindex="0">
|
<div class="post {% if reply %}reply{% endif %}" data-takahe-id="{{ post.id }}" role="article" tabindex="0">
|
||||||
|
|
||||||
<a href="{{ post.author.urls.view }}" tabindex="-1">
|
<a href="{{ post.author.urls.view }}" tabindex="-1">
|
||||||
<img src="{{ post.author.local_icon_url }}" class="icon">
|
<img src="{{ post.author.local_icon_url.relative }}" class="icon">
|
||||||
</a>
|
</a>
|
||||||
|
|
||||||
<time _="on click go url {% if link_original %}{{ post.url }}{% else %}{{ post.urls.view }}{% endif %} then halt">
|
<time _="on click go url {% if link_original %}{{ post.url }}{% else %}{{ post.urls.view }}{% endif %} then halt">
|
||||||
|
@ -75,8 +75,8 @@
|
||||||
<div class="attachments">
|
<div class="attachments">
|
||||||
{% for attachment in post.attachments.all %}
|
{% for attachment in post.attachments.all %}
|
||||||
{% if attachment.is_image %}
|
{% if attachment.is_image %}
|
||||||
<a href="{{ attachment.full_url }}" class="image">
|
<a href="{{ attachment.full_url.relative }}" class="image">
|
||||||
<img src="{{ attachment.thumbnail_url }}" title="{{ attachment.name }}" alt="{{ attachment.name }}" />
|
<img src="{{ attachment.thumbnail_url.relative }}" title="{{ attachment.name }}" alt="{{ attachment.name }}" />
|
||||||
</a>
|
</a>
|
||||||
{% endif %}
|
{% endif %}
|
||||||
{% endfor %}
|
{% endfor %}
|
||||||
|
|
|
@ -6,7 +6,7 @@
|
||||||
<section class="icon-menu">
|
<section class="icon-menu">
|
||||||
{% for identity, details in identities %}
|
{% for identity, details in identities %}
|
||||||
<a class="option" href="{{ identity.urls.view }}">
|
<a class="option" href="{{ identity.urls.view }}">
|
||||||
<img src="{{ identity.local_icon_url }}">
|
<img src="{{ identity.local_icon_url.relative }}">
|
||||||
<span class="handle">
|
<span class="handle">
|
||||||
{{ identity.name_or_handle }}
|
{{ identity.name_or_handle }}
|
||||||
<small>@{{ identity.handle }}</small>
|
<small>@{{ identity.handle }}</small>
|
||||||
|
|
|
@ -48,7 +48,7 @@
|
||||||
<img src="{% static "img/unknown-icon-128.png" %}" title="No identity selected">
|
<img src="{% static "img/unknown-icon-128.png" %}" title="No identity selected">
|
||||||
{% else %}
|
{% else %}
|
||||||
{{ request.identity.username }}
|
{{ request.identity.username }}
|
||||||
<img src="{{ request.identity.local_icon_url }}" title="{{ request.identity.handle }}">
|
<img src="{{ request.identity.local_icon_url.relative }}" title="{{ request.identity.handle }}">
|
||||||
{% endif %}
|
{% endif %}
|
||||||
</a>
|
</a>
|
||||||
{% else %}
|
{% else %}
|
||||||
|
|
|
@ -6,7 +6,7 @@
|
||||||
<section class="icon-menu">
|
<section class="icon-menu">
|
||||||
{% for identity in identities %}
|
{% for identity in identities %}
|
||||||
<a class="option" href="{{ identity.urls.activate }}">
|
<a class="option" href="{{ identity.urls.activate }}">
|
||||||
<img src="{{ identity.local_icon_url }}">
|
<img src="{{ identity.local_icon_url.relative }}">
|
||||||
<span class="handle">
|
<span class="handle">
|
||||||
{{ identity.name_or_handle }}
|
{{ identity.name_or_handle }}
|
||||||
<small>@{{ identity.handle }}</small>
|
<small>@{{ identity.handle }}</small>
|
||||||
|
|
|
@ -13,10 +13,10 @@
|
||||||
{% block content %}
|
{% block content %}
|
||||||
<h1 class="identity">
|
<h1 class="identity">
|
||||||
{% if identity.local_image_url %}
|
{% if identity.local_image_url %}
|
||||||
<img src="{{ identity.local_image_url }}" class="banner">
|
<img src="{{ identity.local_image_url.relative }}" class="banner">
|
||||||
{% endif %}
|
{% endif %}
|
||||||
|
|
||||||
<img src="{{ identity.local_icon_url }}" class="icon">
|
<img src="{{ identity.local_icon_url.relative }}" class="icon">
|
||||||
|
|
||||||
{% if request.identity %}
|
{% if request.identity %}
|
||||||
{% if identity == request.identity %}
|
{% if identity == request.identity %}
|
||||||
|
|
|
@ -5,7 +5,6 @@ from urllib.parse import urlparse
|
||||||
import httpx
|
import httpx
|
||||||
import urlman
|
import urlman
|
||||||
from asgiref.sync import async_to_sync, sync_to_async
|
from asgiref.sync import async_to_sync, sync_to_async
|
||||||
from django.conf import settings
|
|
||||||
from django.db import IntegrityError, models
|
from django.db import IntegrityError, models
|
||||||
from django.template.defaultfilters import linebreaks_filter
|
from django.template.defaultfilters import linebreaks_filter
|
||||||
from django.templatetags.static import static
|
from django.templatetags.static import static
|
||||||
|
@ -18,6 +17,7 @@ from core.ld import canonicalise, format_ld_date, get_list, media_type_from_file
|
||||||
from core.models import Config
|
from core.models import Config
|
||||||
from core.signatures import HttpSignature, RsaKeys
|
from core.signatures import HttpSignature, RsaKeys
|
||||||
from core.uploads import upload_namer
|
from core.uploads import upload_namer
|
||||||
|
from core.uris import AutoAbsoluteUrl, RelativeAbsoluteUrl
|
||||||
from stator.models import State, StateField, StateGraph, StatorModel
|
from stator.models import State, StateField, StateGraph, StatorModel
|
||||||
from users.models.domain import Domain
|
from users.models.domain import Domain
|
||||||
from users.models.system_actor import SystemActor
|
from users.models.system_actor import SystemActor
|
||||||
|
@ -147,25 +147,26 @@ class Identity(StatorModel):
|
||||||
else:
|
else:
|
||||||
return self.profile_uri
|
return self.profile_uri
|
||||||
|
|
||||||
def local_icon_url(self):
|
def local_icon_url(self) -> RelativeAbsoluteUrl:
|
||||||
"""
|
"""
|
||||||
Returns an icon for us, with fallbacks to a placeholder
|
Returns an icon for use by us, with fallbacks to a placeholder
|
||||||
"""
|
"""
|
||||||
if self.icon:
|
if self.icon:
|
||||||
return self.icon.url
|
return RelativeAbsoluteUrl(self.icon.url)
|
||||||
elif self.icon_uri:
|
elif self.icon_uri:
|
||||||
return f"https://{settings.MAIN_DOMAIN}/proxy/identity_icon/{self.pk}/"
|
return AutoAbsoluteUrl(f"/proxy/identity_icon/{self.pk}/")
|
||||||
else:
|
else:
|
||||||
return static("img/unknown-icon-128.png")
|
return RelativeAbsoluteUrl(static("img/unknown-icon-128.png"))
|
||||||
|
|
||||||
def local_image_url(self):
|
def local_image_url(self) -> RelativeAbsoluteUrl | None:
|
||||||
"""
|
"""
|
||||||
Returns a background image for us, returning None if there isn't one
|
Returns a background image for us, returning None if there isn't one
|
||||||
"""
|
"""
|
||||||
if self.image:
|
if self.image:
|
||||||
return self.image.url
|
return RelativeAbsoluteUrl(self.image.url)
|
||||||
elif self.image_uri:
|
elif self.image_uri:
|
||||||
return f"https://{settings.MAIN_DOMAIN}/proxy/identity_image/{self.pk}/"
|
return AutoAbsoluteUrl(f"/proxy/identity_image/{self.pk}/")
|
||||||
|
return None
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def safe_summary(self):
|
def safe_summary(self):
|
||||||
|
@ -470,6 +471,7 @@ class Identity(StatorModel):
|
||||||
### Mastodon Client API ###
|
### Mastodon Client API ###
|
||||||
|
|
||||||
def to_mastodon_json(self):
|
def to_mastodon_json(self):
|
||||||
|
header_image = self.local_image_url()
|
||||||
return {
|
return {
|
||||||
"id": self.pk,
|
"id": self.pk,
|
||||||
"username": self.username,
|
"username": self.username,
|
||||||
|
@ -477,10 +479,10 @@ class Identity(StatorModel):
|
||||||
"url": self.absolute_profile_uri(),
|
"url": self.absolute_profile_uri(),
|
||||||
"display_name": self.name,
|
"display_name": self.name,
|
||||||
"note": self.summary or "",
|
"note": self.summary or "",
|
||||||
"avatar": self.local_icon_url(),
|
"avatar": self.local_icon_url().absolute,
|
||||||
"avatar_static": self.local_icon_url(),
|
"avatar_static": self.local_icon_url().absolute,
|
||||||
"header": self.local_image_url() or "",
|
"header": header_image.absolute if header_image else None,
|
||||||
"header_static": self.local_image_url() or "",
|
"header_static": header_image.absolute if header_image else None,
|
||||||
"locked": False,
|
"locked": False,
|
||||||
"fields": (
|
"fields": (
|
||||||
[
|
[
|
||||||
|
|
Loading…
Reference in a new issue