]> git.mxchange.org Git - quix0rs-gnu-social.git/commitdiff
Merge branch '0.7.x' into 0.8.x
authorSarven Capadisli <csarven@csarven-laptop.(none)>
Fri, 8 May 2009 18:17:07 +0000 (14:17 -0400)
committerSarven Capadisli <csarven@csarven-laptop.(none)>
Fri, 8 May 2009 18:17:07 +0000 (14:17 -0400)
14 files changed:
actions/twittersettings.php
classes/Fave.php
classes/Notice.php
classes/Notice_tag.php
classes/Profile.php
classes/Reply.php
classes/User.php
classes/User_group.php
db/laconica_pg.sql
db/notice_source.sql
plugins/Comet/CometPlugin.php
plugins/Comet/bayeux.class.inc.php
plugins/Comet/updatetimeline.js
scripts/synctwitterfriends.php

index 1bce5769511734396822ea5f94e94382d85d2a69..2b742788eee55419112a718d271f6e4285960163 100644 (file)
@@ -392,6 +392,8 @@ class TwittersettingsAction extends ConnectSettingsAction
 
         if ($friendsync) {
             save_twitter_friends($user, $twit_user->id, $screen_name, $password);
+            $flink->last_friendsync = common_sql_now();
+            $flink->update();
         }
 
         $this->showForm(_('Twitter settings saved.'), true);
index 24df5938c23ef1fb4b95f31a353012b43018763d..915b4572ffed5dd11e533a664c55e4d687d725b3 100644 (file)
@@ -4,7 +4,7 @@
  */
 require_once INSTALLDIR.'/classes/Memcached_DataObject.php';
 
-class Fave extends Memcached_DataObject 
+class Fave extends Memcached_DataObject
 {
     ###START_AUTOCODE
     /* the code below is auto generated do not remove the above tag */
@@ -31,9 +31,58 @@ class Fave extends Memcached_DataObject
         }
         return $fave;
     }
-    
+
     function &pkeyGet($kv)
     {
         return Memcached_DataObject::pkeyGet('Fave', $kv);
     }
+
+    function stream($user_id, $offset=0, $limit=NOTICES_PER_PAGE)
+    {
+        $ids = Notice::stream(array('Fave', '_streamDirect'),
+                              array($user_id),
+                              'fave:ids_by_user:'.$user_id,
+                              $offset, $limit);
+        return $ids;
+    }
+
+    function _streamDirect($user_id, $offset, $limit, $since_id, $before_id, $since)
+    {
+        $fav = new Fave();
+
+        $fav->user_id = $user_id;
+
+        $fav->selectAdd();
+        $fav->selectAdd('notice_id');
+
+        if ($since_id != 0) {
+            $fav->whereAdd('notice_id > ' . $since_id);
+        }
+
+        if ($before_id != 0) {
+            $fav->whereAdd('notice_id < ' . $before_id);
+        }
+
+        if (!is_null($since)) {
+            $fav->whereAdd('modified > \'' . date('Y-m-d H:i:s', $since) . '\'');
+        }
+
+        // NOTE: we sort by fave time, not by notice time!
+
+        $fav->orderBy('modified DESC');
+
+        if (!is_null($offset)) {
+            $fav->limit($offset, $limit);
+        }
+
+        $ids = array();
+
+        if ($fav->find()) {
+            while ($fav->fetch()) {
+                $ids[] = $fav->notice_id;
+            }
+        }
+
+        return $ids;
+    }
 }
index 19ae8313cdd731ec8c9b9b03bea2e9b8de47d53b..382d160ab3fbe5c122f0e1cffa7b4bcc0ffc517a 100644 (file)
@@ -308,9 +308,9 @@ class Notice extends Memcached_DataObject
             $group_inbox->notice_id = $this->id;
             if ($group_inbox->find()) {
                 while ($group_inbox->fetch()) {
-                    $cache->delete(common_cache_key('group:notices:'.$group_inbox->group_id));
+                    $cache->delete(common_cache_key('user_group:notice_ids:' . $group_inbox->group_id));
                     if ($blowLast) {
-                        $cache->delete(common_cache_key('group:notices:'.$group_inbox->group_id.';last'));
+                        $cache->delete(common_cache_key('user_group:notice_ids:' . $group_inbox->group_id.';last'));
                     }
                     $member = new Group_member();
                     $member->group_id = $group_inbox->group_id;
@@ -337,10 +337,7 @@ class Notice extends Memcached_DataObject
             $tag->notice_id = $this->id;
             if ($tag->find()) {
                 while ($tag->fetch()) {
-                    $cache->delete(common_cache_key('notice_tag:notice_stream:' . $tag->tag));
-                    if ($blowLast) {
-                        $cache->delete(common_cache_key('notice_tag:notice_stream:' . $tag->tag . ';last'));
-                    }
+                    $tag->blowCache($blowLast);
                 }
             }
             $tag->free();
@@ -375,10 +372,10 @@ class Notice extends Memcached_DataObject
     {
         if ($this->is_local) {
             $cache = common_memcache();
-            if ($cache) {
-                $cache->delete(common_cache_key('profile:notices:'.$this->profile_id));
+            if (!empty($cache)) {
+                $cache->delete(common_cache_key('profile:notice_ids:'.$this->profile_id));
                 if ($blowLast) {
-                    $cache->delete(common_cache_key('profile:notices:'.$this->profile_id.';last'));
+                    $cache->delete(common_cache_key('profile:notice_ids:'.$this->profile_id.';last'));
                 }
             }
         }
@@ -392,9 +389,9 @@ class Notice extends Memcached_DataObject
             $reply->notice_id = $this->id;
             if ($reply->find()) {
                 while ($reply->fetch()) {
-                    $cache->delete(common_cache_key('user:replies:'.$reply->profile_id));
+                    $cache->delete(common_cache_key('reply:stream:'.$reply->profile_id));
                     if ($blowLast) {
-                        $cache->delete(common_cache_key('user:replies:'.$reply->profile_id.';last'));
+                        $cache->delete(common_cache_key('reply:stream:'.$reply->profile_id.';last'));
                     }
                 }
             }
@@ -424,9 +421,9 @@ class Notice extends Memcached_DataObject
             $fave->notice_id = $this->id;
             if ($fave->find()) {
                 while ($fave->fetch()) {
-                    $cache->delete(common_cache_key('user:faves:'.$fave->user_id));
+                    $cache->delete(common_cache_key('fave:ids_by_user:'.$fave->user_id));
                     if ($blowLast) {
-                        $cache->delete(common_cache_key('user:faves:'.$fave->user_id.';last'));
+                        $cache->delete(common_cache_key('fave:ids_by_user:'.$fave->user_id.';last'));
                     }
                 }
             }
index f2247299a4d137fba865e32cc350a97de0590fbb..e5b7722430b99be54a6fe2927b13275f86f44df0 100644 (file)
@@ -37,21 +37,62 @@ class Notice_tag extends Memcached_DataObject
     ###END_AUTOCODE
 
     static function getStream($tag, $offset=0, $limit=20) {
-        $qry =
-          'SELECT notice.* ' .
-          'FROM notice JOIN notice_tag ON notice.id = notice_tag.notice_id ' .
-          "WHERE notice_tag.tag = '%s' ";
-
-        return Notice::getStream(sprintf($qry, $tag),
-                                 'notice_tag:notice_stream:' . common_keyize($tag),
-                                 $offset, $limit);
+
+        $ids = Notice::stream(array('Notice_tag', '_streamDirect'),
+                              array($tag),
+                              'notice_tag:notice_ids:' . common_keyize($tag),
+                              $offset, $limit);
+
+        return Notice::getStreamByIds($ids);
+    }
+
+    function _streamDirect($tag, $offset, $limit, $since_id, $before_id, $since)
+    {
+        $nt = new Notice_tag();
+
+        $nt->tag = $tag;
+
+        $nt->selectAdd();
+        $nt->selectAdd('notice_id');
+
+        if ($since_id != 0) {
+            $nt->whereAdd('notice_id > ' . $since_id);
+        }
+
+        if ($before_id != 0) {
+            $nt->whereAdd('notice_id < ' . $before_id);
+        }
+
+        if (!is_null($since)) {
+            $nt->whereAdd('created > \'' . date('Y-m-d H:i:s', $since) . '\'');
+        }
+
+        $nt->orderBy('notice_id DESC');
+
+        if (!is_null($offset)) {
+            $nt->limit($offset, $limit);
+        }
+
+        $ids = array();
+
+        if ($nt->find()) {
+            while ($nt->fetch()) {
+                $ids[] = $nt->notice_id;
+            }
+        }
+
+        return $ids;
     }
 
-    function blowCache()
+    function blowCache($blowLast=false)
     {
         $cache = common_memcache();
         if ($cache) {
-            $cache->delete(common_cache_key('notice_tag:notice_stream:' . $this->tag));
+            $idkey = common_cache_key('notice_tag:notice_ids:' . common_keyize($this->tag));
+            $cache->delete($idkey);
+            if ($blowLast) {
+                $cache->delete($idkey.';last');
+            }
         }
     }
 
index f3bfe299cfb8d474dfae3ad57e29f2b8517f29cd..ae5641d79d69bb2a03d2faa6b1a3b9e43d992cc3 100644 (file)
@@ -155,14 +155,51 @@ class Profile extends Memcached_DataObject
 
     function getNotices($offset=0, $limit=NOTICES_PER_PAGE, $since_id=0, $before_id=0)
     {
-        $qry =
-          'SELECT * ' .
-          'FROM notice ' .
-          'WHERE profile_id = %d ';
-
-        return Notice::getStream(sprintf($qry, $this->id),
-                                 'profile:notices:'.$this->id,
-                                 $offset, $limit, $since_id, $before_id);
+        // XXX: I'm not sure this is going to be any faster. It probably isn't.
+        $ids = Notice::stream(array($this, '_streamDirect'),
+                              array(),
+                              'profile:notice_ids:' . $this->id,
+                              $offset, $limit, $since_id, $before_id);
+
+        return Notice::getStreamByIds($ids);
+    }
+
+    function _streamDirect($offset, $limit, $since_id, $before_id, $since)
+    {
+        $notice = new Notice();
+
+        $notice->profile_id = $this->id;
+
+        $notice->selectAdd();
+        $notice->selectAdd('id');
+
+        if ($since_id != 0) {
+            $notice->whereAdd('id > ' . $since_id);
+        }
+
+        if ($before_id != 0) {
+            $notice->whereAdd('id < ' . $before_id);
+        }
+
+        if (!is_null($since)) {
+            $notice->whereAdd('created > \'' . date('Y-m-d H:i:s', $since) . '\'');
+        }
+
+        $notice->orderBy('id DESC');
+
+        if (!is_null($offset)) {
+            $notice->limit($offset, $limit);
+        }
+
+        $ids = array();
+
+        if ($notice->find()) {
+            while ($notice->fetch()) {
+                $ids[] = $notice->id;
+            }
+        }
+
+        return $ids;
     }
 
     function isMember($group)
index af86aaf878c0ea5392a9579c601c46565a4b884a..4439053b444934f05fb69fa9b4d5b396700f532e 100644 (file)
@@ -4,7 +4,7 @@
  */
 require_once INSTALLDIR.'/classes/Memcached_DataObject.php';
 
-class Reply extends Memcached_DataObject 
+class Reply extends Memcached_DataObject
 {
     ###START_AUTOCODE
     /* the code below is auto generated do not remove the above tag */
@@ -13,7 +13,7 @@ class Reply extends Memcached_DataObject
     public $notice_id;                       // int(4)  primary_key not_null
     public $profile_id;                      // int(4)  primary_key not_null
     public $modified;                        // timestamp()   not_null default_CURRENT_TIMESTAMP
-    public $replied_id;                      // int(4)  
+    public $replied_id;                      // int(4)
 
     /* Static get */
     function staticGet($k,$v=null)
@@ -21,4 +21,47 @@ class Reply extends Memcached_DataObject
 
     /* the code above is auto generated do not remove the tag below */
     ###END_AUTOCODE
+
+    function stream($user_id, $offset=0, $limit=NOTICES_PER_PAGE, $since_id=0, $before_id=0, $since=null)
+    {
+        $ids = Notice::stream(array('Reply', '_streamDirect'),
+                              array($user_id),
+                              'reply:stream:' . $user_id,
+                              $offset, $limit, $since_id, $before_id, $since);
+        return $ids;
+    }
+
+    function _streamDirect($user_id, $offset=0, $limit=NOTICES_PER_PAGE, $since_id=0, $before_id=0, $since=null)
+    {
+        $reply = new Reply();
+        $reply->profile_id = $user_id;
+
+        if ($since_id != 0) {
+            $reply->whereAdd('notice_id > ' . $since_id);
+        }
+
+        if ($before_id != 0) {
+            $reply->whereAdd('notice_id < ' . $before_id);
+        }
+
+        if (!is_null($since)) {
+            $reply->whereAdd('modified > \'' . date('Y-m-d H:i:s', $since) . '\'');
+        }
+
+        $reply->orderBy('notice_id DESC');
+
+        if (!is_null($offset)) {
+            $reply->limit($offset, $limit);
+        }
+
+        $ids = array();
+
+        if ($reply->find()) {
+            while ($reply->fetch()) {
+                $ids[] = $reply->notice_id;
+            }
+        }
+
+        return $ids;
+    }
 }
index ce7ea1464f8356a19fa3d3b1e06c052e50431ac9..b5ac7b22061d7fbbb37c9fc7d9d019fee1c2224d 100644 (file)
@@ -349,30 +349,31 @@ class User extends Memcached_DataObject
         $cache = common_memcache();
 
         // XXX: Kind of a hack.
+
         if ($cache) {
             // This is the stream of favorite notices, in rev chron
             // order. This forces it into cache.
-            $faves = $this->favoriteNotices(0, NOTICE_CACHE_WINDOW);
-            $cnt = 0;
-            while ($faves->fetch()) {
-                if ($faves->id < $notice->id) {
-                    // If we passed it, it's not a fave
-                    return false;
-                } else if ($faves->id == $notice->id) {
-                    // If it matches a cached notice, then it's a fave
-                    return true;
-                }
-                $cnt++;
+
+            $ids = Fave::stream($this->id, 0, NOTICE_CACHE_WINDOW);
+
+            // If it's in the list, then it's a fave
+
+            if (in_array($notice->id, $ids)) {
+                return true;
             }
+
             // If we're not past the end of the cache window,
             // then the cache has all available faves, so this one
             // is not a fave.
-            if ($cnt < NOTICE_CACHE_WINDOW) {
+
+            if (count($ids) < NOTICE_CACHE_WINDOW) {
                 return false;
             }
+
             // Otherwise, cache doesn't have all faves;
             // fall through to the default
         }
+
         $fave = Fave::pkeyGet(array('user_id' => $this->id,
                                     'notice_id' => $notice->id));
         return ((is_null($fave)) ? false : true);
@@ -401,13 +402,9 @@ class User extends Memcached_DataObject
 
     function getReplies($offset=0, $limit=NOTICES_PER_PAGE, $since_id=0, $before_id=0, $since=null)
     {
-        $qry =
-          'SELECT notice.* ' .
-          'FROM notice JOIN reply ON notice.id = reply.notice_id ' .
-          'WHERE reply.profile_id = %d ';
-        return Notice::getStream(sprintf($qry, $this->id),
-                                 'user:replies:'.$this->id,
-                                 $offset, $limit, $since_id, $before_id, null, $since);
+        $ids = Reply::stream($this->id, $offset, $limit, $since_id, $before_id, $since);
+        common_debug("Ids = " . implode(',', $ids));
+        return Notice::getStreamByIds($ids);
     }
 
     function getNotices($offset=0, $limit=NOTICES_PER_PAGE, $since_id=0, $before_id=0, $since=null)
@@ -422,13 +419,8 @@ class User extends Memcached_DataObject
 
     function favoriteNotices($offset=0, $limit=NOTICES_PER_PAGE)
     {
-        $qry =
-          'SELECT notice.* ' .
-          'FROM notice JOIN fave ON notice.id = fave.notice_id ' .
-          'WHERE fave.user_id = %d ';
-        return Notice::getStream(sprintf($qry, $this->id),
-                                 'user:faves:'.$this->id,
-                                 $offset, $limit);
+        $ids = Fave::stream($this->id, $offset, $limit);
+        return Notice::getStreamByIds($ids);
     }
 
     function noticesWithFriends($offset=0, $limit=NOTICES_PER_PAGE, $since_id=0, $before_id=0, $since=null)
@@ -463,8 +455,8 @@ class User extends Memcached_DataObject
         if ($cache) {
             // Faves don't happen chronologically, so we need to blow
             // ;last cache, too
-            $cache->delete(common_cache_key('user:faves:'.$this->id));
-            $cache->delete(common_cache_key('user:faves:'.$this->id).';last');
+            $cache->delete(common_cache_key('fave:ids_by_user:'.$this->id));
+            $cache->delete(common_cache_key('fave:ids_by_user:'.$this->id.';last'));
         }
     }
 
index d152f9d567be0fa2996a7d7a9934c9d7cdc7f96e..7cc31e7026d0aeb693d481c1a7c820b3807f17dc 100755 (executable)
@@ -50,13 +50,50 @@ class User_group extends Memcached_DataObject
 
     function getNotices($offset, $limit)
     {
-        $qry =
-          'SELECT notice.* ' .
-          'FROM notice JOIN group_inbox ON notice.id = group_inbox.notice_id ' .
-          'WHERE group_inbox.group_id = %d ';
-        return Notice::getStream(sprintf($qry, $this->id),
-                                 'group:notices:'.$this->id,
-                                 $offset, $limit);
+        $ids = Notice::stream(array($this, '_streamDirect'),
+                              array(),
+                              'user_group:notice_ids:' . $this->id,
+                              $offset, $limit);
+
+        return Notice::getStreamByIds($ids);
+    }
+
+    function _streamDirect($offset, $limit, $since_id, $before_id, $since)
+    {
+        $inbox = new Group_inbox();
+
+        $inbox->group_id = $this->id;
+
+        $inbox->selectAdd();
+        $inbox->selectAdd('notice_id');
+
+        if ($since_id != 0) {
+            $inbox->whereAdd('notice_id > ' . $since_id);
+        }
+
+        if ($before_id != 0) {
+            $inbox->whereAdd('notice_id < ' . $before_id);
+        }
+
+        if (!is_null($since)) {
+            $inbox->whereAdd('created > \'' . date('Y-m-d H:i:s', $since) . '\'');
+        }
+
+        $inbox->orderBy('notice_id DESC');
+
+        if (!is_null($offset)) {
+            $inbox->limit($offset, $limit);
+        }
+
+        $ids = array();
+
+        if ($inbox->find()) {
+            while ($inbox->fetch()) {
+                $ids[] = $inbox->notice_id;
+            }
+        }
+
+        return $ids;
     }
 
     function allowedNickname($nickname)
@@ -91,7 +128,7 @@ class User_group extends Memcached_DataObject
     function setOriginal($filename)
     {
         $imagefile = new ImageFile($this->id, Avatar::path($filename));
-        
+
         $orig = clone($this);
         $this->original_logo = Avatar::url($filename);
         $this->homepage_logo = Avatar::url($imagefile->resize(AVATAR_PROFILE_SIZE));
index f879d7936ff1c83cf62957c9d9155de84a0abf7c..a27a616f2490875bc9a9573b97baaa0668548af4 100644 (file)
@@ -291,6 +291,8 @@ create table foreign_link (
      noticesync int not null default 1 /* comment 'notice synchronisation, bit 1 = sync outgoing, bit 2 = sync incoming, bit 3 = filter local replies' */,\r
      friendsync int not null default 2 /* comment 'friend synchronisation, bit 1 = sync outgoing, bit 2 = sync incoming */, \r
      profilesync int not null default 1 /* comment 'profile synchronization, bit 1 = sync outgoing, bit 2 = sync incoming' */,\r
+     last_noticesync timestamp default null /* comment 'last time notices were imported' */,\r
+     last_friendsync timestamp default null /* comment 'last time friends were imported' */,\r
      created timestamp not null default CURRENT_TIMESTAMP /* comment 'date this record was created' */,\r
      modified timestamp /* comment 'date this record was modified' */,\r
 \r
index e6e2180a57288852c01f917f09dad165e48dcd71..f026679d50621d40318d68a3660356b1c3455bd4 100644 (file)
@@ -24,6 +24,7 @@ VALUES
     ('peoplebrowsr', 'PeopleBrowsr', 'http://www.peoplebrowsr.com/', now()),
     ('Pikchur','Pikchur','http://www.pikchur.com/', now()),
     ('Ping.fm','Ping.fm','http://ping.fm/', now()),
+    ('pingvine','PingVine','http://pingvine.com/', now()),
     ('pocketwit','PockeTwit','http://code.google.com/p/pocketwit/', now()),
     ('posty','Posty','http://spreadingfunkyness.com/posty/', now()),
     ('royalewithcheese','Royale With Cheese','http://p.hellyeah.org/', now()),
index 2e0bb40a46ce3ffeb0b1baab1495a16fa982776f..45251c66f04c5384127329352aa9f6fe67e52723 100644 (file)
@@ -45,9 +45,11 @@ class CometPlugin extends Plugin
 {
     var $server = null;
 
-    function __construct($server=null)
+    function __construct($server=null, $username=null, $password=null)
     {
-        $this->server = $server;
+        $this->server   = $server;
+        $this->username = $username;
+        $this->password = $password;
 
         parent::__construct();
     }
@@ -82,8 +84,22 @@ class CometPlugin extends Plugin
                          ' ');
         }
 
+        $user = common_current_user();
+
+        if (!empty($user->id)) {
+            $user_id = $user->id;
+        } else {
+            $user_id = 0;
+        }
+
+        $replyurl = common_local_url('newnotice');
+        $favorurl = common_local_url('favor');
+        // FIXME: need to find a better way to pass this pattern in
+        $deleteurl = common_local_url('deletenotice',
+                                      array('notice' => '0000000000'));
+
         $action->elementStart('script', array('type' => 'text/javascript'));
-        $action->raw("$(document).ready(function() { updater.init(\"$this->server\", \"$timeline\");});");
+        $action->raw("$(document).ready(function() { updater.init(\"$this->server\", \"$timeline\", $user_id, \"$replyurl\", \"$favorurl\", \"$deleteurl\"); });");
         $action->elementEnd('script');
 
         return true;
@@ -117,7 +133,7 @@ class CometPlugin extends Plugin
             $json = $this->noticeAsJson($notice);
 
             // Bayeux? Comet? Huh? These terms confuse me
-            $bay = new Bayeux($this->server);
+            $bay = new Bayeux($this->server, $this->user, $this->password);
 
             foreach ($timelines as $timeline) {
                 $this->log(LOG_INFO, "Posting notice $notice->id to '$timeline'.");
@@ -144,6 +160,15 @@ class CometPlugin extends Plugin
         $arr = $act->twitter_status_array($notice, true);
         $arr['url'] = $notice->bestUrl();
         $arr['html'] = htmlspecialchars($notice->rendered);
+        $arr['source'] = htmlspecialchars($arr['source']);
+
+        if (!empty($notice->reply_to)) {
+            $reply_to = Notice::staticGet('id', $notice->reply_to);
+            if (!empty($reply_to)) {
+                $arr['in_reply_to_status_url'] = $reply_to->bestUrl();
+            }
+            $reply_to = null;
+        }
 
         $profile = $notice->getProfile();
         $arr['user']['profile_url'] = $profile->profileurl;
index 785d3e3935add458a7e730fdd44be10a06ae6afa..39ad8a8fc6aa15c0d6b130df382f444fbeb9c97e 100644 (file)
@@ -26,9 +26,12 @@ class Bayeux
     private $oCurl = '';
     private $nNextId = 0;
 
+    private $sUser = '';
+    private $sPassword = '';
+
     public $sUrl = '';
 
-    function __construct($sUrl)
+    function __construct($sUrl, $sUser='', $sPassword='')
     {
         $this->sUrl = $sUrl;
 
@@ -43,6 +46,10 @@ class Bayeux
         curl_setopt($this->oCurl, CURLOPT_POST, 1);
         curl_setopt($this->oCurl, CURLOPT_RETURNTRANSFER,1);
 
+        if (!is_null($sUser) && mb_strlen($sUser) > 0) {
+            curl_setopt($this->oCurl, CURLOPT_USERPWD,"$sUser:$sPassword");
+        }
+
         $this->handShake();
     }
 
index de750baba333551596e0f3b32af2b8944751f000..170949e9bacaba300783a7590d4474c67d7ab04f 100644 (file)
@@ -3,14 +3,26 @@
 
 var updater = function()
 {
+     var _server;
+     var _timeline;
+     var _userid;
+     var _replyurl;
+     var _favorurl;
+     var _deleteurl;
      var _cometd;
 
      return {
-          init: function(server, timeline)
+          init: function(server, timeline, userid, replyurl, favorurl, deleteurl)
           {
                _cometd = $.cometd; // Uses the default Comet object
                _cometd.setLogLevel('debug');
                _cometd.init(server);
+               _server = server;
+               _timeline = timeline;
+               _userid = userid;
+               _favorurl = favorurl;
+               _replyurl = replyurl;
+               _deleteurl = deleteurl;
                _cometd.subscribe(timeline, receive);
                $(window).unload(leave);
           }
@@ -34,7 +46,7 @@ var updater = function()
           var noticeItem = makeNoticeItem(message.data);
           $("#notices_primary .notices").prepend(noticeItem, true);
           $("#notices_primary .notice:first").css({display:"none"});
-          $("#notices_primary .notice:first").fadeIn(2500);
+          $("#notices_primary .notice:first").fadeIn(1000);
           NoticeHover();
           NoticeReply();
      }
@@ -42,7 +54,8 @@ var updater = function()
      function makeNoticeItem(data)
      {
           user = data['user'];
-          html = data['html'].replace(/&amp;/g,'&').replace(/&lt;/g,'<').replace(/&gt;/g,'>');
+          html = data['html'].replace(/&amp;/g,'&').replace(/&lt;/g,'<').replace(/&gt;/g,'>').replace(/&quot;/g,'"');
+          source = data['source'].replace(/&amp;/g,'&').replace(/&lt;/g,'<').replace(/&gt;/g,'>').replace(/&quot;/g,'"');
 
           ni = "<li class=\"hentry notice\" id=\"notice-"+data['id']+"\">"+
                "<div class=\"entry-title\">"+
@@ -65,13 +78,77 @@ var updater = function()
                "</dl>"+
                "<dl class=\"device\">"+
                "<dt>From</dt> "+
-               "<dd>"+data['source']+"</dd>"+
-               "</dl>"+
-               "</div>"+
-               "<div class=\"notice-options\">"+
-               "</div>"+
+               "<dd>"+source+"</dd>"+ // may have a link, I think
+               "</dl>";
+
+          if (data['in_reply_to_status_id']) {
+               ni = ni+" <dl class=\"response\">"+
+                    "<dt>To</dt>"+
+                    "<dd>"+
+                    "<a href=\""+data['in_reply_to_status_url']+"\" rel=\"in-reply-to\">in reply to</a>"+
+                    "</dd>"+
+                    "</dl>";
+          }
+
+          ni = ni+"</div>"+
+               "<div class=\"notice-options\">";
+
+          if (_userid != 0) {
+               var input = $("form#form_notice fieldset input#token");
+               var session_key = input.val();
+               ni = ni+makeFavoriteForm(data['id'], session_key);
+               ni = ni+makeReplyLink(data['id'], data['user']['screen_name']);
+               if (_userid == data['user']['id']) {
+                    ni = ni+makeDeleteLink(data['id']);
+               }
+          }
+
+          ni = ni+"</div>"+
                "</li>";
           return ni;
      }
+
+     function makeFavoriteForm(id, session_key)
+     {
+          var ff;
+
+          ff = "<form id=\"favor-"+id+"\" class=\"form_favor\" method=\"post\" action=\""+_favorurl+"\">"+
+               "<fieldset>"+
+               "<legend>Favor this notice</legend>"+ // XXX: i18n
+               "<input name=\"token-"+id+"\" type=\"hidden\" id=\"token-"+id+"\" value=\""+session_key+"\"/>"+
+               "<input name=\"notice\" type=\"hidden\" id=\"notice-n"+id+"\" value=\""+id+"\"/>"+
+               "<input type=\"submit\" id=\"favor-submit-"+id+"\" name=\"favor-submit-"+id+"\" class=\"submit\" value=\"Favor\" title=\"Favor this notice\"/>"+
+               "</fieldset>"+
+               "</form>";
+          return ff;
+     }
+
+     function makeReplyLink(id, nickname)
+     {
+          var rl;
+          rl = "<dl class=\"notice_reply\">"+
+               "<dt>Reply to this notice</dt>"+
+               "<dd>"+
+               "<a href=\""+_replyurl+"?replyto="+nickname+"\" title=\"Reply to this notice\">Reply <span class=\"notice_id\">"+id+"</span>"+
+               "</a>"+
+               "</dd>"+
+               "</dl>";
+          return rl;
+     }
+
+     function makeDeleteLink(id)
+     {
+          var dl, delurl;
+          delurl = _deleteurl.replace("0000000000", id);
+
+          dl = "<dl class=\"notice_delete\">"+
+               "<dt>Delete this notice</dt>"+
+               "<dd>"+
+               "<a href=\""+delurl+"\" title=\"Delete this notice\">Delete</a>"+
+               "</dd>"+
+               "</dl>";
+
+          return dl;
+     }
 }();
 
index 794301f0f02f8b4af9f6fae0294f1a2bc6ec4c2b..bd08ba58d6a9bcba0e1bcfd3af3941f4aaa461d5 100755 (executable)
@@ -32,8 +32,25 @@ define('LACONICA', true);
 
 require_once(INSTALLDIR . '/lib/common.php');
 
+// Make a lockfile
+$lockfilename = lockFilename();
+if (!($lockfile = @fopen($lockfilename, "w"))) {
+    print "Already running... exiting.\n";
+    exit(1);
+}
+
+// Obtain an exlcusive lock on file (will fail if script is already going)
+if (!@flock( $lockfile, LOCK_EX | LOCK_NB, &$wouldblock) || $wouldblock) {
+    // Script already running - abort
+    @fclose($lockfile);
+    print "Already running... exiting.\n";
+    exit(1);
+}
+
 $flink = new Foreign_link();
 $flink->service = 1; // Twitter
+$flink->orderBy('last_friendsync');
+$flink->limit(25);  // sync this many users during this run
 $cnt = $flink->find();
 
 print "Updating Twitter friends subscriptions for $cnt users.\n";
@@ -60,8 +77,11 @@ while ($flink->fetch()) {
             continue;
         }
 
-        $result = save_twitter_friends($user, $fuser->id,
-                       $fuser->nickname, $flink->credentials);
+        save_twitter_friends($user, $fuser->id, $fuser->nickname, $flink->credentials);
+
+        $flink->last_friendsync = common_sql_now();
+        $flink->update();
+
         if (defined('SCRIPT_DEBUG')) {
             print "\nDONE\n";
         } else {
@@ -70,4 +90,18 @@ while ($flink->fetch()) {
     }
 }
 
+function lockFilename()
+{
+    $piddir = common_config('daemon', 'piddir');
+    if (!$piddir) {
+        $piddir = '/var/run';
+    }
+
+    return $piddir . '/synctwitterfriends.lock';
+}
+
+// Cleanup
+fclose($lockfile);
+unlink($lockfilename);
+
 exit(0);