Browse files

Tweets table updates. Dropped 'status', instead look for empty twitte…

…r_id to indicate whether a tweet was sent successfully. Also changed 'created' to 'sent' and only set it after a tweet is successfully sent.
  • Loading branch information...
1 parent 1234f8c commit b8d92a59af828a6a4fff11329fa65711d422466d @ckieffer ckieffer committed Feb 13, 2011
View
117 3.1/modules/twitter/controllers/twitter.php
@@ -54,13 +54,11 @@ public function callback() {
// If HTTP response is 200 continue otherwise send to connect page to retry
if (200 == $connection->http_code) {
- // The user has been verified and the access tokens can be saved for future use
- $this->save_user($access_token);
- // Redirect to the tweet form
+ $this->_save_user($access_token);
$item = ORM::factory("item", $item_id);
url::redirect(url::abs_site($item_url));
} else {
- // @todo Log HTTP status for application log and/or error message
+ log::error("content", "Twitter", "Unable to retrieve user access token: " . $connection->http_code);
$this->_clear_session();
url::redirect(url::site("twitter/redirect"));
}
@@ -101,7 +99,7 @@ public function dialog($item_id) {
/**
* Redirect user to Twitter authorization page.
*/
- function redirect() {
+ public function redirect() {
require_once(MODPATH . "twitter/lib/twitteroauth.php");
$consumer_key = module::get_var("twitter", "consumer_key");
@@ -136,44 +134,10 @@ function redirect() {
}
/**
- * Save tweets sent and those not sent because of Twitter API issues
- * @param integer $item_id
- * @param object $tweet The tweet sent, or the tweet that couldn't be sent
- */
- public function save_tweet($tweet) {
- if (!empty($tweet->item_id) && !empty($tweet->tweet) && !empty($tweet->status)) {
- $t = ORM::factory("twitter_tweet");
- $t->created = time();
- $t->item_id = $tweet->item_id;
- $t->twitter_id = $tweet->twitter_id;
- $t->tweet = $tweet->tweet;
- $t->status = $tweet->status;
- $t->user_id = identity::active_user()->id;
- $t->save();
- }
- }
-
- /**
- * Save or update the current user's Twitter credentials.
- * @param array $access_token
- * @todo Ensure only one record per twitter_screen_name
- */
- function save_user($access_token) {
- $u = ORM::factory("twitter_user");
- $u->oauth_token = $access_token["oauth_token"];
- $u->oauth_token_secret = $access_token["oauth_token_secret"];
- $u->twitter_user_id = $access_token["user_id"];
- $u->screen_name = $access_token["screen_name"];
- $u->user_id = identity::active_user()->id;
- $u->save();
-
- message::success(t("Twitter access tokens saved!"));
- }
-
- /**
* Post a status update to Twitter
- * @param string $message
+ * @param int $item_id
* @todo Update previously failed tweet, if one exists
+ * @todo Display errors in Tweet dialog
*/
public function tweet($item_id) {
access::verify_csrf();
@@ -183,7 +147,7 @@ public function tweet($item_id) {
if ($form->validate()) {
$item_url = url::abs_site($item->relative_url_cache);
- $u = $this->_get_twitter_user(identity::active_user()->id);
+ $user = $this->_get_twitter_user(identity::active_user()->id);
$consumer_key = module::get_var("twitter", "consumer_key");
$consumer_secret = module::get_var("twitter", "consumer_secret");
@@ -192,28 +156,25 @@ public function tweet($item_id) {
$connection = new TwitterOAuth(
$consumer_key,
$consumer_secret,
- $u->oauth_token,
- $u->oauth_token_secret);
+ $user->oauth_token,
+ $user->oauth_token_secret);
$message = $form->twitter_message->tweet->value;
$response = $connection->post('statuses/update', array('status' => $message));
if (200 == $connection->http_code) {
- $status = 1;
message::success(t("Tweet sent!"));
json::reply(array("result" => "success", "location" => $item->url()));
} else {
- $status = 0;
- log::error("content", "Twitter", "Unable to sent tweet, response code: " . $connection->http_code);
+ log::error("content", "Twitter", "Unable to send tweet: " . $connection->http_code);
message::error(t("Unable to send Tweet. Your message has been saved. Please try again later."));
json::reply(array("result" => "error", "html" => (string)$form));
}
$tweet->item_id = $item_id;
- $tweet->twitter_id = $response->id;
+ (!empty($response->id)) ? $tweet->twitter_id = $response->id : $tweet->twitter_id = NULL;
$tweet->tweet = $message;
- $tweet->status = $status;
- $this->save_tweet($tweet);
+ $this->_save_tweet($tweet);
$this->_delete_failed($item_id);
} else {
@@ -222,14 +183,6 @@ public function tweet($item_id) {
}
/**
- *
- * @param <type> $tweet
- */
- function update_tweet($tweet) {
-
- }
-
- /**
* Clear Twitter module session variables
*/
private function _clear_session() {
@@ -240,8 +193,7 @@ private function _clear_session() {
/**
* Delete all failed tweets by the current user for an item
- * @param integer $item_id
- * @todo Not implemented
+ * @param int $item_id
*/
private function _delete_failed($item_id) {
if (is_numeric($item_id)) {
@@ -250,14 +202,14 @@ private function _delete_failed($item_id) {
->delete("twitter_tweets")
->where("user_id", "=", $user_id)
->where("item_id", "=", $item_id)
- ->where("status", "=", 0)
+ ->where("twitter_id", "=", "")
->execute();
}
}
/**
* Get Twitter credentials for the current user.
- * @param int $user_id
+ * @param int $user_id
* @return mixed object|false
*/
private function _get_twitter_user($user_id) {
@@ -281,4 +233,45 @@ private function _is_token_set($user_id) {
return false;
}
+ /**
+ * Save new tweets
+ * @param object $tweet
+ */
+ private function _save_tweet($tweet) {
+ if (!empty($tweet->item_id) && !empty($tweet->tweet)) {
+ $t = ORM::factory("twitter_tweet");
+ $t->item_id = $tweet->item_id;
+ $t->twitter_id = $tweet->twitter_id;
+ $t->tweet = $tweet->tweet;
+ $t->sent = (!empty($tweet->twitter_id)) ? time() : NULL;
+ $t->user_id = identity::active_user()->id;
+ $t->save();
+ }
+ }
+
+ /**
+ * Save or update the current user's Twitter credentials.
+ * @param array $access_token
+ * @todo Ensure only one record per twitter_screen_name
+ */
+ private function _save_user($access_token) {
+ $u = ORM::factory("twitter_user");
+ $u->oauth_token = $access_token["oauth_token"];
+ $u->oauth_token_secret = $access_token["oauth_token_secret"];
+ $u->twitter_user_id = $access_token["user_id"];
+ $u->screen_name = $access_token["screen_name"];
+ $u->user_id = identity::active_user()->id;
+ $u->save();
+
+ message::success(t("Twitter access tokens saved!"));
+ }
+
+ /**
+ * Update a previously failed tweet
+ * @param object $tweet
+ */
+ private function _update_tweet($tweet) {
+
+ }
+
}
View
3 3.1/modules/twitter/helpers/twitter.php
@@ -98,14 +98,13 @@ static function get_tweet_form($item) {
* Get the most recent failed tweet for an item
* @param integer $item_id
* @return mixed object|false
- * @todo Not implemented
*/
function get_failed($item_id) {
$user_id = identity::active_user()->id;
$t = ORM::factory("twitter_tweet")
->where("item_id", "=", $item_id)
->where("user_id", "=", $user_id)
- ->where("status", "=", 0)
+ ->where("twitter_id", "=", "")
->find();
if ($t->loaded()) {
return $t->tweet;
View
5 3.1/modules/twitter/helpers/twitter_installer.php
@@ -23,12 +23,11 @@ static function install() {
Database::instance()
->query("CREATE TABLE {twitter_tweets} (
`id` int(9) NOT NULL AUTO_INCREMENT,
- `created` int(9) NOT NULL,
`item_id` int(9) NOT NULL,
- `status` tinyint(1) NOT NULL,
`twitter_id` decimal(20,0) NULL,
`tweet` varchar(140) NOT NULL,
- `user_id` int(9) NOT NULL,
+ `sent` int(9) NULL,
+ `created` int(9) NOT NULL,
PRIMARY KEY (`id`))
DEFAULT CHARSET=utf8;");
Database::instance()

0 comments on commit b8d92a5

Please sign in to comment.