]> git.mxchange.org Git - quix0rs-gnu-social.git/commitdiff
Force group scope on notices sent to a private-only group
authorEvan Prodromou <evan@status.net>
Mon, 4 Apr 2011 21:33:42 +0000 (17:33 -0400)
committerEvan Prodromou <evan@status.net>
Mon, 4 Apr 2011 21:33:42 +0000 (17:33 -0400)
For groups that require a private scope, we force every notice to be limited to group scope.

Changed the group-discovery code so we only get groups once -- regardless if they were provided or not.

classes/Notice.php

index 2fac7b9e2cda73d544d8f03dc4c697497e95c6f6..edd1b02cffe8a71d58e8697c33dd932561adfaf2 100644 (file)
@@ -433,6 +433,22 @@ class Notice extends Memcached_DataObject
             }
         }
 
+        // Force the scope for private groups
+
+        if (!isset($groups)) {
+            $groups = self::groupsFromText($notice->content, $profile);
+        }
+
+        foreach ($groups as $groupId) {
+            $group = User_group::staticGet('id', $groupId);
+            if (!empty($group)) {
+                if ($group->force_scope) {
+                    $notice->scope |= Notice::GROUP_SCOPE;
+                    break;
+                }
+            }
+        }
+
         if (Event::handle('StartNoticeSave', array(&$notice))) {
 
             // XXX: some of these functions write to the DB
@@ -496,11 +512,9 @@ class Notice extends Memcached_DataObject
 
         // Note: groups may save tags, so must be run after tags are saved
         // to avoid errors on duplicates.
-        if (isset($groups)) {
-            $notice->saveKnownGroups($groups);
-        } else {
-            $notice->saveGroups();
-        }
+        // Note: groups should always be set.
+
+        $notice->saveKnownGroups($groups);
 
         if (isset($urls)) {
             $notice->saveKnownUrls($urls);
@@ -940,7 +954,15 @@ class Notice extends Memcached_DataObject
                     common_log_db_error($gi, 'INSERT', __FILE__);
                 }
 
-                // @fixme should we save the tags here or not?
+                // we automatically add a tag for every group name, too
+
+                $tag = Notice_tag::pkeyGet(array('tag' => common_canonical_tag($group->nickname),
+                                                 'notice_id' => $this->id));
+
+                if (is_null($tag)) {
+                    $this->saveTag($group->nickname);
+                }
+
                 $groups[] = clone($group);
             } else {
                 common_log(LOG_ERR, "Local delivery to group id $id skipped, doesn't exist");
@@ -962,36 +984,19 @@ class Notice extends Memcached_DataObject
             return array();
         }
 
-        $groups = array();
-
-        /* extract all !group */
-        $count = preg_match_all('/(?:^|\s)!(' . Nickname::DISPLAY_FMT . ')/',
-                                strtolower($this->content),
-                                $match);
-        if (!$count) {
-            return $groups;
-        }
-
         $profile = $this->getProfile();
 
+        $groups = self::groupsFromText($this->content, $profile);
+
         /* Add them to the database */
 
-        foreach (array_unique($match[1]) as $nickname) {
+        foreach ($groups as $group) {
             /* XXX: remote groups. */
-            $group = User_group::getForNickname($nickname, $profile);
 
             if (empty($group)) {
                 continue;
             }
 
-            // we automatically add a tag for every group name, too
-
-            $tag = Notice_tag::pkeyGet(array('tag' => common_canonical_tag($nickname),
-                                             'notice_id' => $this->id));
-
-            if (is_null($tag)) {
-                $this->saveTag($nickname);
-            }
 
             if ($profile->isMember($group)) {
 
@@ -2180,4 +2185,27 @@ class Notice extends Memcached_DataObject
 
         return true;
     }
+
+    static function groupsFromText($text, $profile)
+    {
+        $groups = array();
+
+        /* extract all !group */
+        $count = preg_match_all('/(?:^|\s)!(' . Nickname::DISPLAY_FMT . ')/',
+                                strtolower($text),
+                                $match);
+
+        if (!$count) {
+            return $groups;
+        }
+
+        foreach (array_unique($match[1]) as $nickname) {
+            $group = User_group::getForNickname($nickname, $profile);
+            if (!empty($group) && $profile->isMember($group)) {
+                $groups[] = $group->id;
+            }
+        }
+
+        return $groups;
+    }
 }