]> git.mxchange.org Git - quix0rs-gnu-social.git/commitdiff
First step of making Bookmark saveActivity-compatible
authorMikael Nordfeldth <mmn@hethane.se>
Sat, 10 Oct 2015 21:47:43 +0000 (23:47 +0200)
committerMikael Nordfeldth <mmn@hethane.se>
Sat, 10 Oct 2015 21:47:43 +0000 (23:47 +0200)
plugins/ActivityVerbPost/ActivityVerbPostPlugin.php
plugins/Bookmark/BookmarkPlugin.php
plugins/Bookmark/classes/Bookmark.php

index b1abd616c5e8d51e04995a4203e8f6cced86ebb0..32b1ba2e7cfffab427f4f7e195614ce29d9f5f11 100644 (file)
@@ -25,6 +25,8 @@ if (!defined('GNUSOCIAL')) { exit(1); }
  */
 class ActivityVerbPostPlugin extends ActivityVerbHandlerPlugin
 {
+    // TODO: Implement a "fallback" feature which can handle anything _as_ an activityobject "note"
+
     public function tag()
     {
         return 'post';
index e0f30700ae8c99504ba92029a120fbaa55a2f701..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
      *
@@ -299,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)
index a3a608ce5e4ece02b4a2c9445bac7a6202b284aa..95518631a3468cfb7536f646ec17d4cf44675335 100644 (file)
@@ -105,11 +105,11 @@ class Bookmark extends Managed_DataObject
      *
      * @return Bookmark bookmark found or null
      */
-    static function getByURL($profile, $url)
+    static function getByURL(Profile $profile, $url)
     {
         $nb = new Bookmark();
 
-        $nb->profile_id = $profile->id;
+        $nb->profile_id = $profile->getID();
         $nb->url        = $url;
 
         if (!$nb->find(true)) {
@@ -119,6 +119,61 @@ class Bookmark extends Managed_DataObject
         return $nb;
     }
 
+    /**
+     * Store a Bookmark object
+     *
+     * @param Profile $profile     To save the bookmark for
+     * @param string  $title       Title of the bookmark
+     * @param string  $url         URL of the bookmark
+     * @param string  $description Description of the bookmark
+     *
+     * @return Bookmark the Bookmark object
+     */
+    static function addNew(Notice $stored, $title, $url, $description)
+    {
+        if ($title === '' or is_null($title)) {
+            throw new ClientException(_m('You must provide a non-empty title.'));
+        }
+        if (!common_valid_http_url($url)) {
+            throw new ClientException(_m('Only web bookmarks can be posted (HTTP or HTTPS).'));
+        }
+
+        try {
+            $object = self::getByURL($stored->getProfile(), $url);
+            throw new ClientException(_m('You have already bookmarked this URL.'));
+        } catch (NoResultException $e) {
+            // Alright, so then we have to create it.
+        }
+
+        $nb = new Bookmark();
+
+        $nb->id          = UUID::gen();
+        $nb->uri         = $stored->uri;
+        $nb->profile_id  = $stored->getProfile()->getID();
+        $nb->title       = $title;
+        $nb->url         = $url;
+        $nb->description = $description;
+        $nb->created     = $stored->created;
+
+        $result = $nb->insert();
+        if ($result === false) {
+            throw new ServerException('Could not insert Bookmark into database!');
+        }
+
+        /*$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 $nb;
+    }
+
     /**
      * Save a new notice bookmark
      *