summaryrefslogtreecommitdiffstats
path: root/users
diff options
context:
space:
mode:
Diffstat (limited to 'users')
-rw-r--r--users/middleware.py22
-rw-r--r--users/models/identity.py45
2 files changed, 56 insertions, 11 deletions
diff --git a/users/middleware.py b/users/middleware.py
index e6d4036..9e7f50d 100644
--- a/users/middleware.py
+++ b/users/middleware.py
@@ -13,15 +13,21 @@ class IdentityMiddleware:
self.get_response = get_response
def __call__(self, request):
- identity_id = request.session.get("identity_id")
- if not identity_id:
- request.identity = None
- else:
- try:
- request.identity = Identity.objects.get(id=identity_id)
- User.objects.filter(pk=request.user.pk).update(last_seen=timezone.now())
- except Identity.DoesNotExist:
+ # The API middleware might have set identity already
+ if not hasattr(request, "identity"):
+ # See if we have one in the session
+ identity_id = request.session.get("identity_id")
+ if not identity_id:
request.identity = None
+ else:
+ # Pull it out of the DB and assign it
+ try:
+ request.identity = Identity.objects.get(id=identity_id)
+ User.objects.filter(pk=request.user.pk).update(
+ last_seen=timezone.now()
+ )
+ except Identity.DoesNotExist:
+ request.identity = None
response = self.get_response(request)
return response
diff --git a/users/models/identity.py b/users/models/identity.py
index fe85d41..a8937c9 100644
--- a/users/models/identity.py
+++ b/users/models/identity.py
@@ -5,6 +5,7 @@ from urllib.parse import urlparse
import httpx
import urlman
from asgiref.sync import async_to_sync, sync_to_async
+from django.conf import settings
from django.db import IntegrityError, models
from django.template.defaultfilters import linebreaks_filter
from django.templatetags.static import static
@@ -13,7 +14,7 @@ from django.utils.functional import lazy
from core.exceptions import ActorMismatchError
from core.html import sanitize_post, strip_html
-from core.ld import canonicalise, get_list, media_type_from_filename
+from core.ld import canonicalise, format_ld_date, get_list, media_type_from_filename
from core.models import Config
from core.signatures import HttpSignature, RsaKeys
from core.uploads import upload_namer
@@ -153,7 +154,7 @@ class Identity(StatorModel):
if self.icon:
return self.icon.url
elif self.icon_uri:
- return f"/proxy/identity_icon/{self.pk}/"
+ return f"https://{settings.MAIN_DOMAIN}/proxy/identity_icon/{self.pk}/"
else:
return static("img/unknown-icon-128.png")
@@ -164,7 +165,7 @@ class Identity(StatorModel):
if self.image:
return self.image.url
elif self.image_uri:
- return f"/proxy/identity_image/{self.pk}/"
+ return f"https://{settings.MAIN_DOMAIN}/proxy/identity_image/{self.pk}/"
@property
def safe_summary(self):
@@ -466,6 +467,44 @@ class Identity(StatorModel):
await sync_to_async(self.save)()
return True
+ ### Mastodon Client API ###
+
+ def to_mastodon_json(self):
+ return {
+ "id": self.pk,
+ "username": self.username,
+ "acct": self.username if self.local else self.handle,
+ "url": self.absolute_profile_uri(),
+ "display_name": self.name,
+ "note": self.summary or "",
+ "avatar": self.local_icon_url(),
+ "avatar_static": self.local_icon_url(),
+ "header": self.local_image_url() or "",
+ "header_static": self.local_image_url() or "",
+ "locked": False,
+ "fields": (
+ [
+ {"name": m["name"], "value": m["value"], "verified_at": None}
+ for m in self.metadata
+ ]
+ if self.metadata
+ else []
+ ),
+ "emojis": [],
+ "bot": False,
+ "group": False,
+ "discoverable": self.discoverable,
+ "suspended": False,
+ "limited": False,
+ "created_at": format_ld_date(
+ self.created.replace(hour=0, minute=0, second=0, microsecond=0)
+ ),
+ "last_status_at": None, # TODO: populate
+ "statuses_count": self.posts.count(),
+ "followers_count": self.inbound_follows.count(),
+ "following_count": self.outbound_follows.count(),
+ }
+
### Cryptography ###
async def signed_request(