]> git.mxchange.org Git - quix0rs-gnu-social.git/blobdiff - plugins/OStatus/classes/Ostatus_profile.php
Merged stuff from upstream/master
[quix0rs-gnu-social.git] / plugins / OStatus / classes / Ostatus_profile.php
index 1c4428b16df7ed17e1e90949c9d452ef9f5f383e..b4df92e2daeb8b092dd5bdf4c71cdd869d7e4b40 100644 (file)
@@ -345,7 +345,7 @@ class Ostatus_profile extends Managed_DataObject
         $xml = $entry->getString();
         common_log(LOG_INFO, "Posting to Salmon endpoint $this->salmonuri: $xml");
 
-        Salmon::post($this->salmonuri, $xml, $actor->getUser());
+        Salmon::post($this->salmonuri, $xml, $actor);
     }
 
     /**
@@ -359,7 +359,7 @@ class Ostatus_profile extends Managed_DataObject
     public function notifyActivity($entry, Profile $actor)
     {
         if ($this->salmonuri) {
-            return Salmon::post($this->salmonuri, $this->notifyPrepXml($entry), $actor->getUser());
+            return Salmon::post($this->salmonuri, $this->notifyPrepXml($entry), $actor, $this->localProfile());
         }
         common_debug(__CLASS__.' error: No salmonuri for Ostatus_profile uri: '.$this->uri);
 
@@ -371,17 +371,25 @@ class Ostatus_profile extends Managed_DataObject
      * send immediately but won't get the return value.
      *
      * @param mixed $entry XML string, Notice, or Activity
+     * @param Profile $actor Acting profile
      * @return boolean success
      */
-    public function notifyDeferred($entry, $actor)
+    public function notifyDeferred($entry, Profile $actor)
     {
         if ($this->salmonuri) {
-            $data = array('salmonuri' => $this->salmonuri,
-                          'entry' => $this->notifyPrepXml($entry),
-                          'actor' => $actor->id);
-
-            $qm = QueueManager::get();
-            return $qm->enqueue($data, 'salmon');
+            try {
+                common_debug("OSTATUS: user {$actor->getNickname()} ({$actor->getID()}) wants to ping {$this->localProfile()->getNickname()} on {$this->salmonuri}");
+                $data = array('salmonuri' => $this->salmonuri,
+                              'entry' => $this->notifyPrepXml($entry),
+                              'actor' => $actor->getID(),
+                              'target' => $this->localProfile()->getID());
+
+                $qm = QueueManager::get();
+                return $qm->enqueue($data, 'salmon');
+            } catch (Exception $e) {
+                common_log(LOG_ERR, 'OSTATUS: Something went wrong when creating a Salmon slap: '._ve($e->getMessage()));
+                return false;
+            }
         }
 
         return false;
@@ -443,10 +451,7 @@ class Ostatus_profile extends Managed_DataObject
             return;
         }
 
-        for ($i = 0; $i < $entries->length; $i++) {
-            $entry = $entries->item($i);
-            $this->processEntry($entry, $feed, $source);
-        }
+        $this->processEntries($entries, $feed, $source);
     }
 
     public function processRssFeed(DOMElement $rss, $source)
@@ -464,9 +469,21 @@ class Ostatus_profile extends Managed_DataObject
 
         $items = $channel->getElementsByTagName('item');
 
-        for ($i = 0; $i < $items->length; $i++) {
-            $item = $items->item($i);
-            $this->processEntry($item, $channel, $source);
+        $this->processEntries($items, $channel, $source);
+    }
+
+    public function processEntries(DOMNodeList $entries, DOMElement $feed, $source)
+    {
+        for ($i = 0; $i < $entries->length; $i++) {
+            $entry = $entries->item($i);
+            try {
+                $this->processEntry($entry, $feed, $source);
+            } catch (AlreadyFulfilledException $e) {
+                common_debug('We already had this entry: '.$e->getMessage());
+            } catch (ServerException $e) {
+                // FIXME: This should be UnknownUriException and the ActivityUtils:: findLocalObject should only test one URI
+                common_log(LOG_ERR, 'Entry threw exception while processing a feed from '.$source.': '.$e->getMessage());
+            }
         }
     }
 
@@ -479,14 +496,14 @@ class Ostatus_profile extends Managed_DataObject
      *
      * @return Notice Notice representing the new (or existing) activity
      */
-    public function processEntry($entry, $feed, $source)
+    public function processEntry(DOMElement $entry, DOMElement $feed, $source)
     {
         $activity = new Activity($entry, $feed);
         return $this->processActivity($activity, $source);
     }
 
     // TODO: Make this throw an exception
-    public function processActivity($activity, $source)
+    public function processActivity(Activity $activity, $source)
     {
         $notice = null;
 
@@ -495,26 +512,7 @@ class Ostatus_profile extends Managed_DataObject
         if (Event::handle('StartHandleFeedEntryWithProfile', array($activity, $this->localProfile(), &$notice)) &&
             Event::handle('StartHandleFeedEntry', array($activity))) {
 
-            switch ($activity->verb) {
-            case ActivityVerb::POST:
-                // @todo process all activity objects
-                switch ($activity->objects[0]->type) {
-                case ActivityObject::ARTICLE:
-                case ActivityObject::BLOGENTRY:
-                case ActivityObject::NOTE:
-                case ActivityObject::STATUS:
-                case ActivityObject::COMMENT:
-                case null:
-                    $notice = $this->processPost($activity, $source);
-                    break;
-                default:
-                    // TRANS: Client exception.
-                    throw new ClientException(_m('Cannot handle that kind of post.'));
-                }
-                break;
-            default:
-                common_log(LOG_INFO, "Ignoring activity with unrecognized verb $activity->verb");
-            }
+            common_log(LOG_INFO, "Ignoring activity with unrecognized verb $activity->verb");
 
             Event::handle('EndHandleFeedEntry', array($activity));
             Event::handle('EndHandleFeedEntryWithProfile', array($activity, $this, $notice));
@@ -528,178 +526,21 @@ class Ostatus_profile extends Managed_DataObject
      * @param Activity $activity
      * @param string $method 'push' or 'salmon'
      * @return mixed saved Notice or false
-     * @todo FIXME: Break up this function, it's getting nasty long
      */
-    public function processPost($activity, $method)
+    public function processPost(Activity $activity, $method)
     {
-        $notice = null;
-
-        $profile = ActivityUtils::checkAuthorship($activity, $this->localProfile());
-
-        // It's not always an ActivityObject::NOTE, but... let's just say it is.
-
-        $note = $activity->objects[0];
-
-        // The id URI will be used as a unique identifier for the notice,
-        // protecting against duplicate saves. It isn't required to be a URL;
-        // tag: URIs for instance are found in Google Buzz feeds.
-        $sourceUri = $note->id;
-        $dupe = Notice::getKV('uri', $sourceUri);
-        if ($dupe instanceof Notice) {
-            common_log(LOG_INFO, "OStatus: ignoring duplicate post: $sourceUri");
-            return $dupe;
-        }
-
-        // We'll also want to save a web link to the original notice, if provided.
-        $sourceUrl = null;
-        if ($note->link) {
-            $sourceUrl = $note->link;
-        } else if ($activity->link) {
-            $sourceUrl = $activity->link;
-        } else if (preg_match('!^https?://!', $note->id)) {
-            $sourceUrl = $note->id;
-        }
-
-        // Use summary as fallback for content
-
-        if (!empty($note->content)) {
-            $sourceContent = $note->content;
-        } else if (!empty($note->summary)) {
-            $sourceContent = $note->summary;
-        } else if (!empty($note->title)) {
-            $sourceContent = $note->title;
-        } else {
-            // @todo FIXME: Fetch from $sourceUrl?
-            // TRANS: Client exception. %s is a source URI.
-            throw new ClientException(sprintf(_m('No content for notice %s.'),$sourceUri));
-        }
-
-        // Get (safe!) HTML and text versions of the content
-
-        $rendered = common_purify($sourceContent);
-        $content = common_strip_html($rendered);
-
-        $shortened = common_shorten_links($content);
+        $actor = ActivityUtils::checkAuthorship($activity, $this->localProfile());
 
-        // If it's too long, try using the summary, and make the
-        // HTML an attachment.
-
-        $attachment = null;
-
-        if (Notice::contentTooLong($shortened)) {
-            $attachment = $this->saveHTMLFile($note->title, $rendered);
-            $summary = common_strip_html($note->summary);
-            if (empty($summary)) {
-                $summary = $content;
-            }
-            $shortSummary = common_shorten_links($summary);
-            if (Notice::contentTooLong($shortSummary)) {
-                $url = common_shorten_url($sourceUrl);
-                $shortSummary = substr($shortSummary,
-                                       0,
-                                       Notice::maxContent() - (mb_strlen($url) + 2));
-                $content = $shortSummary . ' ' . $url;
-
-                // We mark up the attachment link specially for the HTML output
-                // so we can fold-out the full version inline.
-
-                // @todo FIXME i18n: This tooltip will be saved with the site's default language
-                // TRANS: Shown when a notice is longer than supported and/or when attachments are present. At runtime
-                // TRANS: this will usually be replaced with localised text from StatusNet core messages.
-                $showMoreText = _m('Show more');
-                $attachUrl = common_local_url('attachment',
-                                              array('attachment' => $attachment->id));
-                $rendered = common_render_text($shortSummary) .
-                            '<a href="' . htmlspecialchars($attachUrl) .'"'.
-                            ' class="attachment more"' .
-                            ' title="'. htmlspecialchars($showMoreText) . '">' .
-                            '&#8230;' .
-                            '</a>';
-            }
-        }
-
-        $options = array('is_local' => Notice::REMOTE,
-                        'url' => $sourceUrl,
-                        'uri' => $sourceUri,
-                        'rendered' => $rendered,
-                        'replies' => array(),
-                        'groups' => array(),
-                        'peopletags' => array(),
-                        'tags' => array(),
-                        'urls' => array());
-
-        // Check for optional attributes...
-
-        if (!empty($activity->time)) {
-            $options['created'] = common_sql_date($activity->time);
-        }
-
-        if ($activity->context) {
-            // TODO: context->attention
-            list($options['groups'], $options['replies'])
-                = self::filterAttention($profile, $activity->context->attention);
-
-            // Maintain direct reply associations
-            // @todo FIXME: What about conversation ID?
-            if (!empty($activity->context->replyToID)) {
-                $orig = Notice::getKV('uri', $activity->context->replyToID);
-                if ($orig instanceof Notice) {
-                    $options['reply_to'] = $orig->id;
-                }
-            }
-            if (!empty($activity->context->conversation)) {
-                // we store the URI here, Notice class can look it up later
-                $options['conversation'] = $activity->context->conversation;
-            }
-
-            $location = $activity->context->location;
-            if ($location) {
-                $options['lat'] = $location->lat;
-                $options['lon'] = $location->lon;
-                if ($location->location_id) {
-                    $options['location_ns'] = $location->location_ns;
-                    $options['location_id'] = $location->location_id;
-                }
-            }
-        }
-
-        if ($this->isPeopletag()) {
-            $options['peopletags'][] = $this->localPeopletag();
-        }
-
-        // Atom categories <-> hashtags
-        foreach ($activity->categories as $cat) {
-            if ($cat->term) {
-                $term = common_canonical_tag($cat->term);
-                if ($term) {
-                    $options['tags'][] = $term;
-                }
-            }
-        }
-
-        // Atom enclosures -> attachment URLs
-        foreach ($activity->enclosures as $href) {
-            // @todo FIXME: Save these locally or....?
-            $options['urls'][] = $href;
-        }
+        $options = array('is_local' => Notice::REMOTE);
 
         try {
-            $saved = Notice::saveNew($profile->id,
-                                     $content,
-                                     'ostatus',
-                                     $options);
-            if ($saved instanceof Notice) {
-                Ostatus_source::saveNew($saved, $this, $method);
-                if ($attachment instanceof File) {
-                    File_to_post::processNew($attachment, $saved);
-                }
-            }
+            $stored = Notice::saveActivity($activity, $actor, $options);
+            Ostatus_source::saveNew($stored, $this, $method);
         } catch (Exception $e) {
             common_log(LOG_ERR, "OStatus save of remote message $sourceUri failed: " . $e->getMessage());
             throw $e;
         }
-        common_log(LOG_INFO, "OStatus saved remote message $sourceUri as notice id $saved->id");
-        return $saved;
+        return $stored;
     }
 
     /**
@@ -710,7 +551,7 @@ class Ostatus_profile extends Managed_DataObject
      */
     static public function filterAttention(Profile $sender, array $attention)
     {
-        common_log(LOG_DEBUG, "Original reply recipients: " . implode(', ', array_keys($attention)));
+        common_debug("Original reply recipients: " . implode(', ', array_keys($attention)));
         $groups = array();
         $replies = array();
         foreach ($attention as $recipient=>$type) {
@@ -732,11 +573,11 @@ class Ostatus_profile extends Managed_DataObject
                     if ($sender->isMember($group)) {
                         $groups[] = $group->id;
                     } else {
-                        common_log(LOG_DEBUG, sprintf('Skipping reply to local group %s as sender %d is not a member', $group->getNickname(), $sender->id));
+                        common_debug(sprintf('Skipping reply to local group %s as sender %d is not a member', $group->getNickname(), $sender->id));
                     }
                     continue;
                 } else {
-                    common_log(LOG_DEBUG, "Skipping reply to bogus group $recipient");
+                    common_debug("Skipping reply to bogus group $recipient");
                 }
             }
 
@@ -754,12 +595,12 @@ class Ostatus_profile extends Managed_DataObject
                 continue;
             } catch (Exception $e) {
                 // Neither a recognizable local nor remote user!
-                common_log(LOG_DEBUG, "Skipping reply to unrecognized profile $recipient: " . $e->getMessage());
+                common_debug("Skipping reply to unrecognized profile $recipient: " . $e->getMessage());
             }
 
         }
-        common_log(LOG_DEBUG, "Local reply recipients: " . implode(', ', $replies));
-        common_log(LOG_DEBUG, "Local group recipients: " . implode(', ', $groups));
+        common_debug("Local reply recipients: " . implode(', ', $replies));
+        common_debug("Local group recipients: " . implode(', ', $groups));
         return array($groups, $replies);
     }
 
@@ -810,14 +651,21 @@ class Ostatus_profile extends Managed_DataObject
             }
         }
 
-        // Try to get some hCard data
+        if (in_array(
+            preg_replace('/\s*;.*$/', '', $response->getHeader('Content-Type')),
+            array('application/rss+xml', 'application/atom+xml', 'application/xml', 'text/xml'))
+        ) {
+            $hints['feedurl'] = $response->getUrl();
+        } else {
+            // Try to get some hCard data
 
-        $body = $response->getBody();
+            $body = $response->getBody();
 
-        $hcardHints = DiscoveryHints::hcardHints($body, $finalUrl);
+            $hcardHints = DiscoveryHints::hcardHints($body, $finalUrl);
 
-        if (!empty($hcardHints)) {
-            $hints = array_merge($hints, $hcardHints);
+            if (!empty($hcardHints)) {
+                $hints = array_merge($hints, $hcardHints);
+            }
         }
 
         // Check if they've got an LRDD header
@@ -1003,11 +851,13 @@ class Ostatus_profile extends Managed_DataObject
             }
         }
 
+        $obj = ActivityUtils::getFeedAuthor($feedEl);
+
         // @todo FIXME: We should check whether this feed has elements
         // with different <author> or <dc:creator> elements, and... I dunno.
         // Do something about that.
 
-        $obj = ActivityObject::fromRssChannel($feedEl);
+        if(empty($obj)) { $obj = ActivityObject::fromRssChannel($feedEl); }
 
         return self::ensureActivityObjectProfile($obj, $hints);
     }
@@ -1041,13 +891,15 @@ class Ostatus_profile extends Managed_DataObject
 
         // @todo FIXME: This should be better encapsulated
         // ripped from oauthstore.php (for old OMB client)
-        $temp_filename = tempnam(sys_get_temp_dir(), 'listener_avatar');
+        $temp_filename = tempnam(common_get_temp_dir(), 'listener_avatar');
         try {
             $imgData = HTTPClient::quickGet($url);
+
             // Make sure it's at least an image file. ImageFile can do the rest.
             if (false === getimagesizefromstring($imgData)) {
                 throw new UnsupportedMediaException(_('Downloaded group avatar was not an image.'));
             }
+
             file_put_contents($temp_filename, $imgData);
             unset($imgData);    // No need to carry this in memory.
 
@@ -1266,7 +1118,7 @@ class Ostatus_profile extends Managed_DataObject
         $discover = false;
 
         if (!$homeuri) {
-            common_log(LOG_DEBUG, __METHOD__ . " empty actor profile URI: " . var_export($activity, true));
+            common_debug(__METHOD__ . " empty actor profile URI: " . var_export($activity, true));
             // TRANS: Exception.
             throw new Exception(_m('No profile URI.'));
         }
@@ -1508,7 +1360,7 @@ class Ostatus_profile extends Managed_DataObject
         // @todo tags from categories
 
         if ($profile->id) {
-            common_log(LOG_DEBUG, "Updating OStatus profile $profile->id from remote info $object->id: " . var_export($object, true) . var_export($hints, true));
+            common_debug("Updating OStatus profile $profile->id from remote info $object->id: " . var_export($object, true) . var_export($hints, true));
             $profile->update($orig);
         }
     }
@@ -1532,12 +1384,12 @@ class Ostatus_profile extends Managed_DataObject
         $group->homepage = self::getActivityObjectHomepage($object, $hints);
 
         if ($group->id) {   // If no id, we haven't called insert() yet, so don't run update()
-            common_log(LOG_DEBUG, "Updating OStatus group $group->id from remote info $object->id: " . var_export($object, true) . var_export($hints, true));
+            common_debug("Updating OStatus group $group->id from remote info $object->id: " . var_export($object, true) . var_export($hints, true));
             $group->update($orig);
         }
     }
 
-    protected static function updatePeopletag($tag, ActivityObject $object, array $hints=array()) {
+    protected static function updatePeopletag(Peopletag $tag, ActivityObject $object, array $hints=array()) {
         $orig = clone($tag);
 
         $tag->tag = $object->title;
@@ -1553,7 +1405,7 @@ class Ostatus_profile extends Managed_DataObject
         $tag->tagger = $tagger->profile_id;
 
         if ($tag->id) {
-            common_log(LOG_DEBUG, "Updating OStatus peopletag $tag->id from remote info $object->id: " . var_export($object, true) . var_export($hints, true));
+            common_debug("Updating OStatus peopletag $tag->id from remote info $object->id: " . var_export($object, true) . var_export($hints, true));
             $tag->update($orig);
         }
     }
@@ -1955,7 +1807,7 @@ class Ostatus_profile extends Managed_DataObject
         } else {
             $actor = $activity->actor;
 
-            if (empty($actor)) {
+            if (!$actor instanceof Profile) {
                 // OK here! assume the default
             } else if ($actor->id == $this->getUri() || $actor->link == $this->getUri()) {
                 $this->updateFromActivityObject($actor);