function handle($data) { // JSON object with Twitter data $status = $data['status']; // Twitter user ID this incoming data belongs to. $receiver = $data['for_user']; $importer = new TwitterImport(); $notice = $importer->importStatus($status); if ($notice) { $flink = Foreign_link::getByForeignID(TWITTER_SERVICE, $receiver); if ($flink) { // @fixme this should go through more regular channels? Inbox::insertNotice($flink->user_id, $notice->id); } } return true; }
function handle($data) { // JSON object with Twitter data $status = $data['status']; // Twitter user ID this incoming data belongs to. $receiver = $data['for_user']; $importer = new TwitterImport(); $notice = $importer->importStatus($status); if ($notice) { $flink = Foreign_link::getByForeignID($receiver, TWITTER_SERVICE); if ($flink) { common_log(LOG_DEBUG, "TweetInQueueHandler - Got flink so add notice " . $notice->id . " to inbox " . $flink->user_id); // @fixme this should go through more regular channels? Inbox::insertNotice($flink->user_id, $notice->id); } else { common_log(LOG_DEBUG, "TweetInQueueHandler - No flink found for foreign user " . $receiver); } } return true; }
static function bulkInsert($notice_id, $user_ids) { foreach ($user_ids as $user_id) { Inbox::insertNotice($user_id, $notice_id); } }
function distribute() { // We always insert for the author so they don't // have to wait $user = User::staticGet('id', $this->profile_id); if (!empty($user)) { Inbox::insertNotice($user->id, $this->id); } if (common_config('queue', 'inboxes')) { // If there's a failure, we want to _force_ // distribution at this point. try { $qm = QueueManager::get(); $qm->enqueue($this, 'distrib'); } catch (Exception $e) { // If the exception isn't transient, this // may throw more exceptions as DQH does // its own enqueueing. So, we ignore them! try { $handler = new DistribQueueHandler(); $handler->handle($this); } catch (Exception $e) { common_log(LOG_ERR, "emergency redistribution resulted in " . $e->getMessage()); } // Re-throw so somebody smarter can handle it. throw $e; } } else { $handler = new DistribQueueHandler(); $handler->handle($this); } }
}); $stream->hookEvent('delete', function ($data, $context) { printf("Deleted status notification: %s\n", $data->status->id); }); $stream->hookEvent('scrub_geo', function ($data, $context) { printf("Req to scrub geo data for user id %s up to status ID %s\n", $data->user_id, $data->up_to_status_id); }); $stream->hookEvent('status', function ($data, $context) { printf("Received status update from %s: %s\n", $data->user->screen_name, $data->text); if (have_option('import')) { $importer = new TwitterImport(); printf("\timporting..."); $notice = $importer->importStatus($data); if ($notice) { global $myuser; Inbox::insertNotice($myuser->id, $notice->id); printf(" %s\n", $notice->id); } else { printf(" FAIL\n"); } } }); $stream->hookEvent('direct_message', function ($data) { printf("Direct message from %s to %s: %s\n", $data->sender->screen_name, $data->recipient->screen_name, $data->text); }); class TwitterManager extends IoManager { function __construct(TwitterStreamReader $stream) { $this->stream = $stream; }
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 saveStatus($status, $flink) { $profile = $this->ensureProfile($status->user); if (empty($profile)) { common_log(LOG_ERR, $this->name() . ' - Problem saving notice. No associated Profile.'); return; } $statusUri = 'http://twitter.com/' . $status->user->screen_name . '/status/' . $status->id; // check to see if we've already imported the status $dupe = $this->checkDupe($profile, $statusUri); if (!empty($dupe)) { common_log(LOG_INFO, $this->name() . " - Ignoring duplicate import: {$statusUri}"); return; } $notice = new Notice(); $notice->profile_id = $profile->id; $notice->uri = $statusUri; $notice->url = $statusUri; $notice->created = strftime('%Y-%m-%d %H:%M:%S', strtotime($status->created_at)); $notice->source = 'twitter'; $notice->reply_to = null; $notice->is_local = Notice::GATEWAY; $notice->content = common_shorten_links($status->text); $notice->rendered = common_render_content($notice->content, $notice); if (Event::handle('StartNoticeSave', array(&$notice))) { $id = $notice->insert(); if (!$id) { common_log_db_error($notice, 'INSERT', __FILE__); common_log(LOG_ERR, $this->name() . ' - Problem saving notice.'); } Event::handle('EndNoticeSave', array($notice)); } $orig = clone $notice; $conv = Conversation::create(); $notice->conversation = $conv->id; if (!$notice->update($orig)) { common_log_db_error($notice, 'UPDATE', __FILE__); common_log(LOG_ERR, $this->name() . ' - Problem saving notice.'); } Inbox::insertNotice($flink->user_id, $notice->id); $notice->blowOnInsert(); return $notice; }