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;
$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);
+ }
+
+ $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;
+ }
+ }
+ }
- if (!empty($notice->reply_to)) {
- $reply = Notice::staticGet('id', $notice->reply_to);
- $notice->conversation = $reply->conversation;
+ // Scope set below
+ }
}
if (!empty($lat) && !empty($lon)) {
}
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
// 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);
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");
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)) {
if (!empty($reply_to)) {
$reply_notice = Notice::staticGet('id', $reply_to);
if (!empty($reply_notice)) {
- return $reply_to;
+ return $reply_notice;
}
}
$last = $recipient->getCurrentNotice();
if (!empty($last)) {
- return $last->id;
+ return $last;
}
+
+ return null;
}
static function maxContent()
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);
$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!
* 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)
{
- // If there's any scope, and there's no logged-in user,
- // not allowed.
+ // If there's no scope, anyone (even anon) is in scope.
+
+ if ($this->scope == 0) {
+ return true;
+ }
- if ($this->scope > 0 && empty($profile)) {
+ // If there's scope, anon cannot be in scope
+
+ if (empty($profile)) {
return false;
}
+ // Author is always in scope
+
+ if ($this->profile_id == $profile->id) {
+ return true;
+ }
+
// Only for users on this site
if ($this->scope & Notice::SITE_SCOPE) {
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;
+ }
}