]> git.mxchange.org Git - quix0rs-gnu-social.git/blobdiff - plugins/Bookmark/BookmarkPlugin.php
Properly parse incoming bookmarks
[quix0rs-gnu-social.git] / plugins / Bookmark / BookmarkPlugin.php
index 1f4f8d9621027d554d6c9f46b3110e0d12bbdfe6..e6afa0b6daf466e295b786b5aca997a1479aa457 100644 (file)
@@ -180,12 +180,12 @@ class BookmarkPlugin extends MicroAppPlugin
     function onPluginVersion(array &$versions)
     {
         $versions[] = array('name' => 'Bookmark',
-                            'version' => self::VERSION,
-                            'author' => 'Evan Prodromou, Stephane Berube, Jean Baptiste Favre',
-                            'homepage' => 'http://status.net/wiki/Plugin:Bookmark',
+                            'version' => GNUSOCIAL_VERSION,
+                            'author' => 'Evan Prodromou, Stephane Berube, Jean Baptiste Favre, Mikael Nordfeldth',
+                            'homepage' => 'https://gnu.io/social',
                             'description' =>
                             // TRANS: Plugin description.
-                            _m('Simple extension for supporting bookmarks. ') .
+                            _m('Plugin for posting bookmarks. ') .
                             'BookmarkList feature has been developped by Stephane Berube. ' .
                             'Integration has been done by Jean Baptiste Favre.');
         return true;
@@ -297,79 +297,36 @@ class BookmarkPlugin extends MicroAppPlugin
      */
     protected function saveObjectFromActivity(Activity $activity, Notice $stored, array $options=array())
     {
-        $actobj = $activity->objects[0];
-
-        $relLinkEls = ActivityUtils::getLinks($actobj->element, 'related');
-
-        if (count($relLinkEls) !== 1) {
-            // TRANS: Client exception thrown when a bookmark is formatted incorrectly.
-            throw new ClientException(sprintf(_m('Expected exactly 1 link rel=related in a Bookmark, got %1$d.'), count($relLinkEls)));
-        }
-
-        return Bookmark::addNew($stored,
-                                 $actobj->title,
-                                 $relLinkEls[0]->getAttribute('href'),
-                                 $actobj->summary);
+        return Bookmark::saveActivityObject($activity->objects[0], $stored);
     }
 
-    function activityObjectFromNotice(Notice $notice)
+    public function onEndNoticeAsActivity(Notice $stored, Activity $act, Profile $scoped=null)
     {
-        assert($this->isMyNotice($notice));
-
-        common_log(LOG_INFO,
-                   "Formatting notice {$notice->uri} as a bookmark.");
-
-        $object = new ActivityObject();
-        $nb = Bookmark::fromStored($notice);
-
-        $object->id      = $notice->uri;
-        $object->type    = ActivityObject::BOOKMARK;
-        $object->title   = $nb->getTitle();
-        $object->summary = $nb->getDescription();
-        $object->link    = $notice->getUrl();
-
-        // Attributes of the URL
-
-        $attachments = $notice->attachments();
-
-        if (count($attachments) != 1) {
-            // TRANS: Server exception thrown when a bookmark has multiple attachments.
-            throw new ServerException(_m('Bookmark notice with the '.
-                                        'wrong number of attachments.'));
-        }
-
-        $target = $attachments[0];
-
-        $attrs = array('rel' => 'related',
-                       'href' => $target->getUrl());
-
-        if (!empty($target->title)) {
-            $attrs['title'] = $target->title;
+        if (!$this->isMyNotice($stored)) {
+            return true;
         }
 
-        $object->extra[] = array('link', $attrs, null);
-
-        // Attributes of the thumbnail, if any
-
-        try {
-            $thumbnail = $target->getThumbnail();
-            $tattrs = array('rel' => 'preview',
-                            'href' => $thumbnail->getUrl());
-
-            if (!empty($thumbnail->width)) {
-                $tattrs['media:width'] = $thumbnail->width;
-            }
-
-            if (!empty($thumbnail->height)) {
-                $tattrs['media:height'] = $thumbnail->height;
-            }
+        $this->extendActivity($stored, $act, $scoped);
+        return false;
+    }
 
-            $object->extra[] = array('link', $tattrs, null);
-        } catch (UnsupportedMediaException $e) {
-            // No image thumbnail metadata available
-        }
+    public function extendActivity(Notice $stored, Activity $act, Profile $scoped=null)
+    {
+        /*$hashtags = array();
+        $taglinks = array();
+
+        foreach ($tags as $tag) {
+            $hashtags[] = '#'.$tag;
+            $attrs      = array('href' => Notice_tag::url($tag),
+                                'rel'  => $tag,
+                                'class' => 'tag');
+            $taglinks[] = XMLStringer::estring('a', $attrs, $tag);
+        }*/
+    }
 
-        return $object;
+    function activityObjectFromNotice(Notice $notice)
+    {
+        return Bookmark::fromStored($notice)->asActivityObject();
     }
 
     function entryForm($out)
@@ -390,14 +347,16 @@ class BookmarkPlugin extends MicroAppPlugin
 
     function onEndUpgrade()
     {
+        printfnq('Making sure Bookmark notices have correct verb and object_type...');
+
         // Version 0.9.x of the plugin didn't stamp notices
         // with verb and object-type (for obvious reasons). Update
         // those notices here.
 
         $notice = new Notice();
         
-        $notice->whereAdd('exists (select uri from bookmark where bookmark.uri = notice.uri)');
-        $notice->whereAdd('((object_type is null) or (object_type = "' .ActivityObject::NOTE.'"))');
+        $notice->joinAdd(array('uri', 'bookmark:uri'));
+        $notice->whereAdd('object_type IS NULL OR object_type = '.$notice->_quote(ActivityObject::NOTE));
 
         $notice->find();
 
@@ -407,6 +366,8 @@ class BookmarkPlugin extends MicroAppPlugin
             $notice->object_type = ActivityObject::BOOKMARK;
             $notice->update($original);
         }
+
+        printfnq("DONE.\n");
     }
 
     public function activityObjectOutputJson(ActivityObject $obj, array &$out)
@@ -421,20 +382,10 @@ class BookmarkPlugin extends MicroAppPlugin
         return true;
     }
 
-    protected function showNoticeItemNotice(NoticeListItem $nli)
-    {
-        $nli->out->elementStart('div', 'entry-title');
-        $nli->showAuthor();
-        $nli->showContent();
-        $nli->out->elementEnd('div');
-    }
-
     protected function showNoticeContent(Notice $stored, HTMLOutputter $out, Profile $scoped=null)
     {
         $nb = Bookmark::fromStored($stored);
 
-        $profile = $stored->getProfile();
-
         // Whether to nofollow
         $attrs = array('href' => $nb->getUrl(), 'class' => 'bookmark-title');
 
@@ -447,7 +398,7 @@ class BookmarkPlugin extends MicroAppPlugin
         }
 
         $out->elementStart('h3');
-        $out->element('a', $attrs, $nb->title);
+        $out->element('a', $attrs, $nb->getTitle());
         $out->elementEnd('h3');
 
         // Replies look like "for:" tags
@@ -465,16 +416,14 @@ class BookmarkPlugin extends MicroAppPlugin
             $out->elementStart('ul', array('class' => 'bookmark-tags'));
 
             foreach ($replies as $reply) {
-                $other = Profile::getKV('id', $reply);
-                if (!empty($other)) {
-                    $out->elementStart('li');
-                    $out->element('a', array('rel' => 'tag',
-                                             'href' => $other->profileurl,
-                                             'title' => $other->getBestName()),
-                                  sprintf('for:%s', $other->nickname));
-                    $out->elementEnd('li');
-                    $out->text(' ');
-                }
+                $other = Profile::getByPK($reply);
+                $out->elementStart('li');
+                $out->element('a', array('rel' => 'tag',
+                                         'href' => $other->getUrl(),
+                                         'title' => $other->getBestName()),
+                              sprintf('for:%s', $other->getNickname()));
+                $out->elementEnd('li');
+                $out->text(' ');
             }
 
             foreach ($tags as $tag) {