Start reworking Bookmark plugin for more modern code

This commit is contained in:
Mikael Nordfeldth 2015-10-10 21:53:45 +02:00
parent 18f07a48e8
commit cae43344ec
3 changed files with 40 additions and 34 deletions

View File

@ -281,10 +281,14 @@ abstract class ActivityHandlerPlugin extends Plugin
* *
* @return boolean hook value * @return boolean hook value
*/ */
function onNoticeDeleteRelated(Notice $notice) public function onNoticeDeleteRelated(Notice $notice)
{ {
if ($this->isMyNotice($notice)) { if ($this->isMyNotice($notice)) {
$this->deleteRelated($notice); try {
$this->deleteRelated($notice);
} catch (AlreadyFulfilledException $e) {
// Nothing to see here, it's obviously already gone...
}
} }
// Always continue this event in our activity handling plugins. // Always continue this event in our activity handling plugins.

View File

@ -280,14 +280,12 @@ class BookmarkPlugin extends MicroAppPlugin
*/ */
function deleteRelated(Notice $notice) function deleteRelated(Notice $notice)
{ {
if ($this->isMyNotice($notice)) { try {
$nb = Bookmark::fromStored($notice);
$nb = Bookmark::getByNotice($notice); } catch (NoResultException $e) {
throw new AlreadyFulfilledException('Bookmark already gone when deleting: '.$e->getMessage());
if (!empty($nb)) { }
$nb->delete(); $nb->delete();
}
}
return true; return true;
} }
@ -388,12 +386,12 @@ class BookmarkPlugin extends MicroAppPlugin
"Formatting notice {$notice->uri} as a bookmark."); "Formatting notice {$notice->uri} as a bookmark.");
$object = new ActivityObject(); $object = new ActivityObject();
$nb = Bookmark::getByNotice($notice); $nb = Bookmark::fromStored($notice);
$object->id = $notice->uri; $object->id = $notice->uri;
$object->type = ActivityObject::BOOKMARK; $object->type = ActivityObject::BOOKMARK;
$object->title = $nb->title; $object->title = $nb->getTitle();
$object->summary = $nb->description; $object->summary = $nb->getDescription();
$object->link = $notice->getUrl(); $object->link = $notice->getUrl();
// Attributes of the URL // Attributes of the URL
@ -481,14 +479,10 @@ class BookmarkPlugin extends MicroAppPlugin
{ {
assert($obj->type == ActivityObject::BOOKMARK); assert($obj->type == ActivityObject::BOOKMARK);
$bm = Bookmark::getKV('uri', $obj->id); $bm = Bookmark::getByPK(array('uri', $obj->id));
if (empty($bm)) { $out['displayName'] = $bm->getTitle();
throw new ServerException("Unknown bookmark: " . $obj->id); $out['targetUrl'] = $bm->getUrl();
}
$out['displayName'] = $bm->title;
$out['targetUrl'] = $bm->url;
return true; return true;
} }
@ -501,24 +495,32 @@ class BookmarkPlugin extends MicroAppPlugin
$nli->out->elementEnd('div'); $nli->out->elementEnd('div');
} }
public function getDescription()
{
return $this->description;
}
public function getTitle()
{
return $this->title;
}
public function getUrl()
{
if (empty($this->url)) {
throw new InvalidUrlException($this->url);
}
return $this->url;
}
protected function showNoticeContent(Notice $stored, HTMLOutputter $out, Profile $scoped=null) protected function showNoticeContent(Notice $stored, HTMLOutputter $out, Profile $scoped=null)
{ {
$nb = Bookmark::getByNotice($stored); $nb = Bookmark::fromStored($stored);
if (empty($nb)) {
common_log(LOG_ERR, "No bookmark for notice {$stored->id}");
parent::showContent();
return;
} else if (empty($nb->url)) {
common_log(LOG_ERR, "No url for bookmark {$nb->id} for notice {$stored->id}");
parent::showContent();
return;
}
$profile = $stored->getProfile(); $profile = $stored->getProfile();
// Whether to nofollow // Whether to nofollow
$attrs = array('href' => $nb->url, 'class' => 'bookmark-title'); $attrs = array('href' => $nb->getUrl(), 'class' => 'bookmark-title');
$nf = common_config('nofollow', 'external'); $nf = common_config('nofollow', 'external');

View File

@ -74,8 +74,8 @@ class Bookmark extends Managed_DataObject
'bookmark_uri_key' => array('uri'), 'bookmark_uri_key' => array('uri'),
), ),
'foreign keys' => array( 'foreign keys' => array(
'bookmark_profile_id_fkey' => array('profile', array('profile_id' => 'id')) 'bookmark_profile_id_fkey' => array('profile', array('profile_id' => 'id')),
'bookmark_uri_fkey' => array('notice', array('uri' => 'uri')) 'bookmark_uri_fkey' => array('notice', array('uri' => 'uri')),
), ),
'indexes' => array('bookmark_created_idx' => array('created'), 'indexes' => array('bookmark_created_idx' => array('created'),
'bookmark_url_idx' => array('url'), 'bookmark_url_idx' => array('url'),