}
// 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)) {
throw $e;
}
}
+ if (!$stored instanceof Notice) {
+ throw new ServerException('StartNoticeSave did not give back a Notice');
+ }
// Save per-notice metadata...
$mentions = array();
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();
-
- foreach ($recipientIds as $recipientId) {
- $user = User::getKV('id', $recipientId);
- if ($user instanceof User) {
- mail_notify_attn($user, $this);
+ if (Event::handle('StartNotifyMentioned', array($this, &$recipientIds))) {
+ foreach ($recipientIds as $recipientId) {
+ $user = User::getKV('id', $recipientId);
+ if ($user instanceof User) {
+ mail_notify_attn($user, $this);
+ }
}
+ Event::handle('EndNotifyMentioned', array($this, $recipientIds));
}
}
$act->objects[] = $repeated->asActivity($cur);
}
} else {
- $act->objects[] = ActivityObject::fromNotice($this);
+ $act->objects[] = $this->asActivityObject();
}
// XXX: should this be handled by default processing for object entry?
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.
*