public static function addNew(Notice $notice, Profile $actor = null) { if (is_null($actor)) { $actor = $notice->getProfile(); } if ($notice->getProfile()->hasRole(Profile_role::DELETED)) { // Don't emit notices if the notice author is (being) deleted return false; } $act = new Activity(); $act->verb = ActivityVerb::DELETE; $act->time = time(); $act->id = $notice->getUri(); $act->content = sprintf(_m('<a href="%1$s">%2$s</a> deleted notice <a href="%3$s">{{%4$s}}</a>.'), htmlspecialchars($actor->getUrl()), htmlspecialchars($actor->getBestName()), htmlspecialchars($notice->getUrl()), htmlspecialchars($notice->getUri())); $act->actor = $actor->asActivityObject(); $act->target = new ActivityObject(); // We don't save the notice object, as it's supposed to be removed! $act->target->id = $notice->getUri(); $act->target->type = $notice->getObjectType(); $act->objects = array(clone $act->target); $url = $notice->getUrl(); $act->selfLink = $url; $act->editLink = $url; // This will make ActivityModeration run saveObjectFromActivity which adds // a new Deleted_notice entry in the database as well as deletes the notice // if the actor has permission to do so. $stored = Notice::saveActivity($act, $actor); return $stored; }
/** * If poster is in one of the forced groups, make sure their notice * gets saved into that group even if not explicitly mentioned. * * @param Notice $notice * @return boolean event hook return */ function onStartNoticeDistribute($notice) { $profile = $notice->getProfile(); $isRemote = !User::getKV('id', $profile->id); if ($isRemote) { /* * Notices from remote users on other sites * will normally not end up here unless they're * specifically directed here, e.g.: via explicit * post to a remote (to them) group. But remote * notices can also be `pulled in' as a result of * local users subscribing to the remote user; * from the remote user's perspective, this results * in group-forcing appearing effectively random. * So let's be consistent, and just never force * incoming remote notices into a ForceGroup: */ return true; } foreach ($this->post as $nickname) { $group = User_group::getForNickname($nickname); if ($group && $profile->isMember($group)) { $notice->addToGroupInbox($group); } } return true; }
/** * If poster is in one of the forced groups, make sure their notice * gets saved into that group even if not explicitly mentioned. * * @param Notice $notice * @return boolean event hook return */ function onStartNoticeDistribute($notice) { $profile = $notice->getProfile(); foreach ($this->post as $nickname) { $group = User_group::getForNickname($nickname); if ($group && $profile->isMember($group)) { $notice->addToGroupInbox($group); } } return true; }
public function activityObjectFromNotice(Notice $notice) { $object = new ActivityObject(); $object->type = $notice->object_type ?: ActivityObject::NOTE; $object->id = $notice->getUri(); $object->title = sprintf('New %1$s by %2$s', ActivityObject::canonicalType($object->type), $notice->getProfile()->getNickname()); $object->content = $notice->getRendered(); $object->link = $notice->getUrl(); $object->extra[] = array('status_net', array('notice_id' => $notice->getID())); return $object; }
/** * extra information for XMPP messages, as defined by Twitter * * @param Profile $profile Profile of the sending user * @param Notice $notice Notice being sent * * @return string Extra information (Atom, HTML, addresses) in string format */ protected function format_entry(Notice $notice) { $profile = $notice->getProfile(); $entry = $notice->asAtomEntry(true, true); $xs = new XMLStringer(); $xs->elementStart('html', array('xmlns' => 'http://jabber.org/protocol/xhtml-im')); $xs->elementStart('body', array('xmlns' => 'http://www.w3.org/1999/xhtml')); $xs->element('a', array('href' => $profile->profileurl), $profile->nickname); try { $parent = $notice->getParent(); $orig_profile = $parent->getProfile(); $orig_profurl = $orig_profile->getUrl(); $xs->text(" => "); $xs->element('a', array('href' => $orig_profurl), $orig_profile->nickname); $xs->text(": "); } catch (InvalidUrlException $e) { $xs->text(sprintf(' => %s', $orig_profile->nickname)); } catch (NoParentNoticeException $e) { $xs->text(": "); } if (!empty($notice->rendered)) { $notice->rendered = str_replace("\t", "", $notice->rendered); $xs->raw($notice->rendered); } else { $xs->raw(common_render_content($notice->content, $notice)); } $xs->text(" "); $xs->element('a', array('href' => common_local_url('conversation', array('id' => $notice->conversation)) . '#notice-' . $notice->id), sprintf(_m('[%u]'), $notice->id)); $xs->elementEnd('body'); $xs->elementEnd('html'); $html = $xs->getString(); return $html . ' ' . $entry; }
/** * When saving a notice, check its groups. If any of them has * privacy == always, force a group private message to all mentioned groups. * If any of the groups disallows private messages, skip it. * * @param */ function onStartNoticeSave(Notice &$notice) { // Look for group tags // FIXME: won't work for remote groups // @fixme if Notice::saveNew is refactored so we can just pull its list // of groups between processing and saving, make use of it $count = preg_match_all('/(?:^|\\s)!(' . Nickname::DISPLAY_FMT . ')/', strtolower($notice->content), $match); $groups = array(); $ignored = array(); $forcePrivate = false; $profile = $notice->getProfile(); if ($count > 0) { /* Add them to the database */ foreach (array_unique($match[1]) as $nickname) { $group = User_group::getForNickname($nickname, $profile); if (empty($group)) { continue; } $gps = Group_privacy_settings::forGroup($group); switch ($gps->allow_privacy) { case Group_privacy_settings::ALWAYS: $forcePrivate = true; // fall through // fall through case Group_privacy_settings::SOMETIMES: $groups[] = $group; break; case Group_privacy_settings::NEVER: $ignored[] = $group; break; } } if ($forcePrivate) { foreach ($ignored as $group) { common_log(LOG_NOTICE, "Notice forced to group direct message " . "but group " . $group->nickname . " does not allow them."); } $user = User::getKV('id', $notice->profile_id); if (empty($user)) { common_log(LOG_WARNING, "Notice forced to group direct message " . "but profile " . $notice->profile_id . " is not a local user."); } else { foreach ($groups as $group) { Group_message::send($user, $group, $notice->content); } } // Don't save the notice! // FIXME: this is probably cheating. // TRANS: Client exception thrown when a private group message has to be forced. throw new ClientException(sprintf(_m('Forced notice to private group message.')), 200); } } return true; }
public function onEndFavorNotice(Profile $actor, Notice $target) { try { $notice_author = $target->getProfile(); // Don't notify ourselves of our own favorite on our own notice, // or if it's a remote user (since we don't know their email addresses etc.) if ($notice_author->id == $actor->id || !$notice_author->isLocal()) { return true; } $local_user = $notice_author->getUser(); mail_notify_fave($local_user, $actor, $target); } catch (Exception $e) { // Mm'kay, probably not a local user. Let's skip this favor notification. } }
/** * makes a plain-text formatted version of a notice, suitable for IM distribution * * @param Notice $notice notice being sent * * @return string plain-text version of the notice, with user nickname prefixed */ protected function formatNotice(Notice $notice) { $profile = $notice->getProfile(); try { $parent = $notice->getParent(); $orig_profile = $parent->getProfile(); $nicknames = sprintf('%1$s => %2$s', $profile->nickname, $orig_profile->nickname); } catch (NoParentNoticeException $e) { $nicknames = $profile->nickname; } return sprintf('%1$s: %2$s [%3$u]', $nicknames, $notice->content, $notice->id); }
protected function showEvent(Notice $stored, HTMLOutputter $out, Profile $scoped = null) { $profile = $stored->getProfile(); $event = Happening::fromNotice($stored); if (!$event instanceof Happening) { // TRANS: Content for a deleted RSVP list item (RSVP stands for "please respond"). $out->element('p', null, _m('Deleted.')); return; } $out->elementStart('div', 'h-event'); $out->elementStart('h3', 'p-summary p-name'); try { $out->element('a', array('href' => $event->getUrl()), $event->title); } catch (InvalidUrlException $e) { $out->text($event->title); } $out->elementEnd('h3'); $now = new DateTime(); $startDate = new DateTime($event->start_time); $endDate = new DateTime($event->end_time); $userTz = new DateTimeZone(common_timezone()); // Localize the time for the observer $now->setTimeZone($userTz); $startDate->setTimezone($userTz); $endDate->setTimezone($userTz); $thisYear = $now->format('Y'); $startYear = $startDate->format('Y'); $endYear = $endDate->format('Y'); $dateFmt = 'D, F j, '; // e.g.: Mon, Aug 31 if ($startYear != $thisYear || $endYear != $thisYear) { $dateFmt .= 'Y,'; // append year if we need to think about years } $startDateStr = $startDate->format($dateFmt); $endDateStr = $endDate->format($dateFmt); $timeFmt = 'g:ia'; $startTimeStr = $startDate->format($timeFmt); $endTimeStr = $endDate->format("{$timeFmt} (T)"); $out->elementStart('div', 'event-times'); // VEVENT/EVENT-TIMES IN // TRANS: Field label for event description. $out->element('strong', null, _m('Time:')); $out->element('time', array('class' => 'dt-start', 'datetime' => common_date_iso8601($event->start_time)), $startDateStr . ' ' . $startTimeStr); $out->text(' – '); $out->element('time', array('class' => 'dt-end', 'datetime' => common_date_iso8601($event->end_time)), $startDateStr != $endDateStr ? "{$endDateStr} {$endTimeStr}" : $endTimeStr); $out->elementEnd('div'); // VEVENT/EVENT-TIMES OUT if (!empty($event->location)) { $out->elementStart('div', 'event-location'); // TRANS: Field label for event description. $out->element('strong', null, _m('Location:')); $out->element('span', 'p-location', $event->location); $out->elementEnd('div'); } if (!empty($event->description)) { $out->elementStart('div', 'event-description'); // TRANS: Field label for event description. $out->element('strong', null, _m('Description:')); $out->element('div', 'p-description', $event->description); $out->elementEnd('div'); } $rsvps = $event->getRSVPs(); $out->elementStart('div', 'event-rsvps'); // TRANS: Field label for event description. $out->element('strong', null, _m('Attending:')); $out->elementStart('ul', 'attending-list'); foreach ($rsvps as $verb => $responses) { $out->elementStart('li', 'rsvp-list'); switch ($verb) { case RSVP::POSITIVE: $out->text(_('Yes:')); break; case RSVP::NEGATIVE: $out->text(_('No:')); break; case RSVP::POSSIBLE: $out->text(_('Maybe:')); break; } $ids = array(); foreach ($responses as $response) { $ids[] = $response->profile_id; } $ids = array_slice($ids, 0, ProfileMiniList::MAX_PROFILES + 1); $minilist = new ProfileMiniList(Profile::multiGet('id', $ids), $out); $minilist->show(); $out->elementEnd('li'); } $out->elementEnd('ul'); $out->elementEnd('div'); if ($scoped instanceof Profile) { $rsvp = $event->getRSVP($scoped); if (empty($rsvp)) { $form = new RSVPForm($event, $out); } else { $form = new CancelRSVPForm($rsvp, $out); } $form->show(); } $out->elementEnd('div'); }
/** * Save a new notice and push it out to subscribers' inboxes. * Poster's permissions are checked before sending. * * @param int $profile_id Profile ID of the poster * @param string $content source message text; links may be shortened * per current user's preference * @param string $source source key ('web', 'api', etc) * @param array $options Associative array of optional properties: * string 'created' timestamp of notice; defaults to now * int 'is_local' source/gateway ID, one of: * Notice::LOCAL_PUBLIC - Local, ok to appear in public timeline * Notice::REMOTE - Sent from a remote service; * hide from public timeline but show in * local "and friends" timelines * Notice::LOCAL_NONPUBLIC - Local, but hide from public timeline * Notice::GATEWAY - From another non-OStatus service; * will not appear in public views * float 'lat' decimal latitude for geolocation * float 'lon' decimal longitude for geolocation * int 'location_id' geoname identifier * int 'location_ns' geoname namespace to interpret location_id * int 'reply_to'; notice ID this is a reply to * int 'repeat_of'; notice ID this is a repeat of * string 'uri' unique ID for notice; a unique tag uri (can be url or anything too) * string 'url' permalink to notice; defaults to local notice URL * string 'rendered' rendered HTML version of content * array 'replies' list of profile URIs for reply delivery in * place of extracting @-replies from content. * array 'groups' list of group IDs to deliver to, in place of * extracting ! tags from content * array 'tags' list of hashtag strings to save with the notice * in place of extracting # tags from content * array 'urls' list of attached/referred URLs to save with the * notice in place of extracting links from content * boolean 'distribute' whether to distribute the notice, default true * string 'object_type' URL of the associated object type (default ActivityObject::NOTE) * string 'verb' URL of the associated verb (default ActivityVerb::POST) * int 'scope' Scope bitmask; default to SITE_SCOPE on private sites, 0 otherwise * * @fixme tag override * * @return Notice * @throws ClientException */ static function saveNew($profile_id, $content, $source, array $options = null) { $defaults = array('uri' => null, 'url' => null, 'conversation' => null, 'reply_to' => null, 'repeat_of' => null, 'scope' => null, 'distribute' => true, 'object_type' => null, 'verb' => null); if (!empty($options) && is_array($options)) { $options = array_merge($defaults, $options); extract($options); } else { extract($defaults); } if (!isset($is_local)) { $is_local = Notice::LOCAL_PUBLIC; } $profile = Profile::getKV('id', $profile_id); if (!$profile instanceof Profile) { // TRANS: Client exception thrown when trying to save a notice for an unknown user. throw new ClientException(_('Problem saving notice. Unknown user.')); } $user = User::getKV('id', $profile_id); if ($user instanceof User) { // Use the local user's shortening preferences, if applicable. $final = $user->shortenLinks($content); } else { $final = common_shorten_links($content); } if (Notice::contentTooLong($final)) { // TRANS: Client exception thrown if a notice contains too many characters. throw new ClientException(_('Problem saving notice. Too long.')); } if (common_config('throttle', 'enabled') && !Notice::checkEditThrottle($profile_id)) { common_log(LOG_WARNING, 'Excessive posting by profile #' . $profile_id . '; throttled.'); // TRANS: Client exception thrown when a user tries to post too many notices in a given time frame. throw new ClientException(_('Too many notices too fast; take a breather ' . 'and post again in a few minutes.')); } if (common_config('site', 'dupelimit') > 0 && !Notice::checkDupes($profile_id, $final)) { common_log(LOG_WARNING, 'Dupe posting by profile #' . $profile_id . '; throttled.'); // TRANS: Client exception thrown when a user tries to post too many duplicate notices in a given time frame. throw new ClientException(_('Too many duplicate messages too quickly;' . ' take a breather and post again in a few minutes.')); } if (!$profile->hasRight(Right::NEWNOTICE)) { common_log(LOG_WARNING, "Attempted post from user disallowed to post: " . $profile->nickname); // TRANS: Client exception thrown when a user tries to post while being banned. throw new ClientException(_('You are banned from posting notices on this site.'), 403); } $notice = new Notice(); $notice->profile_id = $profile_id; $autosource = common_config('public', 'autosource'); // Sandboxed are non-false, but not 1, either if (!$profile->hasRight(Right::PUBLICNOTICE) || $source && $autosource && in_array($source, $autosource)) { $notice->is_local = Notice::LOCAL_NONPUBLIC; } else { $notice->is_local = $is_local; } if (!empty($created)) { $notice->created = $created; } else { $notice->created = common_sql_now(); } if (!$notice->isLocal()) { // Only do these checks for non-local notices. Local notices will generate these values later. if (!common_valid_http_url($url)) { common_debug('Bad notice URL: [' . $url . '], URI: [' . $uri . ']. Cannot link back to original! This is normal for shared notices etc.'); } if (empty($uri)) { throw new ServerException('No URI for remote notice. Cannot accept that.'); } } $notice->content = $final; $notice->source = $source; $notice->uri = $uri; $notice->url = $url; // Get the groups here so we can figure out replies and such if (!isset($groups)) { $groups = User_group::idsFromText($notice->content, $profile); } $reply = null; // Handle repeat case if (!empty($options['repeat_of'])) { // Check for a private one $repeat = Notice::getByID($options['repeat_of']); if ($profile->sameAs($repeat->getProfile())) { // 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)) { // TRANS: Client error displayed when trying to repeat an already repeated notice. throw new ClientException(_('You already repeated that notice.')); } $notice->repeat_of = $repeat->id; $notice->conversation = $repeat->conversation; } else { $reply = null; // If $reply_to is specified, we check that it exists, and then // return it if it does if (!empty($reply_to)) { $reply = Notice::getKV('id', $reply_to); } elseif (in_array($source, array('xmpp', 'mail', 'sms'))) { // If the source lacks capability of sending the "reply_to" // metadata, let's try to find an inline replyto-reference. $reply = self::getInlineReplyTo($profile, $final); } if ($reply instanceof Notice) { 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 it's a repeat, the reply_to should be to the original if ($reply->isRepeat()) { $notice->reply_to = $reply->repeat_of; } else { $notice->reply_to = $reply->id; } // But the conversation ought to be the same :) $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 } // If we don't know the reply, we might know the conversation! // This will happen if a known remote user replies to an // unknown remote user - within a known conversation. if (empty($notice->conversation) and !empty($options['conversation'])) { $conv = Conversation::getKV('uri', $options['conversation']); if ($conv instanceof Conversation) { common_debug('Conversation stitched together from (probably) a reply to unknown remote user. Activity creation time (' . $notice->created . ') should maybe be compared to conversation creation time (' . $conv->created . ').'); } else { // Conversation entry with specified URI was not found, so we must create it. common_debug('Conversation URI not found, so we will create it with the URI given in the options to Notice::saveNew: ' . $options['conversation']); // The insert in Conversation::create throws exception on failure $conv = Conversation::create($options['conversation'], $notice->created); } $notice->conversation = $conv->getID(); unset($conv); } } // If it's not part of a conversation, it's the beginning of a new conversation. if (empty($notice->conversation)) { $conv = Conversation::create(); $notice->conversation = $conv->getID(); unset($conv); } $notloc = new Notice_location(); if (!empty($lat) && !empty($lon)) { $notloc->lat = $lat; $notloc->lon = $lon; } if (!empty($location_ns) && !empty($location_id)) { $notloc->location_id = $location_id; $notloc->location_ns = $location_ns; } if (!empty($rendered)) { $notice->rendered = $rendered; } else { $notice->rendered = common_render_content($final, $notice->getProfile(), $notice->hasParent() ? $notice->getParent() : null); } if (empty($verb)) { if ($notice->isRepeat()) { $notice->verb = ActivityVerb::SHARE; $notice->object_type = ActivityObject::ACTIVITY; } else { $notice->verb = ActivityVerb::POST; } } else { $notice->verb = $verb; } if (empty($object_type)) { $notice->object_type = empty($notice->reply_to) ? ActivityObject::NOTE : ActivityObject::COMMENT; } else { $notice->object_type = $object_type; } if (is_null($scope) && $reply instanceof Notice) { $notice->scope = $reply->scope; } else { $notice->scope = $scope; } $notice->scope = self::figureOutScope($profile, $groups, $notice->scope); if (Event::handle('StartNoticeSave', array(&$notice))) { // XXX: some of these functions write to the DB try { $notice->insert(); // throws exception on failure, if successful we have an ->id if ($notloc->lat && $notloc->lon || $notloc->location_id && $notloc->location_ns) { $notloc->notice_id = $notice->getID(); $notloc->insert(); // store the notice location if it had any information } } catch (Exception $e) { // Let's test if we managed initial insert, which would imply // failing on some update-part (check 'insert()'). Delete if // something had been stored to the database. if (!empty($notice->id)) { $notice->delete(); } throw $e; } } // Only save 'attention' and metadata stuff (URLs, tags...) stuff if // the activityverb is a POST (since stuff like repeat, favorite etc. // reasonably handle notifications themselves. if (ActivityUtils::compareVerbs($notice->verb, array(ActivityVerb::POST))) { if (isset($replies)) { $notice->saveKnownReplies($replies); } else { $notice->saveReplies(); } if (isset($tags)) { $notice->saveKnownTags($tags); } else { $notice->saveTags(); } // Note: groups may save tags, so must be run after tags are saved // to avoid errors on duplicates. // Note: groups should always be set. $notice->saveKnownGroups($groups); if (isset($urls)) { $notice->saveKnownUrls($urls); } else { $notice->saveUrls(); } } if ($distribute) { // Prepare inbox delivery, may be queued to background. $notice->distribute(); } return $notice; }
/** * Notify a user that they have received an "attn:" message AKA "@-reply" * * @param User $user The user who recevied the notice * @param Notice $notice The notice that was sent * * @return void */ function mail_notify_attn($user, $notice) { if (!$user->receivesEmailNotifications()) { return; } $sender = $notice->getProfile(); if ($sender->id == $user->id) { return; } // See if the notice's author who mentions this user is sandboxed if (!$sender->hasRight(Right::EMAILONREPLY)) { return; } // If the author has blocked the author, don't spam them with a notification. if ($user->hasBlocked($sender)) { return; } $bestname = $sender->getBestName(); common_switch_locale($user->language); if ($notice->hasConversation()) { $conversationUrl = common_local_url('conversation', array('id' => $notice->conversation)) . '#notice-' . $notice->id; // TRANS: Line in @-reply notification e-mail. %s is conversation URL. $conversationEmailText = sprintf(_("The full conversation can be read here:\n\n" . "\t%s"), $conversationUrl) . "\n\n"; } else { $conversationEmailText = ''; } // TRANS: E-mail subject for notice notification. // TRANS: %1$s is the sending user's long name, %2$s is the adding user's nickname. $subject = sprintf(_('%1$s (@%2$s) sent a notice to your attention'), $bestname, $sender->nickname); // TRANS: Body of @-reply notification e-mail. // TRANS: %1$s is the sending user's name, $2$s is the StatusNet sitename, // TRANS: %3$s is a URL to the notice, %4$s is the notice text, // TRANS: %5$s is the text "The full conversation can be read here:" and a URL to the full conversion if it exists (otherwise empty), // TRANS: %6$s is a URL to reply to the notice, %7$s is a URL to all @-replies for the addressed user, $body = sprintf(_("%1\$s just sent a notice to your attention (an '@-reply') on %2\$s.\n\n" . "The notice is here:\n\n" . "\t%3\$s\n\n" . "It reads:\n\n" . "\t%4\$s\n\n" . "%5\$s" . "You can reply back here:\n\n" . "\t%6\$s\n\n" . "The list of all @-replies for you here:\n\n" . "%7\$s"), $sender->getFancyName(), common_config('site', 'name'), common_local_url('shownotice', array('notice' => $notice->id)), $notice->content, $conversationEmailText, common_local_url('newnotice', array('replyto' => $sender->nickname, 'inreplyto' => $notice->id)), common_local_url('replies', array('nickname' => $user->nickname))) . mail_footer_block(); $headers = _mail_prepare_headers('mention', $user->nickname, $sender->nickname); common_switch_locale(); mail_to_user($user, $subject, $body, $headers); }
public function extendActivity(Notice $stored, Activity $act, Profile $scoped = null) { // TODO: How to handle repeats of deleted notices? $target = Notice::getByID($stored->repeat_of); // TRANS: A repeat activity's title. %1$s is repeater's nickname // and %2$s is the repeated user's nickname. $act->title = sprintf(_('%1$s repeated a notice by %2$s'), $stored->getProfile()->getNickname(), $target->getProfile()->getNickname()); $act->objects[] = $target->asActivity($scoped); }
public function activityObjectFromNotice(Notice $stored) { // Repeat is a little bit special. As it's an activity, our // ActivityObject is instead turned into an Activity $object = new Activity(); $object->actor = $stored->getProfile()->asActivityObject(); $object->verb = ActivityVerb::SHARE; $object->content = $stored->getRendered(); $this->extendActivity($stored, $object); return $object; }
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; }
/** * extra information for XMPP messages, as defined by Twitter * * @param Profile $profile Profile of the sending user * @param Notice $notice Notice being sent * * @return string Extra information (Atom, HTML, addresses) in string format */ function format_entry($notice) { $profile = $notice->getProfile(); $entry = $notice->asAtomEntry(true, true); $xs = new XMLStringer(); $xs->elementStart('html', array('xmlns' => 'http://jabber.org/protocol/xhtml-im')); $xs->elementStart('body', array('xmlns' => 'http://www.w3.org/1999/xhtml')); $xs->element('a', array('href' => $profile->profileurl), $profile->nickname); $xs->text(": "); if (!empty($notice->rendered)) { $xs->raw($notice->rendered); } else { $xs->raw(common_render_content($notice->content, $notice)); } $xs->text(" "); $xs->element('a', array('href' => common_local_url('conversation', array('id' => $notice->conversation)) . '#notice-' . $notice->id), sprintf(_m('[%s]'), $notice->id)); $xs->elementEnd('body'); $xs->elementEnd('html'); $html = $xs->getString(); return $html . ' ' . $entry; }
function onEndWebFingerNoticeLinks(XML_XRD $xrd, Notice $target) { $author = $target->getProfile(); $profiletype = $this->profileTypeString($author); $salmon_url = common_local_url("{$profiletype}salmon", array('id' => $author->id)); $xrd->links[] = new XML_XRD_Element_Link(Salmon::REL_SALMON, $salmon_url); return true; }
/** * makes a plain-text formatted version of a notice, suitable for IM distribution * * @param Notice $notice notice being sent * * @return string plain-text version of the notice, with user nickname prefixed */ protected function formatNotice(Notice $notice) { $profile = $notice->getProfile(); $nicknames = $profile->getNickname(); try { $parent = $notice->getParent(); $orig_profile = $parent->getProfile(); $nicknames = sprintf('%1$s => %2$s', $profile->getNickname(), $orig_profile->getNickname()); } catch (NoParentNoticeException $e) { // Not a reply, no parent notice stored } catch (NoResultException $e) { // Parent notice was probably deleted } return sprintf('%1$s: %2$s [%3$u]', $nicknames, $notice->content, $notice->id); }
/** * Find @-mentions in the given text, using the given notice object as context. * References will be resolved with common_relative_profile() against the user * who posted the notice. * * Note the return data format is internal, to be used for building links and * such. Should not be used directly; rather, call common_linkify_mentions(). * * @param string $text * @param Notice $notice notice in whose context we're building links * * @return array * * @access private */ function common_find_mentions($text, Notice $notice) { // The getProfile call throws NoProfileException on failure $sender = $notice->getProfile(); $mentions = array(); if (Event::handle('StartFindMentions', array($sender, $text, &$mentions))) { // Get the context of the original notice, if any $origAuthor = null; $origNotice = null; $origMentions = array(); // Is it a reply? if ($notice instanceof Notice) { try { $origNotice = $notice->getParent(); $origAuthor = $origNotice->getProfile(); $ids = $origNotice->getReplies(); foreach ($ids as $id) { $repliedTo = Profile::getKV('id', $id); if ($repliedTo instanceof Profile) { $origMentions[$repliedTo->nickname] = $repliedTo; } } } catch (NoProfileException $e) { common_log(LOG_WARNING, sprintf('Notice %d author profile id %d does not exist', $origNotice->id, $origNotice->profile_id)); } catch (NoParentNoticeException $e) { // This notice is not in reply to anything } catch (Exception $e) { common_log(LOG_WARNING, __METHOD__ . ' got exception ' . get_class($e) . ' : ' . $e->getMessage()); } } $matches = common_find_mentions_raw($text); foreach ($matches as $match) { try { $nickname = Nickname::normalize($match[0]); } catch (NicknameException $e) { // Bogus match? Drop it. continue; } // Try to get a profile for this nickname. // Start with conversation context, then go to // sender context. if ($origAuthor instanceof Profile && $origAuthor->nickname == $nickname) { $mentioned = $origAuthor; } else { if (!empty($origMentions) && array_key_exists($nickname, $origMentions)) { $mentioned = $origMentions[$nickname]; } else { $mentioned = common_relative_profile($sender, $nickname); } } if ($mentioned instanceof Profile) { $user = User::getKV('id', $mentioned->id); if ($user instanceof User) { $url = common_local_url('userbyid', array('id' => $user->id)); } else { $url = $mentioned->profileurl; } $mention = array('mentioned' => array($mentioned), 'type' => 'mention', 'text' => $match[0], 'position' => $match[1], 'url' => $url); if (!empty($mentioned->fullname)) { $mention['title'] = $mentioned->fullname; } $mentions[] = $mention; } } // @#tag => mention of all subscriptions tagged 'tag' preg_match_all('/(?:^|[\\s\\.\\,\\:\\;]+)@#([\\pL\\pN_\\-\\.]{1,64})/', $text, $hmatches, PREG_OFFSET_CAPTURE); foreach ($hmatches[1] as $hmatch) { $tag = common_canonical_tag($hmatch[0]); $plist = Profile_list::getByTaggerAndTag($sender->id, $tag); if (!$plist instanceof Profile_list || $plist->private) { continue; } $tagged = $sender->getTaggedSubscribers($tag); $url = common_local_url('showprofiletag', array('tagger' => $sender->nickname, 'tag' => $tag)); $mentions[] = array('mentioned' => $tagged, 'type' => 'list', 'text' => $hmatch[0], 'position' => $hmatch[1], 'url' => $url); } preg_match_all('/(?:^|[\\s\\.\\,\\:\\;]+)!(' . Nickname::DISPLAY_FMT . ')/', $text, $hmatches, PREG_OFFSET_CAPTURE); foreach ($hmatches[1] as $hmatch) { $nickname = Nickname::normalize($hmatch[0]); $group = User_group::getForNickname($nickname, $sender); if (!$group instanceof User_group || !$sender->isMember($group)) { continue; } $profile = $group->getProfile(); $mentions[] = array('mentioned' => array($profile), 'type' => 'group', 'text' => $hmatch[0], 'position' => $hmatch[1], 'url' => $group->permalink(), 'title' => $group->getFancyName()); } Event::handle('EndFindMentions', array($sender, $text, &$mentions)); } return $mentions; }
protected function getActivityForm(ManagedAction $action, $verb, Notice $target, Profile $scoped) { if (!$scoped instanceof Profile || !($scoped->sameAs($target->getProfile()) || $scoped->hasRight(Right::DELETEOTHERSNOTICE))) { throw new AuthorizationException(_('You are not allowed to delete other user\'s notices')); } return DeletenoticeForm($action, array('notice' => $target)); }
/** * Build an Atom entry similar to search.twitter.com's based on * a given notice * * @param Notice $notice the notice to use * * @return void */ function showEntry($notice) { $server = common_config('site', 'server'); $profile = $notice->getProfile(); $nurl = common_local_url('shownotice', array('notice' => $notice->id)); $this->elementStart('entry'); $taguribase = TagURI::base(); $this->element('id', null, "tag:{$taguribase}:{$notice->id}"); $this->element('published', null, common_date_w3dtf($notice->created)); $this->element('link', array('type' => 'text/html', 'rel' => 'alternate', 'href' => $nurl)); $this->element('title', null, common_xml_safe_str(trim($notice->content))); $this->element('content', array('type' => 'html'), $notice->rendered); $this->element('updated', null, common_date_w3dtf($notice->created)); $this->element('link', array('type' => 'image/png', 'rel' => 'related', 'href' => $profile->avatarUrl())); // TODO: Here is where we'd put in a link to an atom feed for threads $this->element("twitter:source", null, htmlentities($this->sourceLink($notice->source))); $this->elementStart('author'); $name = $profile->nickname; if ($profile->fullname) { $name .= ' (' . $profile->fullname . ')'; } $this->element('name', null, $name); $this->element('uri', null, common_profile_uri($profile)); $this->elementEnd('author'); $this->elementEnd('entry'); }
/** * notify a user that they have received an "attn:" message AKA "@-reply" * * @param User $user The user who recevied the notice * @param Notice $notice The notice that was sent * * @return void */ function mail_notify_attn($user, $notice) { if (!$user->email || !$user->emailnotifyattn) { return; } $sender = $notice->getProfile(); if ($sender->id == $user->id) { return; } if (!$sender->hasRight(Right::EMAILONREPLY)) { return; } $bestname = $sender->getBestName(); common_switch_locale($user->language); if ($notice->hasConversation()) { $conversationUrl = common_local_url('conversation', array('id' => $notice->conversation)) . '#notice-' . $notice->id; // TRANS: Line in @-reply notification e-mail. %s is conversation URL. $conversationEmailText = sprintf(_("The full conversation can be read here:\n\n" . "\t%s"), $conversationUrl) . "\n\n"; } else { $conversationEmailText = ''; } // TRANS: E-mail subject for notice notification. // TRANS: %1$s is the sending user's long name, %2$s is the adding user's nickname. $subject = sprintf(_('%1$s (@%2$s) sent a notice to your attention'), $bestname, $sender->nickname); // TRANS: Body of @-reply notification e-mail. // TRANS: %1$s is the sending user's long name, $2$s is the StatusNet sitename, // TRANS: %3$s is a URL to the notice, %4$s is the notice text, // TRANS: %5$s is a URL to the full conversion if it exists (otherwise empty), // TRANS: %6$s is a URL to reply to the notice, %7$s is a URL to all @-replied for the addressed user, // TRANS: %8$s is a URL to the addressed user's e-mail settings, %9$s is the sender's nickname. $body = sprintf(_("%1\$s (@%9\$s) just sent a notice to your attention (an '@-reply') on %2\$s.\n\n" . "The notice is here:\n\n" . "\t%3\$s\n\n" . "It reads:\n\n" . "\t%4\$s\n\n" . "%5\$s" . "You can reply back here:\n\n" . "\t%6\$s\n\n" . "The list of all @-replies for you here:\n\n" . "%7\$s\n\n" . "Faithfully yours,\n" . "%2\$s\n\n" . "P.S. You can turn off these email notifications here: %8\$s\n"), $bestname, common_config('site', 'name'), common_local_url('shownotice', array('notice' => $notice->id)), $notice->content, $conversationEmailText, common_local_url('newnotice', array('replyto' => $sender->nickname, 'inreplyto' => $notice->id)), common_local_url('replies', array('nickname' => $user->nickname)), common_local_url('emailsettings'), $sender->nickname); //%9 $headers = _mail_prepare_headers('mention', $user->nickname, $sender->nickname); common_switch_locale(); mail_to_user($user, $subject, $body, $headers); }
/** * makes a plain-text formatted version of a notice, suitable for IM distribution * * @param Notice $notice notice being sent * * @return string plain-text version of the notice, with user nickname prefixed */ function formatNotice($notice) { $profile = $notice->getProfile(); return $profile->nickname . ': ' . $notice->content . ' [' . $notice->id . ']'; }
/** * Find @-mentions in the given text, using the given notice object as context. * References will be resolved with common_relative_profile() against the user * who posted the notice. * * Note the return data format is internal, to be used for building links and * such. Should not be used directly; rather, call common_linkify_mentions(). * * @param string $text * @param Profile $sender the Profile that is sending the current text * @param Notice $parent the Notice this text is in reply to, if any * * @return array * * @access private */ function common_find_mentions($text, Profile $sender, Notice $parent = null) { $mentions = array(); if (Event::handle('StartFindMentions', array($sender, $text, &$mentions))) { // Get the context of the original notice, if any $origMentions = array(); // Does it have a parent notice for context? if ($parent instanceof Notice) { $ids = $parent->getReplies(); // replied-to _profile ids_ foreach ($ids as $id) { try { $repliedTo = Profile::getByID($id); $origMentions[$repliedTo->getNickname()] = $repliedTo; } catch (NoResultException $e) { // continue foreach } } } $matches = common_find_mentions_raw($text); foreach ($matches as $match) { try { $nickname = Nickname::normalize($match[0]); } catch (NicknameException $e) { // Bogus match? Drop it. continue; } // Try to get a profile for this nickname. // Start with conversation context, then go to // sender context. if ($parent instanceof Notice && $parent->getProfile()->getNickname() === $nickname) { $mentioned = $parent->getProfile(); } else { if (!empty($origMentions) && array_key_exists($nickname, $origMentions)) { $mentioned = $origMentions[$nickname]; } else { // sets to null if no match $mentioned = common_relative_profile($sender, $nickname); } } if ($mentioned instanceof Profile) { $user = User::getKV('id', $mentioned->id); try { $url = $mentioned->getUrl(); } catch (InvalidUrlException $e) { $url = common_local_url('userbyid', array('id' => $mentioned->getID())); } $mention = array('mentioned' => array($mentioned), 'type' => 'mention', 'text' => $match[0], 'position' => $match[1], 'length' => mb_strlen($match[0]), 'title' => $mentioned->getFullname(), 'url' => $url); $mentions[] = $mention; } } // @#tag => mention of all subscriptions tagged 'tag' preg_match_all('/(?:^|[\\s\\.\\,\\:\\;]+)@#([\\pL\\pN_\\-\\.]{1,64})/', $text, $hmatches, PREG_OFFSET_CAPTURE); foreach ($hmatches[1] as $hmatch) { $tag = common_canonical_tag($hmatch[0]); $plist = Profile_list::getByTaggerAndTag($sender->getID(), $tag); if (!$plist instanceof Profile_list || $plist->private) { continue; } $tagged = $sender->getTaggedSubscribers($tag); $url = common_local_url('showprofiletag', array('nickname' => $sender->getNickname(), 'tag' => $tag)); $mentions[] = array('mentioned' => $tagged, 'type' => 'list', 'text' => $hmatch[0], 'position' => $hmatch[1], 'length' => mb_strlen($hmatch[0]), 'url' => $url); } preg_match_all('/(?:^|[\\s\\.\\,\\:\\;]+)!(' . Nickname::DISPLAY_FMT . ')/', $text, $hmatches, PREG_OFFSET_CAPTURE); foreach ($hmatches[1] as $hmatch) { $nickname = Nickname::normalize($hmatch[0]); $group = User_group::getForNickname($nickname, $sender); if (!$group instanceof User_group || !$sender->isMember($group)) { continue; } $profile = $group->getProfile(); $mentions[] = array('mentioned' => array($profile), 'type' => 'group', 'text' => $hmatch[0], 'position' => $hmatch[1], 'length' => mb_strlen($hmatch[0]), 'url' => $group->permalink(), 'title' => $group->getFancyName()); } Event::handle('EndFindMentions', array($sender, $text, &$mentions)); } return $mentions; }
/** * constructor * * Also initializes the profile attribute. * * @param Notice $notice The notice we'll display */ function __construct($notice) { $this->notice = $notice; $this->profile = $notice->getProfile(); $this->buildResult(); }
* This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU Affero General Public License for more details. * * You should have received a copy of the GNU Affero General Public License * along with this program. If not, see <http://www.gnu.org/licenses/>. */ // Abort if called from a web server if (isset($_SERVER) && array_key_exists('REQUEST_METHOD', $_SERVER)) { print "This script must be run from the command line\n"; exit; } define('INSTALLDIR', realpath(dirname(__FILE__) . '/..')); define('GNUSOCIAL', true); define('STATUSNET', true); // compatibility require_once INSTALLDIR . '/lib/common.php'; common_log(LOG_INFO, 'Starting to do old notices.'); $notice = new Notice(); $cnt = $notice->find(); while ($notice->fetch()) { common_log(LOG_INFO, 'Getting tags for notice #' . $notice->id); $notice->saveTags(); $original = clone $notice; $notice->rendered = common_render_content($notice->content, $notice->getProfile(), $notice->hasParent() ? $notice->getParent() : null); $result = $notice->update($original); if (!$result) { common_log_db_error($notice, 'UPDATE', __FILE__); } }
public function onHandleQueuedNotice(Notice $notice) { $paths = array(); // Add to the author's timeline try { $profile = $notice->getProfile(); } catch (Exception $e) { $this->log(LOG_ERR, $e->getMessage()); return true; } try { $user = $profile->getUser(); $paths[] = array('showstream', $user->nickname, null); } catch (NoSuchUserException $e) { // We really should handle the remote profile views too $user = null; } // Add to the public timeline $is_local = intval($notice->is_local); if ($is_local === Notice::LOCAL_PUBLIC || $is_local === Notice::REMOTE && !common_config('public', 'localonly')) { $paths[] = array('public', null, null); } // Add to the tags timeline $tags = $this->getNoticeTags($notice); if (!empty($tags)) { foreach ($tags as $tag) { $paths[] = array('tag', $tag, null); } } // Add to inbox timelines // XXX: do a join $ni = $notice->whoGets(); foreach (array_keys($ni) as $user_id) { $user = User::getKV('id', $user_id); $paths[] = array('all', $user->nickname, null); } // Add to the replies timeline $reply = new Reply(); $reply->notice_id = $notice->id; if ($reply->find()) { while ($reply->fetch()) { $user = User::getKV('id', $reply->profile_id); if (!empty($user)) { $paths[] = array('replies', $user->nickname, null); } } } // Add to the group timeline // XXX: join $gi = new Group_inbox(); $gi->notice_id = $notice->id; if ($gi->find()) { while ($gi->fetch()) { $ug = User_group::getKV('id', $gi->group_id); $paths[] = array('showgroup', $ug->nickname, null); } } if (count($paths) > 0) { $json = $this->noticeAsJson($notice); $this->_connect(); // XXX: We should probably fan-out here and do a // new queue item for each path foreach ($paths as $path) { list($action, $arg1, $arg2) = $path; $channels = Realtime_channel::getAllChannels($action, $arg1, $arg2); $this->log(LOG_INFO, sprintf(_("%d candidate channels for notice %d"), count($channels), $notice->id)); foreach ($channels as $channel) { // XXX: We should probably fan-out here and do a // new queue item for each user/path combo if (is_null($channel->user_id)) { $profile = null; } else { $profile = Profile::getKV('id', $channel->user_id); } if ($notice->inScope($profile)) { $this->log(LOG_INFO, sprintf(_("Delivering notice %d to channel (%s, %s, %s) for user '%s'"), $notice->id, $channel->action, $channel->arg1, $channel->arg2, $profile ? $profile->nickname : "<public>")); $timeline = $this->_pathToChannel(array($channel->channel_key)); $this->_publish($timeline, $json); } } } $this->_disconnect(); } return true; }
/** * notify a user that they have received an "attn:" message AKA "@-reply" * * @param User $user The user who recevied the notice * @param Notice $notice The notice that was sent * * @return void */ function mail_notify_attn($user, $notice) { if (!$user->email || !$user->emailnotifyattn) { return; } $sender = $notice->getProfile(); $bestname = $sender->getBestName(); common_init_locale($user->language); $subject = sprintf(_('%s sent a notice to your attention'), $bestname); $body = sprintf(_("%1\$s just sent a notice to your attention (an '@-reply') on %2\$s.\n\n" . "The notice is here:\n\n" . "\t%3\$s\n\n" . "It reads:\n\n" . "\t%4\$s\n\n" . "You can reply back here:\n\n" . "\t%5\$s\n\n" . "The list of all @-replies for you here:\n\n" . "%6\$s\n\n" . "Faithfully yours,\n" . "%2\$s\n\n" . "P.S. You can turn off these email notifications here: %7\$s\n"), $bestname, common_config('site', 'name'), common_local_url('shownotice', array('notice' => $notice->id)), $notice->content, common_local_url('newnotice', array('replyto' => $sender->nickname)), common_local_url('replies', array('nickname' => $user->nickname)), common_local_url('emailsettings')); common_init_locale(); mail_to_user($user, $subject, $body); }
/** * extra information for XMPP messages, as defined by Twitter * * @param Profile $profile Profile of the sending user * @param Notice $notice Notice being sent * * @return string Extra information (Atom, HTML, addresses) in string format */ protected function format_entry(Notice $notice) { $profile = $notice->getProfile(); $entry = $notice->asAtomEntry(true, true); $xs = new XMLStringer(); $xs->elementStart('html', array('xmlns' => 'http://jabber.org/protocol/xhtml-im')); $xs->elementStart('body', array('xmlns' => 'http://www.w3.org/1999/xhtml')); $xs->element('a', array('href' => $profile->profileurl), $profile->nickname); try { $parent = $notice->getParent(); $orig_profile = $parent->getProfile(); $orig_profurl = $orig_profile->getUrl(); $xs->text(" => "); $xs->element('a', array('href' => $orig_profurl), $orig_profile->nickname); $xs->text(": "); } catch (InvalidUrlException $e) { $xs->text(sprintf(' => %s', $orig_profile->nickname)); } catch (NoParentNoticeException $e) { $xs->text(": "); } catch (NoResultException $e) { // Parent notice was probably deleted. $xs->text(": "); } // FIXME: Why do we replace \t with ''? is it just to make it pretty? shouldn't whitespace be handled well...? $xs->raw(str_replace("\t", "", $notice->getRendered())); $xs->text(" "); $xs->element('a', array('href' => common_local_url('conversation', array('id' => $notice->conversation)) . '#notice-' . $notice->id), sprintf(_m('[%u]'), $notice->id)); $xs->elementEnd('body'); $xs->elementEnd('html'); $html = $xs->getString(); return $html . ' ' . $entry; }
/** * Build an Atom entry similar to search.twitter.com's based on * a given notice * * @param Notice $notice the notice to use * * @return void */ function showEntry($notice) { $server = common_config('site', 'server'); $profile = $notice->getProfile(); $nurl = common_local_url('shownotice', array('notice' => $notice->id)); $this->elementStart('entry'); $taguribase = TagURI::base(); $this->element('id', null, "tag:{$taguribase}:{$notice->id}"); $this->element('published', null, common_date_w3dtf($notice->created)); $this->element('link', array('type' => 'text/html', 'rel' => 'alternate', 'href' => $nurl)); $this->element('title', null, common_xml_safe_str(trim($notice->content))); $this->element('content', array('type' => 'html'), $notice->getRendered()); $this->element('updated', null, common_date_w3dtf($notice->created)); $this->element('link', array('type' => 'image/png', 'rel' => 'related', 'href' => $profile->avatarUrl())); // @todo: Here is where we'd put in a link to an atom feed for threads $source = null; $ns = $notice->getSource(); if ($ns instanceof Notice_source) { if (!empty($ns->name) && !empty($ns->url)) { $source = '<a href="' . htmlspecialchars($ns->url) . '" rel="nofollow">' . htmlspecialchars($ns->name) . '</a>'; } else { $source = $ns->code; } } $this->element("twitter:source", null, $source); $this->elementStart('author'); $name = $profile->nickname; if ($profile->fullname) { // @todo Needs proper i18n? $name .= ' (' . $profile->fullname . ')'; } $this->element('name', null, $name); $this->element('uri', null, common_profile_uri($profile)); $this->elementEnd('author'); $this->elementEnd('entry'); }
/** * constructor * * Also initializes the profile attribute. * * @param Notice $notice The notice we'll display */ function __construct($notice, $out = null) { parent::__construct($out); $this->notice = $notice; $this->profile = $notice->getProfile(); }