diff --git a/twitter.inc b/twitter.inc index 8c959e7..1448cfc 100644 --- a/twitter.inc +++ b/twitter.inc @@ -4,7 +4,6 @@ * @file * Twitter API functions */ - module_load_include('php', 'oauth_common', 'lib/OAuth'); require_once('twitter.lib.php'); @@ -30,7 +29,7 @@ function twitter_connect($account = NULL) { $auth = $account->get_auth(); if (isset($auth['oauth_token']) && isset($auth['oauth_token_secret'])) { return new Twitter(variable_get('twitter_consumer_key', ''), variable_get('twitter_consumer_secret', ''), - $auth['oauth_token'], $auth['oauth_token_secret']); + $auth['oauth_token'], $auth['oauth_token_secret']); } } return FALSE; @@ -58,9 +57,9 @@ function twitter_account_save($twitter_user, $save_auth = FALSE) { } } db_merge('twitter_account') - ->key(array('twitter_uid' => $values['twitter_uid'])) - ->fields($values) - ->execute(); + ->key(array('twitter_uid' => $values['twitter_uid'])) + ->fields($values) + ->execute(); // Notify other modules of the twitter account save module_invoke_all('twitter_account_save', $values); @@ -79,13 +78,13 @@ function twitter_account_load($id) { if ($values = db_query('SELECT * FROM {twitter_account} WHERE twitter_uid = :id - OR screen_name = :id', - array(':id' => $id))->fetchAssoc()) { + OR screen_name = :id', array(':id' => $id))->fetchAssoc()) { $values['id'] = $values['twitter_uid']; $account = new TwitterUser($values); $account->set_auth($values); $account->import = $values['import']; $account->mentions = $values['mentions']; + $account->favorites = $values['favorites']; $account->is_global = $values['is_global']; return $account; } @@ -137,8 +136,7 @@ function twitter_load_authenticated_accounts() { * An instance of stdClass object with the Tweet data or FALSE. */ function twitter_status_load($status_id) { - return db_query("SELECT * FROM {twitter} WHERE twitter_id = :status_id", - array(':status_id' => $status_id))->fetchObject(); + return db_query("SELECT * FROM {twitter} WHERE twitter_id = :status_id", array(':status_id' => $status_id))->fetchObject(); } /** @@ -157,14 +155,43 @@ function twitter_status_save($status) { 'truncated' => (int) $status->truncated, ); db_merge('twitter') - ->key(array('twitter_id' => $row['twitter_id'])) - ->fields($row) - ->execute(); + ->key(array('twitter_id' => $row['twitter_id'])) + ->fields($row) + ->execute(); // Let other modules know that a status has been saved. module_invoke_all('twitter_status_save', $status); } /** + * Fetches user's favorites. + */ +function twitter_fetch_favorites($id) { + $account = twitter_account_load($id); + $twitter = twitter_connect($account); + + $params = array(); + $statuses = $twitter->favorites_list($account->screen_name, $params); + foreach ($statuses as $status) { + if (!twitter_account_load($status->user->id)) { + twitter_account_save($status->user); + } + twitter_status_save($status); + twitter_favorite_save($account->id, $status->id); + } +} + +/** + * Set a twitter_post as favorited by a twitter_account. + */ +function twitter_favorite_save($uid, $id) { + $fields = array( + 'twitter_uid' => $uid, + 'twitter_id' => $id, + ); + db_merge('twitter_favorites')->key($fields)->execute(); +} + +/** * Post a message to twitter * * @param $twitter_account @@ -231,7 +258,7 @@ function twitter_fetch_mentions_timeline($id) { */ function twitter_tweets($screen_name = NULL) { $query = db_select('twitter', 't') - ->fields('t'); + ->fields('t'); if (isset($screen_name)) { $query->condition('t.screen_name', $screen_name); } diff --git a/twitter.install b/twitter.install index e721596..7854ba5 100644 --- a/twitter.install +++ b/twitter.install @@ -1,4 +1,5 @@ http://dev.twitter.com ' . - 'and set the generated Application keys at the ' . - 'Twitter settings page'); + $t('In order to interact with Twitter, you need to create an application at ' . + 'http://dev.twitter.com ' . + 'and set the generated Application keys at the ' . + 'Twitter settings page'); $requirements['twitter_keys']['severity'] = REQUIREMENT_ERROR; } else { @@ -289,6 +290,13 @@ function twitter_schema() { 'not null' => TRUE, 'default' => 0, ), + 'favorites' => array( + 'description' => "Boolean flag indicating whether the {twitter_user}'s favorites should be pulled in by the site.", + 'type' => 'int', + 'unsigned' => TRUE, + 'not null' => TRUE, + 'default' => 0, + ), 'last_refresh' => array( 'description' => "A UNIX timestamp marking the date Twitter statuses were last fetched on.", 'type' => 'int', @@ -314,6 +322,27 @@ function twitter_schema() { 'primary key' => array('twitter_uid'), ); + $schema['twitter_favorites'] = array( + 'description' => 'Tracks twitter posts favorited by twitter users.', + 'fields' => array( + 'twitter_uid' => array( + 'description' => 'The unique identifier of the {twitter_account}.', + 'type' => 'numeric', + 'not null' => TRUE, + 'precision' => '20', + 'scale' => '0', + ), + 'twitter_id' => array( + 'description' => 'Unique identifier for each {twitter} post.', + 'type' => 'numeric', + 'not null' => TRUE, + 'precision' => '20', + 'scale' => '0', + ), + ), + 'primary key' => array('twitter_uid', 'twitter_id'), + ); + return $schema; } @@ -323,12 +352,12 @@ function twitter_schema() { function twitter_install() { // Set the weight to 3, making it heavier than Pathauto. db_update('system') - ->fields(array( + ->fields(array( 'weight' => 3, )) - ->condition('type', 'module') - ->condition('name', 'twitter') - ->execute(); + ->condition('type', 'module') + ->condition('name', 'twitter') + ->execute(); } /** @@ -386,7 +415,9 @@ function twitter_update_7400() { * * This update has been set empty afterwards as the field is actually needed. */ -function twitter_update_7401() {} +function twitter_update_7401() { + +} /** * Makes the import field not enabled by default at twitter_account table. @@ -408,7 +439,9 @@ function twitter_update_7402() { * * This update has been set empty afterwards as the existing field twitter_account.uid is used. */ -function twitter_update_7403() {} +function twitter_update_7403() { + +} /** * Renames twitter_account.added_by_uid to twitter_account.uid. @@ -427,3 +460,39 @@ function twitter_update_7500() { db_change_field('twitter_account', 'added_by_uid', 'uid', $spec); } } + +/** + * Adds field mentions to twitter_account table and adds twitter_favorites table to relate favorite twitter_posts to twitter_users. + */ +function twitter_update_7501() { + $spec = array( + 'description' => "Boolean flag indicating whether the {twitter_user}'s favorites should be pulled in by the site.", + 'type' => 'int', + 'unsigned' => TRUE, + 'not null' => TRUE, + 'default' => 0, + ); + db_add_field('twitter_account', 'favorites', $spec); + + $spec = array( + 'description' => 'Tracks twitter posts favorited by twitter users.', + 'fields' => array( + 'twitter_uid' => array( + 'description' => 'The unique identifier of the {twitter_account}.', + 'type' => 'numeric', + 'not null' => TRUE, + 'precision' => '20', + 'scale' => '0', + ), + 'twitter_id' => array( + 'description' => 'Unique identifier for each {twitter} post.', + 'type' => 'numeric', + 'not null' => TRUE, + 'precision' => '20', + 'scale' => '0', + ), + ), + 'primary key' => array('twitter_uid', 'twitter_id'), + ); + db_create_table('twitter_favorites', $spec); +} diff --git a/twitter.module b/twitter.module index ec90978..9bd6a65 100644 --- a/twitter.module +++ b/twitter.module @@ -1,13 +1,13 @@ 0 AND import = 1 - ORDER BY last_refresh ASC", - 0, 20); + ORDER BY last_refresh ASC", 0, 20); try { foreach ($result as $account) { - // Fetch tweets and mentions. + // Fetch tweets, mentions, and favorites. twitter_fetch_user_timeline($account->twitter_uid); $twitter_account = twitter_account_load($account->twitter_uid); - if ($twitter_account->is_auth() && $twitter_account->mentions) { - twitter_fetch_mentions_timeline($twitter_account->id); + if ($twitter_account->is_auth()) { + if ($twitter_account->mentions) { + twitter_fetch_mentions_timeline($twitter_account->id); + } + if ($twitter_account->favorites) { + twitter_fetch_favorites($twitter_account->id); + } } // Mark the time this account was updated. db_update('twitter_account') - ->fields(array( - 'last_refresh' => REQUEST_TIME, - )) - ->condition('twitter_uid', $twitter_account->id) - ->execute(); + ->fields(array( + 'last_refresh' => REQUEST_TIME, + )) + ->condition('twitter_uid', $twitter_account->id) + ->execute(); } } catch (TwitterException $e) { // The exception has already been logged so we do not need to do anything here apart from catching it. @@ -195,8 +199,8 @@ function twitter_cron() { // Nuke old statuses. if ($age = variable_get('twitter_expire', 0)) { db_delete('twitter') - ->condition('created_time', REQUEST_TIME - $age, '<') - ->execute(); + ->condition('created_time', REQUEST_TIME - $age, '<') + ->execute(); } } @@ -219,7 +223,7 @@ function twitter_filter_info() { $filters['twitter_links'] = array( 'title' => t('Twitter link converter'), 'description' => t('Makes links in Twitter messages to be opened in new windows and adds ' . - 'rel="nofollow" so these links do not penalize SEO.'), + 'rel="nofollow" so these links do not penalize SEO.'), 'process callback' => '_twitter_filter_link', 'tips callback' => '_twitter_filter_tip_link', ); @@ -239,7 +243,7 @@ function _twitter_filter_tip_username($filter, $format, $long = FALSE) { */ function _twitter_filter_tip_hashtag($format, $long = FALSE) { return t('Twitter-style #hashtags are linked to !url.', array( - '!url' => 'search.twitter.com') + '!url' => 'search.twitter.com') ); } @@ -319,8 +323,8 @@ function twitter_twitter_accounts($account) { module_load_include('inc', 'twitter'); $query = db_select('twitter_account', 'ta') - ->fields('ta', array('twitter_uid')) - ->condition('ta.uid', $account->uid); + ->fields('ta', array('twitter_uid')) + ->condition('ta.uid', $account->uid); $twitter_accounts = array(); foreach ($query->execute()->fetchCol() as $twitter_uid) { diff --git a/twitter.pages.inc b/twitter.pages.inc index feba5a0..c38556b 100644 --- a/twitter.pages.inc +++ b/twitter.pages.inc @@ -1,4 +1,5 @@ 'fieldset', '#title' => t('Twitter Settings'), '#description' => t('The following settings connect Twitter module with external APIs. ' . - 'Change them if, for example, you want to use Identi.ca.'), + 'Change them if, for example, you want to use Identi.ca.'), ); $form['twitter']['twitter_host'] = array( '#type' => 'textfield', @@ -89,7 +90,8 @@ function twitter_admin_form($form, &$form_state) { function twitter_user_settings($account = NULL) { // Verify OAuth keys. if (!twitter_api_keys()) { - $output = t('

You need to authenticate at least one Twitter account in order to use the Twitter API. Please fill out the OAuth fields at Twitter Settings and then return here.

'); + $link = l(t('Twitter Settings'), 'admin/config/services/twitter/settings'); + $output = t('

You need to authenticate at least one Twitter account in order to use the Twitter API. Please fill out the OAuth fields at !link.

', array('!link' => $link)); } else { module_load_include('inc', 'twitter'); @@ -105,10 +107,11 @@ function twitter_user_settings($account = NULL) { // List Twitter accounts. $output['header']['#markup'] = ''; if (user_access('administer site configuration')) { - $output['header']['#markup'] = t('

Tweets are pulled from Twitter by ' . - 'running cron. '); + $link = l(t('running cron'), 'admin/reports/status/run-cron', array('query' => array('destination' => current_path()))); + $output['header']['#markup'] = t('

Tweets are pulled from Twitter by !link.

', array('!link' => $link)); } - $output['header']['#markup'] .= t('You can view the full list of tweets at the Tweets view.

'); + $link = l(t('Tweets'), 'tweets'); + $output['header']['#markup'] .= t('You can view the full list of tweets at the !link view.

', array('!link' => $link)); $output['list_form'] = drupal_get_form('twitter_account_list_form', $twitter_accounts); } else { @@ -214,6 +217,10 @@ function _twitter_account_list_row($account) { '#type' => 'checkbox', '#default_value' => $account->mentions ? $account->mentions : '', ); + $form['favorites'] = array( + '#type' => 'checkbox', + '#default_value' => $account->favorites ? $account->favorites : '', + ); } else { $form['mentions'] = array( @@ -247,8 +254,9 @@ function theme_twitter_account_list_form($variables) { t('Private'), t('Tweets'), t('Mentions'), + t('Favorites'), t('Delete'), - )); + )); $rows = array(); foreach (element_children($form['accounts']) as $key) { @@ -256,7 +264,7 @@ function theme_twitter_account_list_form($variables) { $row = array( drupal_render($element['image']), drupal_render($element['id']) . drupal_render($element['screen_name']) . - drupal_render($element['visible_name']), + drupal_render($element['visible_name']), drupal_render($element['description']), ); if (user_access('administer_twitter_accounts')) { @@ -267,8 +275,9 @@ function theme_twitter_account_list_form($variables) { drupal_render($element['protected']), drupal_render($element['import']), drupal_render($element['mentions']), + drupal_render($element['favorites']), drupal_render($element['delete']), - )); + )); $rows[] = $row; } @@ -289,8 +298,7 @@ function twitter_account_list_form_submit($form, &$form_state) { else { $twitter_account = twitter_account_load($account['id']); twitter_account_delete($account['id']); - drupal_set_message(t('The Twitter account !account was deleted.', - array('!account' => $twitter_account->screen_name))); + drupal_set_message(t('The Twitter account !account was deleted.', array('!account' => $twitter_account->screen_name))); } } drupal_set_message(t('The Twitter account settings were updated.')); @@ -304,8 +312,8 @@ function twitter_auth_account_form($form, $form_state) { '#type' => 'submit', '#value' => t('Go to Twitter to add an authenticated account'), '#prefix' => t('Authenticated accounts can post, sign in and pull mentions. ' . - 'At least one authenticated account is needed for Twitter ' . - 'module to work.
'), + 'At least one authenticated account is needed for Twitter ' . + 'module to work.
'), ); return $form; @@ -319,8 +327,8 @@ function twitter_auth_account_form_validate($form, &$form_state) { $secret = variable_get('twitter_consumer_secret', ''); if ($key == '' || $secret == '') { form_set_error('', t('Please configure your consumer key and secret key at ' . - 'Twitter settings.', array( '!url' => url('admin/config/services/twitter'), - ))); + 'Twitter settings.', array('!url' => url('admin/config/services/twitter'), + ))); } } @@ -347,8 +355,7 @@ function twitter_auth_account_form_submit($form, &$form_state) { } } else { - drupal_set_message(t('Could not obtain a valid token from the Twitter API. Please review the configuration.'), - 'error'); + drupal_set_message(t('Could not obtain a valid token from the Twitter API. Please review the configuration.'), 'error'); } } @@ -465,7 +472,7 @@ function twitter_non_auth_account_form($form, $form_state) { '#required' => TRUE, '#title' => t('Twitter account name'), '#prefix' => t('If you simply want to pull tweets from additional Twitter accounts, ' . - 'enter the Twitter account name below and click on the following button.
'), + 'enter the Twitter account name below and click on the following button.
'), ); $form['submit_non_auth'] = array( @@ -482,8 +489,7 @@ function twitter_non_auth_account_form($form, $form_state) { function twitter_non_auth_account_form_validate($form, &$form_state) { $screen_name = $form_state['values']['screen_name']; if (twitter_account_load($screen_name)) { - form_set_error('screen_name', t('The Twitter account @screen_name has been added already.', - array('@screen_name' => $screen_name))); + form_set_error('screen_name', t('The Twitter account @screen_name has been added already.', array('@screen_name' => $screen_name))); } } @@ -496,7 +502,7 @@ function twitter_non_auth_account_form_submit($form, &$form_state) { $twitter_account = $twitter->users_show($name, FALSE); if (!isset($twitter_account->id)) { form_set_error('screen_name', t('Could not add the Twitter account @name. ' . - 'Check the recent messages log.', array('@name' => $name))); + 'Check the recent messages log.', array('@name' => $name))); } else { global $user; diff --git a/twitter.views.inc b/twitter.views.inc index 6e319f1..52c8459 100644 --- a/twitter.views.inc +++ b/twitter.views.inc @@ -1,4 +1,5 @@ array( 'left_field' => 'screen_name', @@ -176,7 +177,7 @@ function twitter_views_data() { 'users' => array( 'left_field' => 'uid', 'field' => 'uid', - // 'left_table' => 'twitter_user', + // 'left_table' => 'twitter_user', ), ); @@ -347,10 +348,45 @@ function twitter_views_data() { ), ); + $data['twitter_favorites']['table']['group'] = t('Twitter'); + $data['twitter_favorites']['table']['join'] = array( + 'twitter' => array( + 'left_field' => 'twitter_id', + 'field' => 'twitter_id', + ), + 'twitter_account' => array( + 'left_field' => 'twitter_uid', + 'field' => 'twitter_uid', + ), + ); + $data['twitter_favorites']['twitter_id'] = array( + 'title' => t('Favorite Tweets'), + 'help' => t('Tweet that has been favorited.'), + 'relationship' => array( + 'base' => 'twitter', + 'base field' => 'twitter_id', + 'handler' => 'views_handler_relationship', + 'label' => t('Tweet'), + 'title' => t('Favorite Tweet'), + 'help' => t('Relate favorite Tweet information.'), + ), + ); + $data['twitter_favorites']['twitter_uid'] = array( + 'title' => t('Favorite Tweets'), + 'help' => t('Twitter account that has favorited a Tweet.'), + 'relationship' => array( + 'base' => 'twitter_account', + 'base field' => 'twitter_uid', + 'handler' => 'views_handler_relationship', + 'label' => t('Account'), + 'title' => t('Favorite Account'), + 'help' => t('Relate favorite Tweet account information.'), + ), + ); + return $data; } - /** * @todo Please document this function. * @see http://drupal.org/node/1354