From 3c7ef74052592ffaea9e4bc209466da8ac92a937 Mon Sep 17 00:00:00 2001
From: Jacob Champion <jchampion@timescale.com>
Date: Tue, 12 Jul 2022 11:49:17 -0700
Subject: [PATCH] latest.json: allow filtering by author email

The ?author= query parameter can be provided to latest.json in order to
return results where the mail was sent by a specific address. This is
intended for use by the Commitfest app, for attachment filtering.

TODO:
- The LIKE query on messages._from is probably going to perform
  terribly, since there's no index.
---
 django/archives/mailarchives/api.py | 10 +++++++++-
 1 file changed, 9 insertions(+), 1 deletion(-)

diff --git a/django/archives/mailarchives/api.py b/django/archives/mailarchives/api.py
index a6b2536..8aa3c2b 100644
--- a/django/archives/mailarchives/api.py
+++ b/django/archives/mailarchives/api.py
@@ -56,6 +56,7 @@ def latest(request, listname):
     if limit <= 0 or limit > 100:
         limit = 50
 
+    author = None
     extrawhere = []
     extraparams = []
 
@@ -64,6 +65,10 @@ def latest(request, listname):
         if request.GET['a'] == '1':
             extrawhere.append("has_attachment")
 
+    # Return only a specific author's attachments?
+    if 'author' in request.GET:
+        author = request.GET['author']
+
     # Restrict by full text search
     if 's' in request.GET and request.GET['s']:
         extrawhere.append("fti @@ plainto_tsquery('public.pg', %s)")
@@ -76,7 +81,10 @@ def latest(request, listname):
         list = None
         extrawhere = ''
 
-    mlist = Message.objects.defer('bodytxt', 'cc', 'to').select_related().extra(where=extrawhere, params=extraparams).order_by('-date')[:limit]
+    mlist = Message.objects.defer('bodytxt', 'cc', 'to').select_related()
+    if author:
+        mlist = mlist.filter(mailfrom__iendswith=f'<{author}>')
+    mlist = mlist.extra(where=extrawhere, params=extraparams).order_by('-date')[:limit]
     allyearmonths = set([(m.date.year, m.date.month) for m in mlist])
 
     resp = HttpResponse(content_type='application/json')
-- 
2.25.1

