]> git.mxchange.org Git - quix0rs-gnu-social.git/blobdiff - classes/Notice.php
Break up stream code to use separate notice stream classes
[quix0rs-gnu-social.git] / classes / Notice.php
index 4522d1fc3889b4648afb6e8434afddfd0fbe8bd8..114119bfc9f7027f23d56b9877a58b94a4e48f7b 100644 (file)
@@ -45,7 +45,7 @@ require_once INSTALLDIR.'/classes/Memcached_DataObject.php';
 /* We keep 200 notices, the max number of notices available per API request,
  * in the memcached cache. */
 
-define('NOTICE_CACHE_WINDOW', 200);
+define('NOTICE_CACHE_WINDOW', CachingNoticeStream::CACHE_WINDOW);
 
 define('MAX_BOXCARS', 128);
 
@@ -72,6 +72,7 @@ class Notice extends Memcached_DataObject
     public $location_id;                     // int(4)
     public $location_ns;                     // int(4)
     public $repeat_of;                       // int(4)
+    public $object_type;                     // varchar(255)
 
     /* Static get */
     function staticGet($k,$v=NULL)
@@ -153,7 +154,7 @@ class Notice extends Memcached_DataObject
     function saveTags()
     {
         /* extract all #hastags */
-        $count = preg_match_all('/(?:^|\s)#([\pL\pN_\-\.]{1,64})/', strtolower($this->content), $match);
+        $count = preg_match_all('/(?:^|\s)#([\pL\pN_\-\.]{1,64})/u', strtolower($this->content), $match);
         if (!$count) {
             return true;
         }
@@ -241,6 +242,7 @@ class Notice extends Memcached_DataObject
      *              array 'urls' list of attached/referred URLs to save with the
      *                           notice in place of extracting links from content
      *              boolean 'distribute' whether to distribute the notice, default true
+     *              string 'object_type' URL of the associated object type (default ActivityObject::NOTE)
      *
      * @fixme tag override
      *
@@ -310,7 +312,7 @@ class Notice extends Memcached_DataObject
 
         $autosource = common_config('public', 'autosource');
 
-        # Sandboxed are non-false, but not 1, either
+        // Sandboxed are non-false, but not 1, either
 
         if (!$profile->hasRight(Right::PUBLICNOTICE) ||
             ($source && $autosource && in_array($source, $autosource))) {
@@ -360,6 +362,12 @@ class Notice extends Memcached_DataObject
             $notice->rendered = common_render_content($final, $notice);
         }
 
+        if (empty($object_type)) {
+            $notice->object_type = (empty($notice->reply_to)) ? ActivityObject::NOTE : ActivityObject::COMMENT;
+        } else {
+            $notice->object_type = $object_type;
+        }
+
         if (Event::handle('StartNoticeSave', array(&$notice))) {
 
             // XXX: some of these functions write to the DB
@@ -402,8 +410,8 @@ class Notice extends Memcached_DataObject
 
         }
 
-        # Clear the cache for subscribed users, so they'll update at next request
-        # XXX: someone clever could prepend instead of clearing the cache
+        // Clear the cache for subscribed users, so they'll update at next request
+        // XXX: someone clever could prepend instead of clearing the cache
 
         $notice->blowOnInsert();
 
@@ -488,6 +496,13 @@ class Notice extends Memcached_DataObject
         if ($this->isPublic()) {
             self::blow('public;last');
         }
+
+        self::blow('fave:by_notice', $this->id);
+
+        if ($this->conversation) {
+            // In case we're the first, will need to calc a new root.
+            self::blow('notice:conversation_root:%d', $this->conversation);
+        }
     }
 
     /** save all urls in the notice to the db
@@ -533,7 +548,7 @@ class Notice extends Memcached_DataObject
         if (empty($profile)) {
             return false;
         }
-        $notice = $profile->getNotices(0, NOTICE_CACHE_WINDOW);
+        $notice = $profile->getNotices(0, CachingNoticeStream::CACHE_WINDOW);
         if (!empty($notice)) {
             $last = 0;
             while ($notice->fetch()) {
@@ -544,8 +559,8 @@ class Notice extends Memcached_DataObject
                 }
             }
         }
-        # If we get here, oldest item in cache window is not
-        # old enough for dupe limit; do direct check against DB
+        // If we get here, oldest item in cache window is not
+        // old enough for dupe limit; do direct check against DB
         $notice = new Notice();
         $notice->profile_id = $profile_id;
         $notice->content = $content;
@@ -561,16 +576,16 @@ class Notice extends Memcached_DataObject
         if (empty($profile)) {
             return false;
         }
-        # Get the Nth notice
+        // Get the Nth notice
         $notice = $profile->getNotices(common_config('throttle', 'count') - 1, 1);
         if ($notice && $notice->fetch()) {
-            # If the Nth notice was posted less than timespan seconds ago
+            // If the Nth notice was posted less than timespan seconds ago
             if (time() - strtotime($notice->created) <= common_config('throttle', 'timespan')) {
-                # Then we throttle
+                // Then we throttle
                 return false;
             }
         }
-        # Either not N notices in the stream, OR the Nth was not posted within timespan seconds
+        // Either not N notices in the stream, OR the Nth was not posted within timespan seconds
         return true;
     }
 
@@ -614,134 +629,19 @@ class Notice extends Memcached_DataObject
         return $att;
     }
 
-    function getStreamByIds($ids)
-    {
-        $cache = common_memcache();
-
-        if (!empty($cache)) {
-            $notices = array();
-            foreach ($ids as $id) {
-                $n = Notice::staticGet('id', $id);
-                if (!empty($n)) {
-                    $notices[] = $n;
-                }
-            }
-            return new ArrayWrapper($notices);
-        } else {
-            $notice = new Notice();
-            if (empty($ids)) {
-                //if no IDs requested, just return the notice object
-                return $notice;
-            }
-            $notice->whereAdd('id in (' . implode(', ', $ids) . ')');
-
-            $notice->find();
-
-            $temp = array();
-
-            while ($notice->fetch()) {
-                $temp[$notice->id] = clone($notice);
-            }
-
-            $wrapped = array();
-
-            foreach ($ids as $id) {
-                if (array_key_exists($id, $temp)) {
-                    $wrapped[] = $temp[$id];
-                }
-            }
-
-            return new ArrayWrapper($wrapped);
-        }
-    }
 
     function publicStream($offset=0, $limit=20, $since_id=0, $max_id=0)
     {
-        $ids = Notice::stream(array('Notice', '_publicStreamDirect'),
-                              array(),
-                              'public',
-                              $offset, $limit, $since_id, $max_id);
-        return Notice::getStreamByIds($ids);
+        $stream = new PublicNoticeStream();
+        return $stream->getNotices($offset, $limit, $since_id, $max_id);
     }
 
-    function _publicStreamDirect($offset=0, $limit=20, $since_id=0, $max_id=0)
-    {
-        $notice = new Notice();
-
-        $notice->selectAdd(); // clears it
-        $notice->selectAdd('id');
-
-        $notice->orderBy('created DESC, id DESC');
-
-        if (!is_null($offset)) {
-            $notice->limit($offset, $limit);
-        }
-
-        if (common_config('public', 'localonly')) {
-            $notice->whereAdd('is_local = ' . Notice::LOCAL_PUBLIC);
-        } else {
-            # -1 == blacklisted, -2 == gateway (i.e. Twitter)
-            $notice->whereAdd('is_local !='. Notice::LOCAL_NONPUBLIC);
-            $notice->whereAdd('is_local !='. Notice::GATEWAY);
-        }
-
-        Notice::addWhereSinceId($notice, $since_id);
-        Notice::addWhereMaxId($notice, $max_id);
-
-        $ids = array();
-
-        if ($notice->find()) {
-            while ($notice->fetch()) {
-                $ids[] = $notice->id;
-            }
-        }
-
-        $notice->free();
-        $notice = NULL;
-
-        return $ids;
-    }
 
     function conversationStream($id, $offset=0, $limit=20, $since_id=0, $max_id=0)
     {
-        $ids = Notice::stream(array('Notice', '_conversationStreamDirect'),
-                              array($id),
-                              'notice:conversation_ids:'.$id,
-                              $offset, $limit, $since_id, $max_id);
-
-        return Notice::getStreamByIds($ids);
-    }
-
-    function _conversationStreamDirect($id, $offset=0, $limit=20, $since_id=0, $max_id=0)
-    {
-        $notice = new Notice();
-
-        $notice->selectAdd(); // clears it
-        $notice->selectAdd('id');
-
-        $notice->conversation = $id;
-
-        $notice->orderBy('created DESC, id DESC');
-
-        if (!is_null($offset)) {
-            $notice->limit($offset, $limit);
-        }
-
-        Notice::addWhereSinceId($notice, $since_id);
-        Notice::addWhereMaxId($notice, $max_id);
-
-        $ids = array();
+        $stream = new ConversationNoticeStream($id);
 
-        if ($notice->find()) {
-            while ($notice->fetch()) {
-                $ids[] = $notice->id;
-            }
-        }
-
-        $notice->free();
-        $notice = NULL;
-
-        return $ids;
+        return $stream->getNotices($offset, $limit, $since_id, $max_id);
     }
 
     /**
@@ -766,6 +666,35 @@ class Notice extends Memcached_DataObject
         return false;
     }
 
+    /**
+     * Grab the earliest notice from this conversation.
+     *
+     * @return Notice or null
+     */
+    function conversationRoot()
+    {
+        if (!empty($this->conversation)) {
+            $c = self::memcache();
+
+            $key = Cache::key('notice:conversation_root:' . $this->conversation);
+            $notice = $c->get($key);
+            if ($notice) {
+                return $notice;
+            }
+
+            $notice = new Notice();
+            $notice->conversation = $this->conversation;
+            $notice->orderBy('CREATED');
+            $notice->limit(1);
+            $notice->find(true);
+
+            if ($notice->N) {
+                $c->set($key, $notice);
+                return $notice;
+            }
+        }
+        return null;
+    }
     /**
      * Pull up a full list of local recipients who will be getting
      * this notice in their inbox. Results will be cached, so don't
@@ -782,7 +711,7 @@ class Notice extends Memcached_DataObject
         $c = self::memcache();
 
         if (!empty($c)) {
-            $ni = $c->get(common_cache_key('notice:who_gets:'.$this->id));
+            $ni = $c->get(Cache::key('notice:who_gets:'.$this->id));
             if ($ni !== false) {
                 return $ni;
             }
@@ -804,46 +733,53 @@ class Notice extends Memcached_DataObject
 
         $ni = array();
 
-        foreach ($users as $id) {
-            $ni[$id] = NOTICE_INBOX_SOURCE_SUB;
-        }
+        // Give plugins a chance to add folks in at start...
+        if (Event::handle('StartNoticeWhoGets', array($this, &$ni))) {
 
-        foreach ($groups as $group) {
-            $users = $group->getUserMembers();
             foreach ($users as $id) {
-                if (!array_key_exists($id, $ni)) {
-                    $ni[$id] = NOTICE_INBOX_SOURCE_GROUP;
+                $ni[$id] = NOTICE_INBOX_SOURCE_SUB;
+            }
+
+            foreach ($groups as $group) {
+                $users = $group->getUserMembers();
+                foreach ($users as $id) {
+                    if (!array_key_exists($id, $ni)) {
+                        $ni[$id] = NOTICE_INBOX_SOURCE_GROUP;
+                    }
                 }
             }
-        }
 
-        foreach ($recipients as $recipient) {
-            if (!array_key_exists($recipient, $ni)) {
-                $ni[$recipient] = NOTICE_INBOX_SOURCE_REPLY;
+            foreach ($recipients as $recipient) {
+                if (!array_key_exists($recipient, $ni)) {
+                    $ni[$recipient] = NOTICE_INBOX_SOURCE_REPLY;
+                }
             }
-        }
 
-        // Exclude any deleted, non-local, or blocking recipients.
-        $profile = $this->getProfile();
-        $originalProfile = null;
-        if ($this->repeat_of) {
-            // Check blocks against the original notice's poster as well.
-            $original = Notice::staticGet('id', $this->repeat_of);
-            if ($original) {
-                $originalProfile = $original->getProfile();
+            // Exclude any deleted, non-local, or blocking recipients.
+            $profile = $this->getProfile();
+            $originalProfile = null;
+            if ($this->repeat_of) {
+                // Check blocks against the original notice's poster as well.
+                $original = Notice::staticGet('id', $this->repeat_of);
+                if ($original) {
+                    $originalProfile = $original->getProfile();
+                }
             }
-        }
-        foreach ($ni as $id => $source) {
-            $user = User::staticGet('id', $id);
-            if (empty($user) || $user->hasBlocked($profile) ||
-                ($originalProfile && $user->hasBlocked($originalProfile))) {
-                unset($ni[$id]);
+            foreach ($ni as $id => $source) {
+                $user = User::staticGet('id', $id);
+                if (empty($user) || $user->hasBlocked($profile) ||
+                    ($originalProfile && $user->hasBlocked($originalProfile))) {
+                    unset($ni[$id]);
+                }
             }
+
+            // Give plugins a chance to filter out...
+            Event::handle('EndNoticeWhoGets', array($this, &$ni));
         }
 
         if (!empty($c)) {
             // XXX: pack this data better
-            $c->set(common_cache_key('notice:who_gets:'.$this->id), $ni);
+            $c->set(Cache::key('notice:who_gets:'.$this->id), $ni);
         }
 
         return $ni;
@@ -1072,6 +1008,7 @@ class Notice extends Memcached_DataObject
 
             $reply->notice_id  = $this->id;
             $reply->profile_id = $profile->id;
+            $reply->modified   = $this->created;
 
             common_log(LOG_INFO, __METHOD__ . ": saving reply: notice $this->id to profile $profile->id");
 
@@ -1132,6 +1069,7 @@ class Notice extends Memcached_DataObject
 
                 $reply->notice_id  = $this->id;
                 $reply->profile_id = $mentioned->id;
+                $reply->modified   = $this->created;
 
                 $id = $reply->insert();
 
@@ -1250,23 +1188,23 @@ class Notice extends Memcached_DataObject
      * @return Activity activity object representing this Notice.
      */
 
-    function asActivity()
+    function asActivity($cur)
     {
         $act = self::cacheGet(Cache::codeKey('notice:as-activity:'.$this->id));
 
         if (!empty($act)) {
             return $act;
         }
-
         $act = new Activity();
 
         if (Event::handle('StartNoticeAsActivity', array($this, &$act))) {
 
             $profile = $this->getProfile();
 
-            $act->actor     = ActivityObject::fromProfile($profile);
-            $act->verb      = ActivityVerb::POST;
-            $act->objects[] = ActivityObject::fromNotice($this);
+            $act->actor            = ActivityObject::fromProfile($profile);
+            $act->actor->extra[]   = $profile->profileInfo($cur);
+            $act->verb             = ActivityVerb::POST;
+            $act->objects[]        = ActivityObject::fromNotice($this);
 
             // XXX: should this be handled by default processing for object entry?
 
@@ -1333,7 +1271,7 @@ class Notice extends Memcached_DataObject
             $groups = $this->getGroups();
 
             foreach ($groups as $group) {
-                $ctx->attention[] = $group->uri;
+                $ctx->attention[] = $group->getUri();
             }
 
             // XXX: deprecated; use ActivityVerb::SHARE instead
@@ -1404,7 +1342,7 @@ class Notice extends Memcached_DataObject
                          $author=true,
                          $cur=null)
     {
-        $act = $this->asActivity();
+        $act = $this->asActivity($cur);
         $act->extra[] = $this->noticeInfo($cur);
         return $act->asString($namespace, $author, $source);
     }
@@ -1487,61 +1425,6 @@ class Notice extends Memcached_DataObject
         }
     }
 
-    function stream($fn, $args, $cachekey, $offset=0, $limit=20, $since_id=0, $max_id=0)
-    {
-        $cache = common_memcache();
-
-        if (empty($cache) ||
-            $since_id != 0 || $max_id != 0 ||
-            is_null($limit) ||
-            ($offset + $limit) > NOTICE_CACHE_WINDOW) {
-            return call_user_func_array($fn, array_merge($args, array($offset, $limit, $since_id,
-                                                                      $max_id)));
-        }
-
-        $idkey = common_cache_key($cachekey);
-
-        $idstr = $cache->get($idkey);
-
-        if ($idstr !== false) {
-            // Cache hit! Woohoo!
-            $window = explode(',', $idstr);
-            $ids = array_slice($window, $offset, $limit);
-            return $ids;
-        }
-
-        $laststr = $cache->get($idkey.';last');
-
-        if ($laststr !== false) {
-            $window = explode(',', $laststr);
-            $last_id = $window[0];
-            $new_ids = call_user_func_array($fn, array_merge($args, array(0, NOTICE_CACHE_WINDOW,
-                                                                          $last_id, 0, null)));
-
-            $new_window = array_merge($new_ids, $window);
-
-            $new_windowstr = implode(',', $new_window);
-
-            $result = $cache->set($idkey, $new_windowstr);
-            $result = $cache->set($idkey . ';last', $new_windowstr);
-
-            $ids = array_slice($new_window, $offset, $limit);
-
-            return $ids;
-        }
-
-        $window = call_user_func_array($fn, array_merge($args, array(0, NOTICE_CACHE_WINDOW,
-                                                                     0, 0, null)));
-
-        $windowstr = implode(',', $window);
-
-        $result = $cache->set($idkey, $windowstr);
-        $result = $cache->set($idkey . ';last', $windowstr);
-
-        $ids = array_slice($window, $offset, $limit);
-
-        return $ids;
-    }
 
     /**
      * Determine which notice, if any, a new notice is in reply to.
@@ -1681,17 +1564,17 @@ class Notice extends Memcached_DataObject
 
     function repeatStream($limit=100)
     {
-        $cache = common_memcache();
+        $cache = Cache::instance();
 
         if (empty($cache)) {
             $ids = $this->_repeatStreamDirect($limit);
         } else {
-            $idstr = $cache->get(common_cache_key('notice:repeats:'.$this->id));
+            $idstr = $cache->get(Cache::key('notice:repeats:'.$this->id));
             if ($idstr !== false) {
                 $ids = explode(',', $idstr);
             } else {
                 $ids = $this->_repeatStreamDirect(100);
-                $cache->set(common_cache_key('notice:repeats:'.$this->id), implode(',', $ids));
+                $cache->set(Cache::key('notice:repeats:'.$this->id), implode(',', $ids));
             }
             if ($limit < 100) {
                 // We do a max of 100, so slice down to limit
@@ -1699,7 +1582,7 @@ class Notice extends Memcached_DataObject
             }
         }
 
-        return Notice::getStreamByIds($ids);
+        return NoticeStream::getStreamByIds($ids);
     }
 
     function _repeatStreamDirect($limit)
@@ -1857,10 +1740,10 @@ class Notice extends Memcached_DataObject
 
         if ($tag->find()) {
             while ($tag->fetch()) {
-                self::blow('profile:notice_ids_tagged:%d:%s', $this->profile_id, common_keyize($tag->tag));
-                self::blow('profile:notice_ids_tagged:%d:%s;last', $this->profile_id, common_keyize($tag->tag));
-                self::blow('notice_tag:notice_ids:%s', common_keyize($tag->tag));
-                self::blow('notice_tag:notice_ids:%s;last', common_keyize($tag->tag));
+                self::blow('profile:notice_ids_tagged:%d:%s', $this->profile_id, Cache::keyize($tag->tag));
+                self::blow('profile:notice_ids_tagged:%d:%s;last', $this->profile_id, Cache::keyize($tag->tag));
+                self::blow('notice_tag:notice_ids:%s', Cache::keyize($tag->tag));
+                self::blow('notice_tag:notice_ids:%s;last', Cache::keyize($tag->tag));
                 $tag->delete();
             }
         }
@@ -1989,6 +1872,11 @@ class Notice extends Memcached_DataObject
                 $this->is_local == Notice::LOCAL_NONPUBLIC);
     }
 
+    /**
+     * Get the list of hash tags saved with this notice.
+     *
+     * @return array of strings
+     */
     public function getTags()
     {
         $tags = array();