return;
}
- $fuser = DB_DataObject::factory('foreign_user');
- $fuser->id = $twitter_id;
- $fuser->service = 1; // Twitter
- $fuser->uri = "http://www.twitter.com/$twitter_username";
- $fuser->nickname = $twitter_username;
- $fuser->created = common_sql_now();
- $result = $fuser->insert();
+ $fuser = null;
+ $result = null;
+
+ // Check to see whether the Twitter user is already in the system,
+ // and update its username and uri if so.
+ $fuser = Foreign_User::getForeignUser($twitter_id, 1);
+
+ if ($fuser) {
+
+ $original = clone($fuser);
+
+ $fuser->nickname = $twitter_username;
+ $fuser->uri = "http://www.twitter.com/$twitter_username";
+
+ $result = $fuser->updateKeys($original);
+
+ if (!$result) {
+ common_log_db_error($fuser, 'UPDATE', __FILE__);
+ }
+
+ } else {
+
+ // Otherwise, add the Twitter user
+ $fuser = DB_DataObject::factory('foreign_user');
+
+ $fuser->nickname = $twitter_username;
+ $fuser->uri = "http://www.twitter.com/$twitter_username";
+ $fuser->id = $twitter_id;
+ $fuser->service = 1; // Twitter
+ $fuser->created = common_sql_now();
+ $result = $fuser->insert();
+
+ if (!$result) {
+ common_log_db_error($fuser, 'INSERT', __FILE__);
+ }
+
+ }
if (!$result) {
- common_log_db_error($fuser, 'INSERT', __FILE__);
$this->show_form(_('Unable to save your Twitter settings!'));
return;
}
$flink->created = common_sql_now();
$this->set_flags($flink, $noticesync, $replysync, $friendsync);
-
+
$flink_id = $flink->insert();
if (!$flink_id) {
// For now we assume one Twitter acct per Laconica acct
$flink = Foreign_link::getForeignLink($user->id, 1);
- $fuser = Foreign_user::getForeignUser($flink->foreign_id, 1);
$flink_foreign_id = $this->arg('flink_foreign_id');
if (!$flink) {
return;
}
- $result = $fuser->delete();
-
- if (!$result) {
- common_log_db_error($fuser, 'DELETE', __FILE__);
- $this->show_form(_('Couldn\'t remove Twitter user.'));
- return;
- }
-
$result = $flink->delete();
if (!$result) {
$flink->query('BEGIN');
$original = clone($flink);
-
+
$this->set_flags($flink, $noticesync, $replysync, $friendsync);
$result = $flink->update($original);
} else {
$flink->noticesync &= ~FOREIGN_NOTICE_SEND;
}
-
+
if ($replysync) {
$flink->noticesync |= FOREIGN_NOTICE_SEND_REPLY;
} else {
} else {
$flink->friendsync &= ~FOREIGN_FRIEND_RECV;
}
-
+
$flink->profilesync = 0; // XXX: leave as default?
}
}
\ No newline at end of file
return NULL;
}
+ function updateKeys(&$orig) {
+ $parts = array();
+ foreach (array('id', 'service', 'uri', 'nickname') as $k) {
+ if (strcmp($this->$k, $orig->$k) != 0) {
+ $parts[] = $k . ' = ' . $this->_quote($this->$k);
+ }
+ }
+ if (count($parts) == 0) {
+ # No changes
+ return true;
+ }
+ $toupdate = implode(', ', $parts);
+
+ $table = $this->tableName();
+ if(common_config('db','quote_identifiers')) {
+ $table = '"' . $table . '"';
+ }
+ $qry = 'UPDATE ' . $table . ' SET ' . $toupdate .
+ ' WHERE id = ' . $this->id;
+ $orig->decache();
+ $result = $this->query($qry);
+ if ($result) {
+ $this->encache();
+ }
+ return $result;
+ }
+
+
}