[aur-dev] [PATCH] Move notification logic to separate function

Nicolas Cornu nicolac76 at yahoo.fr
Sat Jul 28 03:42:21 EDT 2012


Move notification logic to separate function
And optimize database requests in notification
---
 web/lib/pkgfuncs.inc.php | 58 ++++++++++++++++++++++++++++--------------------
 1 file changed, 34 insertions(+), 24 deletions(-)

diff --git a/web/lib/pkgfuncs.inc.php b/web/lib/pkgfuncs.inc.php
index 0610617..5420c8f 100644
--- a/web/lib/pkgfuncs.inc.php
+++ b/web/lib/pkgfuncs.inc.php
@@ -200,24 +200,14 @@ function package_comments($pkgid, $dbh=NULL) {
 	return $comments;
 }
 
-# Add a comment to a package page and send out appropriate notifications
-# TODO: Move notification logic to separate function where it belongs
-function add_package_comment($pkgid, $uid, $comment, $dbh=NULL) {
+function send_notifications($pkgid, $uid, $subject, $body, $dbh=NULL, $pkg_name="") {
 	if(!$dbh) {
 		$dbh = db_connect();
 	}
 
-	$q = 'INSERT INTO PackageComments ';
-	$q.= '(PackageID, UsersID, Comments, CommentTS) VALUES (';
-	$q.= intval($pkgid) . ', ' . $uid . ', ';
-	$q.= "'" . db_escape_string($comment) . "', ";
-	$q.= 'UNIX_TIMESTAMP())';
-	db_query($q, $dbh);
-
 	# Send email notifications
-	$q = 'SELECT CommentNotify.*, Users.Email ';
-	$q.= 'FROM CommentNotify, Users ';
-	$q.= 'WHERE Users.ID = CommentNotify.UserID ';
+	$q = 'SELECT Users.Email FROM Users ';
+	$q.= 'INNER JOIN CommentNotify ON Users.ID=CommentNotify.UserID';
 	$q.= 'AND CommentNotify.UserID != ' . $uid . ' ';
 	$q.= 'AND CommentNotify.PkgID = ' . intval($pkgid);
 	$result = db_query($q, $dbh);
@@ -228,25 +218,45 @@ function add_package_comment($pkgid, $uid, $comment, $dbh=NULL) {
 			array_push($bcc, $row['Email']);
 		}
 
-		$q = 'SELECT Packages.* ';
-		$q.= 'FROM Packages ';
-		$q.= 'WHERE Packages.ID = ' . intval($pkgid);
-		$result = db_query($q, $dbh);
-		$row = mysql_fetch_assoc($result);
+		if($pkg_name == "") {
+			$q = 'SELECT Packages.Name ';
+			$q.= 'FROM Packages ';
+			$q.= 'WHERE Packages.ID = ' . intval($pkgid);
+			$result = db_query($q, $dbh);
+			$row = mysql_fetch_assoc($result);
+			$pkg_name = $row['Name'];
+		}
 
 		# TODO: native language emails for users, based on their prefs
 		# Simply making these strings translatable won't work, users would be
 		# getting emails in the language that the user who posted the comment was in
-		$body =
-		'from ' . $AUR_LOCATION . '/' . get_pkg_uri($row['Name']) . "\n"
-		. username_from_sid($_COOKIE['AURSID'], $dbh) . " wrote:\n\n"
-		. $comment
-		. "\n\n---\nIf you no longer wish to receive notifications about this package, please go the the above package page and click the UnNotify button.";
+		$body = 'from ' . $AUR_LOCATION . '/' . get_pkg_uri($pkg_name) . "\n" . $body;
+		$body.= "\n\n---\nIf you no longer wish to receive notifications about this package, please go the the above package page and click the UnNotify button.";
 		$body = wordwrap($body, 70);
 		$bcc = implode(', ', $bcc);
 		$headers = "Bcc: $bcc\nReply-to: nobody at archlinux.org\nFrom: aur-notify at archlinux.org\nX-Mailer: AUR\n";
-		@mail('undisclosed-recipients: ;', "AUR Comment for " . $row['Name'], $body, $headers);
+		@mail('undisclosed-recipients: ;', $subject . " for " . $pkg_name, $body, $headers);
+	}
+}
+
+# Add a comment to a package page and send out appropriate notifications
+function add_package_comment($pkgid, $uid, $comment, $dbh=NULL) {
+	if(!$dbh) {
+		$dbh = db_connect();
 	}
+
+	$q = 'INSERT INTO PackageComments ';
+	$q.= '(PackageID, UsersID, Comments, CommentTS) VALUES (';
+	$q.= intval($pkgid) . ', ' . $uid . ', ';
+	$q.= "'" . db_escape_string($comment) . "', ";
+	$q.= 'UNIX_TIMESTAMP())';
+	db_query($q, $dbh);
+
+	# Send notifications
+	$subject = "AUR Comment";
+	$body = username_from_sid($_COOKIE['AURSID'], $dbh) . " wrote:\n\n";
+	$body.= $comment;
+	send_notifications($pkgid, $uid, $subject, $body, $dbh);
 }
 
 # grab package sources
-- 
1.7.11.3



More information about the aur-dev mailing list