]> git.mxchange.org Git - quix0rs-gnu-social.git/commitdiff
Twitter-integration - Twitter settings tab now saves Twitter credentials
authorzach <zach@controlyourself.ca>
Tue, 26 Aug 2008 22:56:15 +0000 (18:56 -0400)
committerzach <zach@controlyourself.ca>
Tue, 26 Aug 2008 22:56:15 +0000 (18:56 -0400)
darcs-hash:20080826225615-462f3-8d881eda7be43623e10b83e8d1e157f4096734cd.gz

actions/twittersettings.php
classes/Foreign_user.php

index 95649f70661e10711b6115bda9b171e2425a0576..1389666f97cc1feed84a1a01fe8970a1f54ca051 100644 (file)
@@ -67,17 +67,59 @@ class TwittersettingsAction extends SettingsAction {
                        return;
                }
                
+               // Verify this is a real Twitter user.
                if (!$this->verify_credentials($twitter_username, $twitter_password)) {
                        $this->show_form(_('Could not verify your Twitter credentials!'));
                        return;
                }
                
-
+               // Now that we have a valid Twitter user, we have to make another api call to 
+               // find its Twitter ID.
+               $twitter_id = $this->get_twitter_id($twitter_username);
+               
+               if (!$twitter_id) {
+                       $this->show_form(sprintf(_('Unable to retrieve account information for "%s" from Twitter.'), $twitter_username));
+                       return;
+               }
+                       
                $user = common_current_user();
-
+                               
+               $fuser = Foreign_user::save(
+                               array(
+                                       'id' => $twitter_id,
+                                       'service' => '0', // Twitter
+                                       'uri' => "http://www.twitter.com/$twitter_username",
+                                       'nickname' => $twitter_username, 
+                                       'user_id' => $user->id,
+                                       'credentials' => $twitter_password
+                               ));
+               
+               if (!$fuser) {
+                       $this->show_form(_('Unable to save your Twitter credentials!'));
+               }
+               
                $this->show_form(_('Twitter settings saved.'), true);
+       }
+
+       function get_twitter_id($twitter_username) {
+               
+               $uri = "http://twitter.com/users/show/$twitter_username.json";
+               
+               common_debug("uri; $uri");
+                       
+               $data = $this->get_twitter_data($uri);
+               
+               if (!$data) {
+                       return NULL;
+               }
                
+               $user = json_decode($data);
 
+               if (!$user) {
+                       return NULL;
+               }
+               
+               return $user->id;
        }
 
        function verify_credentials($user, $password) {
@@ -103,7 +145,7 @@ class TwittersettingsAction extends SettingsAction {
        }
        
        // PHP's cURL the best thing to use here? -- Zach
-       function get_twitter_data($uri, $user, $password) {
+       function get_twitter_data($uri, $user=NULL, $password=NULL) {
                $options = array(
                                CURLOPT_USERPWD => "$user:$password",
                                CURLOPT_RETURNTRANSFER  => true,
index d19370aa01430167953dc62bd90a91f2d52881e9..25a6ac979fcbecc0fdb0a0c6d14407c7220a4306 100644 (file)
@@ -22,4 +22,29 @@ class Foreign_user extends DB_DataObject
 
     /* the code above is auto generated do not remove the tag below */
     ###END_AUTOCODE
+       
+       static function save($fields) {
+               
+               extract($fields);
+                               
+               $fuser = new Foreign_user();
+               
+               $fuser->id = $id;
+               $fuser->service = $service;
+               $fuser->uri = $uri;
+               $fuser->nickname = $nickname;           
+               $fuser->user_id = $user_id;
+               $fuser->credentials = $credentials;
+               $fuser->created = common_sql_now();
+               
+               $result = $fuser->insert();
+
+               if (!$result) {
+                       common_log_db_error($fuser, 'INSERT', __FILE__);
+                       return FALSE;
+               }
+
+               return $fuser;
+       }
+       
 }