]> git.mxchange.org Git - quix0rs-gnu-social.git/blobdiff - plugins/OStatus/lib/activity.php
more work on salmon
[quix0rs-gnu-social.git] / plugins / OStatus / lib / activity.php
index 11aab2848650944cf0afcf4fad21581a26758845..3d02e358489a40dd4ed79ddb1dc3ab7dd0b24fd0 100644 (file)
@@ -63,19 +63,78 @@ class ActivityUtils
      * @return string related link, if any
      */
 
-    static function getLink($element)
+    static function getPermalink($element)
+    {
+        return self::getLink($element, 'alternate', 'text/html');
+    }
+
+    /**
+     * Get the permalink for an Activity object
+     *
+     * @param DOMElement $element A DOM element
+     *
+     * @return string related link, if any
+     */
+
+    static function getLink($element, $rel, $type=null)
     {
         $links = $element->getElementsByTagnameNS(self::ATOM, self::LINK);
 
         foreach ($links as $link) {
-            if ($link->getAttributeNS(self::ATOM, self::REL) == 'alternate' &&
-                $link->getAttributeNS(self::ATOM, self::TYPE) == 'text/html') {
-                return $link->getAttributeNS(self::ATOM, self::HREF);
+
+            $linkRel = $link->getAttribute(self::REL);
+            $linkType = $link->getAttribute(self::TYPE);
+
+            if ($linkRel == $rel &&
+                (is_null($type) || $linkType == $type)) {
+                return $link->getAttribute(self::HREF);
             }
         }
 
         return null;
     }
+
+    /**
+     * Gets the first child element with the given tag
+     *
+     * @param DOMElement $element   element to pick at
+     * @param string     $tag       tag to look for
+     * @param string     $namespace Namespace to look under
+     *
+     * @return DOMElement found element or null
+     */
+
+    static function child($element, $tag, $namespace=self::ATOM)
+    {
+        $els = $element->getElementsByTagnameNS($namespace, $tag);
+
+        if (empty($els) || $els->length == 0) {
+            return null;
+        } else {
+            return $els->item(0);
+        }
+    }
+
+    /**
+     * Grab the text content of a DOM element child of the current element
+     *
+     * @param DOMElement $element   Element whose children we examine
+     * @param string     $tag       Tag to look up
+     * @param string     $namespace Namespace to use, defaults to Atom
+     *
+     * @return string content of the child
+     */
+
+    static function childContent($element, $tag, $namespace=self::ATOM)
+    {
+        $el = self::child($element, $tag, $namespace);
+
+        if (empty($el)) {
+            return null;
+        } else {
+            return $el->textContent;
+        }
+    }
 }
 
 /**
@@ -123,9 +182,11 @@ class ActivityObject
     const ID      = 'id';
     const SOURCE  = 'source';
 
-    const NAME = 'name';
-    const URI  = 'uri';
+    const NAME  = 'name';
+    const URI   = 'uri';
+    const EMAIL = 'email';
 
+    public $element;
     public $type;
     public $id;
     public $title;
@@ -146,7 +207,7 @@ class ActivityObject
 
     function __construct($element)
     {
-        $this->source = $element;
+        $this->element = $element;
 
         if ($element->tagName == 'author') {
 
@@ -154,42 +215,56 @@ class ActivityObject
             $this->title = $this->_childContent($element, self::NAME);
             $this->id    = $this->_childContent($element, self::URI);
 
+            if (empty($this->id)) {
+                $email = $this->_childContent($element, self::EMAIL);
+                if (!empty($email)) {
+                    // XXX: acct: ?
+                    $this->id = 'mailto:'.$email;
+                }
+            }
+
         } else {
 
             $this->type = $this->_childContent($element, Activity::OBJECTTYPE,
                                                Activity::SPEC);
 
+            if (empty($this->type)) {
+                $this->type = ActivityObject::NOTE;
+            }
+
             $this->id      = $this->_childContent($element, self::ID);
             $this->title   = $this->_childContent($element, self::TITLE);
             $this->summary = $this->_childContent($element, self::SUMMARY);
             $this->content = $this->_childContent($element, self::CONTENT);
-            $this->source  = $this->_childContent($element, self::SOURCE);
 
-            $this->link = ActivityUtils::getLink($element);
+            $this->source  = $this->_getSource($element);
+
+            $this->link = ActivityUtils::getPermalink($element);
 
             // XXX: grab PoCo stuff
         }
     }
 
-    /**
-     * Grab the text content of a DOM element child of the current element
-     *
-     * @param DOMElement $element   Element whose children we examine
-     * @param string     $tag       Tag to look up
-     * @param string     $namespace Namespace to use, defaults to Atom
-     *
-     * @return string content of the child
-     */
+    private function _childContent($element, $tag, $namespace=ActivityUtils::ATOM)
+    {
+        return ActivityUtils::childContent($element, $tag, $namespace);
+    }
+
+    // Try to get a unique id for the source feed
 
-    private function _childContent($element, $tag, $namespace=Activity::ATOM)
+    private function _getSource($element)
     {
-        $els = $element->getElementsByTagnameNS($namespace, $tag);
+        $sourceEl = ActivityUtils::child($element, 'source');
 
-        if (empty($els) || $els->length == 0) {
+        if (empty($sourceEl)) {
             return null;
         } else {
-            $el = $els->item(0);
-            return $el->textContent;
+            $href = ActivityUtils::getLink($sourceEl, 'self');
+            if (!empty($href)) {
+                return $href;
+            } else {
+                return ActivityUtils::childContent($sourceEl, 'id');
+            }
         }
     }
 }
@@ -252,6 +327,7 @@ class Activity
 
     const AUTHOR    = 'author';
     const PUBLISHED = 'published';
+    const UPDATED   = 'updated';
 
     public $actor;   // an ActivityObject
     public $verb;    // a string (the URL)
@@ -281,10 +357,15 @@ class Activity
             $this->time = strtotime($pubEl->textContent);
         } else {
             // XXX technically an error; being liberal. Good idea...?
-            $this->time = null;
+            $updateEl = $this->_child($entry, self::UPDATED, self::ATOM);
+            if (!empty($updateEl)) {
+                $this->time = strtotime($updateEl->textContent);
+            } else {
+                $this->time = null;
+            }
         }
 
-        $this->link = ActivityUtils::getLink($entry);
+        $this->link = ActivityUtils::getPermalink($entry);
 
         $verbEl = $this->_child($entry, self::VERB);
 
@@ -317,6 +398,11 @@ class Activity
         } else if ($authorEl = $this->_child($entry, self::AUTHOR, self::ATOM)) {
 
             $this->actor = new ActivityObject($authorEl);
+
+        } else if (!empty($feed) && $authorEl = $this->_child($feed, self::AUTHOR,
+                                                              self::ATOM)) {
+
+            $this->actor = new ActivityObject($authorEl);
         }
 
         $contextEl = $this->_child($entry, self::CONTEXT);
@@ -343,24 +429,8 @@ class Activity
         return null;
     }
 
-    /**
-     * Gets the first child element with the given tag
-     *
-     * @param DOMElement $element   element to pick at
-     * @param string     $tag       tag to look for
-     * @param string     $namespace Namespace to look under
-     *
-     * @return DOMElement found element or null
-     */
-
     private function _child($element, $tag, $namespace=self::SPEC)
     {
-        $els = $element->getElementsByTagnameNS($namespace, $tag);
-
-        if (empty($els) || $els->length == 0) {
-            return null;
-        } else {
-            return $els->item(0);
-        }
+        return ActivityUtils::child($element, $tag, $namespace);
     }
 }
\ No newline at end of file