]> git.mxchange.org Git - quix0rs-gnu-social.git/commitdiff
Refactor and centralize notice source link calculation
authorZach Copley <zach@status.net>
Thu, 6 May 2010 07:20:10 +0000 (00:20 -0700)
committerZach Copley <zach@status.net>
Tue, 18 May 2010 22:15:27 +0000 (15:15 -0700)
actions/twitapisearchatom.php
classes/Notice.php
lib/apiaction.php
lib/noticelist.php

index 24aa619bd71ff00abe81b9b7c6fba1f87763e04e..3eb54ccc36fe27b09f7a920c6ee41fdd0c107767 100644 (file)
@@ -342,10 +342,21 @@ class TwitapisearchatomAction extends ApiAction
                                      'rel' => 'related',
                                      'href' => $profile->avatarUrl()));
 
-        // TODO: Here is where we'd put in a link to an atom feed for threads
+        // @todo: Here is where we'd put in a link to an atom feed for threads
+
+        $source = null;
+
+        $ns = $notice->getSource();
+        if ($ns) {
+            if (!empty($ns->name) && !empty($ns->url)) {
+                $source = '<a href="' . $ns->url . '">' . $ns->name . '</a>';
+            } else {
+                $source = $ns->code;
+            }
+        }
 
         $this->element("twitter:source", null,
-            htmlentities($this->sourceLink($notice->source)));
+            htmlentities($source));
 
         $this->elementStart('author');
 
index 9a9172cbab519f3beb7acd46f4ea1bbe3332e658..2c2c87d560ac06ce2424a5e536cc3ee1411a7e0c 100644 (file)
@@ -703,7 +703,7 @@ class Notice extends Memcached_DataObject
 
     /**
      * Is this notice part of an active conversation?
-     * 
+     *
      * @return boolean true if other messages exist in the same
      *                 conversation, false if this is the only one
      */
@@ -1809,4 +1809,41 @@ class Notice extends Memcached_DataObject
 
         return $result;
     }
+
+    /**
+     * Get the source of the notice
+     *
+     * @return Notice_source $ns A notice source object. 'code' is the only attribute
+     *                           guaranteed to be populated.
+     */
+    function getSource()
+    {
+        $ns = new Notice_source();
+        if (!empty($this->source)) {
+            switch ($this->source) {
+            case 'web':
+            case 'xmpp':
+            case 'mail':
+            case 'omb':
+            case 'system':
+            case 'api':
+                $ns->code = $this->source;
+                break;
+            default:
+                $ns = Notice_source::staticGet($this->source);
+                if (!$ns) {
+                    $ns = new Notice_source();
+                    $ns->code = $this->source;
+                    $app = Oauth_application::staticGet('name', $this->source);
+                    if ($app) {
+                        $ns->name = $app->name;
+                        $ns->url  = $app->source_url;
+                    }
+                }
+                break;
+            }
+        }
+        return $ns;
+    }
+
 }
index f87b04611423f4e9e2a231af51034ecd4f944d50..7a6a5549b21c3e71bd718149f51c8429b8408ecc 100644 (file)
@@ -260,7 +260,19 @@ class ApiAction extends Action
         $twitter_status['created_at'] = $this->dateTwitter($notice->created);
         $twitter_status['in_reply_to_status_id'] = ($notice->reply_to) ?
             intval($notice->reply_to) : null;
-        $twitter_status['source'] = $this->sourceLink($notice->source);
+
+        $source = null;
+
+        $ns = $notice->getSource();
+        if ($ns) {
+            if (!empty($ns->name) && !empty($ns->url)) {
+                $source = '<a href="' . $ns->url . '">' . $ns->name . '</a>';
+            } else {
+                $source = $ns->code;
+            }
+        }
+
+        $twitter_status['source'] = $source;
         $twitter_status['id'] = intval($notice->id);
 
         $replier_profile = null;
@@ -1298,43 +1310,6 @@ class ApiAction extends Action
         }
     }
 
-    function sourceLink($source)
-    {
-        $source_name = _($source);
-        switch ($source) {
-        case 'web':
-        case 'xmpp':
-        case 'mail':
-        case 'omb':
-        case 'api':
-            break;
-        default:
-
-            $name = null;
-            $url  = null;
-
-            $ns = Notice_source::staticGet($source);
-
-            if ($ns) {
-                $name = $ns->name;
-                $url  = $ns->url;
-            } else {
-                $app = Oauth_application::staticGet('name', $source);
-                if ($app) {
-                    $name = $app->name;
-                    $url  = $app->source_url;
-                }
-            }
-
-            if (!empty($name) && !empty($url)) {
-                $source_name = '<a href="' . $url . '">' . $name . '</a>';
-            }
-
-            break;
-        }
-        return $source_name;
-    }
-
     /**
      * Returns query argument or default value if not found. Certain
      * parameters used throughout the API are lightly scrubbed and
index 4f997a3286a78b339644c8a7dd72f32fd31e07ed..e0d8bf560c86eebfe4bb64d022dee4a495c2428a 100644 (file)
@@ -480,48 +480,44 @@ class NoticeListItem extends Widget
 
     function showNoticeSource()
     {
-        if ($this->notice->source) {
+        $ns = $this->notice->getSource();
+
+        if ($ns) {
+            $source_name = _($ns->code);
             $this->out->text(' ');
             $this->out->elementStart('span', 'source');
             $this->out->text(_('from'));
-            $source_name = _($this->notice->source);
             $this->out->text(' ');
-            switch ($this->notice->source) {
-             case 'web':
-             case 'xmpp':
-             case 'mail':
-             case 'omb':
-             case 'system':
-             case 'api':
-                $this->out->element('span', 'device', $source_name);
-                break;
-             default:
 
-                $name = $source_name;
-                $url  = null;
+            // if $ns->name and $ns->url are populated we have
+            // configured a source attr somewhere
+            if (empty($ns->name) && empty($ns->url)) {
+                // otherwise it's from normal channel such as web or api
+                $this->out->element('span', 'device', $source_name);
+            } else {
+                $name  = null;
+                $url   = null;
+                $title = null;
 
                 if (Event::handle('StartNoticeSourceLink', array($this->notice, &$name, &$url, &$title))) {
-                    $ns = Notice_source::staticGet($this->notice->source);
-
-                    if ($ns) {
-                        $name = $ns->name;
-                        $url  = $ns->url;
-                    } else {
-                        $app = Oauth_application::staticGet('name', $this->notice->source);
-                        if ($app) {
-                            $name = $app->name;
-                            $url  = $app->source_url;
-                        }
-                    }
+                    $name = $source_name;
+                    $url  = $ns->url;
                 }
                 Event::handle('EndNoticeSourceLink', array($this->notice, &$name, &$url, &$title));
 
                 if (!empty($name) && !empty($url)) {
                     $this->out->elementStart('span', 'device');
-                    $this->out->element('a', array('href' => $url,
-                                                   'rel' => 'external',
-                                                   'title' => $title),
-                                        $name);
+
+                    $attrs = array(
+                        'href' => $url,
+                        'rel' => 'external'
+                    );
+
+                    if (isset($title)) {
+                        $attrs['title'] = $title;
+                    }
+
+                    $this->out->element('a', $attrs, $name);
                     $this->out->elementEnd('span');
                 } else {
                     $this->out->element('span', 'device', $name);