X-Git-Url: https://git.mxchange.org/?a=blobdiff_plain;f=classes%2FNotice.php;h=cfc00b0871a77e8857645853333987d337d114bd;hb=4d61760154a53e39d1e0499d5fe7a4a586e7a9f0;hp=3780d52d561d81ebb93768235a50194947f2f8d6;hpb=b0deaad700e72a06bf11f044236ed9c27e0eccff;p=quix0rs-gnu-social.git diff --git a/classes/Notice.php b/classes/Notice.php index 3780d52d56..cfc00b0871 100644 --- a/classes/Notice.php +++ b/classes/Notice.php @@ -90,6 +90,7 @@ class Notice extends Memcached_DataObject const LOCAL_NONPUBLIC = -1; const GATEWAY = -2; + const PUBLIC_SCOPE = 0; // Useful fake constant const SITE_SCOPE = 1; const ADDRESSEE_SCOPE = 2; const GROUP_SCOPE = 4; @@ -341,21 +342,80 @@ class Notice extends Memcached_DataObject $notice->uri = $uri; $notice->url = $url; + // Get the groups here so we can figure out replies and such + + if (!isset($groups)) { + $groups = self::groupsFromText($notice->content, $profile); + } + + $reply = null; + // Handle repeat case if (isset($repeat_of)) { + + // Check for a private one + + $repeat = Notice::staticGet('id', $repeat_of); + + if (empty($repeat)) { + // TRANS: Client exception thrown in notice when trying to repeat a missing or deleted notice. + throw new ClientException(_('Cannot repeat; original notice is missing or deleted.')); + } + + if ($profile->id == $repeat->profile_id) { + // TRANS: Client error displayed when trying to repeat an own notice. + throw new ClientException(_('You cannot repeat your own notice.')); + } + + if ($repeat->scope != Notice::SITE_SCOPE && + $repeat->scope != Notice::PUBLIC_SCOPE) { + // TRANS: Client error displayed when trying to repeat a non-public notice. + throw new ClientException(_('Cannot repeat a private notice.'), 403); + } + + if (!$repeat->inScope($profile)) { + // The generic checks above should cover this, but let's be sure! + // TRANS: Client error displayed when trying to repeat a notice you cannot access. + throw new ClientException(_('Cannot repeat a notice you cannot read.'), 403); + } + + if ($profile->hasRepeated($repeat->id)) { + // TRANS: Client error displayed when trying to repeat an already repeated notice. + throw new ClientException(_('You already repeated that notice.')); + } + $notice->repeat_of = $repeat_of; } else { - $notice->reply_to = self::getReplyTo($reply_to, $profile_id, $source, $final); - } + $reply = self::getReplyTo($reply_to, $profile_id, $source, $final); + + if (!empty($reply)) { + + if (!$reply->inScope($profile)) { + // TRANS: Client error displayed when trying to reply to a notice a the target has no access to. + // TRANS: %1$s is a user nickname, %2$d is a notice ID (number). + throw new ClientException(sprintf(_('%1$s has no access to notice %2$d.'), + $profile->nickname, $reply->id), 403); + } - if (!empty($notice->reply_to)) { - $reply = Notice::staticGet('id', $notice->reply_to); - if (!$reply->inScope($profile)) { - throw new ClientException(sprintf(_("%s has no access to notice %d"), - $profile->nickname, $reply->id), 403); + $notice->reply_to = $reply->id; + $notice->conversation = $reply->conversation; + + // If the original is private to a group, and notice has no group specified, + // make it to the same group(s) + + if (empty($groups) && ($reply->scope | Notice::GROUP_SCOPE)) { + $groups = array(); + $replyGroups = $reply->getGroups(); + foreach ($replyGroups as $group) { + if ($profile->isMember($group)) { + $groups[] = $group->id; + } + } + } + + // Scope set below } - $notice->conversation = $reply->conversation; } if (!empty($lat) && !empty($lon)) { @@ -381,11 +441,39 @@ class Notice extends Memcached_DataObject } if (is_null($scope)) { // 0 is a valid value - $notice->scope = common_config('notice', 'defaultscope'); + if (!empty($reply)) { + $notice->scope = $reply->scope; + } else { + $notice->scope = common_config('notice', 'defaultscope'); + } } else { $notice->scope = $scope; } + // For private streams + + $user = $profile->getUser(); + + if (!empty($user)) { + if ($user->private_stream && + ($notice->scope == Notice::PUBLIC_SCOPE || + $notice->scope == Notice::SITE_SCOPE)) { + $notice->scope |= Notice::FOLLOWER_SCOPE; + } + } + + // Force the scope for private groups + + 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 @@ -449,10 +537,14 @@ 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); + // Note: groups should always be set. + + $notice->saveKnownGroups($groups); + + if (isset($peopletags)) { + $notice->saveProfileTags($peopletags); } else { - $notice->saveGroups(); + $notice->saveProfileTags(); } if (isset($urls)) { @@ -481,6 +573,7 @@ class Notice extends Memcached_DataObject // was not the root of the conversation. What to do now? self::blow('notice:conversation_ids:%d', $this->conversation); + self::blow('conversation::notice_count:%d', $this->conversation); if (!empty($this->repeat_of)) { self::blow('notice:repeats:%d', $this->repeat_of); @@ -632,18 +725,34 @@ class Notice extends Memcached_DataObject } function attachments() { - // XXX: cache this - $att = array(); - $f2p = new File_to_post; - $f2p->post_id = $this->id; - if ($f2p->find()) { - while ($f2p->fetch()) { - $f = File::staticGet($f2p->file_id); - if ($f) { - $att[] = clone($f); + + $keypart = sprintf('notice:file_ids:%d', $this->id); + + $idstr = self::cacheGet($keypart); + + if ($idstr !== false) { + $ids = explode(',', $idstr); + } else { + $ids = array(); + $f2p = new File_to_post; + $f2p->post_id = $this->id; + if ($f2p->find()) { + while ($f2p->fetch()) { + $ids[] = $f2p->file_id; } } + self::cacheSet($keypart, implode(',', $ids)); + } + + $att = array(); + + foreach ($ids as $id) { + $f = File::staticGet('id', $id); + if (!empty($f)) { + $att[] = clone($f); + } } + return $att; } @@ -744,6 +853,7 @@ class Notice extends Memcached_DataObject } $users = $this->getSubscribedUsers(); + $ptags = $this->getProfileTags(); // FIXME: kind of ignoring 'transitional'... // we'll probably stop supporting inboxless mode @@ -767,27 +877,39 @@ class Notice extends Memcached_DataObject } } + foreach ($ptags as $ptag) { + $users = $ptag->getUserSubscribers(); + foreach ($users as $id) { + if (!array_key_exists($id, $ni)) { + $user = User::staticGet('id', $id); + if (!$user->hasBlocked($profile)) { + $ni[$id] = NOTICE_INBOX_SOURCE_PROFILE_TAG; + } + } + } + } + foreach ($recipients as $recipient) { if (!array_key_exists($recipient, $ni)) { $ni[$recipient] = NOTICE_INBOX_SOURCE_REPLY; } - } - // Exclude any deleted, non-local, or blocking recipients. - $profile = $this->getProfile(); - $originalProfile = null; - if ($this->repeat_of) { - // Check blocks against the original notice's poster as well. - $original = Notice::staticGet('id', $this->repeat_of); - if ($original) { - $originalProfile = $original->getProfile(); + // Exclude any deleted, non-local, or blocking recipients. + $profile = $this->getProfile(); + $originalProfile = null; + if ($this->repeat_of) { + // Check blocks against the original notice's poster as well. + $original = Notice::staticGet('id', $this->repeat_of); + if ($original) { + $originalProfile = $original->getProfile(); + } } - } - foreach ($ni as $id => $source) { - $user = User::staticGet('id', $id); - if (empty($user) || $user->hasBlocked($profile) || - ($originalProfile && $user->hasBlocked($originalProfile))) { - unset($ni[$id]); + foreach ($ni as $id => $source) { + $user = User::staticGet('id', $id); + if (empty($user) || $user->hasBlocked($profile) || + ($originalProfile && $user->hasBlocked($originalProfile))) { + unset($ni[$id]); + } } } @@ -867,6 +989,39 @@ class Notice extends Memcached_DataObject return $ids; } + function getProfileTags() + { + // Don't save ptags for repeats, for now. + + if (!empty($this->repeat_of)) { + return array(); + } + + // XXX: cache me + + $ptags = array(); + + $ptagi = new Profile_tag_inbox(); + + $ptagi->selectAdd(); + $ptagi->selectAdd('profile_tag_id'); + + $ptagi->notice_id = $this->id; + + if ($ptagi->find()) { + while ($ptagi->fetch()) { + $profile_list = Profile_list::staticGet('id', $ptagi->profile_tag_id); + if ($profile_list) { + $ptags[] = $profile_list; + } + } + } + + $ptagi->free(); + + return $ptags; + } + /** * Record this notice to the given group inboxes for delivery. * Overrides the regular parsing of !group markup. @@ -893,7 +1048,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"); @@ -915,36 +1078,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)) { @@ -988,6 +1134,69 @@ class Notice extends Memcached_DataObject return true; } + /** + * record targets into profile_tag_inbox. + * @return array of Profile_list objects + */ + function saveProfileTags($known=array()) + { + // Don't save ptags for repeats, for now + + if (!empty($this->repeat_of)) { + return array(); + } + + if (is_array($known)) { + $ptags = $known; + } else { + $ptags = array(); + } + + $ptag = new Profile_tag(); + $ptag->tagged = $this->profile_id; + + if($ptag->find()) { + while($ptag->fetch()) { + $plist = Profile_list::getByTaggerAndTag($ptag->tagger, $ptag->tag); + $ptags[] = clone($plist); + } + } + + foreach ($ptags as $target) { + $this->addToProfileTagInbox($target); + } + + return $ptags; + } + + function addToProfileTagInbox($plist) + { + $ptagi = Profile_tag_inbox::pkeyGet(array('profile_tag_id' => $plist->id, + 'notice_id' => $this->id)); + + if (empty($ptagi)) { + + $ptagi = new Profile_tag_inbox(); + + $ptagi->query('BEGIN'); + $ptagi->profile_tag_id = $plist->id; + $ptagi->notice_id = $this->id; + $ptagi->created = $this->created; + + $result = $ptagi->insert(); + if (!$result) { + common_log_db_error($ptagi, 'INSERT', __FILE__); + throw new ServerException(_('Problem saving profile_tag inbox.')); + } + + $ptagi->query('COMMIT'); + + self::blow('profile_tag:notice_ids:%d', $ptagi->profile_tag_id); + } + + return true; + } + /** * Save reply records indicating that this notice needs to be * delivered to the local users with the given URIs. @@ -1115,23 +1324,28 @@ class Notice extends Memcached_DataObject */ function getReplies() { - // XXX: cache me + $keypart = sprintf('notice:reply_ids:%d', $this->id); - $ids = array(); + $idstr = self::cacheGet($keypart); - $reply = new Reply(); - $reply->selectAdd(); - $reply->selectAdd('profile_id'); - $reply->notice_id = $this->id; + if ($idstr !== false) { + $ids = explode(',', $idstr); + } else { + $ids = array(); - if ($reply->find()) { - while($reply->fetch()) { - $ids[] = $reply->profile_id; + $reply = new Reply(); + $reply->selectAdd(); + $reply->selectAdd('profile_id'); + $reply->notice_id = $this->id; + + if ($reply->find()) { + while($reply->fetch()) { + $ids[] = $reply->profile_id; + } } + self::cacheSet($keypart, implode(',', $ids)); } - $reply->free(); - return $ids; } @@ -1173,27 +1387,39 @@ class Notice extends Memcached_DataObject return array(); } - // XXX: cache me + $ids = array(); - $groups = array(); + $keypart = sprintf('notice:groups:%d', $this->id); - $gi = new Group_inbox(); + $idstr = self::cacheGet($keypart); - $gi->selectAdd(); - $gi->selectAdd('group_id'); + if ($idstr !== false) { + $ids = explode(',', $idstr); + } else { + $gi = new Group_inbox(); - $gi->notice_id = $this->id; + $gi->selectAdd(); + $gi->selectAdd('group_id'); - if ($gi->find()) { - while ($gi->fetch()) { - $group = User_group::staticGet('id', $gi->group_id); - if ($group) { - $groups[] = $group; + $gi->notice_id = $this->id; + + if ($gi->find()) { + while ($gi->fetch()) { + $ids[] = $gi->group_id; } } + + self::cacheSet($keypart, implode(',', $ids)); } - $gi->free(); + $groups = array(); + + foreach ($ids as $id) { + $group = User_group::staticGet('id', $id); + if ($group) { + $groups[] = $group; + } + } return $groups; } @@ -1280,9 +1506,9 @@ class Notice extends Memcached_DataObject $reply_ids = $this->getReplies(); foreach ($reply_ids as $id) { - $profile = Profile::staticGet('id', $id); - if (!empty($profile)) { - $ctx->attention[] = $profile->getUri(); + $rprofile = Profile::staticGet('id', $id); + if (!empty($rprofile)) { + $ctx->attention[] = $rprofile->getUri(); } } @@ -1477,7 +1703,7 @@ class Notice extends Memcached_DataObject if (!empty($reply_to)) { $reply_notice = Notice::staticGet('id', $reply_to); if (!empty($reply_notice)) { - return $reply_to; + return $reply_notice; } } @@ -1516,8 +1742,10 @@ class Notice extends Memcached_DataObject $last = $recipient->getCurrentNotice(); if (!empty($last)) { - return $last->id; + return $last; } + + return null; } static function maxContent() @@ -1553,6 +1781,15 @@ class Notice extends Memcached_DataObject return $location; } + /** + * Convenience function for posting a repeat of an existing message. + * + * @param int $repeater_id: profile ID of user doing the repeat + * @param string $source: posting source key, eg 'web', 'api', etc + * @return Notice + * + * @throws Exception on failure or permission problems + */ function repeat($repeater_id, $source) { $author = Profile::staticGet('id', $this->profile_id); @@ -1574,8 +1811,13 @@ class Notice extends Memcached_DataObject $content = mb_substr($content, 0, $maxlen - 4) . ' ...'; } - return self::saveNew($repeater_id, $content, $source, - array('repeat_of' => $this->id)); + // Scope is same as this one's + + return self::saveNew($repeater_id, + $content, + $source, + array('repeat_of' => $this->id, + 'scope' => $this->scope)); } // These are supposed to be in chron order! @@ -1898,14 +2140,24 @@ class Notice extends Memcached_DataObject public function getTags() { $tags = array(); - $tag = new Notice_tag(); - $tag->notice_id = $this->id; - if ($tag->find()) { - while ($tag->fetch()) { - $tags[] = $tag->tag; + + $keypart = sprintf('notice:tags:%d', $this->id); + + $tagstr = self::cacheGet($keypart); + + if ($tagstr !== false) { + $tags = explode(',', $tagstr); + } else { + $tag = new Notice_tag(); + $tag->notice_id = $this->id; + if ($tag->find()) { + while ($tag->fetch()) { + $tags[] = $tag->tag; + } } + self::cacheSet($keypart, implode(',', $tags)); } - $tag->free(); + return $tags; } @@ -2040,12 +2292,26 @@ class Notice extends Memcached_DataObject * 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 + * @param Profile $profile The profile to check; pass null to check for public/unauthenticated users. * * @return boolean whether the profile is in the notice's scope */ - function inScope($profile) + { + $keypart = sprintf('notice:in-scope-for:%d:%d', $this->id, $profile->id); + + $result = self::cacheGet($keypart); + + if ($result === false) { + $bResult = $this->_inScope($profile); + $result = ($bResult) ? 1 : 0; + self::cacheSet($keypart, $result, 0, 300); + } + + return ($result == 1) ? true : false; + } + + protected function _inScope($profile) { // If there's no scope, anyone (even anon) is in scope. @@ -2120,4 +2386,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; + } }