From 2285b3b7a7f3e6812118ca58740758d13eb20dba Mon Sep 17 00:00:00 2001 From: Michael Vogel Date: Tue, 14 Apr 2015 06:54:41 +0200 Subject: [PATCH 1/7] Storing the parent with every notification --- include/enotify.php | 7 ++++++- include/items.php | 5 +++-- mod/ping.php | 8 ++++---- 3 files changed, 13 insertions(+), 7 deletions(-) diff --git a/include/enotify.php b/include/enotify.php index 4327e75b8..431618850 100644 --- a/include/enotify.php +++ b/include/enotify.php @@ -63,6 +63,11 @@ function notification($params) { // e.g. "your post", "David's photo", etc. $possess_desc = t('%s '); + if (isset($params['parent'])) + $parent_id = $params['parent']; + else + $parent_id = 0; + if($params['type'] == NOTIFY_MAIL) { $subject = sprintf( t('[Friendica:Notify] New mail received at %s'),$sitename); @@ -78,7 +83,7 @@ function notification($params) { if($params['type'] == NOTIFY_COMMENT) { // logger("notification: params = " . print_r($params, true), LOGGER_DEBUG); - $parent_id = $params['parent']; + //$parent_id = $params['parent']; $p = q("SELECT `ignored` FROM `thread` WHERE `iid` = %d AND `uid` = %d LIMIT 1", intval($parent_id), diff --git a/include/items.php b/include/items.php index 8a0deb437..1fecc4421 100644 --- a/include/items.php +++ b/include/items.php @@ -1567,7 +1567,8 @@ function item_store($arr,$force_parent = false, $notify = false, $dontcache = fa 'source_link' => $item[0]['author-link'], 'source_photo' => $item[0]['author-avatar'], 'verb' => ACTIVITY_TAG, - 'otype' => 'item' + 'otype' => 'item', + 'parent' => $arr['parent'] )); logger('item_store: Notification sent for contact '.$arr['contact-id'].' and post '.$current_post, LOGGER_DEBUG); } @@ -4021,7 +4022,7 @@ function local_delivery($importer,$data) { 'verb' => $datarray['verb'], 'otype' => 'person', 'activity' => $verb, - + 'parent' => $datarray['parent'] )); } } diff --git a/mod/ping.php b/mod/ping.php index 405edd3c4..620766339 100644 --- a/mod/ping.php +++ b/mod/ping.php @@ -22,23 +22,23 @@ function ping_init(&$a) { $firehose = intval(get_pconfig(local_user(),'system','notify_full')); - $t = q("select count(*) as total from notify where uid = %d and seen = 0", + $t = q("select count(*) as `total` from `notify` where `uid` = %d and `seen` = 0 AND `msg` != ''", intval(local_user()) ); if($t && intval($t[0]['total']) > 49) { - $z = q("select * from notify where uid = %d + $z = q("select * from notify where uid = %d AND `msg` != '' and seen = 0 order by date desc limit 0, 50", intval(local_user()) ); $sysnotify = $t[0]['total']; } else { - $z1 = q("select * from notify where uid = %d + $z1 = q("select * from notify where uid = %d AND `msg` != '' and seen = 0 order by date desc limit 0, 50", intval(local_user()) ); - $z2 = q("select * from notify where uid = %d + $z2 = q("select * from notify where uid = %d AND `msg` != '' and seen = 1 order by date desc limit 0, %d", intval(local_user()), intval(50 - intval($t[0]['total'])) From 7a1db2e08335cc47dddd4712416ac65678efa8e1 Mon Sep 17 00:00:00 2001 From: Michael Vogel Date: Thu, 16 Apr 2015 06:47:09 +0200 Subject: [PATCH 2/7] Only show one notification per parent --- mod/ping.php | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/mod/ping.php b/mod/ping.php index 620766339..6545ab458 100644 --- a/mod/ping.php +++ b/mod/ping.php @@ -22,24 +22,24 @@ function ping_init(&$a) { $firehose = intval(get_pconfig(local_user(),'system','notify_full')); - $t = q("select count(*) as `total` from `notify` where `uid` = %d and `seen` = 0 AND `msg` != ''", + $t = q("select count(*) as `total` from `notify` where `uid` = %d and `seen` = 0 AND `msg` != '' GROUP BY `parent`", intval(local_user()) ); if($t && intval($t[0]['total']) > 49) { $z = q("select * from notify where uid = %d AND `msg` != '' - and seen = 0 order by date desc limit 0, 50", + and seen = 0 GROUP BY `parent` order by date desc limit 0, 50", intval(local_user()) ); $sysnotify = $t[0]['total']; } else { $z1 = q("select * from notify where uid = %d AND `msg` != '' - and seen = 0 order by date desc limit 0, 50", + and seen = 0 GROUP BY `parent` order by date desc limit 0, 50", intval(local_user()) ); $z2 = q("select * from notify where uid = %d AND `msg` != '' - and seen = 1 order by date desc limit 0, %d", + and seen = 1 GROUP BY `parent` order by date desc limit 0, %d", intval(local_user()), intval(50 - intval($t[0]['total'])) ); From d42cea51a2c31d4c7a8cc1a00acc7738a7fbaafe Mon Sep 17 00:00:00 2001 From: Michael Vogel Date: Thu, 16 Apr 2015 07:18:06 +0200 Subject: [PATCH 3/7] The item id is now stored in every notification as well --- boot.php | 2 +- include/dbstructure.php | 1 + include/enotify.php | 13 ++++++++++--- update.php | 2 +- 4 files changed, 13 insertions(+), 5 deletions(-) diff --git a/boot.php b/boot.php index 5e0a70db1..5994b2030 100644 --- a/boot.php +++ b/boot.php @@ -18,7 +18,7 @@ define ( 'FRIENDICA_PLATFORM', 'Friendica'); define ( 'FRIENDICA_CODENAME', 'Lily of the valley'); define ( 'FRIENDICA_VERSION', '3.4.0' ); define ( 'DFRN_PROTOCOL_VERSION', '2.23' ); -define ( 'DB_UPDATE_VERSION', 1183 ); +define ( 'DB_UPDATE_VERSION', 1184 ); define ( 'EOL', "
\r\n" ); define ( 'ATOM_TIME', 'Y-m-d\TH:i:s\Z' ); diff --git a/include/dbstructure.php b/include/dbstructure.php index 083255b8a..354c06a43 100644 --- a/include/dbstructure.php +++ b/include/dbstructure.php @@ -925,6 +925,7 @@ function db_definition() { "msg" => array("type" => "mediumtext", "not null" => "1"), "uid" => array("type" => "int(11)", "not null" => "1", "default" => "0"), "link" => array("type" => "varchar(255)", "not null" => "1", "default" => ""), + "iid" => array("type" => "int(11)", "not null" => "1", "default" => "0"), "parent" => array("type" => "int(11)", "not null" => "1", "default" => "0"), "seen" => array("type" => "tinyint(1)", "not null" => "1", "default" => "0"), "verb" => array("type" => "varchar(255)", "not null" => "1", "default" => ""), diff --git a/include/enotify.php b/include/enotify.php index 431618850..027293d4f 100644 --- a/include/enotify.php +++ b/include/enotify.php @@ -27,7 +27,7 @@ function notification($params) { $hostname = $a->get_hostname(); if(strpos($hostname,':')) $hostname = substr($hostname,0,strpos($hostname,':')); - + $sender_email = $a->config['sender_email']; if (empty($sender_email)) { $sender_email = t('noreply') . '@' . $hostname; @@ -63,6 +63,11 @@ function notification($params) { // e.g. "your post", "David's photo", etc. $possess_desc = t('%s '); + if (isset($params['item']['id'])) + $item_id = $params['item']['id']; + else + $item_id = 0; + if (isset($params['parent'])) $parent_id = $params['parent']; else @@ -405,6 +410,7 @@ function notification($params) { $datarray['date'] = datetime_convert(); $datarray['uid'] = $params['uid']; $datarray['link'] = $itemlink; + $datarray['iid'] = $item_id; $datarray['parent'] = $parent_id; $datarray['type'] = $params['type']; $datarray['verb'] = $params['verb']; @@ -420,8 +426,8 @@ function notification($params) { // create notification entry in DB - $r = q("insert into notify (hash,name,url,photo,date,uid,link,parent,type,verb,otype) - values('%s','%s','%s','%s','%s',%d,'%s',%d,%d,'%s','%s')", + $r = q("insert into notify (hash,name,url,photo,date,uid,link,iid,parent,type,verb,otype) + values('%s','%s','%s','%s','%s',%d,'%s',%d,%d,%d,'%s','%s')", dbesc($datarray['hash']), dbesc($datarray['name']), dbesc($datarray['url']), @@ -429,6 +435,7 @@ function notification($params) { dbesc($datarray['date']), intval($datarray['uid']), dbesc($datarray['link']), + intval($datarray['iid']), intval($datarray['parent']), intval($datarray['type']), dbesc($datarray['verb']), diff --git a/update.php b/update.php index 35aaff2c0..ca86c8557 100644 --- a/update.php +++ b/update.php @@ -1,6 +1,6 @@ Date: Tue, 21 Apr 2015 10:14:20 +0200 Subject: [PATCH 4/7] The ID field will be stored with notifications as well. --- include/diaspora.php | 2 ++ include/items.php | 3 +++ mod/ping.php | 4 ++++ 3 files changed, 9 insertions(+) diff --git a/include/diaspora.php b/include/diaspora.php index 6b42e3c55..689f5765b 100755 --- a/include/diaspora.php +++ b/include/diaspora.php @@ -1473,6 +1473,8 @@ function diaspora_comment($importer,$xml,$msg) { DiasporaFetchGuid($datarray); $message_id = item_store($datarray); + $datarray['id'] = $message_id; + //if($message_id) { //q("update item set plink = '%s' where id = %d", // //dbesc($a->get_baseurl() . '/display/' . $importer['nickname'] . '/' . $message_id), diff --git a/include/items.php b/include/items.php index 1fecc4421..ff31e84f7 100644 --- a/include/items.php +++ b/include/items.php @@ -3660,6 +3660,9 @@ function local_delivery($importer,$data) { $parent = 0; if($posted_id) { + + $datarray["id"] = $posted_id; + $r = q("SELECT `parent`, `parent-uri` FROM `item` WHERE `id` = %d AND `uid` = %d LIMIT 1", intval($posted_id), intval($importer['importer_uid']) diff --git a/mod/ping.php b/mod/ping.php index 6545ab458..82265530d 100644 --- a/mod/ping.php +++ b/mod/ping.php @@ -22,6 +22,10 @@ function ping_init(&$a) { $firehose = intval(get_pconfig(local_user(),'system','notify_full')); +/* +select notify.id, notify.type, iid, visible, deleted, CASE notify.iid WHEN 0 THEN 1 ELSE item.visible END as vis, CASE notify.iid WHEN 0 THEN 0 ELSE item.deleted END as del from notify left join item on item.id = notify.iid where notify.uid=1 group by notify.parent order by notify.id desc limit 10; +*/ + $t = q("select count(*) as `total` from `notify` where `uid` = %d and `seen` = 0 AND `msg` != '' GROUP BY `parent`", intval(local_user()) ); From 04cda104caf1e3c714aec772f363135ae3f6be53 Mon Sep 17 00:00:00 2001 From: Michael Vogel Date: Wed, 22 Apr 2015 08:39:27 +0200 Subject: [PATCH 5/7] New function that will prevent notifications from deleted posts --- mod/ping.php | 54 ++++++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 52 insertions(+), 2 deletions(-) diff --git a/mod/ping.php b/mod/ping.php index 82265530d..f907506d5 100644 --- a/mod/ping.php +++ b/mod/ping.php @@ -29,6 +29,11 @@ select notify.id, notify.type, iid, visible, deleted, CASE notify.iid WHEN 0 THE $t = q("select count(*) as `total` from `notify` where `uid` = %d and `seen` = 0 AND `msg` != '' GROUP BY `parent`", intval(local_user()) ); + + $z = ping_get_notifications(local_user()); + $sysnotify = 0; // we will update this in a moment + +/* if($t && intval($t[0]['total']) > 49) { $z = q("select * from notify where uid = %d AND `msg` != '' and seen = 0 GROUP BY `parent` order by date desc limit 0, 50", @@ -50,7 +55,7 @@ select notify.id, notify.type, iid, visible, deleted, CASE notify.iid WHEN 0 THE $z = array_merge($z1,$z2); $sysnotify = 0; // we will update this in a moment } - +*/ $tags = array(); @@ -309,7 +314,7 @@ select notify.id, notify.type, iid, visible, deleted, CASE notify.iid WHEN 0 THE } unset($_SESSION['sysmsg_info']); } - + echo " "; echo" "; @@ -317,3 +322,48 @@ select notify.id, notify.type, iid, visible, deleted, CASE notify.iid WHEN 0 THE killme(); } +function ping_get_notifications($uid) { + + $result = array(); + $offset = 0; + $seen = false; + $seensql = "NOT"; + $quit = false; + + do { + $r = q("SELECT `notify`.*, `item`.`visible`, `item`.`spam`, `item`.`deleted` + FROM `notify` LEFT JOIN `item` ON `item`.`id` = `notify`.`iid` + WHERE `notify`.`uid` = %d AND `notify`.`msg` != '' + AND $seensql `notify`.`seen` ORDER BY `notify`.`date` DESC LIMIT %d, 50", + intval($uid), intval($offset) + ); + + if (!$r AND !$seen) { + $seen = true; + $seensql = ""; + $offset = 0; + } elseif (!$r) + $quit = true; + else + $offset += 50; + + foreach ($r AS $notification) { + if (is_null($notification["visible"])) + $notification["visible"] = true; + + if (is_null($notification["spam"])) + $notification["spam"] = 0; + + if (is_null($notification["deleted"])) + $notification["deleted"] = 0; + + if ($notification["visible"] AND !$notification["spam"] AND + !$notification["deleted"] AND !is_array($result[$notification["parent"]])) + $result[$notification["parent"]] = $notification; + } + + } while ((count($result) < 50) AND !$quit); + + return($result); +} + From 7254f42333045835e27d3148777f5e2efe6441e8 Mon Sep 17 00:00:00 2001 From: Michael Vogel Date: Thu, 23 Apr 2015 08:37:31 +0200 Subject: [PATCH 6/7] Show unread mails and introductions as well. --- mod/ping.php | 74 +++++++++++++++++++++++----------------------------- 1 file changed, 33 insertions(+), 41 deletions(-) diff --git a/mod/ping.php b/mod/ping.php index f907506d5..c8fb582d1 100644 --- a/mod/ping.php +++ b/mod/ping.php @@ -22,42 +22,9 @@ function ping_init(&$a) { $firehose = intval(get_pconfig(local_user(),'system','notify_full')); -/* -select notify.id, notify.type, iid, visible, deleted, CASE notify.iid WHEN 0 THEN 1 ELSE item.visible END as vis, CASE notify.iid WHEN 0 THEN 0 ELSE item.deleted END as del from notify left join item on item.id = notify.iid where notify.uid=1 group by notify.parent order by notify.id desc limit 10; -*/ - - $t = q("select count(*) as `total` from `notify` where `uid` = %d and `seen` = 0 AND `msg` != '' GROUP BY `parent`", - intval(local_user()) - ); - $z = ping_get_notifications(local_user()); $sysnotify = 0; // we will update this in a moment -/* - if($t && intval($t[0]['total']) > 49) { - $z = q("select * from notify where uid = %d AND `msg` != '' - and seen = 0 GROUP BY `parent` order by date desc limit 0, 50", - intval(local_user()) - ); - $sysnotify = $t[0]['total']; - } - else { - $z1 = q("select * from notify where uid = %d AND `msg` != '' - and seen = 0 GROUP BY `parent` order by date desc limit 0, 50", - intval(local_user()) - ); - - $z2 = q("select * from notify where uid = %d AND `msg` != '' - and seen = 1 GROUP BY `parent` order by date desc limit 0, %d", - intval(local_user()), - intval(50 - intval($t[0]['total'])) - ); - $z = array_merge($z1,$z2); - $sysnotify = 0; // we will update this in a moment - } -*/ - - $tags = array(); $comments = array(); $likes = array(); @@ -137,13 +104,12 @@ select notify.id, notify.type, iid, visible, deleted, CASE notify.iid WHEN 0 THE $myurl = $a->get_baseurl() . '/profile/' . $a->user['nickname'] ; - $mails = q("SELECT *, COUNT(*) AS `total` FROM `mail` + $mails = q("SELECT * FROM `mail` WHERE `uid` = %d AND `seen` = 0 AND `from-url` != '%s' ", intval(local_user()), dbesc($myurl) ); - if($mails) - $mail = $mails[0]['total']; + $mail = count($mails); if ($a->config['register_policy'] == REGISTER_APPROVE && is_site_admin()){ $regs = q("SELECT `contact`.`name`, `contact`.`url`, `contact`.`micro`, `register`.`created`, COUNT(*) as `total` FROM `contact` RIGHT JOIN `register` ON `register`.`uid`=`contact`.`uid` WHERE `contact`.`self`=1"); @@ -235,7 +201,24 @@ select notify.id, notify.type, iid, visible, deleted, CASE notify.iid WHEN 0 THE } } - echo ' '; + echo ' '; + + if ($intro>0){ + foreach ($intros as $i) { + echo xmlize( $a->get_baseurl().'/notifications/intros/'.$i['id'], $i['name'], $i['url'], $i['photo'], relative_date($i['datetime']), 'notify-unseen',t("{0} wants to be your friend") ); + }; + } + if ($mail>0){ + foreach ($mails as $i) { + echo xmlize( $a->get_baseurl().'/message/'.$i['id'], $i['from-name'], $i['from-url'], $i['from-photo'], relative_date($i['created']), 'notify-unseen',t("{0} sent you a message") ); + }; + } + if ($register>0){ + foreach ($regs as $i) { + echo xmlize( $a->get_baseurl().'/admin/users/', $i['name'], $i['url'], $i['micro'], relative_date($i['created']), 'notify-unseen',t("{0} requested registration") ); + }; + } + if(count($z)) { foreach($z as $zz) { echo xmlize($a->get_baseurl() . '/notify/view/' . $zz['id'], $zz['name'],$zz['url'],$zz['photo'],relative_date($zz['date']), ($zz['seen'] ? 'notify-seen' : 'notify-unseen'), ($zz['seen'] ? '' : '→ ') .strip_tags(bbcode($zz['msg']))); @@ -250,12 +233,12 @@ select notify.id, notify.type, iid, visible, deleted, CASE notify.iid WHEN 0 THE }; } if ($mail>0){ - foreach ($mails as $i) { + foreach ($mails as $i) { echo xmlize( $a->get_baseurl().'/message/'.$i['id'], $i['from-name'], $i['from-url'], $i['from-photo'], relative_date($i['created']), 'notify-unseen',t("{0} sent you a message") ); }; } if ($register>0){ - foreach ($regs as $i) { + foreach ($regs as $i) { echo xmlize( $a->get_baseurl().'/admin/users/', $i['name'], $i['url'], $i['micro'], relative_date($i['created']), 'notify-unseen',t("{0} requested registration") ); }; } @@ -334,8 +317,12 @@ function ping_get_notifications($uid) { $r = q("SELECT `notify`.*, `item`.`visible`, `item`.`spam`, `item`.`deleted` FROM `notify` LEFT JOIN `item` ON `item`.`id` = `notify`.`iid` WHERE `notify`.`uid` = %d AND `notify`.`msg` != '' + AND NOT (`notify`.`type` IN (%d, %d)) AND $seensql `notify`.`seen` ORDER BY `notify`.`date` DESC LIMIT %d, 50", - intval($uid), intval($offset) + intval($uid), + intval(NOTIFY_INTRO), + intval(NOTIFY_MAIL), + intval($offset) ); if (!$r AND !$seen) { @@ -357,6 +344,12 @@ function ping_get_notifications($uid) { if (is_null($notification["deleted"])) $notification["deleted"] = 0; + // Replace the name with {0} but ensure to make that only once + // The {0} is used later and prints the name in bold. + $pos = strpos($notification["msg"],$notification['name']); + if ($pos !== false) + $notification["msg"] = substr_replace($notification["msg"],"{0}",$pos,strlen($notification['name'])); + if ($notification["visible"] AND !$notification["spam"] AND !$notification["deleted"] AND !is_array($result[$notification["parent"]])) $result[$notification["parent"]] = $notification; @@ -366,4 +359,3 @@ function ping_get_notifications($uid) { return($result); } - From c348f27e84f4c461acf1fffdb9bbf85a209d24a0 Mon Sep 17 00:00:00 2001 From: Michael Vogel Date: Thu, 23 Apr 2015 08:59:30 +0200 Subject: [PATCH 7/7] Show unread notifications in a different order than read notifications --- mod/ping.php | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/mod/ping.php b/mod/ping.php index c8fb582d1..15e7dfab1 100644 --- a/mod/ping.php +++ b/mod/ping.php @@ -311,6 +311,7 @@ function ping_get_notifications($uid) { $offset = 0; $seen = false; $seensql = "NOT"; + $order = ""; $quit = false; do { @@ -318,7 +319,7 @@ function ping_get_notifications($uid) { FROM `notify` LEFT JOIN `item` ON `item`.`id` = `notify`.`iid` WHERE `notify`.`uid` = %d AND `notify`.`msg` != '' AND NOT (`notify`.`type` IN (%d, %d)) - AND $seensql `notify`.`seen` ORDER BY `notify`.`date` DESC LIMIT %d, 50", + AND $seensql `notify`.`seen` ORDER BY `notify`.`date` $order LIMIT %d, 50", intval($uid), intval(NOTIFY_INTRO), intval(NOTIFY_MAIL), @@ -328,6 +329,7 @@ function ping_get_notifications($uid) { if (!$r AND !$seen) { $seen = true; $seensql = ""; + $order = "DESC"; $offset = 0; } elseif (!$r) $quit = true;