]> 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 2a075300f2f9b6418de2db350a55b5d4e3695f1d..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
@@ -28,27 +28,22 @@ require_once INSTALLDIR . '/plugins/TwitterBridge/twitteroauthclient.php';
 
 function add_twitter_user($twitter_id, $screen_name)
 {
-    $new_uri = 'http://twitter.com/' . $screen_name;
-
     // Clear out any bad old foreign_users with the new user's legit URL
     // This can happen when users move around or fakester accounts get
     // repoed, and things like that.
 
-    $luser = new Foreign_user();
-    $luser->uri = $new_uri;
-    $luser->service = TWITTER_SERVICE;
-    $result = $luser->delete();
+    $luser = Foreign_user::getForeignUser($twitter_id, TWITTER_SERVICE);
 
-    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)."
+            );
+        }
     }
 
-    $luser->free();
-    unset($luser);
-
-    // Otherwise, create a new Twitter user
-
     $fuser = new Foreign_user();
 
     $fuser->nickname = $screen_name;
@@ -129,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);
     }
@@ -176,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);
     }
@@ -257,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;
 }
 
@@ -269,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 ' .
@@ -309,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');
@@ -328,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);
 }