]> git.mxchange.org Git - quix0rs-gnu-social.git/blobdiff - classes/Notice.php
Avoid having to check for notices without rendered copies in upgrade.php
[quix0rs-gnu-social.git] / classes / Notice.php
index ebbd9e0462b7e64cb416c1a1eda7c8fd8076885c..25e8da4aaf72fa23e89fdf4c3e6612147310978b 100644 (file)
@@ -90,7 +90,7 @@ class Notice extends Managed_DataObject
                 'source' => array('type' => 'varchar', 'length' => 32, 'description' => 'source of comment, like "web", "im", or "clientname"'),
                 'conversation' => array('type' => 'int', 'description' => 'id of root notice in this conversation'),
                 'repeat_of' => array('type' => 'int', 'description' => 'notice this is a repeat of'),
-                'object_type' => array('type' => 'varchar', 'length' => 191, 'description' => 'URI representing activity streams object type', 'default' => 'http://activitystrea.ms/schema/1.0/note'),
+                'object_type' => array('type' => 'varchar', 'length' => 191, 'description' => 'URI representing activity streams object type', 'default' => null),
                 'verb' => array('type' => 'varchar', 'length' => 191, 'description' => 'URI representing activity streams verb', 'default' => 'http://activitystrea.ms/schema/1.0/post'),
                 'scope' => array('type' => 'int',
                                  'description' => 'bit map for distribution scope; 0 = everywhere; 1 = this server only; 2 = addressees; 4 = followers; null = default'),
@@ -259,6 +259,14 @@ class Notice extends Managed_DataObject
 
     public function getRendered()
     {
+        if (is_null($this->rendered) || $this->rendered === '') {
+            // update to include rendered content on-the-fly, so we don't have to have a fix-up script in upgrade.php
+            $orig = clone($this);
+            $this->rendered = common_render_content($this->getContent(),
+                                                    $this->getProfile(),
+                                                    $this->hasParent() ? $this->getParent() : null);
+            $this->update($orig);
+        }
         return $this->rendered;
     }
 
@@ -592,21 +600,25 @@ class Notice extends Managed_DataObject
                 $conv = Conversation::getKV('uri', $options['conversation']);
                 if ($conv instanceof Conversation) {
                     common_debug('Conversation stitched together from (probably) a reply to unknown remote user. Activity creation time ('.$notice->created.') should maybe be compared to conversation creation time ('.$conv->created.').');
-                    $notice->conversation = $conv->id;
                 } else {
-                    // Conversation URI was not found, so we must create it. But we can't create it
-                    // until we have a Notice ID because of the database layout...
-                    // $options['conversation'] will be used later after the $notice->insert();
-                    common_debug('Conversation URI not found, so we have to create it after inserting this Notice: '.$options['conversation']);
+                    // Conversation entry with specified URI was not found, so we must create it.
+                    common_debug('Conversation URI not found, so we will create it with the URI given in the options to Notice::saveNew: '.$options['conversation']);
+                    // The insert in Conversation::create throws exception on failure
+                    $conv = Conversation::create($options['conversation'], $notice->created);
                 }
-            } else {
-                // If we're not using the attached conversation URI let's remove it
-                // so we don't mistake ourselves later, when creating our own Conversation.
-                // This implies that the notice knows which conversation it belongs to.
-                $options['conversation'] = null;
+                $notice->conversation = $conv->getID();
+                unset($conv);
             }
         }
 
+        // If it's not part of a conversation, it's the beginning of a new conversation.
+        if (empty($notice->conversation)) {
+            $conv = Conversation::create();
+            $notice->conversation = $conv->getID();
+            unset($conv);
+        }
+
+
         $notloc = new Notice_location();
         if (!empty($lat) && !empty($lon)) {
             $notloc->lat = $lat;
@@ -621,7 +633,9 @@ class Notice extends Managed_DataObject
         if (!empty($rendered)) {
             $notice->rendered = $rendered;
         } else {
-            $notice->rendered = common_render_content($final, $notice);
+            $notice->rendered = common_render_content($final,
+                                                      $notice->getProfile(),
+                                                      $notice->hasParent() ? $notice->getParent() : null);
         }
 
         if (empty($verb)) {
@@ -660,17 +674,6 @@ class Notice extends Managed_DataObject
                     $notloc->notice_id = $notice->getID();
                     $notloc->insert();  // store the notice location if it had any information
                 }
-
-                // If it's not part of a conversation, it's
-                // the beginning of a new conversation.
-                if (empty($notice->conversation)) {
-                    $orig = clone($notice);
-                    // $act->context->conversation will be null if it was not provided
-
-                    $conv = Conversation::create($notice, $options['conversation']);
-                    $notice->conversation = $conv->id;
-                    $notice->update($orig);
-                }
             } catch (Exception $e) {
                 // Let's test if we managed initial insert, which would imply
                 // failing on some update-part (check 'insert()'). Delete if
@@ -682,33 +685,33 @@ class Notice extends Managed_DataObject
             }
         }
 
-        // Clear the cache for subscribed users, so they'll update at next request
-        // XXX: someone clever could prepend instead of clearing the cache
-
-        // Save per-notice metadata...
-
-        if (isset($replies)) {
-            $notice->saveKnownReplies($replies);
-        } else {
-            $notice->saveReplies();
-        }
+        // Only save 'attention' and metadata stuff (URLs, tags...) stuff if
+        // the activityverb is a POST (since stuff like repeat, favorite etc.
+        // reasonably handle notifications themselves.
+        if (ActivityUtils::compareVerbs($notice->verb, array(ActivityVerb::POST))) {
+            if (isset($replies)) {
+                $notice->saveKnownReplies($replies);
+            } else {
+                $notice->saveReplies();
+            }
 
-        if (isset($tags)) {
-            $notice->saveKnownTags($tags);
-        } else {
-            $notice->saveTags();
-        }
+            if (isset($tags)) {
+                $notice->saveKnownTags($tags);
+            } else {
+                $notice->saveTags();
+            }
 
-        // Note: groups may save tags, so must be run after tags are saved
-        // to avoid errors on duplicates.
-        // Note: groups should always be set.
+            // Note: groups may save tags, so must be run after tags are saved
+            // to avoid errors on duplicates.
+            // Note: groups should always be set.
 
-        $notice->saveKnownGroups($groups);
+            $notice->saveKnownGroups($groups);
 
-        if (isset($urls)) {
-            $notice->saveKnownUrls($urls);
-        } else {
-            $notice->saveUrls();
+            if (isset($urls)) {
+                $notice->saveKnownUrls($urls);
+            } else {
+                $notice->saveUrls();
+            }
         }
 
         if ($distribute) {
@@ -736,6 +739,7 @@ class Notice extends Managed_DataObject
         }
 
         // Get ActivityObject properties
+        $actobj = null;
         if (!empty($act->id)) {
             // implied object
             $options['uri'] = $act->id;
@@ -780,7 +784,9 @@ class Notice extends Managed_DataObject
             $stored->uri = $uri;
             if ($stored->find()) {
                 common_debug('cannot create duplicate Notice URI: '.$stored->uri);
-                throw new Exception('Notice URI already exists');
+                // I _assume_ saving a Notice with a colliding URI means we're really trying to
+                // save the same notice again...
+                throw new AlreadyFulfilledException('Notice URI already exists');
             }
         }
 
@@ -811,10 +817,14 @@ class Notice extends Managed_DataObject
         $stored->url = $url;
         $stored->verb = $act->verb;
 
-        // Use the local user's shortening preferences, if applicable.
-        $stored->rendered = $actor->isLocal()
-                                ? $actor->shortenLinks($act->content)
-                                : $act->content;
+        // Notice content. We trust local users to provide HTML we like, but of course not remote users.
+        // FIXME: What about local users importing feeds? Mirror functions must filter out bad HTML first...
+        $content = $act->content ?: $act->summary;
+        if (is_null($content) && !is_null($actobj)) {
+            $content = $actobj->content ?: $actobj->summary;
+        }
+        $stored->rendered = $actor->isLocal() ? $content : common_purify($content);
+        // yeah, just don't use getRendered() here since it's not inserted yet ;)
         $stored->content = common_strip_html($stored->rendered);
 
         // Maybe a missing act-time should be fatal if the actor is not local?
@@ -845,7 +855,6 @@ class Notice extends Managed_DataObject
             // If the original is private to a group, and notice has no group specified,
             // make it to the same group(s)
             if (empty($groups) && ($reply->scope & Notice::GROUP_SCOPE)) {
-                $groups = array();
                 $replyGroups = $reply->getGroups();
                 foreach ($replyGroups as $group) {
                     if ($actor->isMember($group)) {
@@ -865,16 +874,24 @@ class Notice extends Managed_DataObject
                 $conv = Conversation::getKV('uri', $act->context->conversation);
                 if ($conv instanceof Conversation) {
                     common_debug('Conversation stitched together from (probably) a reply activity to unknown remote user. Activity creation time ('.$stored->created.') should maybe be compared to conversation creation time ('.$conv->created.').');
-                    $stored->conversation = $conv->getID();
                 } else {
-                    // Conversation URI was not found, so we must create it. But we can't create it
-                    // until we have a Notice ID because of the database layout...
-                    // $options['conversation'] will be used later after the $stored->insert();
-                    common_debug('Conversation URI from activity context not found, so we have to create it after inserting this Notice: '.$act->context->conversation);
+                    // Conversation entry with specified URI was not found, so we must create it.
+                    common_debug('Conversation URI not found, so we will create it with the URI given in the context of the activity: '.$act->context->conversation);
+                    // The insert in Conversation::create throws exception on failure
+                    $conv = Conversation::create($act->context->conversation, $stored->created);
                 }
+                $stored->conversation = $conv->getID();
+                unset($conv);
             }
         }
 
+        // If it's not part of a conversation, it's the beginning of a new conversation.
+        if (empty($stored->conversation)) {
+            $conv = Conversation::create();
+            $stored->conversation = $conv->getID();
+            unset($conv);
+        }
+
         $notloc = null;
         if ($act->context instanceof ActivityContext) {
             if ($act->context->location instanceof Location) {
@@ -932,15 +949,7 @@ class Notice extends Managed_DataObject
                     throw new ServerException('Unsuccessful call to StoreActivityObject '.$stored->getUri() . ': '.$act->asString());
                 }
 
-                // If it's not part of a conversation, it's the beginning
-                // of a new one (or belongs to a previously unknown URI).
-                if (empty($stored->conversation)) {
-                    // $act->context->conversation will be null if it was not provided
-                    common_debug('Creating a new conversation for stored notice ID='.$stored->getID().' with URI: '.$act->context->conversation);
-                    $conv = Conversation::create($stored, $act->context->conversation);
-                    $stored->conversation = $conv->getID();
-                }
-
+                // If something changed in the Notice during StoreActivityObject
                 $stored->update($orig);
             } catch (Exception $e) {
                 if (empty($stored->id)) {
@@ -958,34 +967,39 @@ class Notice extends Managed_DataObject
 
         // Save per-notice metadata...
         $mentions = array();
-        $groups   = array();
+        $group_ids   = array();
 
         // This event lets plugins filter out non-local recipients (attentions we don't care about)
         // Used primarily for OStatus (and if we don't federate, all attentions would be local anyway)
-        Event::handle('GetLocalAttentions', array($actor, $act->context->attention, &$mentions, &$groups));
+        Event::handle('GetLocalAttentions', array($actor, $act->context->attention, &$mentions, &$group_ids));
 
-        if (!empty($mentions)) {
-            $stored->saveKnownReplies($mentions);
-        } else {
-            $stored->saveReplies();
-        }
+        // Only save 'attention' and metadata stuff (URLs, tags...) stuff if
+        // the activityverb is a POST (since stuff like repeat, favorite etc.
+        // reasonably handle notifications themselves.
+        if (ActivityUtils::compareVerbs($stored->verb, array(ActivityVerb::POST))) {
+            if (!empty($mentions)) {
+                $stored->saveKnownReplies($mentions);
+            } else {
+                $stored->saveReplies();
+            }
 
-        if (!empty($tags)) {
-            $stored->saveKnownTags($tags);
-        } else {
-            $stored->saveTags();
-        }
+            if (!empty($tags)) {
+                $stored->saveKnownTags($tags);
+            } else {
+                $stored->saveTags();
+            }
 
-        // Note: groups may save tags, so must be run after tags are saved
-        // to avoid errors on duplicates.
-        // Note: groups should always be set.
+            // Note: groups may save tags, so must be run after tags are saved
+            // to avoid errors on duplicates.
+            // Note: groups should always be set.
 
-        $stored->saveKnownGroups($groups);
+            $stored->saveKnownGroups($group_ids);
 
-        if (!empty($urls)) {
-            $stored->saveKnownUrls($urls);
-        } else {
-            $stored->saveUrls();
+            if (!empty($urls)) {
+                $stored->saveKnownUrls($urls);
+            } else {
+                $stored->saveUrls();
+            }
         }
 
         if ($distribute) {
@@ -1035,8 +1049,10 @@ class Notice extends Managed_DataObject
             $this->blowStream('networkpublic');
         }
 
-        self::blow('notice:list-ids:conversation:%s', $this->conversation);
-        self::blow('conversation:notice_count:%d', $this->conversation);
+        if ($this->conversation) {
+            self::blow('notice:list-ids:conversation:%s', $this->conversation);
+            self::blow('conversation:notice_count:%d', $this->conversation);
+        }
 
         if ($this->isRepeat()) {
             // XXX: we should probably only use one of these
@@ -1505,13 +1521,8 @@ class Notice extends Managed_DataObject
      *        best with generalizations on user_group to support
      *        remote groups better.
      */
-    function saveKnownGroups($group_ids)
+    function saveKnownGroups(array $group_ids)
     {
-        if (!is_array($group_ids)) {
-            // TRANS: Server exception thrown when no array is provided to the method saveKnownGroups().
-            throw new ServerException(_('Bad type provided to saveKnownGroups.'));
-        }
-
         $groups = array();
         foreach (array_unique($group_ids) as $id) {
             $group = User_group::getKV('id', $id);
@@ -1587,7 +1598,7 @@ class Notice extends Managed_DataObject
             return;
         }
 
-        $sender = Profile::getKV($this->profile_id);
+        $sender = $this->getProfile();
 
         foreach (array_unique($uris) as $uri) {
             try {
@@ -1602,11 +1613,9 @@ class Notice extends Managed_DataObject
                 continue;
             }
 
-            $this->saveReply($profile->id);
-            self::blow('reply:stream:%d', $profile->id);
+            $this->saveReply($profile->getID());
+            self::blow('reply:stream:%d', $profile->getID());
         }
-
-        return;
     }
 
     /**
@@ -1621,12 +1630,6 @@ class Notice extends Managed_DataObject
 
     function saveReplies()
     {
-        // Don't save reply data for repeats
-
-        if ($this->isRepeat()) {
-            return array();
-        }
-
         $sender = $this->getProfile();
 
         $replied = array();
@@ -1635,19 +1638,21 @@ class Notice extends Managed_DataObject
         try {
             $parent = $this->getParent();
             $parentauthor = $parent->getProfile();
-            $this->saveReply($parentauthor->id);
-            $replied[$parentauthor->id] = 1;
-            self::blow('reply:stream:%d', $parentauthor->id);
+            $this->saveReply($parentauthor->getID());
+            $replied[$parentauthor->getID()] = 1;
+            self::blow('reply:stream:%d', $parentauthor->getID());
         } catch (NoParentNoticeException $e) {
             // Not a reply, since it has no parent!
+            $parent = null;
         } catch (NoResultException $e) {
             // Parent notice was probably deleted
+            $parent = null;
         }
 
         // @todo ideally this parser information would only
         // be calculated once.
 
-        $mentions = common_find_mentions($this->content, $this);
+        $mentions = common_find_mentions($this->content, $sender, $parent);
 
         // store replied only for first @ (what user/notice what the reply directed,
         // we assume first @ is it)
@@ -1718,9 +1723,9 @@ class Notice extends Managed_DataObject
      *
      * @return array of Profiles
      */
-    function getReplyProfiles()
+    function getAttentionProfiles()
     {
-        $ids = $this->getReplies();
+        $ids = array_unique(array_merge($this->getReplies(), $this->getGroupProfileIDs()));
 
         $profiles = Profile::multiGet('id', $ids);
 
@@ -1736,7 +1741,6 @@ class Notice extends Managed_DataObject
     function sendReplyNotifications()
     {
         // Don't send reply notifications for repeats
-
         if ($this->isRepeat()) {
             return array();
         }
@@ -1746,15 +1750,34 @@ class Notice extends Managed_DataObject
             require_once INSTALLDIR.'/lib/mail.php';
 
             foreach ($recipientIds as $recipientId) {
-                $user = User::getKV('id', $recipientId);
-                if ($user instanceof User) {
+                try {
+                    $user = User::getByID($recipientId);
                     mail_notify_attn($user, $this);
+                } catch (NoResultException $e) {
+                    // No such user
                 }
             }
             Event::handle('EndNotifyMentioned', array($this, $recipientIds));
         }
     }
 
+    /**
+     * Pull list of Profile IDs of groups this notice addresses.
+     *
+     * @return array of Group _profile_ IDs
+     */
+
+    function getGroupProfileIDs()
+    {
+        $ids = array();
+
+               foreach ($this->getGroups() as $group) {
+                   $ids[] = $group->profile_id;
+               }
+
+        return $ids;
+    }
+
     /**
      * Pull list of groups this notice needs to be delivered to,
      * as previously recorded by saveKnownGroups().
@@ -1821,7 +1844,7 @@ class Notice extends Managed_DataObject
                 // The notice is probably a share or similar, which don't
                 // have a representational URL of their own.
             }
-            $act->content = common_xml_safe_str($this->rendered);
+            $act->content = common_xml_safe_str($this->getRendered());
 
             $profile = $this->getProfile();
 
@@ -2053,8 +2076,9 @@ class Notice extends Managed_DataObject
         if (Event::handle('StartActivityObjectFromNotice', array($this, &$object))) {
             $object->type    = $this->object_type ?: ActivityObject::NOTE;
             $object->id      = $this->getUri();
+            //FIXME: = $object->title ?: sprintf(... because we might get a title from StartActivityObjectFromNotice
             $object->title   = sprintf('New %1$s by %2$s', ActivityObject::canonicalType($object->type), $this->getProfile()->getNickname());
-            $object->content = $this->rendered;
+            $object->content = $this->getRendered();
             $object->link    = $this->getUrl();
 
             $object->extra[] = array('status_net', array('notice_id' => $this->id));
@@ -2773,24 +2797,33 @@ class Notice extends Managed_DataObject
         return false;
     }
 
+    public function hasParent()
+    {
+        try {
+            $this->getParent();
+        } catch (NoParentNoticeException $e) {
+            return false;
+        }
+        return true;
+    }
+
     public function getParent()
     {
-       $reply_to_id = null;
+        $reply_to_id = null;
 
         if (empty($this->reply_to)) {
             throw new NoParentNoticeException($this);
         }
 
-       // The reply_to ID in the table Notice could exist with a number
-       // however, the replied to notice might not exist in the database.
-       // Thus we need to catch the exception and throw the NoParentNoticeException else 
-       // the timeline will not display correctly.
-       try {
-               $reply_to_id = self::getByID($this->reply_to);
-       } catch(Exception $e){
-               throw new NoParentNoticeException($this);
-       }
-
+        // The reply_to ID in the table Notice could exist with a number
+        // however, the replied to notice might not exist in the database.
+        // Thus we need to catch the exception and throw the NoParentNoticeException else
+        // the timeline will not display correctly.
+        try {
+            $reply_to_id = self::getByID($this->reply_to);
+        } catch(Exception $e){
+            throw new NoParentNoticeException($this);
+        }
 
         return $reply_to_id;
     }