*/
protected function saveObjectFromActivity(Activity $activity, Notice $stored, array $options=array())
{
- $actobj = $activity->objects[0];
-
- $relLinkEls = ActivityUtils::getLinks($actobj->element, 'related');
-
- if (count($relLinkEls) !== 1) {
- // TRANS: Client exception thrown when a bookmark is formatted incorrectly.
- throw new ClientException(sprintf(_m('Expected exactly 1 link rel=related in a Bookmark, got %1$d.'), count($relLinkEls)));
- }
-
- return Bookmark::addNew($stored,
- $actobj->title,
- $relLinkEls[0]->getAttribute('href'),
- $actobj->summary);
+ return Bookmark::saveActivityObject($activity->objects[0], $stored);
}
- function activityObjectFromNotice(Notice $notice)
+ public function onEndNoticeAsActivity(Notice $stored, Activity $act, Profile $scoped=null)
{
- assert($this->isMyNotice($notice));
-
- common_log(LOG_INFO,
- "Formatting notice {$notice->uri} as a bookmark.");
-
- $object = new ActivityObject();
- $nb = Bookmark::fromStored($notice);
-
- $object->id = $notice->uri;
- $object->type = ActivityObject::BOOKMARK;
- $object->title = $nb->getTitle();
- $object->summary = $nb->getDescription();
- $object->link = $notice->getUrl();
-
- // Attributes of the URL
-
- $attachments = $notice->attachments();
-
- if (count($attachments) != 1) {
- // TRANS: Server exception thrown when a bookmark has multiple attachments.
- throw new ServerException(_m('Bookmark notice with the '.
- 'wrong number of attachments.'));
- }
-
- $target = $attachments[0];
-
- $attrs = array('rel' => 'related',
- 'href' => $target->getUrl());
-
- if (!empty($target->title)) {
- $attrs['title'] = $target->title;
+ if (!$this->isMyNotice($stored)) {
+ return true;
}
- $object->extra[] = array('link', $attrs, null);
-
- // Attributes of the thumbnail, if any
-
- try {
- $thumbnail = $target->getThumbnail();
- $tattrs = array('rel' => 'preview',
- 'href' => $thumbnail->getUrl());
-
- if (!empty($thumbnail->width)) {
- $tattrs['media:width'] = $thumbnail->width;
- }
-
- if (!empty($thumbnail->height)) {
- $tattrs['media:height'] = $thumbnail->height;
- }
+ common_debug('Extending activity '.$stored->id.' with '.get_called_class());
+ $this->extendActivity($stored, $act, $scoped);
+ return false;
+ }
- $object->extra[] = array('link', $tattrs, null);
- } catch (UnsupportedMediaException $e) {
- // No image thumbnail metadata available
- }
+ public function extendActivity(Notice $stored, Activity $act, Profile $scoped=null)
+ {
+ /*$hashtags = array();
+ $taglinks = array();
+
+ foreach ($tags as $tag) {
+ $hashtags[] = '#'.$tag;
+ $attrs = array('href' => Notice_tag::url($tag),
+ 'rel' => $tag,
+ 'class' => 'tag');
+ $taglinks[] = XMLStringer::estring('a', $attrs, $tag);
+ }*/
+ }
- return $object;
+ function activityObjectFromNotice(Notice $notice)
+ {
+ return Bookmark::fromStored($notice)->asActivityObject();
}
function entryForm($out)
* @license http://www.fsf.org/licensing/licenses/agpl-3.0.html AGPL 3.0
* @link http://status.net/
*/
-class NewbookmarkAction extends Action
+class NewbookmarkAction extends FormAction
{
- protected $user = null;
- protected $error = null;
protected $complete = null;
protected $title = null;
protected $url = null;
protected $tags = null;
protected $description = null;
- /**
- * Returns the title of the action
- *
- * @return string Action title
- */
function title()
{
// TRANS: Title for action to create a new bookmark.
return _m('New bookmark');
}
- /**
- * For initializing members of the class.
- *
- * @param array $argarray misc. arguments
- *
- * @return boolean true
- */
- function prepare($argarray)
+ protected function doPreparation()
{
- parent::prepare($argarray);
-
- if ($this->boolean('ajax')) {
- GNUsocial::setApi(true);
- }
-
- $this->user = common_current_user();
-
- if (empty($this->user)) {
- // TRANS: Client exception thrown when trying to create a new bookmark while not logged in.
- throw new ClientException(_m('Must be logged in to post a bookmark.'),
- 403);
- }
-
- if ($this->isPost()) {
- $this->checkSessionToken();
- }
-
$this->title = $this->trimmed('title');
$this->url = $this->trimmed('url');
$this->tags = preg_split('/[\s,]+/', $this->trimmed('tags'), null, PREG_SPLIT_NO_EMPTY);
return true;
}
- /**
- * Handler method
- *
- * @param array $argarray is ignored since it's now passed in in prepare()
- *
- * @return void
- */
- function handle($argarray=null)
- {
- parent::handle($argarray);
-
- if ($this->isPost()) {
- $this->newBookmark();
- } else {
- $this->showPage();
- }
-
- return;
- }
-
/**
* Add a new bookmark
*
* @return void
*/
- function newBookmark()
+ function handlePost()
{
- try {
- if (empty($this->title)) {
- // TRANS: Client exception thrown when trying to create a new bookmark without a title.
- throw new ClientException(_m('Bookmark must have a title.'));
- }
-
- if (empty($this->url)) {
- // TRANS: Client exception thrown when trying to create a new bookmark without a URL.
- throw new ClientException(_m('Bookmark must have an URL.'));
- }
-
- $options = array();
+ if (empty($this->title)) {
+ // TRANS: Client exception thrown when trying to create a new bookmark without a title.
+ throw new ClientException(_m('Bookmark must have a title.'));
+ }
- ToSelector::fillOptions($this, $options);
+ if (empty($this->url)) {
+ // TRANS: Client exception thrown when trying to create a new bookmark without a URL.
+ throw new ClientException(_m('Bookmark must have an URL.'));
+ }
- $saved = Bookmark::saveNew($this->user->getProfile(),
- $this->title,
- $this->url,
- $this->tags,
- $this->description,
- $options);
+ $options = array();
- } catch (ClientException $ce) {
- if ($this->boolean('ajax')) {
- $this->startHTML('text/xml;charset=utf-8');
- $this->elementStart('head');
- // TRANS: Page title after an AJAX error occurs
- $this->element('title', null, _('Ajax Error'));
- $this->elementEnd('head');
- $this->elementStart('body');
- $this->element('p', array('id' => 'error'), $ce->getMessage());
- $this->elementEnd('body');
- $this->endHTML();
- return;
- } else {
- $this->error = $ce->getMessage();
- $this->showPage();
- return;
- }
- }
+ ToSelector::fillOptions($this, $options);
- if ($this->boolean('ajax')) {
- $this->startHTML('text/xml;charset=utf-8');
- $this->elementStart('head');
- // TRANS: Page title after posting a bookmark.
- $this->element('title', null, _m('Bookmark posted'));
- $this->elementEnd('head');
- $this->elementStart('body');
- $this->showNotice($saved);
- $this->elementEnd('body');
- $this->endHTML();
- } else {
- common_redirect($saved->getUrl(), 303);
- }
+ $saved = Bookmark::addNew($this->scoped,
+ $this->title,
+ $this->url,
+ $this->tags,
+ $this->description,
+ $options);
}
/**
return self::getByPK(array('uri' => $stored->getUri()));
}
+ public function getStored()
+ {
+ return Notice::getByKeys(array('uri' => $this->getUri()));
+ }
+
public function getDescription()
{
return $this->description;
return $this->title;
}
+ public function getUri()
+ {
+ return $this->uri;
+ }
+
public function getUrl()
{
if (empty($this->url)) {
*
* @return Bookmark the Bookmark object
*/
- static function addNew(Notice $stored, $title, $url, $description)
+ static function saveActivityObject(ActivityObject $actobj, Notice $stored)
{
- if ($title === '' or is_null($title)) {
+ $relLinkEls = ActivityUtils::getLinks($actobj->element, 'related');
+
+ if (count($relLinkEls) !== 1) {
+ // TRANS: Client exception thrown when a bookmark is formatted incorrectly.
+ throw new ClientException(sprintf(_m('Expected exactly 1 link rel=related in a Bookmark, got %1$d.'), count($relLinkEls)));
+ }
+
+ $url = $relLinkEls[0]->getAttribute('href');
+
+ if (!strlen($actobj->title)) {
throw new ClientException(_m('You must provide a non-empty title.'));
}
if (!common_valid_http_url($url)) {
$nb = new Bookmark();
$nb->id = UUID::gen();
- $nb->uri = $stored->uri;
+ $nb->uri = $stored->getUri();
$nb->profile_id = $stored->getProfile()->getID();
- $nb->title = $title;
+ $nb->title = $actobj->title;
$nb->url = $url;
- $nb->description = $description;
+ $nb->description = $actobj->summary;
$nb->created = $stored->created;
$result = $nb->insert();
throw new ServerException('Could not insert Bookmark into database!');
}
- /*$hashtags = array();
- $taglinks = array();
+ return $nb;
+ }
- foreach ($tags as $tag) {
- $hashtags[] = '#'.$tag;
- $attrs = array('href' => Notice_tag::url($tag),
- 'rel' => $tag,
- 'class' => 'tag');
- $taglinks[] = XMLStringer::estring('a', $attrs, $tag);
- }*/
+ public function asActivityObject()
+ {
+ $stored = $this->getStored();
- return $nb;
+ $object = new ActivityObject();
+ $object->id = $this->getUri();
+ $object->type = ActivityObject::BOOKMARK;
+ $object->title = $this->getTitle();
+ $object->summary = $this->getDescription();
+ $object->link = $stored->getUrl();
+ $object->content = $stored->getRendered();
+
+ // Attributes of the URL
+
+ $attachments = $stored->attachments();
+
+ if (count($attachments) != 1) {
+ // TRANS: Server exception thrown when a bookmark has multiple attachments.
+ throw new ServerException(_m('Bookmark notice with the '.
+ 'wrong number of attachments.'));
+ }
+
+ $bookmarkedurl = $attachments[0];
+
+ $attrs = array('rel' => 'related',
+ 'href' => $bookmarkedurl->getUrl());
+
+ if (!strlen($bookmarkedurl->title)) {
+ $attrs['title'] = $bookmarkedurl->title;
+ }
+
+ $object->extra[] = array('link', $attrs, null);
+
+ // Attributes of the thumbnail, if any
+
+ try {
+ $thumbnail = $bookmarkedurl->getThumbnail();
+ $tattrs = array('rel' => 'preview',
+ 'href' => $thumbnail->getUrl());
+
+ if (!empty($thumbnail->width)) {
+ $tattrs['media:width'] = $thumbnail->width;
+ }
+
+ if (!empty($thumbnail->height)) {
+ $tattrs['media:height'] = $thumbnail->height;
+ }
+
+ $object->extra[] = array('link', $tattrs, null);
+ } catch (UnsupportedMediaException $e) {
+ // No image thumbnail metadata available
+ }
+
+ return $object;
}
/**
*
* @return Notice saved notice
*/
- static function saveNew(Profile $profile, $title, $url, $rawtags, $description,
- array $options=array())
+ static function addNew(Profile $actor, $title, $url, array $rawtags, $description, array $options=array())
{
- if (!common_valid_http_url($url)) {
- throw new ClientException(_m('Only web bookmarks can be posted (HTTP or HTTPS).'));
- }
-
- try {
- $object = self::getByURL($profile, $url);
- return $object;
- } catch (NoResultException $e) {
- // Alright, so then we have to create it.
- }
-
- if (array_key_exists('uri', $options)) {
- $other = Bookmark::getKV('uri', $options['uri']);
- if (!empty($other)) {
- // TRANS: Client exception thrown when trying to save a new bookmark that already exists.
- throw new ClientException(_m('Bookmark already exists.'));
- }
- }
-
- $nb = new Bookmark();
-
- $nb->id = UUID::gen();
- $nb->profile_id = $profile->id;
- $nb->url = $url;
- $nb->title = $title;
- $nb->description = $description;
-
- if (array_key_exists('created', $options)) {
- $nb->created = $options['created'];
- } else {
- $nb->created = common_sql_now();
- }
-
- if (array_key_exists('uri', $options)) {
- $nb->uri = $options['uri'];
- } else {
- // FIXME: hacks to work around router bugs in
- // queue daemons
-
- $r = Router::get();
+ $act = new Activity();
+ $act->verb = ActivityVerb::POST;
+ $act->time = time();
+ $act->actor = $actor->asActivityObject();
- $path = $r->build('showbookmark',
- array('id' => $nb->id));
-
- if (empty($path)) {
- $nb->uri = common_path('bookmark/'.$nb->id, false, false);
- } else {
- $nb->uri = common_local_url('showbookmark',
- array('id' => $nb->id),
- null,
- null,
- false);
- }
- }
+ $actobj = new ActivityObject();
+ $actobj->type = ActivityObject::BOOKMARK;
+ $actobj->title = $title;
+ $actobj->summary = $description;
+ $actobj->extra[] = array('link', $attrs, null);
+ $act->objects[] = $actobj;
- $nb->insert();
+ $act->enclosures[] = $url;
$tags = array();
$replies = array();
// filter "for:nickname" tags
-
foreach ($rawtags as $tag) {
if (strtolower(mb_substr($tag, 0, 4)) == 'for:') {
// skip if done by caller
if (!array_key_exists('replies', $options)) {
$nickname = mb_substr($tag, 4);
- $other = common_relative_profile($profile,
- $nickname);
+ $other = common_relative_profile($actor, $nickname);
if (!empty($other)) {
$replies[] = $other->getUri();
}
}
// Use user's preferences for short URLs, if possible
-
+ // FIXME: Should be possible to with the Profile object...
try {
- $user = User::getKV('id', $profile->id);
-
- $shortUrl = File_redirection::makeShort($url,
- empty($user) ? null : $user);
+ $user = $actor->getUser();
+ $shortUrl = File_redirection::makeShort($url, empty($user) ? null : $user);
} catch (Exception $e) {
// Don't let this stop us.
$shortUrl = $url;
}
- // TRANS: Bookmark content.
- // TRANS: %1$s is a title, %2$s is a short URL, %3$s is the bookmark description,
- // TRANS: %4$s is space separated list of hash tags.
- $content = sprintf(_m('"%1$s" %2$s %3$s %4$s'),
- $title,
- $shortUrl,
- $description,
- implode(' ', $hashtags));
-
// TRANS: Rendered bookmark content.
// TRANS: %1$s is a URL, %2$s the bookmark title, %3$s is the bookmark description,
- // TRANS: %4$s is space separated list of hash tags.
- $rendered = sprintf(_m('<span class="xfolkentry">'.
- '<a class="taggedlink" href="%1$s">%2$s</a> '.
- '<span class="description">%3$s</span> '.
- '<span class="meta">%4$s</span>'.
- '</span>'),
- htmlspecialchars($url),
- htmlspecialchars($title),
- htmlspecialchars($description),
- implode(' ', $taglinks));
+ // TRANS: %4$s is space separated list of hash tags.
+ $actobj->content = sprintf(_m('<span class="xfolkentry">'.
+ '<a class="taggedlink" href="%1$s">%2$s</a> '.
+ '<span class="description">%3$s</span> '.
+ '<span class="meta">%4$s</span>'.
+ '</span>'),
+ htmlspecialchars($url),
+ htmlspecialchars($title),
+ htmlspecialchars($description),
+ implode(' ', $taglinks));
+
+ foreach ($tags as $term) {
+ $catEl = new AtomCategory();
+ $catEl->term = $term;
+ $activity->categories[] = $catEl;
+ }
$options = array_merge(array('urls' => array($url),
'rendered' => $rendered,
'object_type' => ActivityObject::BOOKMARK),
$options);
- $options['uri'] = $nb->uri;
-
- try {
- $saved = Notice::saveNew($profile->id,
- $content,
- array_key_exists('source', $options) ?
- $options['source'] : 'web',
- $options);
- } catch (Exception $e) {
- $nb->delete();
- throw $e;
- }
-
- if (empty($saved)) {
- $nb->delete();
- }
-
- return $saved;
+ return Notice::saveActivity($act, $actor, $options);
}
}