minor: Notifications sent using ActivityQueueService
This commit is contained in:
parent
f50d423690
commit
fb27549193
@ -1,5 +1,3 @@
|
|||||||
import logging
|
|
||||||
|
|
||||||
import requests
|
import requests
|
||||||
from django.conf import settings
|
from django.conf import settings
|
||||||
from django.contrib.auth import get_user_model
|
from django.contrib.auth import get_user_model
|
||||||
@ -13,10 +11,14 @@ from django.urls import NoReverseMatch, get_resolver
|
|||||||
from django.utils.translation import ugettext_lazy as _
|
from django.utils.translation import ugettext_lazy as _
|
||||||
from djangoldp.fields import LDPUrlField
|
from djangoldp.fields import LDPUrlField
|
||||||
from djangoldp.models import Model
|
from djangoldp.models import Model
|
||||||
from threading import Thread
|
from djangoldp.activities.services import ActivityQueueService, ActivityPubService, activity_sending_finished
|
||||||
from djangoldp_notification.middlewares import MODEL_MODIFICATION_USER_FIELD
|
from djangoldp_notification.middlewares import MODEL_MODIFICATION_USER_FIELD
|
||||||
from djangoldp_notification.permissions import InboxPermissions, SubscriptionsPermissions
|
from djangoldp_notification.permissions import InboxPermissions, SubscriptionsPermissions
|
||||||
from djangoldp_notification.views import LDPNotificationsViewSet
|
from djangoldp_notification.views import LDPNotificationsViewSet
|
||||||
|
import logging
|
||||||
|
|
||||||
|
|
||||||
|
logger = logging.getLogger('djangoldp')
|
||||||
|
|
||||||
|
|
||||||
class Notification(Model):
|
class Notification(Model):
|
||||||
@ -115,7 +117,10 @@ def create_nested_subscribers(sender, instance, created, **kwargs):
|
|||||||
@receiver(post_delete, dispatch_uid="delete_callback_notif")
|
@receiver(post_delete, dispatch_uid="delete_callback_notif")
|
||||||
def send_notification(sender, instance, **kwargs):
|
def send_notification(sender, instance, **kwargs):
|
||||||
if sender != Notification:
|
if sender != Notification:
|
||||||
threads = []
|
# don't send notifications for foreign resources
|
||||||
|
if hasattr(instance, 'urlid') and Model.is_external(instance.urlid):
|
||||||
|
return
|
||||||
|
|
||||||
recipients = []
|
recipients = []
|
||||||
try:
|
try:
|
||||||
url_container = settings.BASE_URL + Model.container_id(instance)
|
url_container = settings.BASE_URL + Model.container_id(instance)
|
||||||
@ -125,31 +130,42 @@ def send_notification(sender, instance, **kwargs):
|
|||||||
|
|
||||||
# dispatch a notification for every Subscription on this resource
|
# dispatch a notification for every Subscription on this resource
|
||||||
for subscription in Subscription.objects.filter(models.Q(object=url_resource) | models.Q(object=url_container)):
|
for subscription in Subscription.objects.filter(models.Q(object=url_resource) | models.Q(object=url_container)):
|
||||||
if not instance.is_backlink and subscription.inbox not in recipients and \
|
if subscription.inbox not in recipients and (not subscription.is_backlink or not kwargs.get("created")):
|
||||||
(not subscription.is_backlink or not kwargs.get("created")):
|
|
||||||
# I may have configured to send the subscription to a foreign key
|
# I may have configured to send the subscription to a foreign key
|
||||||
if subscription.field is not None and len(subscription.field) > 1:
|
if subscription.field is not None and len(subscription.field) > 1:
|
||||||
try:
|
try:
|
||||||
instance = getattr(instance, subscription.field, instance)
|
instance = getattr(instance, subscription.field, instance)
|
||||||
|
|
||||||
|
# don't send notifications for foreign resources
|
||||||
|
if hasattr(instance, 'urlid') and Model.is_external(instance.urlid):
|
||||||
|
continue
|
||||||
|
|
||||||
url_resource = settings.BASE_URL + Model.resource_id(instance)
|
url_resource = settings.BASE_URL + Model.resource_id(instance)
|
||||||
except NoReverseMatch:
|
except NoReverseMatch:
|
||||||
continue
|
continue
|
||||||
except ObjectDoesNotExist:
|
except ObjectDoesNotExist:
|
||||||
continue
|
continue
|
||||||
|
|
||||||
process = Thread(target=send_request, args=[subscription.inbox, url_resource, instance,
|
send_request(subscription.inbox, url_resource, instance, kwargs.get("created", False))
|
||||||
kwargs.get("created", False)])
|
|
||||||
process.start()
|
|
||||||
threads.append(process)
|
|
||||||
recipients.append(subscription.inbox)
|
recipients.append(subscription.inbox)
|
||||||
|
|
||||||
|
|
||||||
|
@receiver(activity_sending_finished, sender=ActivityQueueService)
|
||||||
|
def _handle_prosody_response(sender, response, saved_activity, **kwargs):
|
||||||
|
'''callback function for handling a response from Prosody on a notification'''
|
||||||
|
# if text is defined in the response body then it's an error
|
||||||
|
if saved_activity is not None:
|
||||||
|
response_body = saved_activity.response_to_json()
|
||||||
|
if 'condition' in response_body:
|
||||||
|
logger.error("[DjangoLDP-Notification.models._handle_prosody_response] error in Prosody response " +
|
||||||
|
str(response_body))
|
||||||
|
|
||||||
|
|
||||||
def send_request(target, object_iri, instance, created):
|
def send_request(target, object_iri, instance, created):
|
||||||
unknown = str(_("Auteur inconnu"))
|
unknown = str(_("Auteur inconnu"))
|
||||||
author = getattr(getattr(instance, MODEL_MODIFICATION_USER_FIELD, unknown), "urlid", unknown)
|
author = getattr(getattr(instance, MODEL_MODIFICATION_USER_FIELD, unknown), "urlid", unknown)
|
||||||
request_type = "creation" if created else "update"
|
request_type = "creation" if created else "update"
|
||||||
|
|
||||||
try:
|
|
||||||
# local inbox
|
# local inbox
|
||||||
if target.startswith(settings.SITE_URL):
|
if target.startswith(settings.SITE_URL):
|
||||||
user = Model.resolve_parent(target.replace(settings.SITE_URL, ''))
|
user = Model.resolve_parent(target.replace(settings.SITE_URL, ''))
|
||||||
@ -162,10 +178,7 @@ def send_request(target, object_iri, instance, created):
|
|||||||
"author": author,
|
"author": author,
|
||||||
"type": request_type
|
"type": request_type
|
||||||
}
|
}
|
||||||
requests.post(target, json=json, headers={"Content-Type": "application/ld+json"})
|
ActivityQueueService.send_activity(target, json)
|
||||||
except Exception as e:
|
|
||||||
logging.error('Djangoldp_notifications: Error with request: {}'.format(e))
|
|
||||||
return True
|
|
||||||
|
|
||||||
|
|
||||||
@receiver(post_save, sender=Notification)
|
@receiver(post_save, sender=Notification)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user