Update files, emoji, fan_out

This commit is contained in:
Jamie Bliss 2024-01-12 00:05:30 -05:00
parent d8acdf4005
commit cd160050ac
No known key found for this signature in database
3 changed files with 92 additions and 86 deletions

View file

@ -2,7 +2,6 @@ import mimetypes
from functools import partial
from typing import ClassVar
import httpx
import urlman
from cachetools import TTLCache, cached
from django.conf import settings
@ -12,6 +11,7 @@ from django.db import models
from django.utils.safestring import mark_safe
from PIL import Image
from core import httpy
from core.files import get_remote_file
from core.html import FediverseHtmlParser
from core.ld import format_ld_date
@ -45,7 +45,7 @@ class EmojiStates(StateGraph):
timeout=settings.SETUP.REMOTE_TIMEOUT,
max_size=settings.SETUP.EMOJI_MAX_IMAGE_FILESIZE_KB * 1024,
)
except httpx.RequestError:
except httpy.RequestError:
return
if file:

View file

@ -1,7 +1,7 @@
import httpx
from django.db import models
from activities.models.timeline_event import TimelineEvent
from core import httpy
from core.ld import canonicalise
from stator.models import State, StateField, StateGraph, StatorModel
from users.models import Block, FollowStates
@ -75,33 +75,33 @@ class FanOutStates(StateGraph):
case (FanOut.Types.post, False):
post = instance.subject_post
# Sign it and send it
try:
post.author.signed_request(
method="post",
uri=(
instance.identity.shared_inbox_uri
or instance.identity.inbox_uri
),
body=canonicalise(post.to_create_ap()),
)
except httpx.RequestError:
return
with httpy.Client(actor=post.author) as client:
try:
client.post2(
(
instance.identity.shared_inbox_uri
or instance.identity.inbox_uri
),
activity=canonicalise(post.to_create_ap()),
)
except httpy.RequestError:
return
# Handle sending remote posts update
case (FanOut.Types.post_edited, False):
post = instance.subject_post
# Sign it and send it
try:
post.author.signed_request(
method="post",
uri=(
instance.identity.shared_inbox_uri
or instance.identity.inbox_uri
),
body=canonicalise(post.to_update_ap()),
)
except httpx.RequestError:
return
with httpy.Client(actor=post.author) as client:
try:
client.post2(
(
instance.identity.shared_inbox_uri
or instance.identity.inbox_uri
),
activity=canonicalise(post.to_update_ap()),
)
except httpy.RequestError:
return
# Handle deleting local posts
case (FanOut.Types.post_deleted, True):
@ -117,17 +117,17 @@ class FanOutStates(StateGraph):
case (FanOut.Types.post_deleted, False):
post = instance.subject_post
# Send it to the remote inbox
try:
post.author.signed_request(
method="post",
uri=(
instance.identity.shared_inbox_uri
or instance.identity.inbox_uri
),
body=canonicalise(post.to_delete_ap()),
)
except httpx.RequestError:
return
with httpy.Client(actor=post.author) as client:
try:
client.post2(
(
instance.identity.shared_inbox_uri
or instance.identity.inbox_uri
),
activity=canonicalise(post.to_delete_ap()),
)
except httpy.RequestError:
return
# Handle local boosts/likes
case (FanOut.Types.interaction, True):
@ -164,23 +164,24 @@ class FanOutStates(StateGraph):
case (FanOut.Types.interaction, False):
interaction = instance.subject_post_interaction
# Send it to the remote inbox
try:
with httpy.Client(actor=interaction.identity) as client:
if interaction.type == interaction.Types.vote:
body = interaction.to_create_ap()
elif interaction.type == interaction.Types.pin:
body = interaction.to_add_ap()
else:
body = interaction.to_ap()
interaction.identity.signed_request(
method="post",
uri=(
instance.identity.shared_inbox_uri
or instance.identity.inbox_uri
),
body=canonicalise(body),
)
except httpx.RequestError:
return
try:
client.post2(
(
instance.identity.shared_inbox_uri
or instance.identity.inbox_uri
),
activity=canonicalise(body),
)
except httpy.RequestError:
return
# Handle undoing local boosts/likes
case (FanOut.Types.undo_interaction, True): # noqa:F841
@ -196,51 +197,55 @@ class FanOutStates(StateGraph):
case (FanOut.Types.undo_interaction, False): # noqa:F841
interaction = instance.subject_post_interaction
# Send an undo to the remote inbox
try:
if interaction.type == interaction.Types.pin:
body = interaction.to_remove_ap()
else:
body = interaction.to_undo_ap()
interaction.identity.signed_request(
method="post",
uri=(
instance.identity.shared_inbox_uri
or instance.identity.inbox_uri
),
body=canonicalise(body),
)
except httpx.RequestError:
return
with httpy.Client(actor=interaction.identity) as client:
try:
if interaction.type == interaction.Types.pin:
body = interaction.to_remove_ap()
else:
body = interaction.to_undo_ap()
client.post2(
(
instance.identity.shared_inbox_uri
or instance.identity.inbox_uri
),
activity=canonicalise(body),
)
except httpy.RequestError:
return
# Handle sending identity edited to remote
case (FanOut.Types.identity_edited, False):
identity = instance.subject_identity
try:
identity.signed_request(
method="post",
uri=(
instance.identity.shared_inbox_uri
or instance.identity.inbox_uri
),
body=canonicalise(instance.subject_identity.to_update_ap()),
)
except httpx.RequestError:
return
with httpy.Client(actor=identity) as client:
try:
client.post2(
(
instance.identity.shared_inbox_uri
or instance.identity.inbox_uri
),
activity=canonicalise(
instance.subject_identity.to_update_ap()
),
)
except httpy.RequestError:
return
# Handle sending identity deleted to remote
case (FanOut.Types.identity_deleted, False):
identity = instance.subject_identity
try:
identity.signed_request(
method="post",
uri=(
instance.identity.shared_inbox_uri
or instance.identity.inbox_uri
),
body=canonicalise(instance.subject_identity.to_delete_ap()),
)
except httpx.RequestError:
return
with httpy.Client(actor=identity) as client:
try:
client.post2(
(
instance.identity.shared_inbox_uri
or instance.identity.inbox_uri
),
activity=canonicalise(
instance.subject_identity.to_delete_ap()
),
)
except httpy.RequestError:
return
# Handle sending identity moved to remote
case (FanOut.Types.identity_moved, False):

View file

@ -1,12 +1,13 @@
import io
import blurhash
import httpx
from django.conf import settings
from django.core.files import File
from django.core.files.base import ContentFile
from PIL import Image, ImageOps
from . import httpy
class ImageFile(File):
image: Image
@ -70,7 +71,7 @@ def get_remote_file(
"User-Agent": settings.TAKAHE_USER_AGENT,
}
with httpx.Client(headers=headers) as client:
with httpy.Client(headers=headers) as client:
with client.stream(
"GET", url, timeout=timeout, follow_redirects=True
) as stream: