public function getProfile()
{
if (!isset($this->_profile[$this->profile_id])) {
- $this->_setProfile(Profile::getKV('id', $this->profile_id));
+ // We could've sent getKV directly to _setProfile, but occasionally we get
+ // a "false" (instead of null), likely because it indicates a cache miss.
+ $profile = Profile::getKV('id', $this->profile_id);
+ $this->_setProfile($profile instanceof Profile ? $profile : null);
}
return $this->_profile[$this->profile_id];
}
}
return $title;
}
+
+ public function getContent()
+ {
+ return $this->content;
+ }
/*
* Get the original representation URL of this notice.
* Record the given set of hash tags in the db for this notice.
* Given tag strings will be normalized and checked for dupes.
*/
- function saveKnownTags($hashtags)
+ function saveKnownTags(array $hashtags)
{
//turn each into their canonical tag
//this is needed to remove dupes before saving e.g. #hash.tag = #hashtag
}
// If it's a repeat, the reply_to should be to the original
- if (!empty($reply->repeat_of)) {
+ if ($reply->isRepeat()) {
$notice->reply_to = $reply->repeat_of;
} else {
$notice->reply_to = $reply->id;
}
if (empty($verb)) {
- if (!empty($notice->repeat_of)) {
+ if ($notice->isRepeat()) {
$notice->verb = ActivityVerb::SHARE;
$notice->object_type = ActivityObject::ACTIVITY;
} else {
// XXX: some of these functions write to the DB
- $id = $notice->insert();
-
- if (!$id) {
- common_log_db_error($notice, 'INSERT', __FILE__);
- // TRANS: Server exception thrown when a notice cannot be saved.
- throw new ServerException(_('Problem saving notice.'));
- }
-
- // Update ID-dependent columns: URI, conversation
-
- $orig = clone($notice);
-
- $changed = false;
-
- // We can only get here if it's a local notice, since remote notices
- // should've bailed out earlier due to lacking a URI.
- if (empty($notice->uri)) {
- $notice->uri = sprintf('%s%s=%d:%s=%s',
- TagURI::mint(),
- 'noticeId', $notice->id,
- 'objectType', $notice->get_object_type(true));
- $changed = true;
- }
-
- // If it's not part of a conversation, it's
- // the beginning of a new conversation.
-
- if (empty($notice->conversation)) {
- $conv = Conversation::create($notice);
- $notice->conversation = $conv->id;
- $changed = true;
- }
-
- if ($changed) {
- if ($notice->update($orig) === false) {
- common_log_db_error($notice, 'UPDATE', __FILE__);
- // TRANS: Server exception thrown when a notice cannot be updated.
- throw new ServerException(_('Problem saving notice.'));
+ try {
+ $notice->insert(); // throws exception on failure
+ } 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();
}
}
-
}
// Clear the cache for subscribed users, so they'll update at next request
// XXX: someone clever could prepend instead of clearing the cache
- $notice->blowOnInsert();
-
// Save per-notice metadata...
if (isset($replies)) {
'and post again in a few minutes.'));
}
-/* This interferes with stuff like Favorites from old StatusNet installations (first object in objects is the favored notice)
// Get ActivityObject properties
- $actobj = count($act->objects)==1 ? $act->objects[0] : null;
- if (!is_null($actobj) && $actobj->id) {
- $options['uri'] = $actobj->id;
- if ($actobj->link) {
- $options['url'] = $actobj->link;
- } elseif ($act->link) {
- $options['url'] = $act->link;
- } elseif (preg_match('!^https?://!', $actobj->id)) {
- $options['url'] = $actobj->id;
- }
- } else {
+ if (!empty($act->id)) {
// implied object
$options['uri'] = $act->id;
$options['url'] = $act->link;
+ } else {
+ $actobj = count($act->objects)==1 ? $act->objects[0] : null;
+ if (!is_null($actobj) && !empty($actobj->id)) {
+ $options['uri'] = $actobj->id;
+ if ($actobj->link) {
+ $options['url'] = $actobj->link;
+ } elseif (preg_match('!^https?://!', $actobj->id)) {
+ $options['url'] = $actobj->id;
+ }
+ }
}
-*/
$defaults = array(
'groups' => array(),
$stored->url = $url;
$stored->verb = $act->verb;
+ // Use the local user's shortening preferences, if applicable.
+ $stored->rendered = $actor->isLocal()
+ ? $actor->shortenLinks($act->content)
+ : $act->content;
+ $stored->content = common_strip_html($stored->rendered);
+
$autosource = common_config('public', 'autosource');
// Sandboxed are non-false, but not 1, either
throw $e;
}
}
+ if (!$stored instanceof Notice) {
+ throw new ServerException('StartNoticeSave did not give back a Notice');
+ }
- common_debug(get_called_class().' looking up mentions metadata etc.');
// Save per-notice metadata...
$mentions = array();
$groups = array();
$stored->distribute();
}
- common_debug(get_called_class().' returning stored activity - success!');
-
return $stored;
}
self::blow('notice:list-ids:conversation:%s', $this->conversation);
self::blow('conversation:notice_count:%d', $this->conversation);
- if (!empty($this->repeat_of)) {
+ if ($this->isRepeat()) {
// XXX: we should probably only use one of these
$this->blowStream('notice:repeats:%d', $this->repeat_of);
self::blow('notice:list-ids:repeat_of:%d', $this->repeat_of);
// Exclude any deleted, non-local, or blocking recipients.
$profile = $this->getProfile();
$originalProfile = null;
- if ($this->repeat_of) {
+ if ($this->isRepeat()) {
// Check blocks against the original notice's poster as well.
$original = Notice::getKV('id', $this->repeat_of);
if ($original instanceof Notice) {
{
// Don't save reply data for repeats
- if (!empty($this->repeat_of)) {
+ if ($this->isRepeat()) {
return array();
}
{
// Don't send reply notifications for repeats
- if (!empty($this->repeat_of)) {
+ if ($this->isRepeat()) {
return array();
}
$recipientIds = $this->getReplies();
+ if (Event::handle('StartNotifyMentioned', array($this, &$recipientIds))) {
+ require_once INSTALLDIR.'/lib/mail.php';
- foreach ($recipientIds as $recipientId) {
- $user = User::getKV('id', $recipientId);
- if ($user instanceof User) {
- mail_notify_attn($user, $this);
+ foreach ($recipientIds as $recipientId) {
+ $user = User::getKV('id', $recipientId);
+ if ($user instanceof User) {
+ mail_notify_attn($user, $this);
+ }
}
+ Event::handle('EndNotifyMentioned', array($this, $recipientIds));
}
}
/**
* Convert a notice into an activity for export.
*
- * @param User $cur Current user
+ * @param Profile $scoped The currently logged in/scoped profile
*
* @return Activity activity object representing this Notice.
*/
- function asActivity($cur=null)
+ function asActivity(Profile $scoped=null)
{
$act = self::cacheGet(Cache::codeKey('notice:as-activity:'.$this->id));
}
$act = new Activity();
- if (Event::handle('StartNoticeAsActivity', array($this, &$act))) {
+ if (Event::handle('StartNoticeAsActivity', array($this, $act, $scoped))) {
$act->id = $this->uri;
$act->time = strtotime($this->created);
$profile = $this->getProfile();
- $act->actor = ActivityObject::fromProfile($profile);
- $act->actor->extra[] = $profile->profileInfo($cur);
+ $act->actor = $profile->asActivityObject();
+ $act->actor->extra[] = $profile->profileInfo($scoped);
$act->verb = $this->verb;
if ($this->repeat_of) {
$repeated = Notice::getKV('id', $this->repeat_of);
if ($repeated instanceof Notice) {
- $act->objects[] = $repeated->asActivity($cur);
+ // 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'),
+ $this->getProfile()->getNickname(),
+ $repeated->getProfile()->getNickname());
+ $act->objects[] = $repeated->asActivity($scoped);
}
} else {
- $act->objects[] = ActivityObject::fromNotice($this);
+ $act->objects[] = $this->asActivityObject();
}
// XXX: should this be handled by default processing for object entry?
$act->editLink = $act->selfLink;
}
- Event::handle('EndNoticeAsActivity', array($this, &$act));
+ Event::handle('EndNoticeAsActivity', array($this, $act, $scoped));
}
self::cacheSet(Cache::codeKey('notice:as-activity:'.$this->id), $act);
function asAtomEntry($namespace=false,
$source=false,
$author=true,
- $cur=null)
+ Profile $scoped=null)
{
- $act = $this->asActivity($cur);
- $act->extra[] = $this->noticeInfo($cur);
+ $act = $this->asActivity($scoped);
+ $act->extra[] = $this->noticeInfo($scoped);
return $act->asString($namespace, $author, $source);
}
* Clients use some extra notice info in the atom stream.
* This gives it to them.
*
- * @param User $cur Current user
+ * @param Profile $scoped The currently logged in/scoped profile
*
* @return array representation of <statusnet:notice_info> element
*/
- function noticeInfo($cur)
+ function noticeInfo(Profile $scoped=null)
{
// local notice ID (useful to clients for ordering)
// favorite and repeated
- $scoped = null;
- if (!empty($cur)) {
- $scoped = $cur->getProfile();
+ if ($scoped instanceof Profile) {
$noticeInfoAttr['repeated'] = ($scoped->hasRepeated($this)) ? "true" : "false";
}
function asActivityNoun($element)
{
- $noun = ActivityObject::fromNotice($this);
+ $noun = $this->asActivityObject();
return $noun->asString('activity:' . $element);
}
+ public function asActivityObject()
+ {
+ $object = new ActivityObject();
+
+ if (Event::handle('StartActivityObjectFromNotice', array($this, &$object))) {
+ $object->type = $this->object_type ?: ActivityObject::NOTE;
+ $object->id = $this->getUri();
+ $object->title = sprintf('New %1$s by %2$s', ActivityObject::canonicalType($object->type), $this->getProfile()->getNickname());
+ $object->content = $this->rendered;
+ $object->link = $this->getUrl();
+
+ $object->extra[] = array('status_net', array('notice_id' => $this->id));
+
+ Event::handle('EndActivityObjectFromNotice', array($this, &$object));
+ }
+
+ return $object;
+ }
+
/**
* Determine which notice, if any, a new notice is in reply to.
*
{
$result = parent::insert();
- if ($result !== false) {
- // Profile::hasRepeated() abuses pkeyGet(), so we
- // have to clear manually
- if (!empty($this->repeat_of)) {
- $c = self::memcache();
- if (!empty($c)) {
- $ck = self::multicacheKey('Notice',
- array('profile_id' => $this->profile_id,
- 'repeat_of' => $this->repeat_of));
- $c->delete($ck);
- }
+ if ($result === false) {
+ common_log_db_error($this, 'INSERT', __FILE__);
+ // TRANS: Server exception thrown when a stored object entry cannot be saved.
+ throw new ServerException('Could not save Notice');
+ }
+
+ // Profile::hasRepeated() abuses pkeyGet(), so we
+ // have to clear manually
+ if (!empty($this->repeat_of)) {
+ $c = self::memcache();
+ if (!empty($c)) {
+ $ck = self::multicacheKey('Notice',
+ array('profile_id' => $this->profile_id,
+ 'repeat_of' => $this->repeat_of));
+ $c->delete($ck);
}
}
+ // Update possibly ID-dependent columns: URI, conversation
+ // (now that INSERT has added the notice's local id)
+ $orig = clone($this);
+ $changed = false;
+
+ // We can only get here if it's a local notice, since remote notices
+ // should've bailed out earlier due to lacking a URI.
+ if (empty($this->uri)) {
+ $this->uri = sprintf('%s%s=%d:%s=%s',
+ TagURI::mint(),
+ 'noticeId', $this->id,
+ 'objectType', $this->get_object_type(true));
+ $changed = true;
+ }
+
+ // If it's not part of a conversation, it's
+ // the beginning of a new conversation.
+ if (empty($this->conversation)) {
+ $conv = Conversation::create($this);
+ $this->conversation = $conv->id;
+ $changed = true;
+ }
+
+ if ($changed && $this->update($orig) === false) {
+ common_log_db_error($notice, 'UPDATE', __FILE__);
+ // TRANS: Server exception thrown when a notice cannot be updated.
+ throw new ServerException(_('Problem saving notice.'));
+ }
+
+ $this->blowOnInsert();
+
return $result;
}
$this->is_local == Notice::LOCAL_NONPUBLIC);
}
+ public function isRepeat()
+ {
+ return !empty($this->repeat_of);
+ }
+
/**
* Get the list of hash tags saved with this notice.
*