]> git.mxchange.org Git - quix0rs-gnu-social.git/blobdiff - plugins/TwitterBridge/daemons/twitterdaemon.php
Add executable permission to script missing it
[quix0rs-gnu-social.git] / plugins / TwitterBridge / daemons / twitterdaemon.php
old mode 100644 (file)
new mode 100755 (executable)
index f97f317..8b77488
  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
  */
 
-define('INSTALLDIR', realpath(dirname(__FILE__) . '/..'));
+define('INSTALLDIR', realpath(dirname(__FILE__) . '/../../..'));
 
 $shortoptions = 'fi::a';
 $longoptions = array('id::', 'foreground', 'all');
 
-$helptext = <<<END_OF_XMPP_HELP
+$helptext = <<<END_OF_TWITTERDAEMON_HELP
 Daemon script for receiving new notices from Twitter users.
 
     -i --id           Identity (default none)
@@ -31,12 +31,10 @@ Daemon script for receiving new notices from Twitter users.
                       (requires Stomp queue handler, status_network setup)
     -f --foreground   Stay in the foreground (default background)
 
-END_OF_XMPP_HELP;
+END_OF_TWITTERDAEMON_HELP;
 
 require_once INSTALLDIR.'/scripts/commandline.inc';
 
-require_once INSTALLDIR . '/lib/jabber.php';
-
 class TwitterDaemon extends SpawningDaemon
 {
     protected $allsites = false;
@@ -82,13 +80,11 @@ class TwitterMaster extends IoMaster
      */
     function initManagers()
     {
-        if (common_config('twitter', 'enabled')) {
-            $qm = QueueManager::get();
-            $qm->setActiveGroup('twitter');
-            $this->instantiate($qm);
-            $this->instantiate(TwitterManager::get());
-            $this->instantiate($this->processManager);
-        }
+        $qm = QueueManager::get();
+        $qm->setActiveGroup('twitter');
+        $this->instantiate($qm);
+        $this->instantiate(new TwitterManager());
+        $this->instantiate($this->processManager);
     }
 }
 
@@ -100,12 +96,8 @@ class TwitterManager extends IoManager
     const USERS_PER_STREAM = 100;
     const STREAMS_PER_SECOND = 20;
 
-    protected $twitterStreams;
-    protected $twitterUsers;
-
-    function __construct()
-    {
-    }
+    protected $streams;
+    protected $users;
 
     /**
      * Pull the site's active Twitter-importing users and start spawning
@@ -114,8 +106,9 @@ class TwitterManager extends IoManager
      * @fixme check their last-id and check whether we'll need to do a manual pull.
      * @fixme abstract out the fetching so we can work over multiple sites.
      */
-    function initStreams()
+    protected function initStreams()
     {
+        common_log(LOG_INFO, 'init...');
         // Pull Twitter user IDs for all users we want to pull data for
         $flink = new Foreign_link();
         $flink->service = TWITTER_SERVICE;
@@ -144,9 +137,9 @@ class TwitterManager extends IoManager
      * Prepare a Site Stream connection for the given chunk of users.
      * The actual connection will be opened later.
      *
-     * @param $users array of Twitter-side user IDs
+     * @param $userIds array of Twitter-side user IDs
      */
-    function spawnStream($users)
+    protected function spawnStream($userIds)
     {
         $stream = $this->initSiteStream();
         $stream->followUsers($userIds);
@@ -168,7 +161,7 @@ class TwitterManager extends IoManager
      *
      * @return TwitterStreamReader
      */
-    function initSiteStream()
+    protected function initSiteStream()
     {
         $auth = $this->siteStreamAuth();
         $stream = new TwitterSiteStream($auth);
@@ -190,7 +183,7 @@ class TwitterManager extends IoManager
      *
      * @return TwitterOAuthClient
      */
-    function siteStreamAuth()
+    protected function siteStreamAuth()
     {
         $token = common_config('twitter', 'stream_token');
         $secret = common_config('twitter', 'stream_secret');
@@ -205,7 +198,7 @@ class TwitterManager extends IoManager
      *
      * @return array of resources
      */
-    function getSockets()
+    public function getSockets()
     {
         $sockets = array();
         foreach ($this->streams as $stream) {
@@ -213,7 +206,7 @@ class TwitterManager extends IoManager
                 $sockets[] = $socket;
             }
         }
-        return $streams;
+        return $sockets;
     }
 
     /**
@@ -223,7 +216,7 @@ class TwitterManager extends IoManager
      * @param resource $socket
      * @return boolean success
      */
-    function handleInput($socket)
+    public function handleInput($socket)
     {
         foreach ($this->streams as $stream) {
             foreach ($stream->getSockets() as $aSocket) {
@@ -236,11 +229,12 @@ class TwitterManager extends IoManager
     }
 
     /**
-     * Start the system up!
+     * Start the i/o system up! Prepare our connections and start opening them.
+     *
      * @fixme do some rate-limiting on the stream setup
      * @fixme do some sensible backoff on failure etc
      */
-    function start()
+    public function start()
     {
         $this->initStreams();
         foreach ($this->streams as $stream) {
@@ -249,7 +243,10 @@ class TwitterManager extends IoManager
         return true;
     }
 
-    function finish()
+    /**
+     * Close down our connections when the daemon wraps up for business.
+     */
+    public function finish()
     {
         foreach ($this->streams as $index => $stream) {
             $stream->close();
@@ -268,7 +265,7 @@ class TwitterManager extends IoManager
      *
      * @fixme add more event types as we add handling for them
      */
-    protected function setupEvents(TwitterStream $stream)
+    protected function setupEvents(TwitterStreamReader $stream)
     {
         $handlers = array(
             'status',
@@ -280,33 +277,21 @@ class TwitterManager extends IoManager
 
     /**
      * Event callback notifying that a user has a new message in their home timeline.
+     * We store the incoming message into the queues for processing, keeping our own
+     * daemon running as shiny-fast as possible.
      *
-     * @param object $data JSON data: Twitter status update
+     * @param object $status JSON data: Twitter status update
+     * @fixme in all-sites mode we may need to route queue items into another site's
+     *        destination queues, or multiple sites.
      */
-    protected function onTwitterStatus($data, $context)
+    protected function onTwitterStatus($status, $context)
     {
-        $importer = new TwitterImport();
-        $notice = $importer->importStatus($data);
-        if ($notice) {
-            $user = $this->getTwitterUser($context);
-            Inbox::insertNotice($user->id, $notice->id);
-        }
-    }
-
-    /**
-     * @fixme what about handling multiple sites?
-     */
-    function getTwitterUser($context)
-    {
-        if ($context->source != 'sitestream') {
-            throw new ServerException("Unexpected stream source");
-        }
-        $flink = Foreign_link::getByForeignID(TWITTER_SERVICE, $context->for_user);
-        if ($flink) {
-            return $flink->getUser();
-        } else {
-            throw new ServerException("No local user for this Twitter ID");
-        }
+        $data = array(
+            'status' => $status,
+            'for_user' => $context->for_user,
+        );
+        $qm = QueueManager::get();
+        $qm->enqueue($data, 'tweetin');
     }
 }