Deliver is now a proper celery task
This commit is contained in:
parent
2a4ce0a48c
commit
64e88b83f7
|
@ -1,18 +1,5 @@
|
||||||
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 serializers
|
||||||
from . import signing
|
from . import tasks
|
||||||
|
|
||||||
logger = logging.getLogger(__name__)
|
|
||||||
|
|
||||||
ACTIVITY_TYPES = [
|
ACTIVITY_TYPES = [
|
||||||
'Accept',
|
'Accept',
|
||||||
|
@ -65,31 +52,16 @@ OBJECT_TYPES = [
|
||||||
|
|
||||||
|
|
||||||
def deliver(activity, on_behalf_of, to=[]):
|
def deliver(activity, on_behalf_of, to=[]):
|
||||||
from . import actors
|
return tasks.send.delay(
|
||||||
logger.info('Preparing activity delivery to %s', to)
|
activity=activity,
|
||||||
auth = signing.get_auth(
|
actor_id=on_behalf_of.pk,
|
||||||
on_behalf_of.private_key, on_behalf_of.private_key_id)
|
to=to
|
||||||
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)
|
|
||||||
|
|
||||||
|
|
||||||
def accept_follow(follow):
|
def accept_follow(follow):
|
||||||
serializer = serializers.AcceptFollowSerializer(follow)
|
serializer = serializers.AcceptFollowSerializer(follow)
|
||||||
deliver(
|
return deliver(
|
||||||
serializer.data,
|
serializer.data,
|
||||||
to=[follow.actor.url],
|
to=[follow.actor.url],
|
||||||
on_behalf_of=follow.target)
|
on_behalf_of=follow.target)
|
||||||
|
|
|
@ -1,9 +1,48 @@
|
||||||
|
import json
|
||||||
|
import logging
|
||||||
|
|
||||||
|
from django.conf import settings
|
||||||
|
|
||||||
from requests.exceptions import RequestException
|
from requests.exceptions import RequestException
|
||||||
|
|
||||||
|
from funkwhale_api.common import session
|
||||||
from funkwhale_api.taskapp import celery
|
from funkwhale_api.taskapp import celery
|
||||||
|
|
||||||
|
from . import actors
|
||||||
from . import library as lb
|
from . import library as lb
|
||||||
from . import models
|
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(
|
@celery.app.task(
|
||||||
|
|
|
@ -4,12 +4,13 @@ from funkwhale_api.federation import activity
|
||||||
from funkwhale_api.federation import serializers
|
from funkwhale_api.federation import serializers
|
||||||
|
|
||||||
|
|
||||||
def test_deliver(nodb_factories, r_mock, mocker):
|
def test_deliver(factories, r_mock, mocker, settings):
|
||||||
to = nodb_factories['federation.Actor']()
|
settings.CELERY_TASK_ALWAYS_EAGER = True
|
||||||
|
to = factories['federation.Actor']()
|
||||||
mocker.patch(
|
mocker.patch(
|
||||||
'funkwhale_api.federation.actors.get_actor',
|
'funkwhale_api.federation.actors.get_actor',
|
||||||
return_value=to)
|
return_value=to)
|
||||||
sender = nodb_factories['federation.Actor']()
|
sender = factories['federation.Actor']()
|
||||||
ac = {
|
ac = {
|
||||||
'id': 'http://test.federation/activity',
|
'id': 'http://test.federation/activity',
|
||||||
'type': 'Create',
|
'type': 'Create',
|
||||||
|
|
Loading…
Reference in New Issue