]> git.mxchange.org Git - quix0rs-gnu-social.git/blobdiff - classes/Notice.php
Merge remote branch 'gitorious/0.9.x' into 0.9.x
[quix0rs-gnu-social.git] / classes / Notice.php
index 9a6f180e569e9bff38c73f5ebb9d1a50c5f0302a..85c7dabea48f5d9f31667042cab2c5b07ed8fccd 100644 (file)
@@ -524,10 +524,8 @@ class Notice extends Memcached_DataObject
         $notice = new Notice();
         $notice->profile_id = $profile_id;
         $notice->content = $content;
-        if (common_config('db','type') == 'pgsql')
-          $notice->whereAdd('extract(epoch from now() - created) < ' . common_config('site', 'dupelimit'));
-        else
-          $notice->whereAdd('now() - created < ' . common_config('site', 'dupelimit'));
+        $threshold = common_sql_date(time() - common_config('site', 'dupelimit'));
+        $notice->whereAdd(sprintf("created > '%s'", $notice->escape($threshold)));
 
         $cnt = $notice->count();
         return ($cnt == 0);
@@ -745,6 +743,7 @@ class Notice extends Memcached_DataObject
                 1,
                 1
             );
+
             if ($conversation->N > 0) {
                 return true;
             }
@@ -753,8 +752,15 @@ 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)
     {
@@ -787,27 +793,27 @@ 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]);
             }
         }
 
@@ -896,7 +902,7 @@ class Notice extends Memcached_DataObject
     {
         if (!is_array($group_ids)) {
             // TRANS: Server exception thrown when no array is provided to the method saveKnownGroups().
-            throw new ServerException(_("Bad type provided to saveKnownGroups"));
+            throw new ServerException(_('Bad type provided to saveKnownGroups.'));
         }
 
         $groups = array();
@@ -1109,7 +1115,7 @@ class Notice extends Memcached_DataObject
                     common_log_db_error($reply, 'INSERT', __FILE__);
                     // TRANS: Server exception thrown when a reply cannot be saved.
                     // TRANS: %1$d is a notice ID, %2$d is the ID of the mentioned user.
-                    throw new ServerException(sprintf(_("Could not save reply for %1$d, %2$d."), $this->id, $mentioned->id));
+                    throw new ServerException(sprintf(_('Could not save reply for %1$d, %2$d.'), $this->id, $mentioned->id));
                 } else {
                     $replied[$mentioned->id] = 1;
                     self::blow('reply:stream:%d', $mentioned->id);
@@ -1218,16 +1224,16 @@ class Notice extends Memcached_DataObject
 
         $act = new Activity();
 
-        $act->actor     = Activity::fromProfile($profile);
+        $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_string($this->rendered);
+        $act->content = common_xml_safe_str($this->rendered);
         $act->id      = $this->uri;
-        $act->title   = common_xml_safe_string($this->content);
+        $act->title   = common_xml_safe_str($this->content);
 
         $ctx = new ActivityContext();
 
@@ -1255,7 +1261,7 @@ class Notice extends Memcached_DataObject
         foreach ($reply_ids as $id) {
             $profile = Profile::staticGet('id', $id);
             if (!empty($profile)) {
-                $ctx->attention[] = $profile->uri;
+                $ctx->attention[] = $profile->getUri();
             }
         }
 
@@ -1298,13 +1304,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
@@ -1608,6 +1611,35 @@ class Notice extends Memcached_DataObject
             Event::handle('EndActivityGeo', array(&$this, &$xs, $lat, $lon));
         }
 
+        // @fixme check this logic
+
+        if ($this->isLocal()) {
+
+            $selfUrl = common_local_url('ApiStatusesShow', array('id' => $this->id,
+                                                                 'format' => 'atom'));
+
+            if (Event::handle('StartActivityRelSelf', array(&$this, &$xs, &$selfUrl))) {
+                $xs->element('link', array('rel' => 'self',
+                                           'type' => 'application/atom+xml',
+                                           'href' => $selfUrl));
+                Event::handle('EndActivityRelSelf', array(&$this, &$xs, $selfUrl));
+            }
+
+            if (!empty($cur) && $cur->id == $this->profile_id) {
+
+                // note: $selfUrl may have been changed by a plugin
+                $relEditUrl = common_local_url('ApiStatusesShow', array('id' => $this->id,
+                                                                        'format' => 'atom'));
+
+                if (Event::handle('StartActivityRelEdit', array(&$this, &$xs, &$relEditUrl))) {
+                    $xs->element('link', array('rel' => 'edit',
+                                               'type' => 'application/atom+xml',
+                                               'href' => $relEditUrl));
+                    Event::handle('EndActivityRelEdit', array(&$this, &$xs, $relEditUrl));
+                }
+            }
+        }
+
         if (Event::handle('StartActivityEnd', array(&$this, &$xs))) {
             $xs->elementEnd('entry');
             Event::handle('EndActivityEnd', array(&$this, &$xs));
@@ -1892,7 +1924,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;
 
@@ -1904,7 +1935,6 @@ class Notice extends Memcached_DataObject
             }
 
         } else if (!empty($lat) && !empty($lon)) {
-
             $options['lat'] = $lat;
             $options['lon'] = $lon;
 
@@ -1915,7 +1945,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;
@@ -2032,6 +2061,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)) {