]> git.mxchange.org Git - quix0rs-gnu-social.git/blobdiff - plugins/Bookmark/BookmarkPlugin.php
First step of making Bookmark saveActivity-compatible
[quix0rs-gnu-social.git] / plugins / Bookmark / BookmarkPlugin.php
index 547efa2370104187891241cfbd7ba90d7ad689c8..1f8b548d1ffde7f8a1787f694e1fd8b2afd3ee3a 100644 (file)
@@ -28,9 +28,7 @@
  * @link      http://status.net/
  */
 
-if (!defined('STATUSNET')) {
-    exit(1);
-}
+if (!defined('GNUSOCIAL')) { exit(1); }
 
 /**
  * Bookmark plugin main class
@@ -48,8 +46,6 @@ class BookmarkPlugin extends MicroAppPlugin
     const VERSION         = '0.1';
     const IMPORTDELICIOUS = 'BookmarkPlugin:IMPORTDELICIOUS';
 
-    var $oldSaveNew = true;
-
     /**
      * Authorization for importing delicious bookmarks
      *
@@ -280,14 +276,12 @@ class BookmarkPlugin extends MicroAppPlugin
      */
     function deleteRelated(Notice $notice)
     {
-       if ($this->isMyNotice($notice)) {
-               
-               $nb = Bookmark::getByNotice($notice);
-
-               if (!empty($nb)) {
-               $nb->delete();
-               }
-       }
+        try {
+            $nb = Bookmark::fromStored($notice);
+        } catch (NoResultException $e) {
+            throw new AlreadyFulfilledException('Bookmark already gone when deleting: '.$e->getMessage());
+        }
+        $nb->delete();
        
         return true;
     }
@@ -301,83 +295,21 @@ class BookmarkPlugin extends MicroAppPlugin
      *
      * @return Notice resulting notice
      */
-    function saveNoticeFromActivity(Activity $activity, Profile $actor, array $options=array())
+    protected function saveObjectFromActivity(Activity $activity, Notice $stored, array $options=array())
     {
-        $bookmark = $activity->objects[0];
+        $actobj = $activity->objects[0];
 
-        $relLinkEls = ActivityUtils::getLinks($bookmark->element, 'related');
+        $relLinkEls = ActivityUtils::getLinks($actobj->element, 'related');
 
-        if (count($relLinkEls) < 1) {
+        if (count($relLinkEls) !== 1) {
             // TRANS: Client exception thrown when a bookmark is formatted incorrectly.
-            throw new ClientException(_m('Expected exactly 1 link '.
-                                        'rel=related in a Bookmark.'));
-        }
-
-        if (count($relLinkEls) > 1) {
-            common_log(LOG_WARNING,
-                       "Got too many link rel=related in a Bookmark.");
-        }
-
-        $linkEl = $relLinkEls[0];
-
-        $url = $linkEl->getAttribute('href');
-
-        $tags = array();
-
-        foreach ($activity->categories as $category) {
-            $tags[] = common_canonical_tag($category->term);
-        }
-
-        if (!empty($activity->time)) {
-            $options['created'] = common_sql_date($activity->time);
-        }
-
-        // Fill in location if available
-
-        $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;
-            }
-        }
-
-        $options['groups']  = array();
-        $options['replies'] = array();  // TODO: context->attention
-
-        foreach ($activity->context->attention as $attnUrl=>$type) {
-            try {
-                $other = Profile::fromUri($attnUrl);
-                if ($other->isGroup()) {
-                    $options['groups'][] = $other->id;
-                } else {
-                    $options['replies'][] = $attnUrl;
-                }
-            } catch (UnknownUriException $e) {
-                // We simply don't know this URI, despite lookup attempts.
-            }
-        }
-
-        // Maintain direct reply associations
-        // @fixme what about conversation ID?
-
-        if (!empty($activity->context->replyToID)) {
-            $orig = Notice::getKV('uri',
-                                      $activity->context->replyToID);
-            if (!empty($orig)) {
-                $options['reply_to'] = $orig->id;
-            }
+            throw new ClientException(sprintf(_m('Expected exactly 1 link rel=related in a Bookmark, got %1$d.'), count($relLinkEls)));
         }
 
-        return Bookmark::saveNew($actor,
-                                 $bookmark->title,
-                                 $url,
-                                 $tags,
-                                 $bookmark->summary,
-                                 $options);
+        return Bookmark::addNew($stored,
+                                 $actobj->title,
+                                 $relLinkEls[0]->getAttribute('href'),
+                                 $actobj->summary);
     }
 
     function activityObjectFromNotice(Notice $notice)
@@ -388,12 +320,12 @@ class BookmarkPlugin extends MicroAppPlugin
                    "Formatting notice {$notice->uri} as a bookmark.");
 
         $object = new ActivityObject();
-        $nb = Bookmark::getByNotice($notice);
+        $nb = Bookmark::fromStored($notice);
 
         $object->id      = $notice->uri;
         $object->type    = ActivityObject::BOOKMARK;
-        $object->title   = $nb->title;
-        $object->summary = $nb->description;
+        $object->title   = $nb->getTitle();
+        $object->summary = $nb->getDescription();
         $object->link    = $notice->getUrl();
 
         // Attributes of the URL
@@ -481,14 +413,10 @@ class BookmarkPlugin extends MicroAppPlugin
     {
         assert($obj->type == ActivityObject::BOOKMARK);
 
-        $bm = Bookmark::getKV('uri', $obj->id);
+        $bm = Bookmark::getByPK(array('uri' => $obj->id));
 
-        if (empty($bm)) {
-            throw new ServerException("Unknown bookmark: " . $obj->id);
-        }
-
-        $out['displayName'] = $bm->title;
-        $out['targetUrl']   = $bm->url;
+        $out['displayName'] = $bm->getTitle();
+        $out['targetUrl']   = $bm->getUrl();
 
         return true;
     }
@@ -501,24 +429,32 @@ class BookmarkPlugin extends MicroAppPlugin
         $nli->out->elementEnd('div');
     }
 
-    protected function showNoticeContent(Notice $stored, HTMLOutputter $out, Profile $scoped=null)
+    public function getDescription()
     {
-        $nb = Bookmark::getByNotice($stored);
-
-        if (empty($nb)) {
-            common_log(LOG_ERR, "No bookmark for notice {$stored->id}");
-            parent::showContent();
-            return;
-        } else if (empty($nb->url)) {
-            common_log(LOG_ERR, "No url for bookmark {$nb->id} for notice {$stored->id}");
-            parent::showContent();
-            return;
+        return $this->description;
+    }
+
+    public function getTitle()
+    {
+        return $this->title;
+    }
+
+    public function getUrl()
+    {
+        if (empty($this->url)) {
+            throw new InvalidUrlException($this->url);
         }
+        return $this->url;
+    }
+
+    protected function showNoticeContent(Notice $stored, HTMLOutputter $out, Profile $scoped=null)
+    {
+        $nb = Bookmark::fromStored($stored);
 
         $profile = $stored->getProfile();
 
         // Whether to nofollow
-        $attrs = array('href' => $nb->url, 'class' => 'bookmark-title');
+        $attrs = array('href' => $nb->getUrl(), 'class' => 'bookmark-title');
 
         $nf = common_config('nofollow', 'external');