]> git.mxchange.org Git - quix0rs-gnu-social.git/blobdiff - classes/Notice.php
Merge branch '0.9.x' of gitorious.org:statusnet/mainline into 1.0.x
[quix0rs-gnu-social.git] / classes / Notice.php
index 7ca160509563b7a72a6c2f3ef37c3eaa56ba1595..a8ec0529f26b419a9a25f85d92681ba213ddd857 100644 (file)
@@ -583,7 +583,9 @@ class Notice extends Memcached_DataObject
         if ($f2p->find()) {
             while ($f2p->fetch()) {
                 $f = File::staticGet($f2p->file_id);
-                $att[] = clone($f);
+                if ($f) {
+                    $att[] = clone($f);
+                }
             }
         }
         return $att;
@@ -743,6 +745,7 @@ class Notice extends Memcached_DataObject
                 1,
                 1
             );
+
             if ($conversation->N > 0) {
                 return true;
             }
@@ -751,15 +754,22 @@ class Notice extends Memcached_DataObject
     }
 
     /**
-     * @param $groups array of Group *objects*
-     * @param $recipients array of profile *ids*
+     * Pull up a full list of local recipients who will be getting
+     * this notice in their inbox. Results will be cached, so don't
+     * change the input data wily-nilly!
+     *
+     * @param array $groups optional list of Group objects;
+     *              if left empty, will be loaded from group_inbox records
+     * @param array $recipient optional list of reply profile ids
+     *              if left empty, will be loaded from reply records
+     * @return array associating recipient user IDs with an inbox source constant
      */
     function whoGets($groups=null, $recipients=null)
     {
         $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;
             }
@@ -785,33 +795,33 @@ class Notice extends Memcached_DataObject
             $ni[$id] = NOTICE_INBOX_SOURCE_SUB;
         }
 
-        $profile = $this->getProfile();
-
         foreach ($groups as $group) {
             $users = $group->getUserMembers();
             foreach ($users as $id) {
                 if (!array_key_exists($id, $ni)) {
-                    $user = User::staticGet('id', $id);
-                    if (!$user->hasBlocked($profile)) {
-                        $ni[$id] = NOTICE_INBOX_SOURCE_GROUP;
-                    }
+                    $ni[$id] = NOTICE_INBOX_SOURCE_GROUP;
                 }
             }
         }
 
         foreach ($recipients as $recipient) {
-
             if (!array_key_exists($recipient, $ni)) {
-                $recipientUser = User::staticGet('id', $recipient);
-                if (!empty($recipientUser)) {
-                    $ni[$recipient] = NOTICE_INBOX_SOURCE_REPLY;
-                }
+                $ni[$recipient] = NOTICE_INBOX_SOURCE_REPLY;
+            }
+        }
+
+        // Exclude any deleted, non-local, or blocking recipients.
+        $profile = $this->getProfile();
+        foreach ($ni as $id => $source) {
+            $user = User::staticGet('id', $id);
+            if (empty($user) || $user->hasBlocked($profile)) {
+                unset($ni[$id]);
             }
         }
 
         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;
@@ -1210,6 +1220,64 @@ class Notice extends Memcached_DataObject
         return $groups;
     }
 
+    function asActivity()
+    {
+        $profile = $this->getProfile();
+
+        $act = new Activity();
+
+        $act->actor     = ActivityObject::fromProfile($profile);
+        $act->verb      = ActivityVerb::POST;
+        $act->objects[] = ActivityObject::fromNotice($this);
+
+        $act->time    = strtotime($this->created);
+        $act->link    = $this->bestUrl();
+
+        $act->content = common_xml_safe_str($this->rendered);
+        $act->id      = $this->uri;
+        $act->title   = common_xml_safe_str($this->content);
+
+        $ctx = new ActivityContext();
+
+        if (!empty($this->reply_to)) {
+            $reply = Notice::staticGet('id', $this->reply_to);
+            if (!empty($reply)) {
+                $ctx->replyToID  = $reply->uri;
+                $ctx->replyToUrl = $reply->bestUrl();
+            }
+        }
+
+        $ctx->location = $this->getLocation();
+
+        $conv = null;
+
+        if (!empty($this->conversation)) {
+            $conv = Conversation::staticGet('id', $this->conversation);
+            if (!empty($conv)) {
+                $ctx->conversation = $conv->uri;
+            }
+        }
+
+        $reply_ids = $this->getReplies();
+
+        foreach ($reply_ids as $id) {
+            $profile = Profile::staticGet('id', $id);
+            if (!empty($profile)) {
+                $ctx->attention[] = $profile->getUri();
+            }
+        }
+
+        $groups = $this->getGroups();
+
+        foreach ($groups as $group) {
+            $ctx->attention[] = $group->uri;
+        }
+
+        $act->context = $ctx;
+
+        return $act;
+    }
+
     // This has gotten way too long. Needs to be sliced up into functional bits
     // or ideally exported to a utility class.
 
@@ -1238,13 +1306,10 @@ class Notice extends Memcached_DataObject
         }
 
         if (Event::handle('StartActivitySource', array(&$this, &$xs))) {
-
             if ($source) {
-
                 $atom_feed = $profile->getAtomFeed();
 
                 if (!empty($atom_feed)) {
-
                     $xs->elementStart('source');
 
                     // XXX: we should store the actual feed ID
@@ -1595,7 +1660,7 @@ class Notice extends Memcached_DataObject
                                                                       $max_id)));
         }
 
-        $idkey = common_cache_key($cachekey);
+        $idkey = Cache::key($cachekey);
 
         $idstr = $cache->get($idkey);
 
@@ -1782,12 +1847,12 @@ class Notice extends Memcached_DataObject
         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
@@ -1832,7 +1897,6 @@ class Notice extends Memcached_DataObject
         $options = array();
 
         if (!empty($location_id) && !empty($location_ns)) {
-
             $options['location_id'] = $location_id;
             $options['location_ns'] = $location_ns;
 
@@ -1844,7 +1908,6 @@ class Notice extends Memcached_DataObject
             }
 
         } else if (!empty($lat) && !empty($lon)) {
-
             $options['lat'] = $lat;
             $options['lon'] = $lon;
 
@@ -1855,7 +1918,6 @@ class Notice extends Memcached_DataObject
                 $options['location_ns'] = $location->location_ns;
             }
         } else if (!empty($profile)) {
-
             if (isset($profile->lat) && isset($profile->lon)) {
                 $options['lat'] = $profile->lat;
                 $options['lon'] = $profile->lon;
@@ -1972,6 +2034,7 @@ class Notice extends Memcached_DataObject
     {
         // We always insert for the author so they don't
         // have to wait
+        Event::handle('StartNoticeDistribute', array($this));
 
         $user = User::staticGet('id', $this->profile_id);
         if (!empty($user)) {