]> git.mxchange.org Git - quix0rs-gnu-social.git/blobdiff - classes/Notice.php
Modernize File_to_post to use Managed_DataObject functions
[quix0rs-gnu-social.git] / classes / Notice.php
index c631c1fcc6768dd690bb449c8115096c1806f644..9246c26919ac13897a22e261e5cc3b044f2fb3ad 100644 (file)
@@ -84,7 +84,7 @@ class Notice extends Managed_DataObject
                 'id' => array('type' => 'serial', 'not null' => true, 'description' => 'unique identifier'),
                 'profile_id' => array('type' => 'int', 'not null' => true, 'description' => 'who made the update'),
                 'uri' => array('type' => 'varchar', 'length' => 191, 'description' => 'universally unique identifier, usually a tag URI'),
-                'content' => array('type' => 'text', 'description' => 'update content', 'collate' => 'utf8_general_ci'),
+                'content' => array('type' => 'text', 'description' => 'update content', 'collate' => 'utf8mb4_general_ci'),
                 'rendered' => array('type' => 'text', 'description' => 'HTML version of the content'),
                 'url' => array('type' => 'varchar', 'length' => 191, 'description' => 'URL of any attachment (image, video, bookmark, whatever)'),
                 'created' => array('type' => 'datetime', 'not null' => true, 'description' => 'date this record was created'),
@@ -892,6 +892,12 @@ class Notice extends Managed_DataObject
                 $stored->insert();    // throws exception on error
                 $orig = clone($stored); // for updating later in this try clause
 
+                $object = null;
+                Event::handle('StoreActivityObject', array($act, $stored, $options, &$object));
+                if (empty($object)) {
+                    throw new ServerException('Unsuccessful call to StoreActivityObject '.$stored->uri . ': '.$act->asString());
+                }
+
                 // If it's not part of a conversation, it's
                 // the beginning of a new conversation.
                 if (empty($stored->conversation)) {
@@ -900,12 +906,6 @@ class Notice extends Managed_DataObject
                     $stored->conversation = $conv->id;
                 }
 
-                $object = null;
-                Event::handle('StoreActivityObject', array($act, $stored, $options, &$object));
-                if (empty($object)) {
-                    throw new ServerException('No object from StoreActivityObject '.$stored->uri . ': '.$act->asString());
-                }
-                $stored->object_type = ActivityUtils::resolveUri($object->getObjectType(), true);
                 $stored->update($orig);
             } catch (Exception $e) {
                 if (empty($stored->id)) {
@@ -1099,7 +1099,7 @@ class Notice extends Managed_DataObject
      */
     function saveUrls() {
         if (common_config('attachments', 'process_links')) {
-            common_replace_urls_callback($this->content, array($this, 'saveUrl'), $this->id);
+            common_replace_urls_callback($this->content, array($this, 'saveUrl'), $this);
         }
     }
 
@@ -1116,11 +1116,7 @@ class Notice extends Managed_DataObject
         if (common_config('attachments', 'process_links')) {
             // @fixme validation?
             foreach (array_unique($urls) as $url) {
-                try {
-                    File::processNew($url, $this->id);
-                } catch (ServerException $e) {
-                    // Could not save URL. Log it?
-                }
+                $this->saveUrl($url, $this);
             }
         }
     }
@@ -1128,9 +1124,9 @@ class Notice extends Managed_DataObject
     /**
      * @private callback
      */
-    function saveUrl($url, $notice_id) {
+    function saveUrl($url, Notice $notice) {
         try {
-            File::processNew($url, $notice_id);
+            File::processNew($url, $notice);
         } catch (ServerException $e) {
             // Could not save URL. Log it?
         }
@@ -1301,7 +1297,7 @@ class Notice extends Managed_DataObject
                     $last = $parent;
                     continue;
                 }
-            } catch (Exception $e) {
+            } catch (NoParentNoticeException $e) {
                 // Latest notice has no parent
             }
             // No parent, or parent out of scope
@@ -1607,7 +1603,7 @@ class Notice extends Managed_DataObject
             $this->saveReply($parentauthor->id);
             $replied[$parentauthor->id] = 1;
             self::blow('reply:stream:%d', $parentauthor->id);
-        } catch (Exception $e) {
+        } catch (NoParentNoticeException $e) {
             // Not a reply, since it has no parent!
         }
 
@@ -1624,8 +1620,7 @@ class Notice extends Managed_DataObject
             foreach ($mention['mentioned'] as $mentioned) {
 
                 // skip if they're already covered
-
-                if (!empty($replied[$mentioned->id])) {
+                if (array_key_exists($mentioned->id, $replied)) {
                     continue;
                 }
 
@@ -1808,17 +1803,7 @@ class Notice extends Managed_DataObject
 
             $act->verb = $this->verb;
 
-            if ($this->repeat_of) {
-                $repeated = Notice::getKV('id', $this->repeat_of);
-                if ($repeated instanceof Notice) {
-                    // TRANS: A repeat activity's title. %1$s is repeater's nickname
-                    //        and %2$s is the repeated user's nickname.
-                    $act->title = sprintf(_('%1$s repeated a notice by %2$s'),
-                                          $this->getProfile()->getNickname(),
-                                          $repeated->getProfile()->getNickname());
-                    $act->objects[] = $repeated->asActivity($scoped);
-                }
-            } else {
+            if (!$this->repeat_of) {
                 $act->objects[] = $this->asActivityObject();
             }
 
@@ -1841,9 +1826,9 @@ class Notice extends Managed_DataObject
             $attachments = $this->attachments();
 
             foreach ($attachments as $attachment) {
-                // Save local attachments
+                // Include local attachments in Activity
                 if (!empty($attachment->filename)) {
-                    $act->attachments[] = ActivityObject::fromFile($attachment);
+                    $act->enclosures[] = $attachment->getEnclosure();
                 }
             }
 
@@ -1852,8 +1837,8 @@ class Notice extends Managed_DataObject
             try {
                 $reply = $this->getParent();
                 $ctx->replyToID  = $reply->getUri();
-                $ctx->replyToUrl = $reply->getUrl();
-            } catch (Exception $e) {
+                $ctx->replyToUrl = $reply->getUrl(true);    // true for fallback to local URL, less messy
+            } catch (NoParentNoticeException $e) {
                 // This is not a reply to something
             }
 
@@ -2763,13 +2748,10 @@ class Notice extends Managed_DataObject
 
     public function getParent()
     {
-        $parent = Notice::getKV('id', $this->reply_to);
-
-        if (!$parent instanceof Notice) {
-            throw new ServerException('Notice has no parent');
+        if (empty($this->reply_to)) {
+            throw new NoParentNoticeException($this);
         }
-
-        return $parent;
+        return self::getByID($this->reply_to);
     }
 
     /**
@@ -2901,31 +2883,4 @@ class Notice extends Managed_DataObject
             $notice->_setReplies($ids);
         }
     }
-
-    protected $_repeats = array();
-
-    function getRepeats()
-    {
-        if (isset($this->_repeats[$this->id])) {
-            return $this->_repeats[$this->id];
-        }
-        $repeatMap = Notice::listGet('repeat_of', array($this->id));
-        $this->_repeats[$this->id] = $repeatMap[$this->id];
-        return $this->_repeats[$this->id];
-    }
-
-    function _setRepeats($repeats)
-    {
-        $this->_repeats[$this->id] = $repeats;
-    }
-
-    static function fillRepeats(&$notices)
-    {
-        $ids = self::_idsOf($notices);
-        $repeatMap = Notice::listGet('repeat_of', $ids);
-        foreach ($notices as $notice) {
-               $repeats = $repeatMap[$notice->id];
-            $notice->_setRepeats($repeats);
-        }
-    }
 }