2019-02-20 03:18:46 +00:00
|
|
|
import logging
|
|
|
|
from threading import Thread
|
2019-03-06 11:35:54 +00:00
|
|
|
|
2019-03-14 15:40:39 +00:00
|
|
|
import requests
|
|
|
|
from django.conf import settings
|
|
|
|
from django.contrib.admin.models import LogEntry
|
2019-03-06 11:35:54 +00:00
|
|
|
from django.contrib.sessions.models import Session
|
2019-03-18 11:57:57 +00:00
|
|
|
from django.core.mail import send_mail
|
2019-01-09 09:27:54 +00:00
|
|
|
from django.db import models
|
2019-03-14 15:40:39 +00:00
|
|
|
from django.db.models.signals import post_save
|
|
|
|
from django.dispatch import receiver
|
2019-03-06 12:31:52 +00:00
|
|
|
from oidc_provider.models import Token
|
2019-04-30 15:07:20 +00:00
|
|
|
from django.urls import NoReverseMatch
|
2019-03-06 12:31:52 +00:00
|
|
|
|
2019-02-28 01:21:03 +00:00
|
|
|
from djangoldp.fields import LDPUrlField
|
2019-02-20 03:18:46 +00:00
|
|
|
from djangoldp.models import Model
|
2019-01-09 09:27:54 +00:00
|
|
|
|
2019-05-02 14:36:08 +00:00
|
|
|
from django.template import loader
|
2019-08-23 12:56:03 +00:00
|
|
|
from .permissions import InboxPermissions
|
2019-05-02 14:36:08 +00:00
|
|
|
|
2019-03-14 15:40:39 +00:00
|
|
|
|
|
|
|
class Notification(Model):
|
2019-04-30 15:07:20 +00:00
|
|
|
user = models.ForeignKey(settings.AUTH_USER_MODEL, related_name='inbox', on_delete=models.deletion.CASCADE)
|
2019-05-07 09:01:26 +00:00
|
|
|
author = LDPUrlField()
|
2019-03-01 04:20:21 +00:00
|
|
|
object = LDPUrlField()
|
2019-01-09 09:27:54 +00:00
|
|
|
type = models.CharField(max_length=255)
|
|
|
|
summary = models.TextField()
|
|
|
|
date = models.DateTimeField(auto_now_add=True)
|
2019-04-04 10:18:24 +00:00
|
|
|
unread = models.BooleanField(default=True)
|
2019-03-14 15:40:39 +00:00
|
|
|
|
2019-04-25 11:44:00 +00:00
|
|
|
class Meta(Model.Meta):
|
2019-08-07 13:57:57 +00:00
|
|
|
owner_field = 'user'
|
2019-06-17 01:34:38 +00:00
|
|
|
ordering = ['-date']
|
2019-08-23 12:56:03 +00:00
|
|
|
permission_classes = [InboxPermissions]
|
2019-08-01 12:03:00 +00:00
|
|
|
anonymous_perms = ['add']
|
2019-08-07 13:57:57 +00:00
|
|
|
authenticated_perms = ['inherit']
|
|
|
|
owner_perms = ['view', 'change', 'control']
|
2019-01-09 09:27:54 +00:00
|
|
|
|
|
|
|
def __str__(self):
|
|
|
|
return '{}'.format(self.type)
|
2019-01-31 09:44:41 +00:00
|
|
|
|
2019-03-14 15:40:39 +00:00
|
|
|
|
|
|
|
class Subscription(Model):
|
2019-01-31 09:44:41 +00:00
|
|
|
object = models.URLField()
|
|
|
|
inbox = models.URLField()
|
|
|
|
|
|
|
|
def __str__(self):
|
|
|
|
return '{}'.format(self.object)
|
|
|
|
|
2019-03-14 15:40:39 +00:00
|
|
|
|
2019-01-31 09:44:41 +00:00
|
|
|
# --- SUBSCRIPTION SYSTEM ---
|
2019-02-20 03:18:46 +00:00
|
|
|
@receiver(post_save, dispatch_uid="callback_notif")
|
|
|
|
def send_notification(sender, instance, **kwargs):
|
2019-04-24 08:20:11 +00:00
|
|
|
if sender != Notification:
|
2019-02-20 03:18:46 +00:00
|
|
|
threads = []
|
2019-04-24 08:20:11 +00:00
|
|
|
try:
|
|
|
|
urlContainer = settings.BASE_URL + Model.container_id(instance)
|
|
|
|
urlResource = settings.BASE_URL + Model.resource_id(instance)
|
|
|
|
except NoReverseMatch:
|
|
|
|
return
|
|
|
|
|
2019-04-24 07:56:18 +00:00
|
|
|
for subscription in Subscription.objects.filter(models.Q(object=urlResource)|models.Q(object=urlContainer)):
|
|
|
|
process = Thread(target=send_request, args=[subscription.inbox, urlResource])
|
2019-02-20 03:18:46 +00:00
|
|
|
process.start()
|
|
|
|
threads.append(process)
|
2019-03-14 15:40:39 +00:00
|
|
|
|
|
|
|
|
2019-03-06 10:38:58 +00:00
|
|
|
def send_request(target, object_iri):
|
2019-02-20 03:18:46 +00:00
|
|
|
try:
|
2019-03-14 15:40:39 +00:00
|
|
|
req = requests.post(target,
|
|
|
|
json={"@context": "https://cdn.happy-dev.fr/owl/hdcontext.jsonld",
|
|
|
|
"object": object_iri, "type": "update"},
|
|
|
|
headers={"Content-Type": "application/ld+json"})
|
2019-02-20 03:18:46 +00:00
|
|
|
except:
|
|
|
|
logging.error('Djangoldp_notifications: Error with request')
|
2019-03-14 15:40:39 +00:00
|
|
|
return True
|
2019-03-18 11:57:57 +00:00
|
|
|
|
|
|
|
|
|
|
|
@receiver(post_save, sender=Notification)
|
2019-06-11 02:06:44 +00:00
|
|
|
def send_email_on_notification(sender, instance, created, **kwargs):
|
|
|
|
if created and instance.summary and settings.JABBER_DEFAULT_HOST and instance.user.email:
|
2019-10-02 14:08:05 +00:00
|
|
|
try:
|
|
|
|
who = requests.get(instance.author).json()['name'] or 'Unknown Person' # I've no idea how to handle dead links.
|
|
|
|
where = requests.get(instance.object).json()['name'] or 'some unknown place' # So let's get to the unknown :)
|
|
|
|
if(instance.author == instance.object):
|
|
|
|
where = "has sent you a private message"
|
|
|
|
else:
|
|
|
|
where = "mention you on " + where
|
|
|
|
html_message = loader.render_to_string(
|
|
|
|
'email.html',
|
|
|
|
{
|
|
|
|
'on': settings.JABBER_DEFAULT_HOST,
|
|
|
|
'instance': instance,
|
|
|
|
'author': who,
|
|
|
|
'object': where
|
|
|
|
}
|
|
|
|
)
|
|
|
|
send_mail(
|
|
|
|
'Notification on ' + settings.JABBER_DEFAULT_HOST,
|
|
|
|
instance.summary,
|
|
|
|
settings.EMAIL_HOST_USER or "noreply@" + settings.JABBER_DEFAULT_HOST,
|
|
|
|
[instance.user.email],
|
|
|
|
fail_silently=True,
|
|
|
|
html_message=html_message
|
|
|
|
)
|
|
|
|
except:
|
|
|
|
logging.error('Djangoldp_notifications: Can\'t mail the user')
|
2019-05-02 14:36:08 +00:00
|
|
|
else:
|
2019-08-16 13:35:58 +00:00
|
|
|
if created:
|
2019-10-02 14:08:05 +00:00
|
|
|
raise Exception('Djangoldp_notifications: Misconfiguration, missing JABBER_DEFAULT_HOST or no mail for user found')
|