]> git.mxchange.org Git - quix0rs-gnu-social.git/blobdiff - classes/Notice.php
Use canonical object type in Notice title as ActivityObject
[quix0rs-gnu-social.git] / classes / Notice.php
index 258652639af3836997bb550e64b538d1539c3def..55af2d1b6ed90d8eb9477d11dd2bbceadbcdb2a1 100644 (file)
@@ -577,7 +577,7 @@ class Notice extends Managed_DataObject
                 }
 
                 // If it's a repeat, the reply_to should be to the original
-                if (!empty($reply->repeat_of)) {
+                if ($reply->isRepeat()) {
                     $notice->reply_to = $reply->repeat_of;
                 } else {
                     $notice->reply_to = $reply->id;
@@ -619,7 +619,7 @@ class Notice extends Managed_DataObject
         }
 
         if (empty($verb)) {
-            if (!empty($notice->repeat_of)) {
+            if ($notice->isRepeat()) {
                 $notice->verb        = ActivityVerb::SHARE;
                 $notice->object_type = ActivityObject::ACTIVITY;
             } else {
@@ -647,54 +647,21 @@ class Notice extends Managed_DataObject
 
             // XXX: some of these functions write to the DB
 
-            $id = $notice->insert();
-
-            if (!$id) {
-                common_log_db_error($notice, 'INSERT', __FILE__);
-                // TRANS: Server exception thrown when a notice cannot be saved.
-                throw new ServerException(_('Problem saving notice.'));
-            }
-
-            // Update ID-dependent columns: URI, conversation
-
-            $orig = clone($notice);
-
-            $changed = false;
-
-            // We can only get here if it's a local notice, since remote notices
-            // should've bailed out earlier due to lacking a URI.
-            if (empty($notice->uri)) {
-                $notice->uri = sprintf('%s%s=%d:%s=%s',
-                                    TagURI::mint(),
-                                    'noticeId', $notice->id,
-                                    'objectType', $notice->get_object_type(true));
-                $changed = true;
-            }
-
-            // If it's not part of a conversation, it's
-            // the beginning of a new conversation.
-
-            if (empty($notice->conversation)) {
-                $conv = Conversation::create($notice);
-                $notice->conversation = $conv->id;
-                $changed = true;
-            }
-
-            if ($changed) {
-                if ($notice->update($orig) === false) {
-                    common_log_db_error($notice, 'UPDATE', __FILE__);
-                    // TRANS: Server exception thrown when a notice cannot be updated.
-                    throw new ServerException(_('Problem saving notice.'));
+            try {
+                $notice->insert();  // throws exception on failure
+            } catch (Exception $e) {
+                // Let's test if we managed initial insert, which would imply
+                // failing on some update-part (check 'insert()'). Delete if
+                // something had been stored to the database.
+                if (!empty($notice->id)) {
+                    $notice->delete();
                 }
             }
-
         }
 
         // Clear the cache for subscribed users, so they'll update at next request
         // XXX: someone clever could prepend instead of clearing the cache
 
-        $notice->blowOnInsert();
-
         // Save per-notice metadata...
 
         if (isset($replies)) {
@@ -905,6 +872,9 @@ class Notice extends Managed_DataObject
                 throw $e;
             }
         }
+        if (!$stored instanceof Notice) {
+            throw new ServerException('StartNoticeSave did not give back a Notice');
+        }
 
         // Save per-notice metadata...
         $mentions = array();
@@ -987,7 +957,7 @@ class Notice extends Managed_DataObject
         self::blow('notice:list-ids:conversation:%s', $this->conversation);
         self::blow('conversation:notice_count:%d', $this->conversation);
 
-        if (!empty($this->repeat_of)) {
+        if ($this->isRepeat()) {
             // XXX: we should probably only use one of these
             $this->blowStream('notice:repeats:%d', $this->repeat_of);
             self::blow('notice:list-ids:repeat_of:%d', $this->repeat_of);
@@ -1363,7 +1333,7 @@ class Notice extends Managed_DataObject
             // Exclude any deleted, non-local, or blocking recipients.
             $profile = $this->getProfile();
             $originalProfile = null;
-            if ($this->repeat_of) {
+            if ($this->isRepeat()) {
                 // Check blocks against the original notice's poster as well.
                 $original = Notice::getKV('id', $this->repeat_of);
                 if ($original instanceof Notice) {
@@ -1577,7 +1547,7 @@ class Notice extends Managed_DataObject
     {
         // Don't save reply data for repeats
 
-        if (!empty($this->repeat_of)) {
+        if ($this->isRepeat()) {
             return array();
         }
 
@@ -1700,17 +1670,19 @@ class Notice extends Managed_DataObject
     {
         // Don't send reply notifications for repeats
 
-        if (!empty($this->repeat_of)) {
+        if ($this->isRepeat()) {
             return array();
         }
 
         $recipientIds = $this->getReplies();
-
-        foreach ($recipientIds as $recipientId) {
-            $user = User::getKV('id', $recipientId);
-            if ($user instanceof User) {
-                mail_notify_attn($user, $this);
+        if (Event::handle('StartNotifyMentioned', array($this, &$recipientIds))) {
+            foreach ($recipientIds as $recipientId) {
+                $user = User::getKV('id', $recipientId);
+                if ($user instanceof User) {
+                    mail_notify_attn($user, $this);
+                }
             }
+            Event::handle('EndNotifyMentioned', array($this, $recipientIds));
         }
     }
 
@@ -1798,7 +1770,7 @@ class Notice extends Managed_DataObject
                     $act->objects[] = $repeated->asActivity($cur);
                 }
             } else {
-                $act->objects[] = ActivityObject::fromNotice($this);
+                $act->objects[] = $this->asActivityObject();
             }
 
             // XXX: should this be handled by default processing for object entry?
@@ -2005,10 +1977,29 @@ class Notice extends Managed_DataObject
 
     function asActivityNoun($element)
     {
-        $noun = ActivityObject::fromNotice($this);
+        $noun = $this->asActivityObject();
         return $noun->asString('activity:' . $element);
     }
 
+    public function asActivityObject()
+    {
+        $object = new ActivityObject();
+
+        if (Event::handle('StartActivityObjectFromNotice', array($this, &$object))) {
+            $object->type    = $this->object_type ?: ActivityObject::NOTE;
+            $object->id      = $this->getUri();
+            $object->title   = sprintf('New %1$s by %2$s', ActivityObject::canonicalType($object->type), $this->getProfile()->getNickname());
+            $object->content = $this->rendered;
+            $object->link    = $this->getUrl();
+
+            $object->extra[] = array('status_net', array('notice_id' => $this->id));
+
+            Event::handle('EndActivityObjectFromNotice', array($this, &$object));
+        }
+
+        return $object;
+    }
+
     /**
      * Determine which notice, if any, a new notice is in reply to.
      *
@@ -2335,20 +2326,55 @@ class Notice extends Managed_DataObject
     {
         $result = parent::insert();
 
-        if ($result !== false) {
-            // Profile::hasRepeated() abuses pkeyGet(), so we
-            // have to clear manually
-            if (!empty($this->repeat_of)) {
-                $c = self::memcache();
-                if (!empty($c)) {
-                    $ck = self::multicacheKey('Notice',
-                                              array('profile_id' => $this->profile_id,
-                                                    'repeat_of' => $this->repeat_of));
-                    $c->delete($ck);
-                }
+        if ($result === false) {
+            common_log_db_error($this, 'INSERT', __FILE__);
+            // TRANS: Server exception thrown when a stored object entry cannot be saved.
+            throw new ServerException('Could not save Notice');
+        }
+
+        // Profile::hasRepeated() abuses pkeyGet(), so we
+        // have to clear manually
+        if (!empty($this->repeat_of)) {
+            $c = self::memcache();
+            if (!empty($c)) {
+                $ck = self::multicacheKey('Notice',
+                                          array('profile_id' => $this->profile_id,
+                                                'repeat_of' => $this->repeat_of));
+                $c->delete($ck);
             }
         }
 
+        // Update possibly ID-dependent columns: URI, conversation
+        // (now that INSERT has added the notice's local id)
+        $orig = clone($this);
+        $changed = false;
+
+        // We can only get here if it's a local notice, since remote notices
+        // should've bailed out earlier due to lacking a URI.
+        if (empty($this->uri)) {
+            $this->uri = sprintf('%s%s=%d:%s=%s',
+                                TagURI::mint(),
+                                'noticeId', $this->id,
+                                'objectType', $this->get_object_type(true));
+            $changed = true;
+        }
+
+        // If it's not part of a conversation, it's
+        // the beginning of a new conversation.
+        if (empty($this->conversation)) {
+            $conv = Conversation::create($this);
+            $this->conversation = $conv->id;
+            $changed = true;
+        }
+
+        if ($changed && $this->update($orig) === false) {
+            common_log_db_error($notice, 'UPDATE', __FILE__);
+            // TRANS: Server exception thrown when a notice cannot be updated.
+            throw new ServerException(_('Problem saving notice.'));
+        }
+
+        $this->blowOnInsert();
+
         return $result;
     }
 
@@ -2400,6 +2426,11 @@ class Notice extends Managed_DataObject
                 $this->is_local == Notice::LOCAL_NONPUBLIC);
     }
 
+    public function isRepeat()
+    {
+        return !empty($this->repeat_of);
+    }
+
     /**
      * Get the list of hash tags saved with this notice.
      *