]> git.mxchange.org Git - quix0rs-gnu-social.git/commitdiff
shorten urls for posts > 140 chars only, from anywhere. Only show long urls in title...
authormillette <millette@controlyourself.ca>
Fri, 28 Nov 2008 21:01:14 +0000 (16:01 -0500)
committermillette <millette@controlyourself.ca>
Fri, 28 Nov 2008 21:01:14 +0000 (16:01 -0500)
darcs-hash:20081128210114-099f7-4e4cde0a983c2ac6d41efb59b46cb7dbf45dc7a6.gz

actions/newmessage.php
actions/newnotice.php
actions/postnotice.php
actions/twitapidirect_messages.php
actions/twitapistatuses.php
classes/Message.php
classes/Notice.php
lib/util.php
scripts/maildaemon.php
scripts/xmppdaemon.php

index d4e289465aa066b50a871e23e4170d1023492c53..67695210ec21ebf5db6add7234fbd1f0d2ee1f33 100644 (file)
@@ -51,13 +51,12 @@ class NewmessageAction extends Action {
                if (!$content) {
                        $this->show_form(_('No content!'));
                        return;
-//             } else if (mb_strlen($content) > 140) {
                } else {
-                       $content = common_shorten_links($content);
+                       $content_shortened = common_shorten_links($content);
 
-                       if (mb_strlen($content) > 140) {
-                               common_debug("Content = '$content'", __FILE__);
-                               common_debug("mb_strlen(\$content) = " . mb_strlen($content), __FILE__);
+                       if (mb_strlen($content_shortened) > 140) {
+                               common_debug("Content = '$content_shortened'", __FILE__);
+                               common_debug("mb_strlen(\$content) = " . mb_strlen($content_shortened), __FILE__);
                                $this->show_form(_('That\'s too long. Max message size is 140 chars.'));
                                return;
                        }
index a79d0a1a2f9e859b01353a97b5ce8cff778ff406..932099c605ef2c5265118ccce970fe762da4f767 100644 (file)
@@ -51,11 +51,11 @@ class NewnoticeAction extends Action {
                        $this->show_form(_('No content!'));
                        return;
                } else {
-                       $content = common_shorten_links($content);
+                       $content_shortened = common_shorten_links($content);
 
-                       if (mb_strlen($content) > 140) {
-                               common_debug("Content = '$content'", __FILE__);
-                               common_debug("mb_strlen(\$content) = " . mb_strlen($content), __FILE__);
+                       if (mb_strlen($content_shortened) > 140) {
+                               common_debug("Content = '$content_shortened'", __FILE__);
+                               common_debug("mb_strlen(\$content) = " . mb_strlen($content_shortened), __FILE__);
                                $this->show_form(_('That\'s too long. Max notice size is 140 chars.'));
                                return;
                        }
@@ -63,7 +63,7 @@ class NewnoticeAction extends Action {
 
                $inter = new CommandInterpreter();
 
-               $cmd = $inter->handle_command($user, $content);
+               $cmd = $inter->handle_command($user, $content_shortened);
 
                if ($cmd) {
                        $cmd->execute(new WebChannel());
index 243aa31633f172ef62b16a7ffe1d59ba99b9d723..243081f1222540eb00e7a828c1205e881536d4a5 100644 (file)
@@ -58,13 +58,11 @@ class PostnoticeAction extends Action {
                        return false;
                }
                $content = $req->get_parameter('omb_notice_content');
-//             if (!$content || strlen($content) > 140) {
-                       $content = common_shorten_links($content);
-                       if (mb_strlen($content) > 140) {
-                               common_user_error(_('Invalid notice content'), 400);
-                               return false;
-                       }
-//             }
+        $content_shortened = common_shorten_links($content);
+        if (mb_strlen($content_shortened) > 140) {
+            common_user_error(_('Invalid notice content'), 400);
+            return false;
+        }
                $notice_uri = $req->get_parameter('omb_notice');
                if (!Validate::uri($notice_uri) &&
                        !common_valid_tag($notice_uri)) {
index a04ae5fa76f0c795ea858051e062052f4768333a..a14a9e567c34757ac7b2b658868d124d3c8e20f1 100644 (file)
@@ -115,8 +115,8 @@ class Twitapidirect_messagesAction extends TwitterapiAction {
                        $this->client_error(_('No message text!'), $code = 406, $apidata['content-type']);
 //             } else if (mb_strlen($status) > 140) {
                } else {
-                       $status = common_shorten_links($status);
-                       if (mb_strlen($status) > 140) {
+                       $content_shortened = common_shorten_links($content);
+                       if (mb_strlen($content_shortened) > 140) {
                                $this->client_error(_('That\'s too long. Max message size is 140 chars.'),
                                        $code = 406, $apidata['content-type']);
                                return;
index 6d6d5266ffb80e99ee8ca3a0bdbfc935fefa4664..947d2103268c4455f68f314bba8a35a0e10dd54d 100644 (file)
@@ -255,9 +255,9 @@ class TwitapistatusesAction extends TwitterapiAction {
 //             } else if (mb_strlen($status) > 140) {
                } else {
                        
-                       $status = common_shorten_links($status);
+                       $status_shortened = common_shorten_links($status);
 
-                       if (mb_strlen($status) > 140) {
+                       if (mb_strlen($status_shortened) > 140) {
 
                                // XXX: Twitter truncates anything over 140, flags the status
                            // as "truncated."  Sending this error may screw up some clients
@@ -271,7 +271,7 @@ class TwitapistatusesAction extends TwitterapiAction {
 
                // Check for commands
                $inter = new CommandInterpreter();
-               $cmd = $inter->handle_command($user, $status);
+               $cmd = $inter->handle_command($user, $status_shortened);
 
                if ($cmd) {
 
index 8674f7f80c636784c981972f3312fdeebb577453..ef4bd03161cf2978c866acd4ed45f4af8043fb4c 100644 (file)
@@ -41,7 +41,7 @@ class Message extends Memcached_DataObject
                
                $msg->from_profile = $from;
                $msg->to_profile = $to;
-               $msg->content = $content;
+               $msg->content = common_shorten_links($content);
                $msg->rendered = common_render_text($content);
                $msg->created = common_sql_now();
                $msg->source = $source;
index ceec7a2fa6ccb5175c1c539085c67ddc8a43b2c7..83862ae9597f3e60474b44124cd204d364506cca 100644 (file)
@@ -127,7 +127,7 @@ class Notice extends Memcached_DataObject
 
                $notice->reply_to = $reply_to;
                $notice->created = common_sql_now();
-               $notice->content = $content;
+               $notice->content = common_shorten_links($content);
                $notice->rendered = common_render_content($notice->content, $notice);
                $notice->source = $source;
                $notice->uri = $uri;
index b14d121e6595d5b5bd5452f4aa06624a3b9d6382..c3d11b074cbc0980ba098278914bfa4c36952920 100644 (file)
@@ -837,10 +837,10 @@ function common_longurl2($uri)  {
 
 function common_shorten_links($text) {
     if (mb_strlen($text) <= 140) return $text;
+    static $cache = array();
+    if (isset($cache[$text])) return $cache[$text];
     // \s = not a horizontal whitespace character (since PHP 5.2.4)
-       // RYM this should prevent * preceded URLs from being processed but it its a char
-//     $r = preg_replace('@[^*](https?://[^)\]>\s]+)@e', "common_shorten_link('\\1')", $r);
-       return preg_replace('@https?://[^)\]>\s]+@e', "common_shorten_link('\\0')", $text);
+       return $cache[$text] = preg_replace('@https?://[^)\]>\s]+@e', "common_shorten_link('\\0')", $text);
 }
 
 function common_shorten_link($long_url) {
index 64f913bf924923f184d87ab03726be198deb5be9..8b809f646eb88c97d2020857bf40e6988aa0e804 100755 (executable)
@@ -120,6 +120,9 @@ class MailerDaemon {
        }
 
        function add_notice($user, $msg) {
+        // should test
+        // $msg_shortened = common_shorten_links($msg);
+        // if (mb_strlen($msg_shortened) > 140) ERROR and STOP
                $notice = Notice::saveNew($user->id, $msg, 'mail');
                if (is_string($notice)) {
                        $this->log(LOG_ERR, $notice);
@@ -209,4 +212,4 @@ class MailerDaemon {
 }
 
 $md = new MailerDaemon();
-$md->handle_message('php://stdin');
\ No newline at end of file
+$md->handle_message('php://stdin');
index 90506a0f42f848a464ee9e833ec4e665b315c96e..e55b9e3e9dbf7eeb82a64369184d7db174cb06ef 100755 (executable)
@@ -236,7 +236,15 @@ class XMPPDaemon extends Daemon {
        }
 
        function add_notice(&$user, &$pl) {
-               $notice = Notice::saveNew($user->id, trim(mb_substr($pl['body'], 0, 140)), 'xmpp');
+        $content_shortened = common_shorten_link($pl['body']);
+        if (mb_strlen($content_shortened) > 140) {
+            $content = trim(mb_substr($pl['body'], 0, 140));
+            $content_shortened = common_shorten_link($content);
+        }
+        else {
+            $content = $pl['body'];
+        }
+               $notice = Notice::saveNew($user->id, $content, 'xmpp');
                if (is_string($notice)) {
                        $this->log(LOG_ERR, $notice);
                        return;