From 74b6d09e89bbe87e94ebb458d8a76415e324d1d3 Mon Sep 17 00:00:00 2001
From: Alexandre Alapetite <alexandre@alapetite.fr>
Date: Wed, 12 Apr 2017 15:11:50 +0200
Subject: [PATCH] Fix GROUP BY in acl_selector, api, notifier, photos, messages

https://github.com/friendica/friendica/issues/3322
---
 include/acl_selectors.php | 4 ++--
 include/api.php           | 8 +++++---
 include/notifier.php      | 2 +-
 include/photos.php        | 4 ++--
 mod/message.php           | 4 ++--
 mod/photos.php            | 2 +-
 mod/videos.php            | 2 +-
 7 files changed, 14 insertions(+), 12 deletions(-)

diff --git a/include/acl_selectors.php b/include/acl_selectors.php
index f4b644d68f..87230a1409 100644
--- a/include/acl_selectors.php
+++ b/include/acl_selectors.php
@@ -502,7 +502,7 @@ function acl_lookup(App $a, $out_type = 'json') {
 				INNER JOIN `group_member` ON `group_member`.`gid`=`group`.`id` AND `group_member`.`uid` = `group`.`uid`
 				WHERE NOT `group`.`deleted` AND `group`.`uid` = %d
 					$sql_extra
-				GROUP BY `group`.`name`
+				GROUP BY `group`.`name`, `group`.`id`
 				ORDER BY `group`.`name`
 				LIMIT %d,%d",
 			intval(local_user()),
@@ -619,7 +619,7 @@ function acl_lookup(App $a, $out_type = 'json') {
 				FROM `item` WHERE `parent` = %d
 					AND (`author-name` LIKE '%%%s%%' OR `author-link` LIKE '%%%s%%')
 					AND `author-link` NOT IN ('%s')
-				GROUP BY `author-link`
+				GROUP BY `author-link`, `author-avatar`, `author-name`
 				ORDER BY `author-name` ASC
 				",
 				intval($conv_id),
diff --git a/include/api.php b/include/api.php
index 9fc853340a..477eb94b4e 100644
--- a/include/api.php
+++ b/include/api.php
@@ -3064,7 +3064,7 @@ use \Friendica\Core\Config;
 	function api_fr_photos_list($type) {
 		if (api_user()===false) throw new ForbiddenException();
 		$r = q("select `resource-id`, max(scale) as scale, album, filename, type from photo
-				where uid = %d and album != 'Contact Photos' group by `resource-id`",
+				where uid = %d and album != 'Contact Photos' group by `resource-id`, album, filename, type",
 			intval(local_user())
 		);
 		$typetoext = array(
@@ -3102,8 +3102,10 @@ use \Friendica\Core\Config;
 		$data_sql = ($scale === false ? "" : "data, ");
 
 		$r = q("select %s `resource-id`, `created`, `edited`, `title`, `desc`, `album`, `filename`,
-						`type`, `height`, `width`, `datasize`, `profile`, min(`scale`) as minscale, max(`scale`) as maxscale
-				from photo where `uid` = %d and `resource-id` = '%s' %s group by `resource-id`",
+					`type`, `height`, `width`, `datasize`, `profile`, min(`scale`) as minscale, max(`scale`) as maxscale
+				from photo where `uid` = %d and `resource-id` = '%s' %s
+				group by `resource-id`, `created`, `edited`, `title`, `desc`, `album`, `filename`,
+					`type`, `height`, `width`, `datasize`, `profile`",
 			$data_sql,
 			intval(local_user()),
 			dbesc($_REQUEST['photo_id']),
diff --git a/include/notifier.php b/include/notifier.php
index e3d7d10d6b..ed34288e8f 100644
--- a/include/notifier.php
+++ b/include/notifier.php
@@ -517,7 +517,7 @@ function notifier_run(&$argv, &$argc){
 			}
 
 			$r1 = q("SELECT DISTINCT(`batch`), `id`, `name`,`network` FROM `contact` WHERE `network` = '%s'
-				AND `uid` = %d AND `rel` != %d AND NOT `blocked` AND NOT `pending` AND NOT `archive` GROUP BY `batch` ORDER BY rand()",
+				AND `uid` = %d AND `rel` != %d AND NOT `blocked` AND NOT `pending` AND NOT `archive` GROUP BY `batch`, `id` ORDER BY rand()",
 				dbesc(NETWORK_DIASPORA),
 				intval($owner['uid']),
 				intval(CONTACT_IS_SHARING)
diff --git a/include/photos.php b/include/photos.php
index 9d8d3309c2..02d1b17403 100644
--- a/include/photos.php
+++ b/include/photos.php
@@ -51,7 +51,7 @@ function photo_albums($uid, $update = false) {
 			$albums = qu("SELECT COUNT(DISTINCT `resource-id`) AS `total`, `album`
 				FROM `photo`
 				WHERE `uid` = %d  AND `album` != '%s' AND `album` != '%s' $sql_extra
-				GROUP BY `album` ORDER BY `created` DESC",
+				GROUP BY `album`, `created` ORDER BY `created` DESC",
 				intval($uid),
 				dbesc('Contact Photos'),
 				dbesc(t('Contact Photos'))
@@ -61,7 +61,7 @@ function photo_albums($uid, $update = false) {
 			$albums = qu("SELECT DISTINCT(`album`), '' AS `total`
 				FROM `photo`
 				WHERE `uid` = %d  AND `album` != '%s' AND `album` != '%s' $sql_extra
-				GROUP BY `album` ORDER BY `created` DESC",
+				GROUP BY `album`, `created` ORDER BY `created` DESC",
 				intval($uid),
 				dbesc('Contact Photos'),
 				dbesc(t('Contact Photos'))
diff --git a/mod/message.php b/mod/message.php
index 9e96691466..05a8d36f68 100644
--- a/mod/message.php
+++ b/mod/message.php
@@ -350,7 +350,7 @@ function message_content(App $a) {
 		$o .= $header;
 
 		$r = q("SELECT count(*) AS `total` FROM `mail`
-			WHERE `mail`.`uid` = %d GROUP BY `parent-uri` ORDER BY `created` DESC",
+			WHERE `mail`.`uid` = %d GROUP BY `parent-uri`, `created` ORDER BY `created` DESC",
 			intval(local_user())
 		);
 
@@ -533,7 +533,7 @@ function get_messages($user, $lstart, $lend) {
 		`mail`.* , `contact`.`name`, `contact`.`url`, `contact`.`thumb` , `contact`.`network`,
 		count( * ) as count
 		FROM `mail` LEFT JOIN `contact` ON `mail`.`contact-id` = `contact`.`id`
-		WHERE `mail`.`uid` = %d GROUP BY `parent-uri` ORDER BY `mailcreated` DESC  LIMIT %d , %d ",
+		WHERE `mail`.`uid` = %d GROUP BY `parent-uri`, `mail`.id ORDER BY `mailcreated` DESC  LIMIT %d , %d ",
 		intval($user), intval($lstart), intval($lend)
 	);
 }
diff --git a/mod/photos.php b/mod/photos.php
index 3acd39b2af..a24cee2559 100644
--- a/mod/photos.php
+++ b/mod/photos.php
@@ -1241,7 +1241,7 @@ function photos_content(App $a) {
 		}
 
 		$r = q("SELECT `resource-id`, `id`, `filename`, type, max(`scale`) AS `scale`, `desc` FROM `photo` WHERE `uid` = %d AND `album` = '%s'
-			AND `scale` <= 4 $sql_extra GROUP BY `resource-id` ORDER BY `created` $order LIMIT %d , %d",
+			AND `scale` <= 4 $sql_extra GROUP BY `resource-id`, `id` ORDER BY `created` $order LIMIT %d , %d",
 			intval($owner_uid),
 			dbesc($album),
 			intval($a->pager['start']),
diff --git a/mod/videos.php b/mod/videos.php
index 3828b8f1fe..ea2e0d4a24 100644
--- a/mod/videos.php
+++ b/mod/videos.php
@@ -358,7 +358,7 @@ function videos_content(App $a) {
 
 	$r = q("SELECT hash, `id`, `filename`, filetype FROM `attach`
 		WHERE `uid` = %d AND filetype LIKE '%%video%%'
-		$sql_extra GROUP BY hash ORDER BY `created` DESC LIMIT %d , %d",
+		$sql_extra GROUP BY hash, `id` ORDER BY `created` DESC LIMIT %d , %d",
 		intval($a->data['user']['uid']),
 		intval($a->pager['start']),
 		intval($a->pager['itemspage'])