Verified Commit 64e88b83 authored by Eliot Berriot's avatar Eliot Berriot
Browse files

Deliver is now a proper celery task

parent 2a4ce0a4
import logging
import json
import requests_http_signature
import uuid
from django.conf import settings
from funkwhale_api.common import session
from funkwhale_api.common import utils as funkwhale_utils
from . import models
from . import serializers
from . import signing
logger = logging.getLogger(__name__)
from . import tasks
ACTIVITY_TYPES = [
'Accept',
......@@ -65,31 +52,16 @@ OBJECT_TYPES = [
def deliver(activity, on_behalf_of, to=[]):
from . import actors
logger.info('Preparing activity delivery to %s', to)
auth = signing.get_auth(
on_behalf_of.private_key, on_behalf_of.private_key_id)
for url in to:
recipient_actor = actors.get_actor(url)
logger.debug('delivering to %s', recipient_actor.inbox_url)
logger.debug('activity content: %s', json.dumps(activity))
response = session.get_session().post(
auth=auth,
json=activity,
url=recipient_actor.inbox_url,
timeout=5,
verify=settings.EXTERNAL_REQUESTS_VERIFY_SSL,
headers={
'Content-Type': 'application/activity+json'
}
)
response.raise_for_status()
logger.debug('Remote answered with %s', response.status_code)
return tasks.send.delay(
activity=activity,
actor_id=on_behalf_of.pk,
to=to
)
def accept_follow(follow):
serializer = serializers.AcceptFollowSerializer(follow)
deliver(
return deliver(
serializer.data,
to=[follow.actor.url],
on_behalf_of=follow.target)
import json
import logging
from django.conf import settings
from requests.exceptions import RequestException
from funkwhale_api.common import session
from funkwhale_api.taskapp import celery
from . import actors
from . import library as lb
from . import models
from . import signing
logger = logging.getLogger(__name__)
@celery.app.task(
name='federation.send',
autoretry_for=[RequestException],
retry_backoff=30,
max_retries=5)
@celery.require_instance(models.Actor, 'actor')
def send(activity, actor, to):
logger.info('Preparing activity delivery to %s', to)
auth = signing.get_auth(
actor.private_key, actor.private_key_id)
for url in to:
recipient_actor = actors.get_actor(url)
logger.debug('delivering to %s', recipient_actor.inbox_url)
logger.debug('activity content: %s', json.dumps(activity))
response = session.get_session().post(
auth=auth,
json=activity,
url=recipient_actor.inbox_url,
timeout=5,
verify=settings.EXTERNAL_REQUESTS_VERIFY_SSL,
headers={
'Content-Type': 'application/activity+json'
}
)
response.raise_for_status()
logger.debug('Remote answered with %s', response.status_code)
@celery.app.task(
......
......@@ -4,12 +4,13 @@ from funkwhale_api.federation import activity
from funkwhale_api.federation import serializers
def test_deliver(nodb_factories, r_mock, mocker):
to = nodb_factories['federation.Actor']()
def test_deliver(factories, r_mock, mocker, settings):
settings.CELERY_TASK_ALWAYS_EAGER = True
to = factories['federation.Actor']()
mocker.patch(
'funkwhale_api.federation.actors.get_actor',
return_value=to)
sender = nodb_factories['federation.Actor']()
sender = factories['federation.Actor']()
ac = {
'id': 'http://test.federation/activity',
'type': 'Create',
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment