From 396f72c8fa035d5e29b653ea8f7650189d2461c6 Mon Sep 17 00:00:00 2001
From: Jacob Champion <jchampion@timescale.com>
Date: Tue, 12 Jul 2022 11:53:55 -0700
Subject: [PATCH] Allow users to search for their own attachments only

Add a checkbox to the attach-thread dialog which restricts displayed
attachments to only those authored by the user. This defaults to on,
but the browser's autocomplete should keep the last setting. A popover
describing the functionality has been added as well.

Restrictions:
- This only searches for the single "sender email" registered in the CF
  app.

This patch requires ?author= support in pgarchives; see accompanying
patch.
---
 pgcommitfest/commitfest/ajax.py                            | 7 +++++++
 pgcommitfest/commitfest/static/commitfest/js/commitfest.js | 7 +++++++
 pgcommitfest/commitfest/templates/thread_attach.inc        | 6 ++++++
 3 files changed, 20 insertions(+)

diff --git a/pgcommitfest/commitfest/ajax.py b/pgcommitfest/commitfest/ajax.py
index c188684..ce1302a 100644
--- a/pgcommitfest/commitfest/ajax.py
+++ b/pgcommitfest/commitfest/ajax.py
@@ -54,10 +54,17 @@ def getThreads(request):
     else:
         attachonly = 0
 
+    mineonly = False
+    if request.user.is_authenticated:
+        if request.GET.get('m', '0') == '1':
+            mineonly = True
+
     # Make a JSON api call to the archives server
     params = {'n': 100, 'a': attachonly}
     if search:
         params['s'] = search
+    if mineonly:
+        params['author'] = request.user.email
 
     r = _archivesAPI('/list/pgsql-hackers/latest.json', params)
     return sorted(r, key=lambda x: x['date'], reverse=True)
diff --git a/pgcommitfest/commitfest/static/commitfest/js/commitfest.js b/pgcommitfest/commitfest/static/commitfest/js/commitfest.js
index 2f1edda..093ddf8 100644
--- a/pgcommitfest/commitfest/static/commitfest/js/commitfest.js
+++ b/pgcommitfest/commitfest/static/commitfest/js/commitfest.js
@@ -12,10 +12,12 @@ function verify_next() {
 }
 function findLatestThreads() {
    $('#attachThreadListWrap').addClass('loading');
+   $('#attachThreadMineOnly').addClass('disabled');
    $('#attachThreadSearchButton').addClass('disabled');
    $.get('/ajax/getThreads/', {
       's': $('#attachThreadSearchField').val(),
       'a': $('#attachThreadAttachOnly').val(),
+      'm': $('#attachThreadMineOnly')[0].checked ? 1 : 0,
    }).success(function(data) {
          sel = $('#attachThreadList');
          sel.find('option').remove();
@@ -24,6 +26,7 @@ function findLatestThreads() {
          });
    }).always(function() {
       $('#attachThreadListWrap').removeClass('loading');
+      $('#attachThreadMineOnly').removeClass('disabled');
       $('#attachThreadSearchButton').removeClass('disabled');
       attachThreadChanged();
    });
@@ -31,6 +34,8 @@ function findLatestThreads() {
 }
 
 function browseThreads(attachfunc, closefunc) {
+   $('[data-toggle="popover"]').popover()
+
    $('#attachThreadList').find('option').remove();
    $('#attachThreadMessageId').val('');
    $('#attachModal').off('hidden.bs.modal');
@@ -49,12 +54,14 @@ function browseThreads(attachfunc, closefunc) {
       }
 
       $('#attachThreadListWrap').addClass('loading');
+      $('#attachThreadMineOnly').addClass('disabled');
       $('#attachThreadSearchButton').addClass('disabled');
       $('#attachThreadButton').addClass('disabled');
       if (attachfunc(msgid)) {
          $('#attachModal').modal('hide');
       }
       $('#attachThreadListWrap').removeClass('loading');
+      $('#attachThreadMineOnly').removeClass('disabled');
       $('#attachThreadSearchButton').removeClass('disabled');
       attachThreadChanged();
    });
diff --git a/pgcommitfest/commitfest/templates/thread_attach.inc b/pgcommitfest/commitfest/templates/thread_attach.inc
index 4e538cc..9b80774 100644
--- a/pgcommitfest/commitfest/templates/thread_attach.inc
+++ b/pgcommitfest/commitfest/templates/thread_attach.inc
@@ -12,6 +12,12 @@
        <input id="attachThreadSearchField" type="text" class="span2 search-query">
        <button id="attachThreadSearchButton" onclick="return findLatestThreads()" class="btn btn-default disabled">Search</button>
      </div>
+     <input type="checkbox" id="attachThreadMineOnly" value="mine" onchange="findLatestThreads()" checked>
+     <label for="attachThreadMineOnly">Show only my attachments</label>
+     <a href="#" data-toggle="popover" data-container="#attachModal" data-trigger="hover focus" data-placement="right"
+        data-content="Limits the search to attachments that you have uploaded.  If this doesn't show you the threads you expect, double-check your commitfest profile's email settings.">
+       <span class="glyphicon glyphicon-info-sign"></span>
+     </a>
    </form>
    <div>Pick one of the recent emails from pgsql-hackers, or search above for subject or name:</div>
    <div id="attachThreadListWrap">
-- 
2.25.1

