oembed discovery

This commit is contained in:
Hannes Mannerheim 2016-01-20 20:39:50 +01:00
parent ba1816a6fd
commit d614b9fca8
3 changed files with 149 additions and 3 deletions

View File

@ -153,7 +153,10 @@ class QvitterPlugin extends Plugin {
// route/reroute urls // route/reroute urls
public function onRouterInitialized($m) public function onRouterInitialized($m)
{ {
$m->connect('api/qvitter/oembed_notice/:id.:format',
array('action' => 'apiqvitteroembednotice',
'id' => '[0-9]+',
'format' => '(xml|json)'));
$m->connect('api/qvitter/check_email.json', $m->connect('api/qvitter/check_email.json',
array('action' => 'ApiQvitterCheckEmail')); array('action' => 'ApiQvitterCheckEmail'));
$m->connect('api/qvitter/:nickname/lists/:id/subscribers.json', $m->connect('api/qvitter/:nickname/lists/:id/subscribers.json',

View File

@ -0,0 +1,128 @@
<?php
/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
· ·
· Qvitter's Oembed response for notices ·
· ·
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
· ·
· ·
· Q V I T T E R ·
· ·
· https://git.gnu.io/h2p/Qvitter ·
· ·
· ·
· <o) ·
· /_//// ·
· (____/ ·
· (o< ·
· o> \\\\_\ ·
· \\) \____) ·
· ·
· ·
· ·
· Qvitter is free software: you can redistribute it and / or modify it ·
· under the terms of the GNU Affero General Public License as published by ·
· the Free Software Foundation, either version three of the License or (at ·
· your option) any later version. ·
· ·
· Qvitter is distributed in hope that it will be useful but WITHOUT ANY ·
· WARRANTY; without even the implied warranty of MERCHANTABILTY 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 Qvitter. If not, see <http://www.gnu.org/licenses/>. ·
· ·
· Contact h@nnesmannerhe.im if you have any questions. ·
· ·
· · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · */
if (!defined('GNUSOCIAL')) { exit(1); }
class ApiQvitterOembedNoticeAction extends ApiAction
{
var $id = null;
var $format = null;
/**
* Take arguments for running
*
* @param array $args $_REQUEST args
*
* @return boolean success flag
*/
protected function prepare(array $args=array())
{
parent::prepare($args);
$this->id = $this->arg('id');
$this->format = $this->arg('format');
return true;
}
/**
* Handle the request
*
* @param array $args $_REQUEST data (unused)
*
* @return void
*/
protected function handle()
{
parent::handle();
$notice = Notice::getKV('id',$this->id);
if(!$notice instanceof Notice){
// TRANS: Client error displayed in oEmbed action when notice not found.
// TRANS: %s is a notice.
$this->clientError(sprintf(_("Notice %s not found."),$this->id), 404);
}
$profile = $notice->getProfile();
if (!$profile instanceof Profile) {
// TRANS: Server error displayed in oEmbed action when notice has not profile.
$this->serverError(_('Notice has no profile.'), 500);
}
$authorname = $profile->getFancyName();
$oembed=array();
$oembed['version']='1.0';
$oembed['provider_name']=common_config('site', 'name');
$oembed['provider_url']=common_root_url();
// TRANS: oEmbed title. %1$s is the author name, %2$s is the creation date.
$oembed['title'] = sprintf(_('%1$s\'s status on %2$s'),
$authorname,
common_exact_date($notice->created));
$oembed['author_name']=$authorname;
$oembed['author_url']=$profile->profileurl;
$oembed['url']=$notice->getUrl();
$oembed['html']=$notice->getRendered();
if($this->format == 'json') {
$this->initDocument('json');
print json_encode($oembed);
$this->endDocument('json');
} elseif ($this->format == 'xml') {
$this->initDocument('xml');
$this->elementStart('oembed');
foreach(array(
'version', 'type', 'provider_name',
'provider_url', 'title', 'author_name',
'author_url', 'url', 'html'
) as $key) {
if (isset($oembed[$key]) && $oembed[$key]!='') {
$this->element($key, null, $oembed[$key]);
}
}
$this->elementEnd('oembed');
$this->endDocument('xml');
} else {
$this->serverError(sprintf(_('Format %s not supported.'), $this->format), 501);
}
}
}

View File

@ -174,7 +174,23 @@ class QvitterAction extends ApiAction
} }
} }
// oembed discovery for local notices
if(substr($_SERVER['REQUEST_URI'],0,8) == '/notice/'
&& $this->arg('notice')
&& array_key_exists('Oembed', StatusNet::getActivePlugins())) {
$notice = Notice::getKV('id', $this->arg('notice'));
if($notice instanceof Notice) {
if ($notice->isLocal()) {
try {
$notice_url = $notice->getUrl();
print '<link title="oEmbed" href="'.common_local_url('apiqvitteroembednotice', array('id' => $notice->id, 'format'=>'json')).'?url='.urlencode($notice_url).'" type="application/json+oembed" rel="alternate">';
print '<link title="oEmbed" href="'.common_local_url('apiqvitteroembednotice', array('id' => $notice->id, 'format'=>'xml')).'?url='.urlencode($notice_url).'" type="application/xml+oembed" rel="alternate">';
} catch (Exception $e) {
//
}
}
}
}
?> ?>
<script> <script>
@ -563,7 +579,6 @@ class QvitterAction extends ApiAction
// adds temporary support for microformats and linkbacks on the notice page // adds temporary support for microformats and linkbacks on the notice page
if(substr($_SERVER['REQUEST_URI'],0,8) == '/notice/' && $this->arg('notice')) { if(substr($_SERVER['REQUEST_URI'],0,8) == '/notice/' && $this->arg('notice')) {
echo '<ol class="notices xoxo">'; echo '<ol class="notices xoxo">';
$notice = Notice::getKV('id', $this->arg('notice'));
if($notice instanceof Notice) { if($notice instanceof Notice) {
$widget = new NoticeListItem($notice, $this); $widget = new NoticeListItem($notice, $this);
$widget->show(); $widget->show();