function broadcast_twitter($notice) { $flink = Foreign_link::getByUserID($notice->profile_id, TWITTER_SERVICE); if (is_twitter_bound($notice, $flink)) { if (TwitterOAuthClient::isPackedToken($flink->credentials)) { return broadcast_oauth($notice, $flink); } else { return broadcast_basicauth($notice, $flink); } } return true; }
function fetchTwitterFriends($flink) { $friends = array(); $client = null; if (TwitterOAuthClient::isPackedToken($flink->credentials)) { $token = TwitterOAuthClient::unpackToken($flink->credentials); $client = new TwitterOAuthClient($token->key, $token->secret); common_debug($this->name() . '- Grabbing friends IDs with OAuth.'); } else { common_debug("Skipping Twitter friends for {$flink->user_id} since not OAuth."); return $friends; } try { $friends_ids = $client->friendsIds(); } catch (Exception $e) { common_log(LOG_WARNING, $this->name() . ' - error getting friend ids: ' . $e->getMessage()); return $friends; } if (empty($friends_ids)) { common_debug($this->name() . " - Twitter user {$flink->foreign_id} " . 'doesn\'t have any friends!'); return $friends; } common_debug($this->name() . ' - Twitter\'s API says Twitter user id ' . "{$flink->foreign_id} has " . count($friends_ids) . ' friends.'); // Calculate how many pages to get... $pages = ceil(count($friends_ids) / 100); if ($pages == 0) { common_debug($this->name() . " - {$user} seems to have no friends."); } for ($i = 1; $i <= $pages; $i++) { try { $more_friends = $client->statusesFriends(null, null, null, $i); } catch (Exception $e) { common_log(LOG_WARNING, $this->name() . ' - cURL error getting Twitter statuses/friends ' . "page {$i} - " . $e->getCode() . ' - ' . $e->getMessage()); } if (empty($more_friends)) { common_log(LOG_WARNING, $this->name() . " - Couldn't retrieve page {$i} " . "of Twitter user {$flink->foreign_id} friends."); continue; } else { if (is_array($more_friends)) { $friends = array_merge($friends, $more_friends); } } } return $friends; }
/** * Notify remote users when their notices get de-favorited. * * @param Profile $profile Profile person doing the de-faving * @param Notice $notice Notice being favored * * @return hook return value */ function onEndDisfavorNotice(Profile $profile, Notice $notice) { $flink = Foreign_link::getByUserID($profile->id, TWITTER_SERVICE); // twitter service if (empty($flink)) { return true; } if (!TwitterOAuthClient::isPackedToken($flink->credentials)) { $this->log(LOG_INFO, "Skipping fave processing for {$profile->id} since link is not OAuth."); return true; } $status_id = twitter_status_id($notice); if (empty($status_id)) { return true; } try { $token = TwitterOAuthClient::unpackToken($flink->credentials); $client = new TwitterOAuthClient($token->key, $token->secret); $client->favoritesDestroy($status_id); } catch (Exception $e) { common_log(LOG_ERR, "Error attempting to unfavorite bridged notice on Twitter: " . $e->getMessage()); } return true; }
/** * Notify remote users when their notices get de-favorited. * * @param Profile $profile Profile person doing the de-faving * @param Notice $notice Notice being favored * * @return hook return value */ function onEndDisfavorNotice(Profile $profile, Notice $notice) { $flink = Foreign_link::getByUserID($profile->id, TWITTER_SERVICE); // twitter service if (empty($flink)) { return true; } if (!TwitterOAuthClient::isPackedToken($flink->credentials)) { $this->log(LOG_INFO, "Skipping fave processing for {$profile->id} since link is not OAuth."); return true; } $status_id = twitter_status_id($notice); if (empty($status_id)) { return true; } $token = TwitterOAuthClient::unpackToken($flink->credentials); $client = new TwitterOAuthClient($token->key, $token->secret); $client->favoritesDestroy($status_id); return true; }
/** * Check if we need to broadcast a notice over the Twitter bridge, and * do so if necessary. Will determine whether to do a straight post or * a repeat/retweet * * This function is meant to be called directly from TwitterQueueHandler. * * @param Notice $notice * @return boolean true if complete or successful, false if we should retry */ function broadcast_twitter($notice) { $flink = Foreign_link::getByUserID($notice->profile_id, TWITTER_SERVICE); // Don't bother with basic auth, since it's no longer allowed if (!empty($flink) && TwitterOAuthClient::isPackedToken($flink->credentials)) { if (is_twitter_bound($notice, $flink)) { if (!empty($notice->repeat_of) && is_twitter_notice($notice->repeat_of)) { $retweet = retweet_notice($flink, Notice::staticGet('id', $notice->repeat_of)); if (is_object($retweet)) { Notice_to_status::saveNew($notice->id, twitter_id($retweet)); return true; } else { // Our error processing will have decided if we need to requeue // this or can discard safely. return $retweet; } } else { return broadcast_oauth($notice, $flink); } } } return true; }
function getTimeline($flink, $timelineUri = 'home_timeline') { if (empty($flink)) { common_log(LOG_ERR, $this->name() . " - Can't retrieve Foreign_link for foreign ID {$fid}"); return; } common_log(LOG_DEBUG, $this->name() . ' - Trying to get ' . $timelineUri . ' timeline for Twitter user ' . $flink->foreign_id); $client = null; if (TwitterOAuthClient::isPackedToken($flink->credentials)) { $token = TwitterOAuthClient::unpackToken($flink->credentials); $client = new TwitterOAuthClient($token->key, $token->secret); common_log(LOG_DEBUG, $this->name() . ' - Grabbing ' . $timelineUri . ' timeline with OAuth.'); } else { common_log(LOG_ERR, "Skipping " . $timelineUri . " timeline for " . $flink->foreign_id . " since not OAuth."); } $timeline = null; $lastId = Twitter_synch_status::getLastId($flink->foreign_id, $timelineUri); common_log(LOG_DEBUG, "Got lastId value '" . $lastId . "' for foreign id '" . $flink->foreign_id . "' and timeline '" . $timelineUri . "'"); try { $timeline = $client->statusesTimeline($lastId, $timelineUri); } catch (Exception $e) { common_log(LOG_ERR, $this->name() . ' - Unable to get ' . $timelineUri . ' timeline for user ' . $flink->user_id . ' - code: ' . $e->getCode() . 'msg: ' . $e->getMessage()); } if (empty($timeline)) { common_log(LOG_DEBUG, $this->name() . " - Empty '" . $timelineUri . "' timeline."); return; } common_log(LOG_INFO, $this->name() . ' - Retrieved ' . sizeof($timeline) . ' statuses from ' . $timelineUri . ' timeline' . ' - for user ' . $flink->user_id); if (!empty($timeline)) { $qm = QueueManager::get(); // Reverse to preserve order foreach (array_reverse($timeline) as $status) { $data = array('status' => $status, 'for_user' => $flink->foreign_id); $qm->enqueue($data, 'tweetin'); } $lastId = twitter_id($timeline[0]); Twitter_synch_status::setLastId($flink->foreign_id, $timelineUri, $lastId); common_debug("Set lastId value '{$lastId}' for foreign id '{$flink->foreign_id}' and timeline '" . $timelineUri . "'"); } // Okay, record the time we synced with Twitter for posterity $flink->last_noticesync = common_sql_now(); $flink->update(); }
function getTimeline($flink) { if (empty($flink)) { common_log(LOG_WARNING, $this->name() . " - Can't retrieve Foreign_link for foreign ID {$fid}"); return; } common_debug($this->name() . ' - Trying to get timeline for Twitter user ' . $flink->foreign_id); $client = null; if (TwitterOAuthClient::isPackedToken($flink->credentials)) { $token = TwitterOAuthClient::unpackToken($flink->credentials); $client = new TwitterOAuthClient($token->key, $token->secret); common_debug($this->name() . ' - Grabbing friends timeline with OAuth.'); } else { common_debug("Skipping friends timeline for {$flink->foreign_id} since not OAuth."); } $timeline = null; $lastId = Twitter_synch_status::getLastId($flink->foreign_id, 'home_timeline'); common_debug("Got lastId value '{$lastId}' for foreign id '{$flink->foreign_id}' and timeline 'home_timeline'"); try { $timeline = $client->statusesHomeTimeline($lastId); } catch (Exception $e) { common_log(LOG_WARNING, $this->name() . ' - Twitter client unable to get friends timeline for user ' . $flink->user_id . ' - code: ' . $e->getCode() . 'msg: ' . $e->getMessage()); } if (empty($timeline)) { common_log(LOG_WARNING, $this->name() . " - Empty timeline."); return; } common_debug(LOG_INFO, $this->name() . ' - Retrieved ' . sizeof($timeline) . ' statuses from Twitter.'); // Reverse to preserve order foreach (array_reverse($timeline) as $status) { // Hacktastic: filter out stuff coming from this StatusNet $source = mb_strtolower(common_config('integration', 'source')); if (preg_match("/{$source}/", mb_strtolower($status->source))) { common_debug($this->name() . ' - Skipping import of status ' . $status->id . ' with source ' . $source); continue; } // Don't save it if the user is protected // FIXME: save it but treat it as private if ($status->user->protected) { continue; } $notice = $this->saveStatus($status); if (!empty($notice)) { Inbox::insertNotice($flink->user_id, $notice->id); } } if (!empty($timeline)) { Twitter_synch_status::setLastId($flink->foreign_id, 'home_timeline', $timeline[0]->id); common_debug("Set lastId value '{$timeline[0]->id}' for foreign id '{$flink->foreign_id}' and timeline 'home_timeline'"); } // Okay, record the time we synced with Twitter for posterity $flink->last_noticesync = common_sql_now(); $flink->update(); }
function broadcast_twitter($notice) { $flink = Foreign_link::getByUserID($notice->profile_id, TWITTER_SERVICE); // Don't bother with basic auth, since it's no longer allowed if (!empty($flink) && TwitterOAuthClient::isPackedToken($flink->credentials)) { if (!empty($notice->repeat_of) && is_twitter_notice($notice->repeat_of)) { $retweet = retweet_notice($flink, Notice::staticGet('id', $notice->repeat_of)); if (!empty($retweet)) { Notice_to_status::saveNew($notice->id, $retweet->id); } } else { if (is_twitter_bound($notice, $flink)) { return broadcast_oauth($notice, $flink); } } } return true; }
function getTimeline($flink) { if (empty($flink)) { common_log(LOG_WARNING, $this->name() . " - Can't retrieve Foreign_link for foreign ID {$fid}"); return; } common_debug($this->name() . ' - Trying to get timeline for Twitter user ' . $flink->foreign_id); // XXX: Biggest remaining issue - How do we know at which status // to start importing? How many statuses? Right now I'm going // with the default last 20. $client = null; if (TwitterOAuthClient::isPackedToken($flink->credentials)) { $token = TwitterOAuthClient::unpackToken($flink->credentials); $client = new TwitterOAuthClient($token->key, $token->secret); common_debug($this->name() . ' - Grabbing friends timeline with OAuth.'); } else { $client = new TwitterBasicAuthClient($flink); common_debug($this->name() . ' - Grabbing friends timeline with basic auth.'); } $timeline = null; try { $timeline = $client->statusesFriendsTimeline(); } catch (Exception $e) { common_log(LOG_WARNING, $this->name() . ' - Twitter client unable to get friends timeline for user ' . $flink->user_id . ' - code: ' . $e->getCode() . 'msg: ' . $e->getMessage()); } if (empty($timeline)) { common_log(LOG_WARNING, $this->name() . " - Empty timeline."); return; } common_debug(LOG_INFO, $this->name() . ' - Retrieved ' . sizeof($timeline) . ' statuses from Twitter.'); // Reverse to preserve order foreach (array_reverse($timeline) as $status) { // Hacktastic: filter out stuff coming from this StatusNet $source = mb_strtolower(common_config('integration', 'source')); if (preg_match("/{$source}/", mb_strtolower($status->source))) { common_debug($this->name() . ' - Skipping import of status ' . $status->id . ' with source ' . $source); continue; } $this->saveStatus($status, $flink); } // Okay, record the time we synced with Twitter for posterity $flink->last_noticesync = common_sql_now(); $flink->update(); }