]> git.mxchange.org Git - quix0rs-gnu-social.git/blobdiff - plugins/OStatus/classes/Ostatus_profile.php
Unifying HTML stripping functions to common_strip_html
[quix0rs-gnu-social.git] / plugins / OStatus / classes / Ostatus_profile.php
index 3d550872d625e8874594bc39b5ea84887320886b..72a0c34b92128b074da408530df2f87debe7b20d 100644 (file)
@@ -132,7 +132,7 @@ class Ostatus_profile extends Managed_DataObject
         } else if ($this->isPeopletag()) {
             return ActivityObject::fromPeopletag($this->localPeopletag());
         } else {
-            return ActivityObject::fromProfile($this->localProfile());
+            return $this->localProfile()->asActivityObject();
         }
     }
 
@@ -156,7 +156,7 @@ class Ostatus_profile extends Managed_DataObject
             $noun = ActivityObject::fromPeopletag($this->localPeopletag());
             return $noun->asString('activity:' . $element);
         } else {
-            $noun = ActivityObject::fromProfile($this->localProfile());
+            $noun = $this->localProfile()->asActivityObject();
             return $noun->asString('activity:' . $element);
         }
     }
@@ -478,7 +478,7 @@ class Ostatus_profile extends Managed_DataObject
 
         // The "WithProfile" events were added later.
 
-        if (Event::handle('StartHandleFeedEntryWithProfile', array($activity, $this, &$notice)) &&
+        if (Event::handle('StartHandleFeedEntryWithProfile', array($activity, $this->localProfile(), &$notice)) &&
             Event::handle('StartHandleFeedEntry', array($activity))) {
 
             switch ($activity->verb) {
@@ -516,10 +516,9 @@ class Ostatus_profile extends Managed_DataObject
     {
         $notice = null;
 
-        $oprofile = $this->checkAuthorship($activity);
-
-        if (!$oprofile instanceof Ostatus_profile) {
-            common_log(LOG_INFO, "No author matched share activity");
+        try {
+            $profile = ActivityUtils::checkAuthorship($activity, $this->localProfile());
+        } catch (ServerException $e) {
             return null;
         }
 
@@ -608,7 +607,7 @@ class Ostatus_profile extends Managed_DataObject
         // Get (safe!) HTML and text versions of the content
 
         $rendered = $this->purify($sourceContent);
-        $content = html_entity_decode(strip_tags($rendered), ENT_QUOTES, 'UTF-8');
+        $content = common_strip_html($rendered);
 
         $shortened = common_shorten_links($content);
 
@@ -619,7 +618,7 @@ class Ostatus_profile extends Managed_DataObject
 
         if (Notice::contentTooLong($shortened)) {
             $attachment = $this->saveHTMLFile($activity->title, $rendered);
-            $summary = html_entity_decode(strip_tags($activity->summary), ENT_QUOTES, 'UTF-8');
+            $summary = common_strip_html($activity->summary);
             if (empty($summary)) {
                 $summary = $content;
             }
@@ -670,7 +669,7 @@ class Ostatus_profile extends Managed_DataObject
         if ($activity->context) {
             // TODO: context->attention
             list($options['groups'], $options['replies'])
-                = self::filterAttention($oprofile->localProfile(), $activity->context->attention);
+                = self::filterAttention($profile, $activity->context->attention);
 
             // Maintain direct reply associations
             // @todo FIXME: What about conversation ID?
@@ -713,7 +712,7 @@ class Ostatus_profile extends Managed_DataObject
             $options['urls'][] = $href;
         }
 
-        $notice = Notice::saveNew($oprofile->profile_id,
+        $notice = Notice::saveNew($profile->id,
                                   $content,
                                   'ostatus',
                                   $options);
@@ -732,11 +731,7 @@ class Ostatus_profile extends Managed_DataObject
     {
         $notice = null;
 
-        $oprofile = $this->checkAuthorship($activity);
-
-        if (!$oprofile instanceof Ostatus_profile) {
-            return null;
-        }
+        $profile = $this->checkAuthorship($activity, $this->localProfile());
 
         // It's not always an ActivityObject::NOTE, but... let's just say it is.
 
@@ -779,7 +774,7 @@ class Ostatus_profile extends Managed_DataObject
         // Get (safe!) HTML and text versions of the content
 
         $rendered = $this->purify($sourceContent);
-        $content = html_entity_decode(strip_tags($rendered), ENT_QUOTES, 'UTF-8');
+        $content = common_strip_html($rendered);
 
         $shortened = common_shorten_links($content);
 
@@ -790,7 +785,7 @@ class Ostatus_profile extends Managed_DataObject
 
         if (Notice::contentTooLong($shortened)) {
             $attachment = $this->saveHTMLFile($note->title, $rendered);
-            $summary = html_entity_decode(strip_tags($note->summary), ENT_QUOTES, 'UTF-8');
+            $summary = common_strip_html($note->summary);
             if (empty($summary)) {
                 $summary = $content;
             }
@@ -839,7 +834,7 @@ class Ostatus_profile extends Managed_DataObject
         if ($activity->context) {
             // TODO: context->attention
             list($options['groups'], $options['replies'])
-                = self::filterAttention($oprofile->localProfile(), $activity->context->attention);
+                = self::filterAttention($profile, $activity->context->attention);
 
             // Maintain direct reply associations
             // @todo FIXME: What about conversation ID?
@@ -882,7 +877,7 @@ class Ostatus_profile extends Managed_DataObject
         }
 
         try {
-            $saved = Notice::saveNew($oprofile->profile_id,
+            $saved = Notice::saveNew($profile->id,
                                      $content,
                                      'ostatus',
                                      $options);
@@ -1077,17 +1072,17 @@ class Ostatus_profile extends Managed_DataObject
             return null;
         }
 
-        // Is it a known Ostatus profile?
-        $oprofile = Ostatus_profile::getKV('profile_id', $profile->id);
-        if ($oprofile instanceof Ostatus_profile) {
+        try {
+            $oprofile = self::getFromProfile($profile);
+            // We found the profile, return it!
             return $oprofile;
-        }
-
-        // Is it a local user?
-        $user = User::getKV('id', $profile->id);
-        if ($user instanceof User) {
-            // @todo i18n FIXME: use sprintf and add i18n (?)
-            throw new OStatusShadowException($profile, "'$profile_url' is the profile for local user '{$user->nickname}'.");
+        } catch (NoResultException $e) {
+            // Could not find an OStatus profile, is it instead a local user?
+            $user = User::getKV('id', $profile->id);
+            if ($user instanceof User) {
+                // @todo i18n FIXME: use sprintf and add i18n (?)
+                throw new OStatusShadowException($profile, "'$profile_url' is the profile for local user '{$user->nickname}'.");
+            }
         }
 
         // Continue discovery; it's a remote profile
@@ -1097,6 +1092,16 @@ class Ostatus_profile extends Managed_DataObject
         return null;
     }
 
+    static function getFromProfile(Profile $profile)
+    {
+        $oprofile = new Ostatus_profile();
+        $oprofile->profile_id = $profile->id;
+        if (!$oprofile->find(true)) {
+            throw new NoResultException($oprofile);
+        }
+        return $oprofile;
+    }
+
     /**
      * Look up and if necessary create an Ostatus_profile for remote entity
      * with the given update feed. This should never return null -- you will
@@ -2130,7 +2135,7 @@ class Ostatus_profile extends Managed_DataObject
         return $oprofile;
     }
 
-    function checkAuthorship($activity)
+    public function checkAuthorship(Activity $activity)
     {
         if ($this->isGroup() || $this->isPeopletag()) {
             // A group or propletag feed will contain posts from multiple authors.
@@ -2165,7 +2170,7 @@ class Ostatus_profile extends Managed_DataObject
             $oprofile = $this;
         }
 
-        return $oprofile;
+        return $oprofile->localProfile();
     }
 }