[2/2] notify.py: Mention users' relationship to packages in comment notifications

Message ID 20181016031844.30857-3-archlinux@thecybershadow.net
State New
Headers show
Series Mention users' relationship to packages in comment notifications | expand

Commit Message

Vladimir Panteleev Oct. 16, 2018, 3:18 a.m. UTC
Generally, it's useful to distinguish actionable vs. non-actionable
notifications. When receiving a comment notification on a package a
user maintains, it can be easy to miss it in a flood of comment
notifications for packages the user merely follows. To this end,
mention the user's relationship to the package when sending a comment
notification email.
---
 aurweb/scripts/notify.py |  66 +++++++++++++++--
 test/t2500-notify.sh     | 150 +++++++++++++++++++++++++++++++++++++++
 2 files changed, 209 insertions(+), 7 deletions(-)

Patch

diff --git a/aurweb/scripts/notify.py b/aurweb/scripts/notify.py
index c463823..30eb0c3 100755
--- a/aurweb/scripts/notify.py
+++ b/aurweb/scripts/notify.py
@@ -127,18 +127,65 @@  class WelcomeNotification(ResetKeyNotification):
                 'pasting it into your browser.', recipient._lang)
 
 
+class CommentRecipient(Recipient):
+    def __init__(self, to, lang, role_short, role_long):
+        self._role_short = role_short
+        self._role_long = role_long
+        super().__init__(to, lang)
+
+
 class CommentNotification(Notification):
     def __init__(self, conn, uid, pkgbase_id, comment_id):
         self._user = username_from_id(conn, uid)
-        self._pkgbase = pkgbase_from_id(conn, pkgbase_id)
-        cur = conn.execute('SELECT DISTINCT Users.Email, Users.LangPreference '
+        cur = conn.execute('SELECT Name, FlaggerUID, SubmitterUID, ' +
+                           'MaintainerUID, PackagerUID FROM PackageBases ' +
+                           'WHERE ID = ?', [pkgbase_id])
+        (self._pkgbase,
+         flagger_id, submitter_id, maintainer_id, packager_id) = cur.fetchone()
+
+        cur = conn.execute('SELECT DISTINCT Users.ID, Users.Email, ' +
+                           'Users.LangPreference, ' +
+                           'COUNT(PackageComaintainers.UsersID) ' +
                            'FROM Users INNER JOIN PackageNotifications ' +
-                           'ON PackageNotifications.UserID = Users.ID WHERE ' +
+                           'ON PackageNotifications.UserID = Users.ID ' +
+                           'LEFT JOIN PackageComaintainers ' +
+                           'ON PackageComaintainers.UsersID = Users.ID ' +
+                           'AND PackageComaintainers.PackageBaseID = ' +
+                           'PackageNotifications.PackageBaseID WHERE ' +
                            'Users.CommentNotify = 1 AND ' +
                            'PackageNotifications.UserID != ? AND ' +
                            'PackageNotifications.PackageBaseID = ?',
                            [uid, pkgbase_id])
-        self._recipients = [Recipient(to, lang) for to, lang in cur.fetchall()]
+        recipients = []
+        for (user_id, to, lang, is_comaintainer) in cur:
+            if user_id == maintainer_id:
+                role_short = self._l10n.translate(' (maintained by you)', lang)
+                role_long = self._l10n.translate(
+                    'You are currently maintaining this package.', lang)
+            elif is_comaintainer > 0:
+                role_short = self._l10n.translate(
+                    ' (co-maintained by you)', lang)
+                role_long = self._l10n.translate(
+                    'You are currently co-maintaining this package.', lang)
+            elif user_id == packager_id:
+                role_short = self._l10n.translate(' (packaged by you)', lang)
+                role_long = self._l10n.translate(
+                    'You are this package\'s last packager.', lang)
+            elif user_id == submitter_id:
+                role_short = self._l10n.translate(' (submitted by you)', lang)
+                role_long = self._l10n.translate(
+                    'You are this package\'s submitter.', lang)
+            elif user_id == flagger_id:
+                role_short = self._l10n.translate(' (flagged by you)', lang)
+                role_long = self._l10n.translate(
+                    'You have last flagged this package.', lang)
+            else:
+                role_short = None
+                role_long = None
+            recipients.append(CommentRecipient(to, lang,
+                                               role_short, role_long))
+        self._recipients = recipients
+
         cur = conn.execute('SELECT Comments FROM PackageComments WHERE ID = ?',
                            [comment_id])
         self._text = cur.fetchone()[0]
@@ -148,15 +195,20 @@  class CommentNotification(Notification):
         return self._recipients
 
     def get_subject(self, recipient):
-        return self._l10n.translate('AUR Comment for {pkgbase}',
-                                    recipient._lang).format(
-                                        pkgbase=self._pkgbase)
+        subject = self._l10n.translate('AUR Comment for {pkgbase}',
+                                       recipient._lang).format(
+                                           pkgbase=self._pkgbase)
+        if recipient._role_short:
+            subject += recipient._role_short
+        return subject
 
     def get_body(self, recipient):
         body = self._l10n.translate(
                 '{user} [1] added the following comment to {pkgbase} [2]:',
                 recipient._lang).format(user=self._user, pkgbase=self._pkgbase)
         body += '\n\n' + self._text + '\n\n'
+        if recipient._role_long:
+            body += recipient._role_long + '\n\n'
         dnlabel = self._l10n.translate('Disable notifications',
                                        recipient._lang)
         body += self._l10n.translate(
diff --git a/test/t2500-notify.sh b/test/t2500-notify.sh
index c9c7208..c8936e0 100755
--- a/test/t2500-notify.sh
+++ b/test/t2500-notify.sh
@@ -110,6 +110,156 @@  test_expect_success 'Test subject and body of comment notifications.' '
 	test_cmp actual expected
 '
 
+test_expect_success 'Test subject and body of comment notifications for flaggers.' '
+	cat <<-EOD | sqlite3 aur.db &&
+    UPDATE PackageBases SET FlaggerUID = 2 WHERE ID == 1001;
+	EOD
+	>sendmail.out &&
+	"$NOTIFY" comment 1 1001 2001 &&
+	grep ^Subject: sendmail.out >actual &&
+	cat <<-EOD >expected &&
+	Subject: AUR Comment for foobar (flagged by you)
+	EOD
+	test_cmp actual expected &&
+	sed -n "/^\$/,\$p" sendmail.out | base64 -d >actual &&
+	echo >>actual &&
+	cat <<-EOD >expected &&
+	user [1] added the following comment to foobar [2]:
+
+	This is a test comment.
+
+	You have last flagged this package.
+
+	If you no longer wish to receive notifications about this package,
+	please go to the package page [2] and select "Disable notifications".
+	
+	[1] https://aur.archlinux.org/account/user/
+	[2] https://aur.archlinux.org/pkgbase/foobar/
+	EOD
+	test_cmp actual expected
+'
+
+test_expect_success 'Test subject and body of comment notifications for submitters.' '
+	cat <<-EOD | sqlite3 aur.db &&
+	/* Keep and accumulate FlaggerUID and other properties from preceding tests. */
+	/* Only the most "important" relationship should be mentioned. */
+    UPDATE PackageBases SET SubmitterUID = 2 WHERE ID == 1001;
+	EOD
+	>sendmail.out &&
+	"$NOTIFY" comment 1 1001 2001 &&
+	grep ^Subject: sendmail.out >actual &&
+	cat <<-EOD >expected &&
+	Subject: AUR Comment for foobar (submitted by you)
+	EOD
+	test_cmp actual expected &&
+	sed -n "/^\$/,\$p" sendmail.out | base64 -d >actual &&
+	echo >>actual &&
+	cat <<-EOD >expected &&
+	user [1] added the following comment to foobar [2]:
+
+	This is a test comment.
+
+	You are this package'\''s submitter.
+
+	If you no longer wish to receive notifications about this package,
+	please go to the package page [2] and select "Disable notifications".
+	
+	[1] https://aur.archlinux.org/account/user/
+	[2] https://aur.archlinux.org/pkgbase/foobar/
+	EOD
+	test_cmp actual expected
+'
+
+test_expect_success 'Test subject and body of comment notifications for packagers.' '
+	cat <<-EOD | sqlite3 aur.db &&
+    UPDATE PackageBases SET PackagerUID = 2 WHERE ID == 1001;
+	EOD
+	>sendmail.out &&
+	"$NOTIFY" comment 1 1001 2001 &&
+	grep ^Subject: sendmail.out >actual &&
+	cat <<-EOD >expected &&
+	Subject: AUR Comment for foobar (packaged by you)
+	EOD
+	test_cmp actual expected &&
+	sed -n "/^\$/,\$p" sendmail.out | base64 -d >actual &&
+	echo >>actual &&
+	cat <<-EOD >expected &&
+	user [1] added the following comment to foobar [2]:
+
+	This is a test comment.
+
+	You are this package'\''s last packager.
+
+	If you no longer wish to receive notifications about this package,
+	please go to the package page [2] and select "Disable notifications".
+	
+	[1] https://aur.archlinux.org/account/user/
+	[2] https://aur.archlinux.org/pkgbase/foobar/
+	EOD
+	test_cmp actual expected
+'
+
+test_expect_success 'Test subject and body of comment notifications for co-maintainers.' '
+	cat <<-EOD | sqlite3 aur.db &&
+    INSERT INTO PackageComaintainers (PackageBaseID, UsersID, Priority) VALUES (1001, 2, 1);
+	EOD
+	>sendmail.out &&
+	"$NOTIFY" comment 1 1001 2001 &&
+	grep ^Subject: sendmail.out >actual &&
+	cat <<-EOD >expected &&
+	Subject: AUR Comment for foobar (co-maintained by you)
+	EOD
+	test_cmp actual expected &&
+	sed -n "/^\$/,\$p" sendmail.out | base64 -d >actual &&
+	echo >>actual &&
+	cat <<-EOD >expected &&
+	user [1] added the following comment to foobar [2]:
+
+	This is a test comment.
+
+	You are currently co-maintaining this package.
+
+	If you no longer wish to receive notifications about this package,
+	please go to the package page [2] and select "Disable notifications".
+	
+	[1] https://aur.archlinux.org/account/user/
+	[2] https://aur.archlinux.org/pkgbase/foobar/
+	EOD
+	test_cmp actual expected
+'
+
+test_expect_success 'Test subject and body of comment notifications for maintainers.' '
+	cat <<-EOD | sqlite3 aur.db &&
+    UPDATE PackageBases SET MaintainerUID = 2 WHERE ID == 1001;
+	EOD
+	>sendmail.out &&
+	"$NOTIFY" comment 1 1001 2001 &&
+	grep ^Subject: sendmail.out >actual &&
+	cat <<-EOD >expected &&
+	Subject: AUR Comment for foobar (maintained by you)
+	EOD
+	test_cmp actual expected &&
+	sed -n "/^\$/,\$p" sendmail.out | base64 -d >actual &&
+	echo >>actual &&
+	cat <<-EOD >expected &&
+	user [1] added the following comment to foobar [2]:
+
+	This is a test comment.
+
+	You are currently maintaining this package.
+
+	If you no longer wish to receive notifications about this package,
+	please go to the package page [2] and select "Disable notifications".
+	
+	[1] https://aur.archlinux.org/account/user/
+	[2] https://aur.archlinux.org/pkgbase/foobar/
+	EOD
+	test_cmp actual expected
+	cat <<-EOD | sqlite3 aur.db
+	DELETE FROM PackageComaintainers;
+	EOD
+'
+
 test_expect_success 'Test subject and body of update notifications.' '
 	cat <<-EOD | sqlite3 aur.db &&
 	UPDATE Users SET UpdateNotify = 1 WHERE ID = 2;