]> git.mxchange.org Git - quix0rs-gnu-social.git/blobdiff - classes/Notice.php
Add events for filtering and logging new notices
[quix0rs-gnu-social.git] / classes / Notice.php
index bfd22e6f0b5c39cd75e8466ee4a9b7293d29036d..6db59c96ef5775b9b82d4a66311c9e8413592e90 100644 (file)
@@ -63,8 +63,24 @@ class Notice extends Memcached_DataObject
     {
         $this->blowCaches(true);
         $this->blowFavesCache(true);
-        $this->blowInboxes();
-        return parent::delete();
+        $this->blowSubsCache(true);
+
+        $this->query('BEGIN');
+        $related = array('Reply',
+                         'Fave',
+                         'Notice_tag',
+                         'Group_inbox',
+                         'Queue_item');
+        if (common_config('inboxes', 'enabled')) {
+            $related[] = 'Notice_inbox';
+        }
+        foreach ($related as $cls) {
+            $inst = new $cls();
+            $inst->notice_id = $this->id;
+            $inst->delete();
+        }
+        $result = parent::delete();
+        $this->query('COMMIT');
     }
 
     function saveTags()
@@ -75,11 +91,11 @@ class Notice extends Memcached_DataObject
             return true;
         }
 
-        /* elide characters we don't want in the tag */
-        $match[1] = str_replace(array('-', '_', '.'), '', $match[1]);
-
         /* Add them to the database */
         foreach(array_unique($match[1]) as $hashtag) {
+            /* elide characters we don't want in the tag */
+            $hashtag = common_canonical_tag($hashtag);
+
             $tag = DB_DataObject::factory('Notice_tag');
             $tag->notice_id = $this->id;
             $tag->tag = $hashtag;
@@ -129,6 +145,8 @@ class Notice extends Memcached_DataObject
             $notice->is_local = $is_local;
         }
 
+               $notice->query('BEGIN');
+
         $notice->reply_to = $reply_to;
         $notice->created = common_sql_now();
         $notice->content = common_shorten_links($content);
@@ -136,29 +154,37 @@ class Notice extends Memcached_DataObject
         $notice->source = $source;
         $notice->uri = $uri;
 
-        $id = $notice->insert();
-
-        if (!$id) {
-            common_log_db_error($notice, 'INSERT', __FILE__);
-            return _('Problem saving notice.');
-        }
+        if (Event::handle('StartNoticeSave', array(&$notice))) {
 
-        # Update the URI after the notice is in the database
-        if (!$uri) {
-            $orig = clone($notice);
-            $notice->uri = common_notice_uri($notice);
+            $id = $notice->insert();
 
-            if (!$notice->update($orig)) {
-                common_log_db_error($notice, 'UPDATE', __FILE__);
+            if (!$id) {
+                common_log_db_error($notice, 'INSERT', __FILE__);
                 return _('Problem saving notice.');
             }
-        }
 
-        # XXX: do we need to change this for remote users?
+            # Update the URI after the notice is in the database
+            if (!$uri) {
+                $orig = clone($notice);
+                $notice->uri = common_notice_uri($notice);
 
-        common_save_replies($notice);
-        $notice->saveTags();
-        $notice->saveGroups();
+                if (!$notice->update($orig)) {
+                    common_log_db_error($notice, 'UPDATE', __FILE__);
+                    return _('Problem saving notice.');
+                }
+            }
+
+            # XXX: do we need to change this for remote users?
+
+            $notice->saveReplies();
+            $notice->saveTags();
+            $notice->saveGroups();
+
+            $notice->addToInboxes();
+            $notice->query('COMMIT');
+
+            Event::handle('EndNoticeSave', array($notice));
+        }
 
         # Clear the cache for subscribed users, so they'll update at next request
         # XXX: someone clever could prepend instead of clearing the cache
@@ -167,7 +193,6 @@ class Notice extends Memcached_DataObject
             $notice->blowCaches();
         }
 
-        $notice->addToInboxes();
         return $notice;
     }
 
@@ -211,6 +236,16 @@ class Notice extends Memcached_DataObject
                     if ($blowLast) {
                         $cache->delete(common_cache_key('group:notices:'.$group_inbox->group_id.';last'));
                     }
+                    $member = new Group_member();
+                    $member->group_id = $group_inbox->group_id;
+                    if ($member->find()) {
+                        while ($member->fetch()) {
+                            $cache->delete(common_cache_key('user:notices_with_friends:' . $member->profile_id));
+                            if ($blowLast) {
+                                $cache->delete(common_cache_key('user:notices_with_friends:' . $member->profile_id . ';last'));
+                            }
+                        }
+                    }
                 }
             }
             $group_inbox->free();
@@ -243,8 +278,10 @@ class Notice extends Memcached_DataObject
         if ($cache) {
             $user = new User();
 
+            $UT = common_config('db','type')=='pgsql'?'"user"':'user';
             $user->query('SELECT id ' .
-                         'FROM user JOIN subscription ON user.id = subscription.subscriber ' .
+
+                         "FROM $UT JOIN subscription ON $UT.id = subscription.subscriber " .
                          'WHERE subscription.subscribed = ' . $this->profile_id);
 
             while ($user->fetch()) {
@@ -433,7 +470,7 @@ class Notice extends Memcached_DataObject
         # On a cache hit, return a DB-object-like wrapper
 
         if ($notices !== false) {
-            $wrapper = new NoticeWrapper(array_slice($notices, $offset, $limit));
+            $wrapper = new ArrayWrapper(array_slice($notices, $offset, $limit));
             return $wrapper;
         }
 
@@ -473,7 +510,7 @@ class Notice extends Memcached_DataObject
 
                 # return a wrapper of the array for use now
 
-                return new NoticeWrapper(array_slice($notices, $offset, $limit));
+                return new ArrayWrapper(array_slice($notices, $offset, $limit));
             }
         }
 
@@ -504,7 +541,7 @@ class Notice extends Memcached_DataObject
 
         # return a wrapper of the array for use now
 
-        $wrapper = new NoticeWrapper(array_slice($notices, $offset, $limit));
+        $wrapper = new ArrayWrapper(array_slice($notices, $offset, $limit));
 
         return $wrapper;
     }
@@ -538,38 +575,23 @@ class Notice extends Memcached_DataObject
 
         if ($enabled === true || $enabled === 'transitional') {
             $inbox = new Notice_inbox();
+            $UT = common_config('db','type')=='pgsql'?'"user"':'user';
             $qry = 'INSERT INTO notice_inbox (user_id, notice_id, created) ' .
-              'SELECT user.id, ' . $this->id . ', "' . $this->created . '" ' .
-              'FROM user JOIN subscription ON user.id = subscription.subscriber ' .
+              "SELECT $UT.id, " . $this->id . ', "' . $this->created . '" ' .
+              "FROM $UT JOIN subscription ON $UT.id = subscription.subscriber " .
               'WHERE subscription.subscribed = ' . $this->profile_id . ' ' .
               'AND NOT EXISTS (SELECT user_id, notice_id ' .
               'FROM notice_inbox ' .
-              'WHERE user_id = user.id ' .
+              "WHERE user_id = $UT.id " .
               'AND notice_id = ' . $this->id . ' )';
             if ($enabled === 'transitional') {
-                $qry .= ' AND user.inboxed = 1';
+                $qry .= " AND $UT.inboxed = 1";
             }
             $inbox->query($qry);
         }
         return;
     }
 
-    # Delete from inboxes if we're deleted.
-
-    function blowInboxes()
-    {
-
-        $enabled = common_config('inboxes', 'enabled');
-
-        if ($enabled === true || $enabled === 'transitional') {
-            $inbox = new Notice_inbox();
-            $inbox->notice_id = $this->id;
-            $inbox->delete();
-        }
-
-        return;
-    }
-
     function saveGroups()
     {
         $enabled = common_config('inboxes', 'enabled');
@@ -614,19 +636,104 @@ class Notice extends Memcached_DataObject
                 // FIXME: do this in an offline daemon
 
                 $inbox = new Notice_inbox();
+                $UT = common_config('db','type')=='pgsql'?'"user"':'user';
                 $qry = 'INSERT INTO notice_inbox (user_id, notice_id, created, source) ' .
-                  'SELECT user.id, ' . $this->id . ', "' . $this->created . '", 2 ' .
-                  'FROM user JOIN group_member ON user.id = group_member.profile_id ' .
+                  "SELECT $UT.id, " . $this->id . ', "' . $this->created . '", 2 ' .
+                  "FROM $UT JOIN group_member ON $UT.id = group_member.profile_id " .
                   'WHERE group_member.group_id = ' . $group->id . ' ' .
                   'AND NOT EXISTS (SELECT user_id, notice_id ' .
                   'FROM notice_inbox ' .
-                  'WHERE user_id = user.id ' .
+                  "WHERE user_id = $UT.id " .
                   'AND notice_id = ' . $this->id . ' )';
                 if ($enabled === 'transitional') {
-                    $qry .= ' AND user.inboxed = 1';
+                    $qry .= " AND $UT.inboxed = 1";
                 }
                 $result = $inbox->query($qry);
             }
         }
     }
+
+    function saveReplies()
+    {
+        // Alternative reply format
+        $tname = false;
+        if (preg_match('/^T ([A-Z0-9]{1,64}) /', $this->content, $match)) {
+            $tname = $match[1];
+        }
+        // extract all @messages
+        $cnt = preg_match_all('/(?:^|\s)@([a-z0-9]{1,64})/', $this->content, $match);
+
+        $names = array();
+
+        if ($cnt || $tname) {
+            // XXX: is there another way to make an array copy?
+            $names = ($tname) ? array_unique(array_merge(array(strtolower($tname)), $match[1])) : array_unique($match[1]);
+        }
+
+        $sender = Profile::staticGet($this->profile_id);
+
+        $replied = array();
+
+        // store replied only for first @ (what user/notice what the reply directed,
+        // we assume first @ is it)
+
+        for ($i=0; $i<count($names); $i++) {
+            $nickname = $names[$i];
+            $recipient = common_relative_profile($sender, $nickname, $this->created);
+            if (!$recipient) {
+                continue;
+            }
+            if ($i == 0 && ($recipient->id != $sender->id) && !$this->reply_to) { // Don't save reply to self
+                $reply_for = $recipient;
+                $recipient_notice = $reply_for->getCurrentNotice();
+                if ($recipient_notice) {
+                    $orig = clone($this);
+                    $this->reply_to = $recipient_notice->id;
+                    $this->update($orig);
+                }
+            }
+            // Don't save replies from blocked profile to local user
+            $recipient_user = User::staticGet('id', $recipient->id);
+            if ($recipient_user && $recipient_user->hasBlocked($sender)) {
+                continue;
+            }
+            $reply = new Reply();
+            $reply->notice_id = $this->id;
+            $reply->profile_id = $recipient->id;
+            $id = $reply->insert();
+            if (!$id) {
+                $last_error = &PEAR::getStaticProperty('DB_DataObject','lastError');
+                common_log(LOG_ERR, 'DB error inserting reply: ' . $last_error->message);
+                common_server_error(sprintf(_('DB error inserting reply: %s'), $last_error->message));
+                return;
+            } else {
+                $replied[$recipient->id] = 1;
+            }
+        }
+
+        // Hash format replies, too
+        $cnt = preg_match_all('/(?:^|\s)@#([a-z0-9]{1,64})/', $this->content, $match);
+        if ($cnt) {
+            foreach ($match[1] as $tag) {
+                $tagged = Profile_tag::getTagged($sender->id, $tag);
+                foreach ($tagged as $t) {
+                    if (!$replied[$t->id]) {
+                        // Don't save replies from blocked profile to local user
+                        $t_user = User::staticGet('id', $t->id);
+                        if ($t_user && $t_user->hasBlocked($sender)) {
+                            continue;
+                        }
+                        $reply = new Reply();
+                        $reply->notice_id = $this->id;
+                        $reply->profile_id = $t->id;
+                        $id = $reply->insert();
+                        if (!$id) {
+                            common_log_db_error($reply, 'INSERT', __FILE__);
+                            return;
+                        }
+                    }
+                }
+            }
+        }
+    }
 }