From 4ee858740ef75c80149ff90254bbc63b412d5f07 Mon Sep 17 00:00:00 2001 From: Michael Date: Wed, 12 Aug 2020 04:35:24 +0000 Subject: [PATCH 1/9] Create notifications for shared posts --- include/enotify.php | 2 +- src/Model/UserItem.php | 10 ++++++++-- 2 files changed, 9 insertions(+), 3 deletions(-) diff --git a/include/enotify.php b/include/enotify.php index 6b3171dda..732c1d7b4 100644 --- a/include/enotify.php +++ b/include/enotify.php @@ -597,7 +597,7 @@ function check_item_notification($itemid, $uid, $notification_type) { $fields = ['id', 'uri-id', 'mention', 'parent', 'parent-uri-id', 'title', 'body', 'author-link', 'author-name', 'author-avatar', 'author-id', 'guid', 'parent-uri', 'uri', 'contact-id', 'network']; - $condition = ['id' => $itemid, 'gravity' => [GRAVITY_PARENT, GRAVITY_COMMENT], 'deleted' => false]; + $condition = ['id' => $itemid, 'deleted' => false]; $item = Item::selectFirstForUser($uid, $fields, $condition); if (!DBA::isResult($item)) { return false; diff --git a/src/Model/UserItem.php b/src/Model/UserItem.php index afb13829d..0b542dee9 100644 --- a/src/Model/UserItem.php +++ b/src/Model/UserItem.php @@ -27,6 +27,7 @@ use Friendica\Database\DBA; use Friendica\DI; use Friendica\Util\Strings; use Friendica\Model\Tag; +use Friendica\Protocol\Activity; class UserItem { @@ -50,7 +51,8 @@ class UserItem */ public static function setNotification(int $iid) { - $fields = ['id', 'uri-id', 'uid', 'body', 'parent', 'gravity', 'tag', 'contact-id', 'thr-parent', 'parent-uri', 'author-id']; + $fields = ['id', 'uri-id', 'uid', 'body', 'parent', 'gravity', 'tag', + 'contact-id', 'thr-parent', 'parent-uri', 'author-id', 'verb']; $item = Item::selectFirst($fields, ['id' => $iid, 'origin' => false]); if (!DBA::isResult($item)) { return; @@ -197,7 +199,7 @@ class UserItem */ private static function checkShared(array $item, int $uid) { - if ($item['gravity'] != GRAVITY_PARENT) { + if (($item['gravity'] != GRAVITY_PARENT) && ($item['verb'] != Activity::ANNOUNCE)) { return false; } @@ -206,6 +208,10 @@ class UserItem return true; } + if ($item['gravity'] != GRAVITY_PARENT) { + return false; + } + // Or the contact is a mentioned forum $tags = DBA::select('tag-view', ['url'], ['uri-id' => $item['uri-id'], 'type' => [Tag::MENTION, Tag::EXCLUSIVE_MENTION]]); while ($tag = DBA::fetch($tags)) { From 26df7595ac5366bd0f14f94ef961cf8af6b72ebb Mon Sep 17 00:00:00 2001 From: Michael Date: Wed, 12 Aug 2020 04:43:46 +0000 Subject: [PATCH 2/9] removed unneeded notification check --- src/Model/Item.php | 9 +-------- 1 file changed, 1 insertion(+), 8 deletions(-) diff --git a/src/Model/Item.php b/src/Model/Item.php index 4526be3e9..778b872bb 100644 --- a/src/Model/Item.php +++ b/src/Model/Item.php @@ -2018,14 +2018,7 @@ class Item if (!empty($contact['id'])) { $condition = ['uri-id' => $item['parent-uri-id'], 'uid' => $item['uid']]; Item::update(['owner-id' => $item['author-id'], 'contact-id' => $contact['id']], $condition); - $forum_item = Item::selectFirst(['id'], $condition); - if (!empty($forum_item['id'])) { - // This will trigger notifications like "X shared a new post" - UserItem::setNotification($forum_item['id']); - - check_user_notification($forum_item['id']); - } - LOgger::info('Convert message into a forum message', ['uri-id' => $item['uri-id'], 'parent-uri-id' => $item['parent-uri-id'], 'uid' => $item['uid'], 'owner-id' => $item['author-id'], 'contact-id' => $contact['id']]); + Logger::info('Convert message into a forum message', ['uri-id' => $item['uri-id'], 'parent-uri-id' => $item['parent-uri-id'], 'uid' => $item['uid'], 'owner-id' => $item['author-id'], 'contact-id' => $contact['id']]); } } From 60631f8a22a3dca274478f87271c13261bcd1521 Mon Sep 17 00:00:00 2001 From: Michael Date: Wed, 12 Aug 2020 06:50:02 +0000 Subject: [PATCH 3/9] Avoid getting too much notifications --- include/enotify.php | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/include/enotify.php b/include/enotify.php index 732c1d7b4..229ef33df 100644 --- a/include/enotify.php +++ b/include/enotify.php @@ -596,13 +596,17 @@ function check_user_notification($itemid) { function check_item_notification($itemid, $uid, $notification_type) { $fields = ['id', 'uri-id', 'mention', 'parent', 'parent-uri-id', 'title', 'body', 'author-link', 'author-name', 'author-avatar', 'author-id', - 'guid', 'parent-uri', 'uri', 'contact-id', 'network']; + 'guid', 'parent-uri', 'uri', 'contact-id', 'network', 'gravity', 'verb']; $condition = ['id' => $itemid, 'deleted' => false]; $item = Item::selectFirstForUser($uid, $fields, $condition); if (!DBA::isResult($item)) { return false; } + if (!in_array($item['gravity'], [GRAVITY_PARENT, GRAVITY_COMMENT]) && ($item['verb'] != Activity::ANNOUNCE)) { + return false; + } + // Generate the notification array $params = []; $params['uid'] = $uid; From 8488ed76e4dbd4cdb2d5c9e752b169fd1db284a5 Mon Sep 17 00:00:00 2001 From: Michael Date: Wed, 12 Aug 2020 08:37:38 +0000 Subject: [PATCH 4/9] Don't create notifications for activities --- include/enotify.php | 4 ---- src/Model/UserItem.php | 48 ++++++++++++++++++++++++------------------ 2 files changed, 28 insertions(+), 24 deletions(-) diff --git a/include/enotify.php b/include/enotify.php index 229ef33df..dd80c20d0 100644 --- a/include/enotify.php +++ b/include/enotify.php @@ -603,10 +603,6 @@ function check_item_notification($itemid, $uid, $notification_type) { return false; } - if (!in_array($item['gravity'], [GRAVITY_PARENT, GRAVITY_COMMENT]) && ($item['verb'] != Activity::ANNOUNCE)) { - return false; - } - // Generate the notification array $params = []; $params['uid'] = $uid; diff --git a/src/Model/UserItem.php b/src/Model/UserItem.php index 0b542dee9..1680dd272 100644 --- a/src/Model/UserItem.php +++ b/src/Model/UserItem.php @@ -58,6 +58,11 @@ class UserItem return; } + // "Activity::FOLLOW" is an automated activity, so we ignore it here + if ($item['verb'] == Activity::FOLLOW) { + return; + } + // fetch all users in the thread $users = DBA::p("SELECT DISTINCT(`contact`.`uid`) FROM `item` INNER JOIN `contact` ON `contact`.`id` = `item`.`contact-id` AND `contact`.`uid` != 0 @@ -102,32 +107,35 @@ class UserItem return; } - if (self::checkImplicitMention($item, $profiles)) { - $notification_type = $notification_type | self::NOTIF_IMPLICIT_TAGGED; - } + // Only create notifications for posts and comments, not for activities + if (in_array($item['gravity'], [GRAVITY_PARENT, GRAVITY_COMMENT])) { + if (self::checkImplicitMention($item, $profiles)) { + $notification_type = $notification_type | self::NOTIF_IMPLICIT_TAGGED; + } - if (self::checkExplicitMention($item, $profiles)) { - $notification_type = $notification_type | self::NOTIF_EXPLICIT_TAGGED; - } + if (self::checkExplicitMention($item, $profiles)) { + $notification_type = $notification_type | self::NOTIF_EXPLICIT_TAGGED; + } - if (self::checkCommentedThread($item, $contacts)) { - $notification_type = $notification_type | self::NOTIF_THREAD_COMMENT; - } + if (self::checkCommentedThread($item, $contacts)) { + $notification_type = $notification_type | self::NOTIF_THREAD_COMMENT; + } - if (self::checkDirectComment($item, $contacts)) { - $notification_type = $notification_type | self::NOTIF_DIRECT_COMMENT; - } + if (self::checkDirectComment($item, $contacts)) { + $notification_type = $notification_type | self::NOTIF_DIRECT_COMMENT; + } - if (self::checkDirectCommentedThread($item, $contacts)) { - $notification_type = $notification_type | self::NOTIF_DIRECT_THREAD_COMMENT; - } + if (self::checkDirectCommentedThread($item, $contacts)) { + $notification_type = $notification_type | self::NOTIF_DIRECT_THREAD_COMMENT; + } - if (self::checkCommentedParticipation($item, $contacts)) { - $notification_type = $notification_type | self::NOTIF_COMMENT_PARTICIPATION; - } + if (self::checkCommentedParticipation($item, $contacts)) { + $notification_type = $notification_type | self::NOTIF_COMMENT_PARTICIPATION; + } - if (self::checkActivityParticipation($item, $contacts)) { - $notification_type = $notification_type | self::NOTIF_ACTIVITY_PARTICIPATION; + if (self::checkActivityParticipation($item, $contacts)) { + $notification_type = $notification_type | self::NOTIF_ACTIVITY_PARTICIPATION; + } } if (empty($notification_type)) { From fbb1ddd53408440212f74851bc85c5e6be61056e Mon Sep 17 00:00:00 2001 From: Michael Date: Wed, 12 Aug 2020 08:39:04 +0000 Subject: [PATCH 5/9] Unneeded fields removed --- include/enotify.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/include/enotify.php b/include/enotify.php index dd80c20d0..732c1d7b4 100644 --- a/include/enotify.php +++ b/include/enotify.php @@ -596,7 +596,7 @@ function check_user_notification($itemid) { function check_item_notification($itemid, $uid, $notification_type) { $fields = ['id', 'uri-id', 'mention', 'parent', 'parent-uri-id', 'title', 'body', 'author-link', 'author-name', 'author-avatar', 'author-id', - 'guid', 'parent-uri', 'uri', 'contact-id', 'network', 'gravity', 'verb']; + 'guid', 'parent-uri', 'uri', 'contact-id', 'network']; $condition = ['id' => $itemid, 'deleted' => false]; $item = Item::selectFirstForUser($uid, $fields, $condition); if (!DBA::isResult($item)) { From 7cdbcf411f71dff064cdbfe036b3d519640d98c2 Mon Sep 17 00:00:00 2001 From: Michael Date: Wed, 12 Aug 2020 13:54:19 +0000 Subject: [PATCH 6/9] Clarify the functionality --- src/Model/UserItem.php | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/src/Model/UserItem.php b/src/Model/UserItem.php index 1680dd272..11446f6e8 100644 --- a/src/Model/UserItem.php +++ b/src/Model/UserItem.php @@ -207,20 +207,22 @@ class UserItem */ private static function checkShared(array $item, int $uid) { + // Only sheck on starting posts and reshare ("announce") activities, otherwise return if (($item['gravity'] != GRAVITY_PARENT) && ($item['verb'] != Activity::ANNOUNCE)) { return false; } - // Either the contact had posted something directly + // Check if the contact had posted or shared something directly if (DBA::exists('contact', ['id' => $item['contact-id'], 'notify_new_posts' => true])) { return true; } - if ($item['gravity'] != GRAVITY_PARENT) { + // Don't continue when the item had been an announce activity + if ($item['verb'] == Activity::ANNOUNCE) { return false; } - // Or the contact is a mentioned forum + // Check if the contact is a mentioned forum $tags = DBA::select('tag-view', ['url'], ['uri-id' => $item['uri-id'], 'type' => [Tag::MENTION, Tag::EXCLUSIVE_MENTION]]); while ($tag = DBA::fetch($tags)) { $condition = ['nurl' => Strings::normaliseLink($tag['url']), 'uid' => $uid, 'notify_new_posts' => true, 'contact-type' => Contact::TYPE_COMMUNITY]; From 8d8b6b1bf5e08a7599f78ecc98f1ea0bed3b39b6 Mon Sep 17 00:00:00 2001 From: Michael Vogel Date: Wed, 12 Aug 2020 18:18:45 +0200 Subject: [PATCH 7/9] Update src/Model/UserItem.php Co-authored-by: Hypolite Petovan --- src/Model/UserItem.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Model/UserItem.php b/src/Model/UserItem.php index 11446f6e8..144ad46eb 100644 --- a/src/Model/UserItem.php +++ b/src/Model/UserItem.php @@ -207,7 +207,7 @@ class UserItem */ private static function checkShared(array $item, int $uid) { - // Only sheck on starting posts and reshare ("announce") activities, otherwise return + // Only check on original posts and reshare ("announce") activities, otherwise return if (($item['gravity'] != GRAVITY_PARENT) && ($item['verb'] != Activity::ANNOUNCE)) { return false; } From d331a88adca8a3ed20d0a7936271edbd3c56c7ef Mon Sep 17 00:00:00 2001 From: Michael Vogel Date: Wed, 12 Aug 2020 18:19:02 +0200 Subject: [PATCH 8/9] Update src/Model/UserItem.php Co-authored-by: Hypolite Petovan --- src/Model/UserItem.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Model/UserItem.php b/src/Model/UserItem.php index 144ad46eb..4e4ca6c94 100644 --- a/src/Model/UserItem.php +++ b/src/Model/UserItem.php @@ -212,7 +212,7 @@ class UserItem return false; } - // Check if the contact had posted or shared something directly + // Check if the contact posted or shared something directly if (DBA::exists('contact', ['id' => $item['contact-id'], 'notify_new_posts' => true])) { return true; } From 59311e9c491b3f64b7423e032c07df7556c5504f Mon Sep 17 00:00:00 2001 From: Michael Date: Thu, 13 Aug 2020 05:17:37 +0000 Subject: [PATCH 9/9] Changed comment --- src/Model/UserItem.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Model/UserItem.php b/src/Model/UserItem.php index 11446f6e8..457228725 100644 --- a/src/Model/UserItem.php +++ b/src/Model/UserItem.php @@ -217,7 +217,7 @@ class UserItem return true; } - // Don't continue when the item had been an announce activity + // The following check doesn't make sense on activities, so quit here if ($item['verb'] == Activity::ANNOUNCE) { return false; }