]> git.mxchange.org Git - quix0rs-gnu-social.git/commitdiff
Twitter integration - Notices now broadcast (directly) to Twitter from linked accts.
authorZach Copley <zach@controlyourself.ca>
Thu, 28 Aug 2008 19:25:09 +0000 (15:25 -0400)
committerZach Copley <zach@controlyourself.ca>
Thu, 28 Aug 2008 19:25:09 +0000 (15:25 -0400)
darcs-hash:20080828192509-7b5ce-8387c67500c082eb5a0107c0f78d4cf5620825af.gz

classes/Foreign_link.php
lib/util.php

index 4fcad4b171fffccfc4a0844e643e970972593653..f4f8abfc410b77312f0bc9eb109ff5d58325a079 100644 (file)
@@ -29,7 +29,6 @@ class Foreign_link extends DB_DataObject
        // XXX:  This only returns a 1->1 single obj mapping.  Change?  Or make
        // a getForeignUsers() that returns more than one? --Zach
        static function getForeignLink($user_id, $service) {
-               
                $flink = new Foreign_link();
                $flink->whereAdd("service = $service");
                $flink->whereAdd("user_id = $user_id");
@@ -43,4 +42,19 @@ class Foreign_link extends DB_DataObject
                return NULL;            
        }
        
+       // Convenience method
+       function getForeignUser() {             
+               $fuser = new Foreign_user();
+               $fuser->whereAdd('service = ' . $this->service);
+               $fuser->whereAdd('id = ' . $this->foreign_id);
+               $fuser->limit(1);
+               
+               if ($fuser->find()) {
+                       $fuser->fetch();
+                       return $fuser;
+               }
+               
+               return NULL;            
+       }
+               
 }
index 0ac679afc879afdf53d4a83125132ef5269712c4..016821c57af46f468047fcbb103cc04debdd9300 100644 (file)
@@ -1088,6 +1088,16 @@ function common_save_replies($notice) {
 }
 
 function common_broadcast_notice($notice, $remote=false) {
+       
+       // Check to see if notice should go to Twitter
+       $flink = Foreign_link::getForeignLink($notice->profile_id, 1); // 1 == Twitter
+       
+       if ($flink) {
+               if (!common_twitter_broadcast($notice, $flink)) {
+                       common_debug('Unable to send notice: ' . $notice->id . ' to Twitter.', __FILE__);
+               }
+       }
+       
        if (common_config('queue', 'enabled')) {
                # Do it later!
                return common_enqueue_notice($notice);
@@ -1096,6 +1106,61 @@ function common_broadcast_notice($notice, $remote=false) {
        }
 }
 
+function common_twitter_broadcast($notice, $flink) {   
+       $success = true;
+       $fuser = $flink->getForeignUser();
+       $twitter_user = $fuser->nickname;
+       $twitter_password = $flink->credentials;
+       $uri = 'http://www.twitter.com/statuses/update.json';
+       $statustxt = $notice->content;
+       
+       $options = array(
+               CURLOPT_USERPWD                 => "$twitter_user:$twitter_password",
+               CURLOPT_POST                    => true,
+               CURLOPT_POSTFIELDS              => array(
+                                                                       'status'        => $statustxt,
+                                                                       'source'        => 'Laconica'
+                                                                       ),
+               CURLOPT_RETURNTRANSFER  => true,
+               CURLOPT_FAILONERROR             => true,
+               CURLOPT_HEADER                  => false,
+               CURLOPT_FOLLOWLOCATION  => true,
+               // CURLOPT_USERAGENT            => "identi.ca",
+               CURLOPT_CONNECTTIMEOUT  => 120,  // XXX: Scary!!!! How long should this be?
+               CURLOPT_TIMEOUT                 => 120
+       );
+       
+       $ch = curl_init($uri);
+    curl_setopt_array($ch, $options);
+    $data = curl_exec($ch);
+    $errmsg = curl_error($ch);
+
+       if ($errmsg) {
+               common_debug("cURL error: $errmsg - trying to send notice for $twitter_user.", 
+                       __FILE__);
+               $success = false;
+       }
+
+       curl_close($ch);
+       
+       if (!$data) {
+               common_debug("No data returned by Twitter's API trying to send update for $twitter_user",
+                       __FILE__);
+               $success = false;
+       }
+
+       // Twitter should return a status       
+       $status = json_decode($data);
+
+       if (!$status->id) {
+               common_debug("Unexpected data returned by Twitter API trying to send update for $twitter_user",
+                       __FILE__);
+               $success = false;
+       }
+       
+       return $status;
+}
+
 # Stick the notice on the queue
 
 function common_enqueue_notice($notice) {