]> git.mxchange.org Git - quix0rs-gnu-social.git/blobdiff - plugins/TwitterBridge/twitter.php
Removed unnecessary else statement
[quix0rs-gnu-social.git] / plugins / TwitterBridge / twitter.php
index e5afde62ca728771af2432038681a42d0e0704ab..896eee2dac485e87f9fd44434484a182290c48e4 100644 (file)
@@ -1,7 +1,7 @@
 <?php
 /*
  * StatusNet - the distributed open-source microblogging tool
- * Copyright (C) 2008, 2009, StatusNet, Inc.
+ * Copyright (C) 2008-2010 StatusNet, Inc.
  *
  * This program is free software: you can redistribute it and/or modify
  * it under the terms of the GNU Affero General Public License as published by
@@ -33,11 +33,15 @@ function add_twitter_user($twitter_id, $screen_name)
     // repoed, and things like that.
 
     $luser = Foreign_user::getForeignUser($twitter_id, TWITTER_SERVICE);
-    $result = $luser->delete();
 
-    if ($result != false) {
-        common_log(LOG_INFO,
-            "Twitter bridge - removed old Twitter user: $screen_name ($twitter_id).");
+    if (!empty($luser)) {
+        $result = $luser->delete();
+        if ($result != false) {
+            common_log(
+                LOG_INFO,
+                "Twitter bridge - removed old Twitter user: $screen_name ($twitter_id)."
+            );
+        }
     }
 
     $fuser = new Foreign_user();
@@ -120,15 +124,36 @@ function broadcast_twitter($notice)
     return true;
 }
 
+/**
+ * Pull any extra information from a notice that we should transfer over
+ * to Twitter beyond the notice text itself.
+ *
+ * @param Notice $notice
+ * @return array of key-value pairs for Twitter update submission
+ * @access private
+ */
+function twitter_update_params($notice)
+{
+    $params = array();
+    if ($notice->lat || $notice->lon) {
+        $params['lat'] = $notice->lat;
+        $params['long'] = $notice->lon;
+    }
+    return $params;
+}
+
+
 function broadcast_oauth($notice, $flink) {
     $user = $flink->getUser();
     $statustxt = format_status($notice);
+    $params = twitter_update_params($notice);
+
     $token = TwitterOAuthClient::unpackToken($flink->credentials);
     $client = new TwitterOAuthClient($token->key, $token->secret);
     $status = null;
 
     try {
-        $status = $client->statusesUpdate($statustxt);
+        $status = $client->statusesUpdate($statustxt, $params);
     } catch (OAuthClientException $e) {
         return process_error($e, $flink, $notice);
     }
@@ -167,12 +192,13 @@ function broadcast_basicauth($notice, $flink)
     $user = $flink->getUser();
 
     $statustxt = format_status($notice);
+    $params = twitter_update_params($notice);
 
     $client = new TwitterBasicAuthClient($flink);
     $status = null;
 
     try {
-        $status = $client->statusesUpdate($statustxt);
+        $status = $client->statusesUpdate($statustxt, $params);
     } catch (BasicAuthException $e) {
         return process_error($e, $flink, $notice);
     }
@@ -248,8 +274,17 @@ function format_status($notice)
     $statustxt = preg_replace('/^@/', ' @', $notice->content);
 
     // Convert !groups to #hashes
+
+    // XXX: Make this an optional setting?
+
     $statustxt = preg_replace('/(^|\s)!([A-Za-z0-9]{1,64})/', "\\1#\\2", $statustxt);
 
+    if (mb_strlen($statustxt) > 140) {
+        $noticeUrl = common_shorten_url($notice->uri);
+        $urlLen = mb_strlen($noticeUrl);
+        $statustxt = mb_substr($statustxt, 0, 140 - ($urlLen + 3)) . ' … ' . $noticeUrl;
+    }
+
     return $statustxt;
 }
 
@@ -260,7 +295,7 @@ function remove_twitter_link($flink)
     common_log(LOG_INFO, 'Removing Twitter bridge Foreign link for ' .
                "user $user->nickname (user id: $user->id).");
 
-    $result = $flink->delete();
+    $result = $flink->safeDelete();
 
     if (empty($result)) {
         common_log(LOG_ERR, 'Could not remove Twitter bridge ' .
@@ -300,10 +335,10 @@ function remove_twitter_link($flink)
 
 function mail_twitter_bridge_removed($user)
 {
-    common_init_locale($user->language);
-
     $profile = $user->getProfile();
 
+    common_switch_locale($user->language);
+
     $subject = sprintf(_m('Your Twitter bridge has been disabled.'));
 
     $site_name = common_config('site', 'name');
@@ -319,7 +354,7 @@ function mail_twitter_bridge_removed($user)
         common_local_url('twittersettings'),
         common_config('site', 'name'));
 
-    common_init_locale();
+    common_switch_locale();
     return mail_to_user($user, $subject, $body);
 }