]> git.mxchange.org Git - friendica.git/blobdiff - src/Model/Notification.php
Merge pull request #10800 from MrPetovan/task/10739-block
[friendica.git] / src / Model / Notification.php
index 880fa784a61ff1b2acde436be14cb815772fe870..1df420845c7b610a7be2c6112361b6f28c82a1c4 100644 (file)
@@ -168,147 +168,184 @@ class Notification extends BaseModel
        {
                $message = [];
 
-               if ($notification['type'] == Post\UserNotification::NOTIF_NONE) {
-                       return $message;
-               }
-
-               if (empty($notification['target-uri-id'])) {
-                       return $message;
-               }
-
                $user = User::getById($notification['uid']);
                if (empty($user)) {
                        Logger::info('User not found', ['application' => $notification['uid']]);
                        return $message;
                }
 
-               $contact = Contact::getById($notification['actor-id']);
+               $l10n = DI::l10n()->withLang($user['language']);
+
+               $causer = $contact = Contact::getById($notification['actor-id'], ['id', 'name', 'url', 'pending']);
                if (empty($contact)) {
                        Logger::info('Contact not found', ['contact' => $notification['actor-id']]);
                        return $message;
                }
 
-               $like     = Verb::getID(Activity::LIKE);
-               $dislike  = Verb::getID(Activity::DISLIKE);
-               $announce = Verb::getID(Activity::ANNOUNCE);
-               $post     = Verb::getID(Activity::POST);
-
-               if (in_array($notification['type'], [Post\UserNotification::NOTIF_THREAD_COMMENT, Post\UserNotification::NOTIF_COMMENT_PARTICIPATION])) {
-                       $item = Post::selectFirst([], ['uri-id' => $notification['parent-uri-id'], 'uid' => [0, $notification['uid']]]);
-                       if (empty($item)) {
-                               Logger::info('Parent post not found', ['uri-id' => $notification['parent-uri-id']]);
-                               return $message;
+               if ($notification['type'] == Post\UserNotification::NOTIF_NONE) {
+                       if ($contact['pending']) {
+                               $msg = $l10n->t('%1$s wants to follow you');
+                       } else {
+                               $msg = $l10n->t('%1$s had started following you');
                        }
+                       $title = $contact['name'];
+                       $link = DI::baseUrl() . '/contact/' . $contact['id'];
                } else {
-                       $item = Post::selectFirst([], ['uri-id' => $notification['target-uri-id'], 'uid' => [0, $notification['uid']]]);
-                       if (empty($item)) {
-                               Logger::info('Post not found', ['uri-id' => $notification['target-uri-id']]);
+                       if (empty($notification['target-uri-id'])) {
                                return $message;
                        }
 
-                       if ($notification['vid'] == $post) {
-                               $item = Post::selectFirst([], ['uri-id' => $item['thr-parent-id'], 'uid' => [0, $notification['uid']]]);
+                       $like     = Verb::getID(Activity::LIKE);
+                       $dislike  = Verb::getID(Activity::DISLIKE);
+                       $announce = Verb::getID(Activity::ANNOUNCE);
+                       $post     = Verb::getID(Activity::POST);
+
+                       if (in_array($notification['type'], [Post\UserNotification::NOTIF_THREAD_COMMENT, Post\UserNotification::NOTIF_COMMENT_PARTICIPATION, Post\UserNotification::NOTIF_ACTIVITY_PARTICIPATION, Post\UserNotification::NOTIF_EXPLICIT_TAGGED])) {
+                               $item = Post::selectFirst([], ['uri-id' => $notification['parent-uri-id'], 'uid' => [0, $notification['uid']]], ['order' => ['uid' => true]]);
                                if (empty($item)) {
-                                       Logger::info('Thread parent post not found', ['uri-id' => $item['thr-parent-id']]);
+                                       Logger::info('Parent post not found', ['uri-id' => $notification['parent-uri-id']]);
                                        return $message;
                                }
+                       } else {
+                               $item = Post::selectFirst([], ['uri-id' => $notification['target-uri-id'], 'uid' => [0, $notification['uid']]], ['order' => ['uid' => true]]);
+                               if (empty($item)) {
+                                       Logger::info('Post not found', ['uri-id' => $notification['target-uri-id']]);
+                                       return $message;
+                               }
+
+                               if ($notification['vid'] == $post) {
+                                       $item = Post::selectFirst([], ['uri-id' => $item['thr-parent-id'], 'uid' => [0, $notification['uid']]], ['order' => ['uid' => true]]);
+                                       if (empty($item)) {
+                                               Logger::info('Thread parent post not found', ['uri-id' => $item['thr-parent-id']]);
+                                               return $message;
+                                       }
+                               }
                        }
-               }
 
-               $link = DI::baseUrl() . '/display/' . urlencode($item['guid']);
+                       if ($item['owner-id'] != $item['author-id']) {
+                               $cid = $item['owner-id'];
+                       }
+                       if (!empty($item['causer-id']) && ($item['causer-id'] != $item['author-id'])) {
+                               $cid = $item['causer-id'];
+                       }
 
-               $content = Plaintext::getPost($item, 70);
-               if (!empty($content['text'])) {
-                       $title = '"' . trim(str_replace("\n", " ", $content['text'])) . '"';
-               } else {
-                       $title = '';
-               }
+                       if (($notification['type'] == Post\UserNotification::NOTIF_SHARED) && !empty($cid)) {
+                               $causer = Contact::getById($cid, ['id', 'name', 'url']);
+                               if (empty($contact)) {
+                                       Logger::info('Causer not found', ['causer' => $cid]);
+                                       return $message;
+                               }
+                       } elseif (in_array($notification['type'], [Post\UserNotification::NOTIF_COMMENT_PARTICIPATION, Post\UserNotification::NOTIF_ACTIVITY_PARTICIPATION])) {
+                               $contact = Contact::getById($item['author-id'], ['id', 'name', 'url']);
+                               if (empty($contact)) {
+                                       Logger::info('Author not found', ['author' => $item['author-id']]);
+                                       return $message;
+                               }
+                       }
 
-               $l10n = DI::l10n()->withLang($user['language']);
+                       $link = DI::baseUrl() . '/display/' . urlencode($item['guid']);
 
-               switch ($notification['vid']) {
-                       case $like:
-                               switch ($notification['type']) {
-                                       case Post\UserNotification::NOTIF_DIRECT_COMMENT:
-                                               $msg = $l10n->t('%1$s liked your comment %2$s');
-                                               break;
-                                       case Post\UserNotification::NOTIF_DIRECT_THREAD_COMMENT:
-                                               $msg = $l10n->t('%1$s liked your post %2$s');
-                                               break;
-                                       }
-                               break;
-                       case $dislike:
-                               switch ($notification['type']) {
-                                       case Post\UserNotification::NOTIF_DIRECT_COMMENT:
-                                               $msg = $l10n->t('%1$s disliked your comment %2$s');
-                                               break;
-                                       case Post\UserNotification::NOTIF_DIRECT_THREAD_COMMENT:
-                                               $msg = $l10n->t('%1$s disliked your post %2$s');
-                                               break;
-                               }
-                               break;
-                       case $announce:
-                               switch ($notification['type']) {
-                                       case Post\UserNotification::NOTIF_DIRECT_COMMENT:
-                                               $msg = $l10n->t('%1$s shared your comment %2$s');
-                                               break;
-                                       case Post\UserNotification::NOTIF_DIRECT_THREAD_COMMENT:
-                                               $msg = $l10n->t('%1$s shared your post %2$s');
-                                               break;
+                       $content = Plaintext::getPost($item, 70);
+                       if (!empty($content['text'])) {
+                               $title = '"' . trim(str_replace("\n", " ", $content['text'])) . '"';
+                       } else {
+                               $title = '';
+                       }
+
+                       switch ($notification['vid']) {
+                               case $like:
+                                       switch ($notification['type']) {
+                                               case Post\UserNotification::NOTIF_DIRECT_COMMENT:
+                                                       $msg = $l10n->t('%1$s liked your comment %2$s');
+                                                       break;
+                                               case Post\UserNotification::NOTIF_DIRECT_THREAD_COMMENT:
+                                                       $msg = $l10n->t('%1$s liked your post %2$s');
+                                                       break;
+                                               }
+                                       break;
+                               case $dislike:
+                                       switch ($notification['type']) {
+                                               case Post\UserNotification::NOTIF_DIRECT_COMMENT:
+                                                       $msg = $l10n->t('%1$s disliked your comment %2$s');
+                                                       break;
+                                               case Post\UserNotification::NOTIF_DIRECT_THREAD_COMMENT:
+                                                       $msg = $l10n->t('%1$s disliked your post %2$s');
+                                                       break;
                                        }
-                               break;
-                       case $post:
-                               switch ($notification['type']) {
-                                       case Post\UserNotification::NOTIF_EXPLICIT_TAGGED:
-                                               $msg = $l10n->t('%1$s tagged you on %2$s');
-                                               break;
-
-                                       case Post\UserNotification::NOTIF_IMPLICIT_TAGGED:
-                                               $msg = $l10n->t('%1$s replied to you on %2$s');
-                                               break;
-
-                                       case Post\UserNotification::NOTIF_THREAD_COMMENT:
-                                               $msg = $l10n->t('%1$s commented in your thread %2$s');
-                                               break;
-
-                                       case Post\UserNotification::NOTIF_DIRECT_COMMENT:
-                                               $msg = $l10n->t('%1$s commented on your comment %2$s');
-                                               break;
-
-                                       case Post\UserNotification::NOTIF_COMMENT_PARTICIPATION:
-                                               $msg = $l10n->t('%1$s commented in the thread %2$s');
-                                               break;
-
-                                       case Post\UserNotification::NOTIF_ACTIVITY_PARTICIPATION:
-                                               // Unhandled
-                                               break;
-
-                                       case Post\UserNotification::NOTIF_DIRECT_THREAD_COMMENT:
-                                               $msg = $l10n->t('%1$s commented on your thread %2$s');
-                                               break;
-
-                                       case Post\UserNotification::NOTIF_SHARED:
-                                               if ($title != '') {
-                                                       $msg = $l10n->t('%1$s shared the post %2$s');
-                                               } else {
-                                                       $msg = $l10n->t('%1$s shared a post');
+                                       break;
+                               case $announce:
+                                       switch ($notification['type']) {
+                                               case Post\UserNotification::NOTIF_DIRECT_COMMENT:
+                                                       $msg = $l10n->t('%1$s shared your comment %2$s');
+                                                       break;
+                                               case Post\UserNotification::NOTIF_DIRECT_THREAD_COMMENT:
+                                                       $msg = $l10n->t('%1$s shared your post %2$s');
+                                                       break;
                                                }
-                                               break;
-                               }
-                               break;
+                                       break;
+                               case $post:
+                                       switch ($notification['type']) {
+                                               case Post\UserNotification::NOTIF_EXPLICIT_TAGGED:
+                                                       $msg = $l10n->t('%1$s tagged you on %2$s');
+                                                       break;
+
+                                               case Post\UserNotification::NOTIF_IMPLICIT_TAGGED:
+                                                       $msg = $l10n->t('%1$s replied to you on %2$s');
+                                                       break;
+
+                                               case Post\UserNotification::NOTIF_THREAD_COMMENT:
+                                                       $msg = $l10n->t('%1$s commented in your thread %2$s');
+                                                       break;
+
+                                               case Post\UserNotification::NOTIF_DIRECT_COMMENT:
+                                                       $msg = $l10n->t('%1$s commented on your comment %2$s');
+                                                       break;
+
+                                               case Post\UserNotification::NOTIF_COMMENT_PARTICIPATION:
+                                               case Post\UserNotification::NOTIF_ACTIVITY_PARTICIPATION:
+                                                       if (($causer['id'] == $contact['id']) && ($title != '')) {
+                                                               $msg = $l10n->t('%1$s commented in their thread %2$s');
+                                                       } elseif ($causer['id'] == $contact['id']) {
+                                                               $msg = $l10n->t('%1$s commented in their thread');
+                                                       } elseif ($title != '') {
+                                                               $msg = $l10n->t('%1$s commented in the thread %2$s from %3$s');
+                                                       } else {
+                                                               $msg = $l10n->t('%1$s commented in the thread from %3$s');
+                                                       }
+                                                       break;
+
+                                               case Post\UserNotification::NOTIF_DIRECT_THREAD_COMMENT:
+                                                       $msg = $l10n->t('%1$s commented on your thread %2$s');
+                                                       break;
+
+                                               case Post\UserNotification::NOTIF_SHARED:
+                                                       if (($causer['id'] != $contact['id']) && ($title != '')) {
+                                                               $msg = $l10n->t('%1$s shared the post %2$s from %3$s');
+                                                       } elseif ($causer['id'] != $contact['id']) {
+                                                               $msg = $l10n->t('%1$s shared a post from %3$s');
+                                                       } elseif ($title != '') {
+                                                               $msg = $l10n->t('%1$s shared the post %2$s');
+                                                       } else {
+                                                               $msg = $l10n->t('%1$s shared a post');
+                                                       }
+                                                       break;
+                                       }
+                                       break;
+                       }
                }
 
                if (!empty($msg)) {
                        // Name of the notification's causer
-                       $message['causer'] = $contact['name'];
+                       $message['causer'] = $causer['name'];
                        // Format for the "ping" mechanism
-                       $message['notification'] = sprintf($msg, '{0}', $title);
+                       $message['notification'] = sprintf($msg, '{0}', $title, $contact['name']);
                        // Plain text for the web push api
-                       $message['plain']        = sprintf($msg, $contact['name'], $title);
-                       // Rich text for other purposes 
+                       $message['plain']        = sprintf($msg, $causer['name'], $title, $contact['name']);
+                       // Rich text for other purposes
                        $message['rich']         = sprintf($msg,
-                               '[url=' . $contact['url'] . ']' . $contact['name'] . '[/url]',
-                               '[url=' . $link . ']' . $title . '[/url]');
+                               '[url=' . $causer['url'] . ']' . $causer['name'] . '[/url]',
+                               '[url=' . $link . ']' . $title . '[/url]',
+                               '[url=' . $contact['url'] . ']' . $contact['name'] . '[/url]');
                }
 
                return $message;