From ef638b0f22e19e97cfac9fa00e19e75d51f1e05e Mon Sep 17 00:00:00 2001 From: Evan Prodromou Date: Wed, 16 Mar 2011 22:30:31 -0400 Subject: [PATCH 1/8] Add scope bitmap for notices --- classes/Notice.php | 1 + classes/statusnet.ini | 1 + db/core.php | 3 +++ 3 files changed, 5 insertions(+) diff --git a/classes/Notice.php b/classes/Notice.php index 664e5dab9f..36686f6f2d 100644 --- a/classes/Notice.php +++ b/classes/Notice.php @@ -73,6 +73,7 @@ class Notice extends Memcached_DataObject public $location_ns; // int(4) public $repeat_of; // int(4) public $object_type; // varchar(255) + public $scope; // int(4) /* Static get */ function staticGet($k,$v=NULL) diff --git a/classes/statusnet.ini b/classes/statusnet.ini index 338e5c5aea..b598f9fc68 100644 --- a/classes/statusnet.ini +++ b/classes/statusnet.ini @@ -337,6 +337,7 @@ location_id = 1 location_ns = 1 repeat_of = 1 object_type = 2 +scope = 1 [notice__keys] id = N diff --git a/db/core.php b/db/core.php index 16a59462d4..4881cc0fff 100644 --- a/db/core.php +++ b/db/core.php @@ -202,6 +202,9 @@ $schema['notice'] = array( 'location_ns' => array('type' => 'int', 'description' => 'namespace for location'), 'repeat_of' => array('type' => 'int', 'description' => 'notice this is a repeat of'), 'object_type' => array('type' => 'varchar', 'length' => 255, 'description' => 'URI representing activity streams object type', 'default' => 'http://activitystrea.ms/schema/1.0/note'), + 'scope' => array('type' => 'int', + 'default' => '1', + 'description' => 'bit map for distribution scope; 0 = everywhere; 1 = this server only; 2 = addressees; 4 = followers'), ), 'primary key' => array('id'), 'unique keys' => array( From b8735f49117f6e03b9d611e98bb5d82a43d19f00 Mon Sep 17 00:00:00 2001 From: Evan Prodromou Date: Wed, 16 Mar 2011 22:54:57 -0400 Subject: [PATCH 2/8] add scope flags for Notice --- classes/Notice.php | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/classes/Notice.php b/classes/Notice.php index 36686f6f2d..a4dcc10f5a 100644 --- a/classes/Notice.php +++ b/classes/Notice.php @@ -90,6 +90,10 @@ class Notice extends Memcached_DataObject const LOCAL_NONPUBLIC = -1; const GATEWAY = -2; + const SITE_SCOPE = 1; + const ADDRESSEE_SCOPE = 2; + const FOLLOWER_SCOPE = 4; + function getProfile() { $profile = Profile::staticGet('id', $this->profile_id); From 9af92f94bded2ee788785519f957756738e00738 Mon Sep 17 00:00:00 2001 From: Evan Prodromou Date: Wed, 16 Mar 2011 22:55:14 -0400 Subject: [PATCH 3/8] function for checking scope rules for Profile --- classes/Profile.php | 40 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) diff --git a/classes/Profile.php b/classes/Profile.php index 88edf5cbb3..ba7d6296f8 100644 --- a/classes/Profile.php +++ b/classes/Profile.php @@ -1076,4 +1076,44 @@ class Profile extends Memcached_DataObject return $profile; } + + function canRead(Notice $notice) + { + if ($notice->scope & Notice::SITE_SCOPE) { + $user = $this->getUser(); + if (empty($user)) { + return false; + } + } + + if ($notice->scope & Notice::ADDRESSEE_SCOPE) { + $replies = $notice->getReplies(); + + if (!in_array($this->id, $replies)) { + $groups = $notice->getGroups(); + + $foundOne = false; + + foreach ($groups as $group) { + if ($this->isMember($group)) { + $foundOne = true; + break; + } + } + + if (!$foundOne) { + return false; + } + } + } + + if ($notice->scope & Notice::FOLLOWER_SCOPE) { + $author = $notice->getProfile(); + if (!Subscription::exists($this, $author)) { + return false; + } + } + + return true; + } } From 7f74aa6c203f0a21f634ea833679369b0f1fc2bf Mon Sep 17 00:00:00 2001 From: Evan Prodromou Date: Wed, 16 Mar 2011 22:30:31 -0400 Subject: [PATCH 4/8] Add scope bitmap for notices --- classes/Notice.php | 1 + classes/statusnet.ini | 1 + db/core.php | 3 +++ 3 files changed, 5 insertions(+) diff --git a/classes/Notice.php b/classes/Notice.php index b228a49c7c..b432ad1ea5 100644 --- a/classes/Notice.php +++ b/classes/Notice.php @@ -73,6 +73,7 @@ class Notice extends Memcached_DataObject public $location_ns; // int(4) public $repeat_of; // int(4) public $object_type; // varchar(255) + public $scope; // int(4) /* Static get */ function staticGet($k,$v=NULL) diff --git a/classes/statusnet.ini b/classes/statusnet.ini index f648fb3fbf..12c59daae0 100644 --- a/classes/statusnet.ini +++ b/classes/statusnet.ini @@ -337,6 +337,7 @@ location_id = 1 location_ns = 1 repeat_of = 1 object_type = 2 +scope = 1 [notice__keys] id = N diff --git a/db/core.php b/db/core.php index 928186d94d..3e439e5010 100644 --- a/db/core.php +++ b/db/core.php @@ -202,6 +202,9 @@ $schema['notice'] = array( 'location_ns' => array('type' => 'int', 'description' => 'namespace for location'), 'repeat_of' => array('type' => 'int', 'description' => 'notice this is a repeat of'), 'object_type' => array('type' => 'varchar', 'length' => 255, 'description' => 'URI representing activity streams object type', 'default' => 'http://activitystrea.ms/schema/1.0/note'), + 'scope' => array('type' => 'int', + 'default' => '1', + 'description' => 'bit map for distribution scope; 0 = everywhere; 1 = this server only; 2 = addressees; 4 = followers'), ), 'primary key' => array('id'), 'unique keys' => array( From 6cdbe47e722f8e77beade6f5c17b5a7adc99b1ec Mon Sep 17 00:00:00 2001 From: Evan Prodromou Date: Wed, 16 Mar 2011 22:54:57 -0400 Subject: [PATCH 5/8] add scope flags for Notice --- classes/Notice.php | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/classes/Notice.php b/classes/Notice.php index b432ad1ea5..2b437c79f4 100644 --- a/classes/Notice.php +++ b/classes/Notice.php @@ -90,6 +90,10 @@ class Notice extends Memcached_DataObject const LOCAL_NONPUBLIC = -1; const GATEWAY = -2; + const SITE_SCOPE = 1; + const ADDRESSEE_SCOPE = 2; + const FOLLOWER_SCOPE = 4; + function getProfile() { $profile = Profile::staticGet('id', $this->profile_id); From 7fc5679e7e09972fa88f711f0f88eeeab8131dad Mon Sep 17 00:00:00 2001 From: Evan Prodromou Date: Wed, 16 Mar 2011 22:55:14 -0400 Subject: [PATCH 6/8] function for checking scope rules for Profile --- classes/Profile.php | 40 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) diff --git a/classes/Profile.php b/classes/Profile.php index 9566226951..fe097753d9 100644 --- a/classes/Profile.php +++ b/classes/Profile.php @@ -1156,4 +1156,44 @@ class Profile extends Memcached_DataObject return $profile; } + + function canRead(Notice $notice) + { + if ($notice->scope & Notice::SITE_SCOPE) { + $user = $this->getUser(); + if (empty($user)) { + return false; + } + } + + if ($notice->scope & Notice::ADDRESSEE_SCOPE) { + $replies = $notice->getReplies(); + + if (!in_array($this->id, $replies)) { + $groups = $notice->getGroups(); + + $foundOne = false; + + foreach ($groups as $group) { + if ($this->isMember($group)) { + $foundOne = true; + break; + } + } + + if (!$foundOne) { + return false; + } + } + } + + if ($notice->scope & Notice::FOLLOWER_SCOPE) { + $author = $notice->getProfile(); + if (!Subscription::exists($this, $author)) { + return false; + } + } + + return true; + } } From 26a4bd7dbf5ed93c03ff5cb65d86f70792fcb94a Mon Sep 17 00:00:00 2001 From: Evan Prodromou Date: Tue, 22 Mar 2011 11:36:48 -0400 Subject: [PATCH 7/8] move scope check to Notice so we can have a null profile --- classes/Notice.php | 83 ++++++++++++++++++++++++++++++++++++++++++++- classes/Profile.php | 40 ---------------------- 2 files changed, 82 insertions(+), 41 deletions(-) diff --git a/classes/Notice.php b/classes/Notice.php index 2b437c79f4..03ce36640b 100644 --- a/classes/Notice.php +++ b/classes/Notice.php @@ -92,7 +92,8 @@ class Notice extends Memcached_DataObject const SITE_SCOPE = 1; const ADDRESSEE_SCOPE = 2; - const FOLLOWER_SCOPE = 4; + const GROUP_SCOPE = 4; + const FOLLOWER_SCOPE = 8; function getProfile() { @@ -2186,4 +2187,84 @@ class Notice extends Memcached_DataObject ($this->is_local != Notice::GATEWAY)); } } + + /** + * Check that the given profile is allowed to read, respond to, or otherwise + * act on this notice. + * + * The $scope member is a bitmask of scopes, representing a logical AND of the + * scope requirement. So, 0x03 (Notice::ADDRESSEE_SCOPE | Notice::SITE_SCOPE) means + * "only visible to people who are mentioned in the notice AND are users on this site." + * Users on the site who are not mentioned in the notice will not be able to see the + * notice. + * + * @param Profile $profile The profile to check + * + * @return boolean whether the profile is in the notice's scope + */ + + function inScope($profile) + { + // If there's any scope, and there's no logged-in user, + // not allowed. + + if ($this->scope > 0 && empty($profile)) { + return false; + } + + // Only for users on this site + + if ($this->scope & Notice::SITE_SCOPE) { + $user = $profile->getUser(); + if (empty($user)) { + return false; + } + } + + // Only for users mentioned in the notice + + if ($this->scope & Notice::ADDRESSEE_SCOPE) { + + // XXX: just query for the single reply + + $replies = $this->getReplies(); + + if (!in_array($profile->id, $replies)) { + return false; + } + } + + // Only for members of the given group + + if ($this->scope & Notice::GROUP_SCOPE) { + + // XXX: just query for the single membership + + $groups = $this->getGroups(); + + $foundOne = false; + + foreach ($groups as $group) { + if ($profile->isMember($group)) { + $foundOne = true; + break; + } + } + + if (!$foundOne) { + return false; + } + } + + // Only for followers of the author + + if ($this->scope & Notice::FOLLOWER_SCOPE) { + $author = $this->getProfile(); + if (!Subscription::exists($profile, $author)) { + return false; + } + } + + return true; + } } diff --git a/classes/Profile.php b/classes/Profile.php index fe097753d9..9566226951 100644 --- a/classes/Profile.php +++ b/classes/Profile.php @@ -1156,44 +1156,4 @@ class Profile extends Memcached_DataObject return $profile; } - - function canRead(Notice $notice) - { - if ($notice->scope & Notice::SITE_SCOPE) { - $user = $this->getUser(); - if (empty($user)) { - return false; - } - } - - if ($notice->scope & Notice::ADDRESSEE_SCOPE) { - $replies = $notice->getReplies(); - - if (!in_array($this->id, $replies)) { - $groups = $notice->getGroups(); - - $foundOne = false; - - foreach ($groups as $group) { - if ($this->isMember($group)) { - $foundOne = true; - break; - } - } - - if (!$foundOne) { - return false; - } - } - } - - if ($notice->scope & Notice::FOLLOWER_SCOPE) { - $author = $notice->getProfile(); - if (!Subscription::exists($this, $author)) { - return false; - } - } - - return true; - } } From 31e7d46a5b03c9082d38beed413b4af91a050a63 Mon Sep 17 00:00:00 2001 From: Evan Prodromou Date: Tue, 22 Mar 2011 18:15:53 -0400 Subject: [PATCH 8/8] add profile to stream function --- classes/Notice.php | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/classes/Notice.php b/classes/Notice.php index 03ce36640b..4e688218f8 100644 --- a/classes/Notice.php +++ b/classes/Notice.php @@ -1546,8 +1546,17 @@ class Notice extends Memcached_DataObject } } - function stream($fn, $args, $cachekey, $offset=0, $limit=20, $since_id=0, $max_id=0) + function stream($fn, $args, $cachekey, $offset=0, $limit=20, $since_id=0, $max_id=0, $profile=0) { + if ($profile === 0) { + $user = common_current_user(); + if (empty($user)) { + $profile = null; + } else { + $profile = $user->getProfile(); + } + } + $cache = Cache::instance(); if (empty($cache) ||