Skip to content

Commit 8e9f99b

Browse files
JelteFmhagander
authored andcommitted
Allow searching by email Message-ID
1 parent 7a80d4e commit 8e9f99b

File tree

1 file changed

+33
-3
lines changed

1 file changed

+33
-3
lines changed

pgcommitfest/commitfest/views.py

Lines changed: 33 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
from email.mime.text import MIMEText
1515
from email.utils import formatdate, make_msgid
1616
import json
17+
import urllib
1718

1819
from pgcommitfest.mailqueue.util import send_mail, send_simple_mail
1920
from pgcommitfest.userprofile.util import UserWrapper
@@ -22,7 +23,7 @@
2223
from .models import MailThread
2324
from .forms import PatchForm, NewPatchForm, CommentForm, CommitFestFilterForm
2425
from .forms import BulkEmailForm
25-
from .ajax import doAttachThread, refresh_single_thread
26+
from .ajax import doAttachThread, refresh_single_thread, _archivesAPI
2627
from .feeds import ActivityFeed
2728

2829

@@ -249,13 +250,42 @@ def commitfest(request, cfid):
249250
'header_activity_link': 'activity/',
250251
})
251252

253+
def patches_by_messageid(messageid):
254+
# First try to find the messageid in our database
255+
patches = Patch.objects.select_related().filter(mailthread__messageid=messageid).order_by('created',).all()
256+
if patches:
257+
return patches
258+
259+
urlsafe_messageid = urllib.parse.quote(messageid)
260+
261+
# If it's not there, try to find it in the archives
262+
try:
263+
thread = _archivesAPI(f'/message-id.json/{urlsafe_messageid}')
264+
except Http404:
265+
return []
266+
267+
if len(thread) == 0:
268+
return []
269+
270+
first_email = min(thread, key=lambda x: x['date'])
271+
272+
return Patch.objects.select_related().filter(mailthread__messageid=first_email['msgid']).order_by('created',).all()
273+
252274

253275
def global_search(request):
254276
if 'searchterm' not in request.GET:
255277
return HttpResponseRedirect('/')
256-
searchterm = request.GET['searchterm']
278+
searchterm = request.GET['searchterm'].strip()
279+
patches = []
280+
281+
if '@' in searchterm:
282+
# This is probably a messageid, so let's try to look up patches related
283+
# to it. Let's first remove any < and > around it though.
284+
cleaned_id = searchterm.removeprefix('<').removesuffix('>')
285+
patches = patches_by_messageid(cleaned_id)
257286

258-
patches = Patch.objects.select_related().filter(name__icontains=searchterm).order_by('created',).all()
287+
if not patches:
288+
patches = Patch.objects.select_related().filter(name__icontains=searchterm).order_by('created',).all()
259289

260290
if len(patches) == 1:
261291
patch = patches[0]

0 commit comments

Comments
 (0)