Implement thread notification receiving
authorMagnus Hagander <[email protected]>
Wed, 21 Feb 2018 17:31:54 +0000 (18:31 +0100)
committerMagnus Hagander <[email protected]>
Wed, 21 Feb 2018 17:45:19 +0000 (18:45 +0100)
This allows the archivs server to ping the CF app to pick up updates to
mailthreads quicker.

pgcommitfest/commitfest/ajax.py
pgcommitfest/commitfest/util.py
pgcommitfest/commitfest/views.py
pgcommitfest/settings.py
pgcommitfest/urls.py
tools/commitfest/update_archive_threads.py

index cffc33d4a7171b4815e3a388034b62e3e6e9f265..b65de8d5774573cad189f3c3f185014e3d79c081 100644 (file)
@@ -79,6 +79,22 @@ def getMessages(request):
        r = _archivesAPI('/message-id.json/%s' % thread.messageid)
        return sorted(r, key=lambda x: x['date'], reverse=True)
 
+def refresh_single_thread(thread):
+       r = sorted(_archivesAPI('/message-id.json/%s' % thread.messageid), key=lambda x: x['date'])
+       if thread.latestmsgid != r[-1]['msgid']:
+               # There is now a newer mail in the thread!
+               thread.latestmsgid = r[-1]['msgid']
+               thread.latestmessage = r[-1]['date']
+               thread.latestauthor = r[-1]['from']
+               thread.latestsubject = r[-1]['subj']
+               thread.save()
+               parse_and_add_attachments(r, thread)
+               # Potentially update the last mail date - if there wasn't already a mail on each patch
+               # from a *different* thread that had an earlier date.
+               for p in thread.patches.filter(lastmail__lt=thread.latestmessage):
+                       p.lastmail = thread.latestmessage
+                       p.save()
+
 @transaction.atomic
 def annotateMessage(request):
        thread = get_object_or_404(MailThread, pk=int(request.POST['t']))
index 7a38c7b044b272ddede7177ee2b4310510df7bf5..84f543d2fa99445a21144a84cab51aa041071040 100644 (file)
@@ -41,3 +41,4 @@ class DiffableModel(object):
                fields = [field.name for field in self._meta.fields]
                fields.extend([field.name for field in self._meta.many_to_many])
                return model_to_dict(self, fields=fields)
+
index 8eadafa52889e42554bba6fda2824466d42aab9b..68bdbef5ecc16589bb075834db5145133885dc12 100644 (file)
@@ -14,14 +14,16 @@ from django.conf import settings
 from datetime import datetime
 from email.mime.text import MIMEText
 from email.utils import formatdate, make_msgid
+import json
 
 from pgcommitfest.mailqueue.util import send_mail, send_simple_mail
 from pgcommitfest.userprofile.util import UserWrapper
 
 from models import CommitFest, Patch, PatchOnCommitFest, PatchHistory, Committer
+from models import MailThread
 from forms import PatchForm, NewPatchForm, CommentForm, CommitFestFilterForm
 from forms import BulkEmailForm
-from ajax import doAttachThread
+from ajax import doAttachThread, refresh_single_thread
 from feeds import ActivityFeed
 
 def home(request):
@@ -662,3 +664,23 @@ def send_email(request, cfid):
                'breadcrumbs': [{'title': cf.title, 'href': '/%s/' % cf.pk},],
                'savebutton': 'Send email',
        })
+
+
+@csrf_exempt
+def thread_notify(request):
+       if request.method != 'POST':
+               return HttpResponseForbidden("Invalid method")
+
+       j = json.loads(request.body)
+       if j['apikey'] != settings.ARCHIVES_APIKEY:
+               return HttpResponseForbidden("Invalid API key")
+
+       for m in j['messageids']:
+               try:
+                       t = MailThread.objects.get(messageid=m)
+                       refresh_single_thread(t)
+               except Exception, e:
+                       # Just ignore it, we'll check again later
+                       pass
+
+       return HttpResponse(status=200)
index 44106a789cd23978bcfaebcac48dce2f954372a2..111d118aa8b46a7d96ebae84ff0886ab9b520d38 100644 (file)
@@ -163,6 +163,7 @@ ARCHIVES_TIMEOUT=10    # Seconds to wait for calls to the archives
 ARCHIVES_SERVER="localhost"
 ARCHIVES_PORT="8001"
 ARCHIVES_HOST="archives.postgresql.org"    # Host: header to send
+ARCHIVES_APIKEY=None
 
 # Email address to pgsql-hackers. Set to something local to test maybe?
 HACKERS_EMAIL="pgsql-hackers-testing@localhost"
index 5ca20a7a4d80f6624060290428a7824d31d54071..eafb99cf9d3b50f3cafbcc6a6f85462b82c4c691 100644 (file)
@@ -31,6 +31,7 @@ urlpatterns = [
     url(r'^(\d+)/reports/authorstats/$', reports.authorstats),
     url(r'^search/$', views.global_search),
     url(r'^ajax/(\w+)/$', ajax.main),
+    url(r'^thread_notify/$', views.thread_notify),
 
     url(r'^selectable/', include('selectable.urls')),
 
index d32ea1cc8b988702d8f21a1261e705ddba931b6e..19dd221fce10a9555f50af83a0ed9f4788d11cdc 100755 (executable)
@@ -19,7 +19,7 @@ django.setup()
 from django.db import connection
 
 from pgcommitfest.commitfest.models import MailThread
-from pgcommitfest.commitfest.ajax import _archivesAPI, parse_and_add_attachments
+from pgcommitfest.commitfest.ajax import refresh_single_thread
 
 if __name__ == "__main__":
        debug = "--debug" in sys.argv
@@ -32,22 +32,7 @@ if __name__ == "__main__":
        logging.debug("Checking for updated mail threads in the archives")
        for thread in MailThread.objects.filter(patches__commitfests__status__in=(1,2,3)).distinct():
                logging.debug("Checking %s in the archives" % thread.messageid)
-               r = sorted(_archivesAPI('/message-id.json/%s' % thread.messageid), key=lambda x: x['date'])
-               if thread.latestmsgid != r[-1]['msgid']:
-                       # There is now a newer mail in the thread!
-                       logging.info("Thread %s updated" % thread.messageid)
-                       thread.latestmsgid = r[-1]['msgid']
-                       thread.latestmessage = r[-1]['date']
-                       thread.latestauthor = r[-1]['from']
-                       thread.latestsubject = r[-1]['subj']
-                       thread.save()
-                       parse_and_add_attachments(r, thread)
-                       # Potentially update the last mail date - if there wasn't already a mail on each patch
-                       # from a *different* thread that had an earlier date.
-                       for p in thread.patches.filter(lastmail__lt=thread.latestmessage):
-                               logging.debug("Last mail time updated for %s" % thread.messageid)
-                               p.lastmail = thread.latestmessage
-                               p.save()
+               refresh_single_thread(thread)
 
        connection.close()
        logging.debug("Done.")