]> git.mxchange.org Git - quix0rs-gnu-social.git/blobdiff - classes/Notice.php
Improve ShownoticeAction remote redirect code
[quix0rs-gnu-social.git] / classes / Notice.php
index fe1442ea97d1c0db188a4145fdf9d5028270b6f7..e46ed227a179cbb4cd4f9119a2b2bcb4a7e13ffd 100644 (file)
@@ -213,7 +213,25 @@ class Notice extends Managed_DataObject
     public function getUrl()
     {
         // The risk is we start having empty urls and non-http uris...
-        return $this->url ?: $this->uri;
+        // and we can't really handle any other protocol right now.
+        switch (true) {
+        case common_valid_http_url($this->url): // should we allow non-http/https URLs?
+            return $this->url;
+        case $this->isLocal():
+            // let's generate a valid link to our locally available notice on demand
+            return common_local_url('shownotice', array('notice' => $this->id), null, null, false);
+        case common_valid_http_url($this->uri):
+            return $this->uri;
+        default:
+            common_debug('No URL available for notice: id='.$this->id);
+            throw new InvalidUrlException($this->url);
+        }
+    }
+
+    public function get_object_type($canonical=false) {
+        return $canonical
+                ? ActivityObject::canonicalType($this->object_type)
+                : $this->object_type;
     }
 
     public static function getByUri($uri)
@@ -308,7 +326,7 @@ class Notice extends Managed_DataObject
      *              int 'location_ns' geoname namespace to interpret location_id
      *              int 'reply_to'; notice ID this is a reply to
      *              int 'repeat_of'; notice ID this is a repeat of
-     *              string 'uri' unique ID for notice; defaults to local notice URL
+     *              string 'uri' unique ID for notice; a unique tag uri (can be url or anything too)
      *              string 'url' permalink to notice; defaults to local notice URL
      *              string 'rendered' rendered HTML version of content
      *              array 'replies' list of profile URIs for reply delivery in
@@ -410,6 +428,16 @@ class Notice extends Managed_DataObject
             $notice->created = common_sql_now();
         }
 
+        if (!$notice->isLocal()) {
+            // Only do these checks for non-local notices. Local notices will generate these values later.
+            if (!common_valid_http_url($url)) {
+                common_debug('Bad notice URL: ['.$url.'], URI: ['.$uri.']. Cannot link back to original! This is normal for shared notices etc.');
+            }
+            if (empty($uri)) {
+                throw new ServerException('No URI for remote notice. Cannot accept that.');
+            }
+        }
+
         $notice->content = $final;
 
         $notice->source = $source;
@@ -578,8 +606,13 @@ class Notice extends Managed_DataObject
 
             $changed = false;
 
-            if (empty($uri)) {
-                $notice->uri = common_notice_uri($notice);
+            // We can only get here if it's a local notice, since remote notices
+            // should've bailed out earlier due to lacking a URI.
+            if (empty($notice->uri)) {
+                $notice->uri = sprintf('%s%s=%d:%s=%s',
+                                    TagURI::mint(),
+                                    'noticeId', $notice->id,
+                                    'objectType', $notice->get_object_type(true));
                 $changed = true;
             }
 
@@ -979,30 +1012,23 @@ class Notice extends Managed_DataObject
             }
         }
 
-        if (is_null($groups)) {
-            $groups = $this->getGroups();
-        }
-
         if (is_null($recipients)) {
             $recipients = $this->getReplies();
         }
 
-        $users = $this->getSubscribedUsers();
-        $ptags = $this->getProfileTags();
-
-        // FIXME: kind of ignoring 'transitional'...
-        // we'll probably stop supporting inboxless mode
-        // in 0.9.x
-
         $ni = array();
 
         // Give plugins a chance to add folks in at start...
         if (Event::handle('StartNoticeWhoGets', array($this, &$ni))) {
 
+            $users = $this->getSubscribedUsers();
             foreach ($users as $id) {
                 $ni[$id] = NOTICE_INBOX_SOURCE_SUB;
             }
 
+            if (is_null($groups)) {
+                $groups = $this->getGroups();
+            }
             foreach ($groups as $group) {
                 $users = $group->getUserMembers();
                 foreach ($users as $id) {
@@ -1012,12 +1038,10 @@ class Notice extends Managed_DataObject
                 }
             }
 
-            foreach ($ptags as $ptag) {
-                $users = $ptag->getUserSubscribers();
-                foreach ($users as $id) {
-                    if (!array_key_exists($id, $ni)) {
-                        $ni[$id] = NOTICE_INBOX_SOURCE_PROFILE_TAG;
-                    }
+            $ptAtts = $this->getAttentionsFromProfileTags();
+            foreach ($ptAtts as $key=>$val) {
+                if (!array_key_exists($key, $ni)) {
+                    $ni[$key] = $val;
                 }
             }
 
@@ -1104,6 +1128,19 @@ class Notice extends Managed_DataObject
         return $ptags;
     }
 
+    public function getAttentionsFromProfileTags()
+    {
+        $ni = array();
+        $ptags = $this->getProfileTags();
+        foreach ($ptags as $ptag) {
+            $users = $ptag->getUserSubscribers();
+            foreach ($users as $id) {
+                $ni[$id] = NOTICE_INBOX_SOURCE_PROFILE_TAG;
+            }
+        }
+        return $ni;
+    }
+
     /**
      * Record this notice to the given group inboxes for delivery.
      * Overrides the regular parsing of !group markup.